Teresa Charlin | ca58839 | 2021-10-01 11:29:08 +0100 | [diff] [blame^] | 1 | // |
| 2 | // Copyright © 2021 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include <armnn/backends/ICustomAllocator.hpp> |
| 7 | |
| 8 | namespace armnn |
| 9 | { |
| 10 | struct Allocator |
| 11 | { |
| 12 | /// Pointer to @ICustomAllocator. |
| 13 | ICustomAllocator* m_CustomAllocator{}; |
| 14 | /// Value which the size of each buffer (actual data size + padding) has to be a multiple of. |
| 15 | size_t m_Alignment = 0 ; |
| 16 | }; |
| 17 | |
| 18 | struct TensorMemory |
| 19 | { |
| 20 | /// Number of bytes the value is away from the @BufferStorage.m_Buffer. |
| 21 | size_t m_Offset{}; |
| 22 | /// Pointer to the tensor value. |
| 23 | void* m_Data = nullptr; |
| 24 | /// Identifier to be used by the @LoadedNetwork to order the tensors. |
| 25 | unsigned int m_OutputSlotId{}; |
| 26 | }; |
| 27 | |
| 28 | struct BufferStorage |
| 29 | { |
| 30 | /// Vector of pointer to @TensorMemory. |
| 31 | std::vector<TensorMemory*> m_TensorMemoryVector; |
| 32 | /// Total size of the buffer. |
| 33 | size_t m_BufferSize; |
| 34 | /// Pointer to the first element of the buffer. |
| 35 | void* m_Buffer = nullptr; |
| 36 | }; |
| 37 | |
| 38 | class MemoryManager |
| 39 | { |
| 40 | public: |
| 41 | /// Initialization method to store in @m_AllocatorBufferStoragePairVector all information needed. |
| 42 | /// @param[in] bufferStorageVector - Vector of @BufferStorage. |
| 43 | /// @param[in] customAllocator - Pointer to @ICustomAllocator. |
| 44 | /// @param[in] typeAlignment - Optional parameter. Value of which the size of each value has to be multiple of. |
| 45 | void StoreMemToAllocate(std::vector<BufferStorage> bufferStorageVector, |
| 46 | ICustomAllocator* customAllocator, |
| 47 | size_t typeAlignment = 0); |
| 48 | |
| 49 | /// Allocate the amount of memory indicated by @m_BufferSize, and |
| 50 | /// point each @m_Data to each correspondent Tensor so that they are @m_Offset bytes separated. |
| 51 | void Allocate(); |
| 52 | |
| 53 | /// Deallocate memory |
| 54 | void Deallocate(); |
| 55 | |
| 56 | private: |
| 57 | std::vector<std::pair<Allocator, std::vector<BufferStorage>>> m_AllocatorBufferStoragePairVector; |
| 58 | }; |
| 59 | |
| 60 | } // namespace armnn |