blob: 58e8baa1f3de8c8257e0e2e643c4856509ace153 [file] [log] [blame]
Teresa Charlinca588392021-10-01 11:29:08 +01001//
2// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
Finn Williamsb1aad422021-10-28 19:07:32 +01005#pragma once
Teresa Charlinca588392021-10-01 11:29:08 +01006
7#include <armnn/backends/ICustomAllocator.hpp>
8
9namespace armnn
10{
11struct Allocator
12{
13 /// Pointer to @ICustomAllocator.
Finn Williamsb1aad422021-10-28 19:07:32 +010014 std::shared_ptr<ICustomAllocator> m_CustomAllocator{};
Teresa Charlinca588392021-10-01 11:29:08 +010015 /// Value which the size of each buffer (actual data size + padding) has to be a multiple of.
16 size_t m_Alignment = 0 ;
17};
18
19struct TensorMemory
20{
21 /// Number of bytes the value is away from the @BufferStorage.m_Buffer.
22 size_t m_Offset{};
Teresa Charlinca588392021-10-01 11:29:08 +010023 /// Identifier to be used by the @LoadedNetwork to order the tensors.
24 unsigned int m_OutputSlotId{};
Finn Williamsb1aad422021-10-28 19:07:32 +010025 /// Pointer to the tensor value.
26 void* m_Data = nullptr;
Teresa Charlinca588392021-10-01 11:29:08 +010027};
28
29struct BufferStorage
30{
31 /// Vector of pointer to @TensorMemory.
Finn Williamsb1aad422021-10-28 19:07:32 +010032 std::vector<std::shared_ptr<TensorMemory>> m_TensorMemoryVector;
Teresa Charlinca588392021-10-01 11:29:08 +010033 /// Total size of the buffer.
34 size_t m_BufferSize;
35 /// Pointer to the first element of the buffer.
36 void* m_Buffer = nullptr;
37};
38
39class MemoryManager
40{
41public:
Colm Donelan09026932022-09-14 18:04:00 +010042 /// Initialization method to store in m_AllocatorBufferStoragePairVector all information needed.
43 /// @param[in] bufferStorageVector - Vector of BufferStorage.
44 /// @param[in] customAllocator - Pointer to ICustomAllocator.
Teresa Charlinca588392021-10-01 11:29:08 +010045 /// @param[in] typeAlignment - Optional parameter. Value of which the size of each value has to be multiple of.
46 void StoreMemToAllocate(std::vector<BufferStorage> bufferStorageVector,
Finn Williamsb1aad422021-10-28 19:07:32 +010047 std::shared_ptr<ICustomAllocator> customAllocator,
Teresa Charlinca588392021-10-01 11:29:08 +010048 size_t typeAlignment = 0);
49
Colm Donelan09026932022-09-14 18:04:00 +010050 /// Allocate the amount of memory indicated by m_BufferSize, and
51 /// point each m_Data to each correspondent Tensor so that they are m_Offset bytes separated.
Teresa Charlinca588392021-10-01 11:29:08 +010052 void Allocate();
53
54 /// Deallocate memory
55 void Deallocate();
56
57private:
58 std::vector<std::pair<Allocator, std::vector<BufferStorage>>> m_AllocatorBufferStoragePairVector;
59};
60
61} // namespace armnn