ArmNN
 23.08
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  ARMNN_ASSERT(src);
106  memcpy(dest, src, m_TensorInfo.GetNumBytes());
107 }
108 
109 void RefTensorHandle::CopyInFrom(const void* src)
110 {
111  void *dest = GetPointer();
112  ARMNN_ASSERT(dest);
113  memcpy(dest, src, m_TensorInfo.GetNumBytes());
114 }
115 
117 {
118  return static_cast<MemorySourceFlags>(MemorySource::Malloc);
119 }
120 
121 bool RefTensorHandle::Import(void* memory, MemorySource source)
122 {
123  if (source == MemorySource::Malloc)
124  {
125  // Check memory alignment
126  if(!CanBeImported(memory, source))
127  {
128  m_ImportedMemory = nullptr;
129  return false;
130  }
131 
132  m_ImportedMemory = memory;
133  return true;
134  }
135 
136  return false;
137 }
138 
140 {
141  if (source == MemorySource::Malloc)
142  {
143  uintptr_t alignment = GetDataTypeSize(m_TensorInfo.GetDataType());
144  if (reinterpret_cast<uintptr_t>(memory) % alignment)
145  {
146  return false;
147  }
148  return true;
149  }
150  return false;
151 }
152 
153 std::shared_ptr<ITensorHandle> RefTensorHandle::DecorateTensorHandle(const TensorInfo& tensorInfo)
154 {
155  auto decorated = std::make_shared<RefTensorHandleDecorator>(tensorInfo, *this);
156  m_Decorated.emplace_back(decorated);
157  return decorated;
158 }
159 
161 : RefTensorHandle(tensorInfo)
162 , m_TensorInfo(tensorInfo)
163 , m_Parent(parent)
164 {
165 }
166 
168 {
169 }
170 
172 {
173 }
174 
175 const void* RefTensorHandleDecorator::Map(bool unused) const
176 {
177  return m_Parent.Map(unused);
178 }
179 
181 {
182  return static_cast<MemorySourceFlags>(MemorySource::Malloc);
183 }
184 
186 {
187  return false;
188 }
189 
191 {
192  return false;
193 }
194 
195 std::shared_ptr<ITensorHandle> RefTensorHandleDecorator::DecorateTensorHandle(const TensorInfo&)
196 {
197  return nullptr;
198 }
199 
200 
201 }
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
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:175
armnn::RefTensorHandleDecorator::CanBeImported
virtual bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.
Definition: RefTensorHandle.cpp:190
armnn::RefTensorHandle::GetImportFlags
virtual MemorySourceFlags GetImportFlags() const override
Get flags describing supported import sources.
Definition: RefTensorHandle.cpp:116
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:171
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:185
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:195
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:153
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:172
armnn::TensorInfo::GetDataType
DataType GetDataType() const
Definition: Tensor.hpp:198
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:243
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:121
RefTensorHandle.hpp
armnn::RefTensorHandleDecorator::Manage
virtual void Manage() override
Indicate to the memory manager that this resource is active.
Definition: RefTensorHandle.cpp:167
armnn::RefTensorHandleDecorator::RefTensorHandleDecorator
RefTensorHandleDecorator(const TensorInfo &tensorInfo, const RefTensorHandle &parent)
Definition: RefTensorHandle.cpp:160
armnn::RefTensorHandle::CanBeImported
virtual bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.
Definition: RefTensorHandle.cpp:139
armnn::RefTensorHandleDecorator::GetImportFlags
virtual MemorySourceFlags GetImportFlags() const override
Get flags describing supported import sources.
Definition: RefTensorHandle.cpp:180