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