ArmNN
 24.02
RefTensorHandle.cpp
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 #include "RefTensorHandle.hpp"
7 
8 namespace armnn
9 {
10 
11 RefTensorHandle::RefTensorHandle(const TensorInfo& tensorInfo, std::shared_ptr<RefMemoryManager>& memoryManager):
12  m_TensorInfo(tensorInfo),
13  m_MemoryManager(memoryManager),
14  m_Pool(nullptr),
15  m_UnmanagedMemory(nullptr),
16  m_ImportedMemory(nullptr),
17  m_Decorated()
18 {
19 }
20 
22  : m_TensorInfo(tensorInfo),
23  m_Pool(nullptr),
24  m_UnmanagedMemory(nullptr),
25  m_ImportedMemory(nullptr),
26  m_Decorated()
27 {
28 }
29 
31  : m_TensorInfo(tensorInfo),
32  m_MemoryManager(parent.m_MemoryManager),
33  m_Pool(parent.m_Pool),
34  m_UnmanagedMemory(parent.m_UnmanagedMemory),
35  m_ImportedMemory(parent.m_ImportedMemory),
36  m_Decorated()
37 {
38 }
39 
41 {
42  ::operator delete(m_UnmanagedMemory);
43 }
44 
46 {
47  ARMNN_ASSERT_MSG(!m_Pool, "RefTensorHandle::Manage() called twice");
48  ARMNN_ASSERT_MSG(!m_UnmanagedMemory, "RefTensorHandle::Manage() called after Allocate()");
49 
50  if (m_MemoryManager)
51  {
52  m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
53  }
54 }
55 
57 {
58  if (!m_UnmanagedMemory)
59  {
60  if (!m_Pool)
61  {
62  // unmanaged
63  m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes());
64  }
65  else
66  {
67  m_MemoryManager->Allocate(m_Pool);
68  }
69  }
70  else
71  {
72  throw InvalidArgumentException("RefTensorHandle::Allocate Trying to allocate a RefTensorHandle"
73  "that already has allocated memory.");
74  }
75 }
76 
77 const void* RefTensorHandle::Map(bool /*unused*/) const
78 {
79  return GetPointer();
80 }
81 
82 void* RefTensorHandle::GetPointer() const
83 {
84  if (m_ImportedMemory)
85  {
86  return m_ImportedMemory;
87  }
88  else if (m_UnmanagedMemory)
89  {
90  return m_UnmanagedMemory;
91  }
92  else if (m_Pool)
93  {
94  return m_MemoryManager->GetPointer(m_Pool);
95  }
96  else
97  {
98  throw NullPointerException("RefTensorHandle::GetPointer called on unmanaged, unallocated tensor handle");
99  }
100 }
101 
102 void RefTensorHandle::CopyOutTo(void* dest) const
103 {
104  const void* src = GetPointer();
105  if (src == nullptr)
106  {
107  throw NullPointerException("TensorHandle::CopyOutTo called with a null src pointer");
108  }
109  if (dest == nullptr)
110  {
111  throw NullPointerException("TensorHandle::CopyOutTo called with a null dest pointer");
112  }
113  memcpy(dest, src, GetTensorInfo().GetNumBytes());
114 }
115 
116 void RefTensorHandle::CopyInFrom(const void* src)
117 {
118  void* dest = GetPointer();
119  if (dest == nullptr)
120  {
121  throw NullPointerException("RefTensorHandle::CopyInFrom called with a null dest pointer");
122  }
123  if (src == nullptr)
124  {
125  throw NullPointerException("RefTensorHandle::CopyInFrom called with a null src pointer");
126  }
127  memcpy(dest, src, GetTensorInfo().GetNumBytes());
128 }
129 
131 {
132  return static_cast<MemorySourceFlags>(MemorySource::Malloc);
133 }
134 
135 bool RefTensorHandle::Import(void* memory, MemorySource source)
136 {
137  if (source == MemorySource::Malloc)
138  {
139  // Check memory alignment
140  if(!CanBeImported(memory, source))
141  {
142  m_ImportedMemory = nullptr;
143  return false;
144  }
145 
146  m_ImportedMemory = memory;
147  return true;
148  }
149 
150  return false;
151 }
152 
154 {
155  if (source == MemorySource::Malloc)
156  {
157  uintptr_t alignment = GetDataTypeSize(m_TensorInfo.GetDataType());
158  if (reinterpret_cast<uintptr_t>(memory) % alignment)
159  {
160  return false;
161  }
162  return true;
163  }
164  return false;
165 }
166 
167 std::shared_ptr<ITensorHandle> RefTensorHandle::DecorateTensorHandle(const TensorInfo& tensorInfo)
168 {
169  auto decorated = std::make_shared<RefTensorHandleDecorator>(tensorInfo, *this);
170  m_Decorated.emplace_back(decorated);
171  return decorated;
172 }
173 
175 : RefTensorHandle(tensorInfo)
176 , m_TensorInfo(tensorInfo)
177 , m_Parent(parent)
178 {
179 }
180 
182 {
183 }
184 
186 {
187 }
188 
189 const void* RefTensorHandleDecorator::Map(bool unused) const
190 {
191  return m_Parent.Map(unused);
192 }
193 
195 {
196  return static_cast<MemorySourceFlags>(MemorySource::Malloc);
197 }
198 
200 {
201  return false;
202 }
203 
205 {
206  return false;
207 }
208 
209 std::shared_ptr<ITensorHandle> RefTensorHandleDecorator::DecorateTensorHandle(const TensorInfo&)
210 {
211  return nullptr;
212 }
213 
214 
215 }
armnn::MemorySource::Malloc
@ Malloc
armnn::TensorInfo::GetNumBytes
unsigned int GetNumBytes() const
Definition: Tensor.cpp:427
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::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_ASSERT_MSG
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
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::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
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::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::RefTensorHandle
Definition: RefTensorHandle.hpp:17
armnn::RefTensorHandle::RefTensorHandle
RefTensorHandle(const TensorInfo &tensorInfo, std::shared_ptr< RefMemoryManager > &memoryManager)
Definition: RefTensorHandle.cpp:11
armnn::GetDataTypeSize
constexpr unsigned int GetDataTypeSize(DataType dataType)
Definition: TypesUtils.hpp:182
armnn::TensorInfo::GetDataType
DataType GetDataType() const
Definition: Tensor.hpp:200
armnn::RefTensorHandle::Map
virtual const void * Map(bool) const override
Map the tensor data for access.
Definition: RefTensorHandle.cpp:77
armnn::RefTensorHandle::~RefTensorHandle
~RefTensorHandle()
Definition: RefTensorHandle.cpp:40
armnn::MemorySource
MemorySource
Define the Memory Source to reduce copies.
Definition: Types.hpp:244
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
RefTensorHandle.hpp
armnn::RefTensorHandleDecorator::Manage
virtual void Manage() override
Indicate to the memory manager that this resource is active.
Definition: RefTensorHandle.cpp:181
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