ArmNN
 25.02
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
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 
virtual const void * Map(bool blocking=true) const =0
Map the tensor data for access.
void * Map(bool blocking=true)
Map the tensor data for access.
void CopyInFrom(const void *) override
const TensorInfo & GetTensorInfo() const
void Unimport() override
Unimport externally allocated memory.
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
virtual void Unmap() const override
Unmap the tensor data.
TensorShape GetShape() const override
Get the number of elements for each dimension ordered from slowest iterating dimension to fastest ite...
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.
TensorShape GetStrides() const override
Get the strides for each dimension ordered from largest to smallest where the smallest value is the s...
virtual MemorySourceFlags GetImportFlags() const override
Get flags describing supported import sources.
void CopyOutTo(void *) const override
Testing support to be able to verify and set tensor data content.
RefTensorHandleDecorator(const TensorInfo &tensorInfo, const RefTensorHandle &parent)
virtual bool Import(void *memory, MemorySource source) override
Import externally allocated memory.
void Unmap()
Unmap the tensor data that was previously mapped with call to Map().
virtual ITensorHandle * GetParent() const override
Get the parent tensor if this is a subtensor.
virtual void Manage() override
Indicate to the memory manager that this resource is active.
virtual const void * Map(bool blocking=true) const=0
Map the tensor data for access.
virtual bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.
RefTensorHandle(const TensorInfo &tensorInfo, std::shared_ptr< RefMemoryManager > &memoryManager)
const TensorInfo & GetTensorInfo() const
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
virtual void Unmap() const override
Unmap the tensor data.
TensorShape GetShape() const override
Get the number of elements for each dimension ordered from slowest iterating dimension to fastest ite...
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.
TensorShape GetStrides() const override
Get the strides for each dimension ordered from largest to smallest where the smallest value is the s...
virtual MemorySourceFlags GetImportFlags() const override
Get flags describing supported import sources.
virtual bool Import(void *memory, MemorySource source) override
Import externally allocated memory.
virtual ITensorHandle * GetParent() const override
Get the parent tensor if this is a subtensor.
virtual void Manage() override
Indicate to the memory manager that this resource is active.
virtual const void * Map(bool blocking=true) const=0
Map the tensor data for access.
virtual bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
Copyright (c) 2021 ARM Limited and Contributors.
MemorySource
Define the Memory Source to reduce copies.
Definition: Types.hpp:246
TensorShape GetUnpaddedTensorStrides(const TensorInfo &tensorInfo)
unsigned int MemorySourceFlags