blob: 128f623cd33bcd3a776d343e909de76c0e819d78 [file] [log] [blame]
Matthew Bentham4cefc412019-06-18 16:14:34 +01001//
Mike Kelly4cc341c2023-07-07 15:43:06 +01002// Copyright © 2019-2023 Arm Ltd. All rights reserved.
Matthew Bentham4cefc412019-06-18 16:14:34 +01003// SPDX-License-Identifier: MIT
4//
Mike Kelly4cc341c2023-07-07 15:43:06 +01005
Matthew Bentham4cefc412019-06-18 16:14:34 +01006#pragma once
7
Colm Donelan0c479742021-12-10 12:43:54 +00008#include <armnn/backends/TensorHandle.hpp>
Matthew Bentham4cefc412019-06-18 16:14:34 +01009
Matthew Bentham7c1603a2019-06-21 17:22:23 +010010#include "RefMemoryManager.hpp"
11
Matthew Bentham4cefc412019-06-18 16:14:34 +010012namespace armnn
13{
14
Mike Kelly4cc341c2023-07-07 15:43:06 +010015class RefTensorHandleDecorator;
Matthew Bentham4cefc412019-06-18 16:14:34 +010016// An implementation of ITensorHandle with simple "bump the pointer" memory-management behaviour
17class RefTensorHandle : public ITensorHandle
18{
19public:
Mike Kelly4cc341c2023-07-07 15:43:06 +010020 RefTensorHandle(const TensorInfo& tensorInfo, std::shared_ptr<RefMemoryManager>& memoryManager);
Matthew Bentham4cefc412019-06-18 16:14:34 +010021
Matthew Benthamc30abd82022-11-23 12:11:32 +000022 RefTensorHandle(const TensorInfo& tensorInfo);
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010023
Mike Kelly4cc341c2023-07-07 15:43:06 +010024 RefTensorHandle(const TensorInfo& tensorInfo, const RefTensorHandle& parent);
25
Matthew Bentham4cefc412019-06-18 16:14:34 +010026 ~RefTensorHandle();
27
Matthew Bentham7c1603a2019-06-21 17:22:23 +010028 virtual void Manage() override;
29
30 virtual void Allocate() override;
Matthew Bentham4cefc412019-06-18 16:14:34 +010031
32 virtual ITensorHandle* GetParent() const override
33 {
34 return nullptr;
35 }
36
Matthew Bentham7c1603a2019-06-21 17:22:23 +010037 virtual const void* Map(bool /* blocking = true */) const override;
38 using ITensorHandle::Map;
Matthew Bentham4cefc412019-06-18 16:14:34 +010039
40 virtual void Unmap() const override
41 {}
42
Matthew Bentham4cefc412019-06-18 16:14:34 +010043 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 Benthamc30abd82022-11-23 12:11:32 +000058 virtual MemorySourceFlags GetImportFlags() const override;
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010059
60 virtual bool Import(void* memory, MemorySource source) override;
Nikhil Raj53e06592022-01-05 16:04:08 +000061 virtual bool CanBeImported(void* memory, MemorySource source) override;
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010062
Mike Kelly4cc341c2023-07-07 15:43:06 +010063 virtual std::shared_ptr<ITensorHandle> DecorateTensorHandle(const TensorInfo& tensorInfo) override;
64
Matthew Bentham4cefc412019-06-18 16:14:34 +010065private:
66 // Only used for testing
67 void CopyOutTo(void*) const override;
68 void CopyInFrom(const void*) override;
69
Matthew Bentham7c1603a2019-06-21 17:22:23 +010070 void* GetPointer() const;
Matthew Bentham4cefc412019-06-18 16:14:34 +010071
Matthew Bentham7c1603a2019-06-21 17:22:23 +010072 RefTensorHandle(const RefTensorHandle& other) = delete; // noncopyable
73 RefTensorHandle& operator=(const RefTensorHandle& other) = delete; //noncopyable
Matthew Bentham4cefc412019-06-18 16:14:34 +010074
75 TensorInfo m_TensorInfo;
Matthew Bentham7c1603a2019-06-21 17:22:23 +010076
Mike Kelly4cc341c2023-07-07 15:43:06 +010077 mutable std::shared_ptr<RefMemoryManager> m_MemoryManager;
Matthew Bentham7c1603a2019-06-21 17:22:23 +010078 RefMemoryManager::Pool* m_Pool;
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010079 mutable void* m_UnmanagedMemory;
Matthew Bentham6b5f6742022-11-23 18:17:48 +000080 void* m_ImportedMemory;
Mike Kelly4cc341c2023-07-07 15:43:06 +010081 std::vector<std::shared_ptr<RefTensorHandleDecorator>> m_Decorated;
82};
83
84class RefTensorHandleDecorator : public RefTensorHandle
85{
86public:
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
153private:
154 TensorInfo m_TensorInfo;
155 const RefTensorHandle& m_Parent;
Matthew Bentham4cefc412019-06-18 16:14:34 +0100156};
157
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100158}
Mike Kelly4cc341c2023-07-07 15:43:06 +0100159