ArmNN
 22.11
RefTensorHandle.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "RefTensorHandle.hpp"
6 
7 namespace armnn
8 {
9 
10 RefTensorHandle::RefTensorHandle(const TensorInfo &tensorInfo, std::shared_ptr<RefMemoryManager> &memoryManager):
11  m_TensorInfo(tensorInfo),
12  m_MemoryManager(memoryManager),
13  m_Pool(nullptr),
14  m_UnmanagedMemory(nullptr),
15  m_ImportFlags(static_cast<MemorySourceFlags>(MemorySource::Undefined)),
16  m_Imported(false),
17  m_IsImportEnabled(false)
18 {
19 
20 }
21 
23  MemorySourceFlags importFlags)
24  : m_TensorInfo(tensorInfo),
25  m_Pool(nullptr),
26  m_UnmanagedMemory(nullptr),
27  m_ImportFlags(importFlags),
28  m_Imported(false),
29  m_IsImportEnabled(true)
30 {
31 
32 }
33 
35 {
36  if (!m_Pool)
37  {
38  // unmanaged
39  if (!m_Imported)
40  {
41  ::operator delete(m_UnmanagedMemory);
42  }
43  }
44 }
45 
47 {
48  if (!m_IsImportEnabled)
49  {
50  ARMNN_ASSERT_MSG(!m_Pool, "RefTensorHandle::Manage() called twice");
51  ARMNN_ASSERT_MSG(!m_UnmanagedMemory, "RefTensorHandle::Manage() called after Allocate()");
52 
53  m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
54  }
55 }
56 
58 {
59  // If import is enabled, do not allocate the tensor
60  if (!m_IsImportEnabled)
61  {
62 
63  if (!m_UnmanagedMemory)
64  {
65  if (!m_Pool)
66  {
67  // unmanaged
68  m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes());
69  }
70  else
71  {
72  m_MemoryManager->Allocate(m_Pool);
73  }
74  }
75  else
76  {
77  throw InvalidArgumentException("RefTensorHandle::Allocate Trying to allocate a RefTensorHandle"
78  "that already has allocated memory.");
79  }
80  }
81 }
82 
83 const void* RefTensorHandle::Map(bool /*unused*/) const
84 {
85  return GetPointer();
86 }
87 
88 void* RefTensorHandle::GetPointer() const
89 {
90  if (m_UnmanagedMemory)
91  {
92  return m_UnmanagedMemory;
93  }
94  else if (m_Pool)
95  {
96  return m_MemoryManager->GetPointer(m_Pool);
97  }
98  else
99  {
100  throw NullPointerException("RefTensorHandle::GetPointer called on unmanaged, unallocated tensor handle");
101  }
102 }
103 
104 void RefTensorHandle::CopyOutTo(void* dest) const
105 {
106  const void *src = GetPointer();
107  ARMNN_ASSERT(src);
108  memcpy(dest, src, m_TensorInfo.GetNumBytes());
109 }
110 
111 void RefTensorHandle::CopyInFrom(const void* src)
112 {
113  void *dest = GetPointer();
114  ARMNN_ASSERT(dest);
115  memcpy(dest, src, m_TensorInfo.GetNumBytes());
116 }
117 
118 bool RefTensorHandle::Import(void* memory, MemorySource source)
119 {
120  if (m_ImportFlags & static_cast<MemorySourceFlags>(source))
121  {
122  if (m_IsImportEnabled && source == MemorySource::Malloc)
123  {
124  // Check memory alignment
125  if(!CanBeImported(memory, source))
126  {
127  if (m_Imported)
128  {
129  m_Imported = false;
130  m_UnmanagedMemory = nullptr;
131  }
132  return false;
133  }
134 
135  // m_UnmanagedMemory not yet allocated.
136  if (!m_Imported && !m_UnmanagedMemory)
137  {
138  m_UnmanagedMemory = memory;
139  m_Imported = true;
140  return true;
141  }
142 
143  // m_UnmanagedMemory initially allocated with Allocate().
144  if (!m_Imported && m_UnmanagedMemory)
145  {
146  return false;
147  }
148 
149  // m_UnmanagedMemory previously imported.
150  if (m_Imported)
151  {
152  m_UnmanagedMemory = memory;
153  return true;
154  }
155  }
156  }
157 
158  return false;
159 }
160 
162 {
163  if (m_ImportFlags & static_cast<MemorySourceFlags>(source))
164  {
165  if (m_IsImportEnabled && source == MemorySource::Malloc)
166  {
167  uintptr_t alignment = GetDataTypeSize(m_TensorInfo.GetDataType());
168  if (reinterpret_cast<uintptr_t>(memory) % alignment)
169  {
170  return false;
171  }
172  return true;
173  }
174  }
175  return false;
176 }
177 
178 }
virtual bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.
unsigned int GetNumBytes() const
Definition: Tensor.cpp:427
virtual const void * Map(bool) const override
Map the tensor data for access.
unsigned int MemorySourceFlags
Copyright (c) 2021 ARM Limited and Contributors.
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
DataType GetDataType() const
Definition: Tensor.hpp:198
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
RefTensorHandle(const TensorInfo &tensorInfo, std::shared_ptr< RefMemoryManager > &memoryManager)
MemorySource
Define the Memory Source to reduce copies.
Definition: Types.hpp:230
virtual bool Import(void *memory, MemorySource source) override
Import externally allocated memory.
virtual void Manage() override
Indicate to the memory manager that this resource is active.
constexpr unsigned int GetDataTypeSize(DataType dataType)
Definition: TypesUtils.hpp:151