ArmNN
 24.08
RefTensorHandle.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019-2023 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
9 
10 #include "RefMemoryManager.hpp"
11 
12 namespace armnn
13 {
14 
15 class RefTensorHandleDecorator;
16 // An implementation of ITensorHandle with simple "bump the pointer" memory-management behaviour
18 {
19 public:
20  RefTensorHandle(const TensorInfo& tensorInfo, std::shared_ptr<RefMemoryManager>& memoryManager);
21 
22  RefTensorHandle(const TensorInfo& tensorInfo);
23 
24  RefTensorHandle(const TensorInfo& tensorInfo, const RefTensorHandle& parent);
25 
27 
28  virtual void Manage() override;
29 
30  virtual void Allocate() override;
31 
32  virtual ITensorHandle* GetParent() const override
33  {
34  return nullptr;
35  }
36 
37  virtual const void* Map(bool /* blocking = true */) const override;
38  using ITensorHandle::Map;
39 
40  virtual void Unmap() const override
41  {}
42 
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 
58  virtual MemorySourceFlags GetImportFlags() const override;
59 
60  virtual bool Import(void* memory, MemorySource source) override;
61  virtual bool CanBeImported(void* memory, MemorySource source) override;
62 
63  virtual std::shared_ptr<ITensorHandle> DecorateTensorHandle(const TensorInfo& tensorInfo) override;
64 
65 private:
66  // Only used for testing
67  void CopyOutTo(void*) const override;
68  void CopyInFrom(const void*) override;
69 
70  void* GetPointer() const;
71 
72  RefTensorHandle(const RefTensorHandle& other) = delete; // noncopyable
73  RefTensorHandle& operator=(const RefTensorHandle& other) = delete; //noncopyable
74 
75  TensorInfo m_TensorInfo;
76 
77  mutable std::shared_ptr<RefMemoryManager> m_MemoryManager;
78  RefMemoryManager::Pool* m_Pool;
79  mutable void* m_UnmanagedMemory;
80  void* m_ImportedMemory;
81  std::vector<std::shared_ptr<RefTensorHandleDecorator>> m_Decorated;
82 };
83 
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;
156 };
157 
158 }
159 
armnn::RefTensorHandleDecorator::CopyInFrom
void CopyInFrom(const void *) override
Definition: RefTensorHandle.hpp:146
armnn::RefTensorHandleDecorator::GetParent
virtual ITensorHandle * GetParent() const override
Get the parent tensor if this is a subtensor.
Definition: RefTensorHandle.hpp:95
armnn::RefTensorHandleDecorator::GetShape
TensorShape GetShape() const override
Get the number of elements for each dimension ordered from slowest iterating dimension to fastest ite...
Definition: RefTensorHandle.hpp:111
armnn::RefTensorHandleDecorator::Map
virtual const void * Map(bool) const override
Map the tensor data for access.
Definition: RefTensorHandle.cpp:189
armnn::RefTensorHandleDecorator::CanBeImported
virtual bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.
Definition: RefTensorHandle.cpp:204
armnn::RefTensorHandle::GetImportFlags
virtual MemorySourceFlags GetImportFlags() const override
Get flags describing supported import sources.
Definition: RefTensorHandle.cpp:130
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::MemorySourceFlags
unsigned int MemorySourceFlags
Definition: MemorySources.hpp:15
armnn::GetUnpaddedTensorStrides
TensorShape GetUnpaddedTensorStrides(const TensorInfo &tensorInfo)
Definition: TensorHandle.cpp:15
armnn::ITensorHandle
Definition: ITensorHandle.hpp:16
armnn::RefTensorHandle::Unmap
virtual void Unmap() const override
Unmap the tensor data.
Definition: RefTensorHandle.hpp:40
armnn::RefMemoryManager::Pool
Definition: RefMemoryManager.hpp:33
armnn::RefTensorHandle::Manage
virtual void Manage() override
Indicate to the memory manager that this resource is active.
Definition: RefTensorHandle.cpp:45
armnn::RefTensorHandleDecorator::Allocate
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
Definition: RefTensorHandle.cpp:185
armnn::RefTensorHandleDecorator::Map
void * Map(bool blocking=true)
Map the tensor data for access.
Definition: RefTensorHandle.hpp:131
armnn::RefTensorHandle::GetShape
TensorShape GetShape() const override
Get the number of elements for each dimension ordered from slowest iterating dimension to fastest ite...
Definition: RefTensorHandle.hpp:48
armnn::RefTensorHandleDecorator::Import
virtual bool Import(void *memory, MemorySource source) override
Import externally allocated memory.
Definition: RefTensorHandle.cpp:199
armnn::RefTensorHandle::GetTensorInfo
const TensorInfo & GetTensorInfo() const
Definition: RefTensorHandle.hpp:53
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::RefTensorHandleDecorator::DecorateTensorHandle
virtual std::shared_ptr< ITensorHandle > DecorateTensorHandle(const TensorInfo &tensorInfo) override
Returns a decorated version of this TensorHandle allowing us to override the TensorInfo for it.
Definition: RefTensorHandle.cpp:209
RefMemoryManager.hpp
armnn::RefTensorHandle::DecorateTensorHandle
virtual std::shared_ptr< ITensorHandle > DecorateTensorHandle(const TensorInfo &tensorInfo) override
Returns a decorated version of this TensorHandle allowing us to override the TensorInfo for it.
Definition: RefTensorHandle.cpp:167
armnn::RefTensorHandle
Definition: RefTensorHandle.hpp:17
armnn::RefTensorHandle::RefTensorHandle
RefTensorHandle(const TensorInfo &tensorInfo, std::shared_ptr< RefMemoryManager > &memoryManager)
Definition: RefTensorHandle.cpp:11
armnn::RefTensorHandleDecorator::Unmap
virtual void Unmap() const override
Unmap the tensor data.
Definition: RefTensorHandle.hpp:103
armnn::RefTensorHandleDecorator::CopyOutTo
void CopyOutTo(void *) const override
Testing support to be able to verify and set tensor data content.
Definition: RefTensorHandle.hpp:143
armnn::RefTensorHandle::GetParent
virtual ITensorHandle * GetParent() const override
Get the parent tensor if this is a subtensor.
Definition: RefTensorHandle.hpp:32
armnn::RefTensorHandleDecorator::Unmap
void Unmap()
Unmap the tensor data that was previously mapped with call to Map().
Definition: RefTensorHandle.hpp:137
armnn::RefTensorHandleDecorator::Unimport
void Unimport() override
Unimport externally allocated memory.
Definition: RefTensorHandle.hpp:150
armnn::RefTensorHandleDecorator::~RefTensorHandleDecorator
~RefTensorHandleDecorator()=default
TensorHandle.hpp
armnn::RefTensorHandle::Map
virtual const void * Map(bool) const override
Map the tensor data for access.
Definition: RefTensorHandle.cpp:77
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
armnn::RefTensorHandle::~RefTensorHandle
~RefTensorHandle()
Definition: RefTensorHandle.cpp:40
armnn::RefTensorHandleDecorator::GetStrides
TensorShape GetStrides() const override
Get the strides for each dimension ordered from largest to smallest where the smallest value is the s...
Definition: RefTensorHandle.hpp:106
armnn::MemorySource
MemorySource
Define the Memory Source to reduce copies.
Definition: Types.hpp:244
armnn::RefTensorHandleDecorator::GetTensorInfo
const TensorInfo & GetTensorInfo() const
Definition: RefTensorHandle.hpp:116
armnn::RefTensorHandle::Allocate
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
Definition: RefTensorHandle.cpp:56
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::RefTensorHandle::Import
virtual bool Import(void *memory, MemorySource source) override
Import externally allocated memory.
Definition: RefTensorHandle.cpp:135
armnn::RefTensorHandleDecorator::Manage
virtual void Manage() override
Indicate to the memory manager that this resource is active.
Definition: RefTensorHandle.cpp:181
armnn::RefTensorHandle::GetStrides
TensorShape GetStrides() const override
Get the strides for each dimension ordered from largest to smallest where the smallest value is the s...
Definition: RefTensorHandle.hpp:43
armnn::RefTensorHandleDecorator::RefTensorHandleDecorator
RefTensorHandleDecorator(const TensorInfo &tensorInfo, const RefTensorHandle &parent)
Definition: RefTensorHandle.cpp:174
armnn::RefTensorHandle::CanBeImported
virtual bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.
Definition: RefTensorHandle.cpp:153
armnn::RefTensorHandleDecorator::GetImportFlags
virtual MemorySourceFlags GetImportFlags() const override
Get flags describing supported import sources.
Definition: RefTensorHandle.cpp:194
armnn::RefTensorHandleDecorator
Definition: RefTensorHandle.hpp:84
armnn::ITensorHandle::Map
virtual const void * Map(bool blocking=true) const =0
Map the tensor data for access.