Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 1 | // |
Mike Kelly | 4cc341c | 2023-07-07 15:43:06 +0100 | [diff] [blame] | 2 | // Copyright © 2019-2023 Arm Ltd. All rights reserved. |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
Mike Kelly | 4cc341c | 2023-07-07 15:43:06 +0100 | [diff] [blame] | 5 | |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 6 | #pragma once |
| 7 | |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 8 | #include <armnn/backends/TensorHandle.hpp> |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 9 | |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 10 | #include "RefMemoryManager.hpp" |
| 11 | |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 12 | namespace armnn |
| 13 | { |
| 14 | |
Mike Kelly | 4cc341c | 2023-07-07 15:43:06 +0100 | [diff] [blame] | 15 | class RefTensorHandleDecorator; |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 16 | // An implementation of ITensorHandle with simple "bump the pointer" memory-management behaviour |
| 17 | class RefTensorHandle : public ITensorHandle |
| 18 | { |
| 19 | public: |
Mike Kelly | 4cc341c | 2023-07-07 15:43:06 +0100 | [diff] [blame] | 20 | RefTensorHandle(const TensorInfo& tensorInfo, std::shared_ptr<RefMemoryManager>& memoryManager); |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 21 | |
Matthew Bentham | c30abd8 | 2022-11-23 12:11:32 +0000 | [diff] [blame] | 22 | RefTensorHandle(const TensorInfo& tensorInfo); |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 23 | |
Mike Kelly | 4cc341c | 2023-07-07 15:43:06 +0100 | [diff] [blame] | 24 | RefTensorHandle(const TensorInfo& tensorInfo, const RefTensorHandle& parent); |
| 25 | |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 26 | ~RefTensorHandle(); |
| 27 | |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 28 | virtual void Manage() override; |
| 29 | |
| 30 | virtual void Allocate() override; |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 31 | |
| 32 | virtual ITensorHandle* GetParent() const override |
| 33 | { |
| 34 | return nullptr; |
| 35 | } |
| 36 | |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 37 | virtual const void* Map(bool /* blocking = true */) const override; |
| 38 | using ITensorHandle::Map; |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 39 | |
| 40 | virtual void Unmap() const override |
| 41 | {} |
| 42 | |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 43 | TensorShape GetStrides() const override |
| 44 | { |
| 45 | return GetUnpaddedTensorStrides(m_TensorInfo); |
| 46 | } |
| 47 | |
| 48 | TensorShape GetShape() const override |
| 49 | { |
| 50 | return m_TensorInfo.GetShape(); |
| 51 | } |
| 52 | |
| 53 | const TensorInfo& GetTensorInfo() const |
| 54 | { |
| 55 | return m_TensorInfo; |
| 56 | } |
| 57 | |
Matthew Bentham | c30abd8 | 2022-11-23 12:11:32 +0000 | [diff] [blame] | 58 | virtual MemorySourceFlags GetImportFlags() const override; |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 59 | |
| 60 | virtual bool Import(void* memory, MemorySource source) override; |
Nikhil Raj | 53e0659 | 2022-01-05 16:04:08 +0000 | [diff] [blame] | 61 | virtual bool CanBeImported(void* memory, MemorySource source) override; |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 62 | |
Mike Kelly | 4cc341c | 2023-07-07 15:43:06 +0100 | [diff] [blame] | 63 | virtual std::shared_ptr<ITensorHandle> DecorateTensorHandle(const TensorInfo& tensorInfo) override; |
| 64 | |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 65 | private: |
| 66 | // Only used for testing |
| 67 | void CopyOutTo(void*) const override; |
| 68 | void CopyInFrom(const void*) override; |
| 69 | |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 70 | void* GetPointer() const; |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 71 | |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 72 | RefTensorHandle(const RefTensorHandle& other) = delete; // noncopyable |
| 73 | RefTensorHandle& operator=(const RefTensorHandle& other) = delete; //noncopyable |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 74 | |
| 75 | TensorInfo m_TensorInfo; |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 76 | |
Mike Kelly | 4cc341c | 2023-07-07 15:43:06 +0100 | [diff] [blame] | 77 | mutable std::shared_ptr<RefMemoryManager> m_MemoryManager; |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 78 | RefMemoryManager::Pool* m_Pool; |
Narumol Prangnawarat | 3b90af6 | 2020-06-26 11:00:21 +0100 | [diff] [blame] | 79 | mutable void* m_UnmanagedMemory; |
Matthew Bentham | 6b5f674 | 2022-11-23 18:17:48 +0000 | [diff] [blame] | 80 | void* m_ImportedMemory; |
Mike Kelly | 4cc341c | 2023-07-07 15:43:06 +0100 | [diff] [blame] | 81 | std::vector<std::shared_ptr<RefTensorHandleDecorator>> m_Decorated; |
| 82 | }; |
| 83 | |
| 84 | class RefTensorHandleDecorator : public RefTensorHandle |
| 85 | { |
| 86 | public: |
| 87 | RefTensorHandleDecorator(const TensorInfo& tensorInfo, const RefTensorHandle& parent); |
| 88 | |
| 89 | ~RefTensorHandleDecorator() = default; |
| 90 | |
| 91 | virtual void Manage() override; |
| 92 | |
| 93 | virtual void Allocate() override; |
| 94 | |
| 95 | virtual ITensorHandle* GetParent() const override |
| 96 | { |
| 97 | return nullptr; |
| 98 | } |
| 99 | |
| 100 | virtual const void* Map(bool /* blocking = true */) const override; |
| 101 | using ITensorHandle::Map; |
| 102 | |
| 103 | virtual void Unmap() const override |
| 104 | {} |
| 105 | |
| 106 | TensorShape GetStrides() const override |
| 107 | { |
| 108 | return GetUnpaddedTensorStrides(m_TensorInfo); |
| 109 | } |
| 110 | |
| 111 | TensorShape GetShape() const override |
| 112 | { |
| 113 | return m_TensorInfo.GetShape(); |
| 114 | } |
| 115 | |
| 116 | const TensorInfo& GetTensorInfo() const |
| 117 | { |
| 118 | return m_TensorInfo; |
| 119 | } |
| 120 | |
| 121 | virtual MemorySourceFlags GetImportFlags() const override; |
| 122 | |
| 123 | virtual bool Import(void* memory, MemorySource source) override; |
| 124 | virtual bool CanBeImported(void* memory, MemorySource source) override; |
| 125 | |
| 126 | virtual std::shared_ptr<ITensorHandle> DecorateTensorHandle(const TensorInfo& tensorInfo) override; |
| 127 | |
| 128 | /// Map the tensor data for access. Must be paired with call to Unmap(). |
| 129 | /// \param blocking hint to block the calling thread until all other accesses are complete. (backend dependent) |
| 130 | /// \return pointer to the first element of the mapped data. |
| 131 | void* Map(bool blocking=true) |
| 132 | { |
| 133 | return const_cast<void*>(static_cast<const ITensorHandle*>(this)->Map(blocking)); |
| 134 | } |
| 135 | |
| 136 | /// Unmap the tensor data that was previously mapped with call to Map(). |
| 137 | void Unmap() |
| 138 | { |
| 139 | return static_cast<const ITensorHandle*>(this)->Unmap(); |
| 140 | } |
| 141 | |
| 142 | /// Testing support to be able to verify and set tensor data content |
| 143 | void CopyOutTo(void* /* memory */) const override |
| 144 | {}; |
| 145 | |
| 146 | void CopyInFrom(const void* /* memory */) override |
| 147 | {}; |
| 148 | |
| 149 | /// Unimport externally allocated memory |
| 150 | void Unimport() override |
| 151 | {}; |
| 152 | |
| 153 | private: |
| 154 | TensorInfo m_TensorInfo; |
| 155 | const RefTensorHandle& m_Parent; |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 156 | }; |
| 157 | |
Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 158 | } |
Mike Kelly | 4cc341c | 2023-07-07 15:43:06 +0100 | [diff] [blame] | 159 | |