ArmNN
 20.05
SampleTensorHandle.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "SampleTensorHandle.hpp"
7 
8 namespace armnn
9 {
10 
12  std::shared_ptr<SampleMemoryManager> &memoryManager)
13  : m_TensorInfo(tensorInfo),
14  m_MemoryManager(memoryManager),
15  m_Pool(nullptr),
16  m_UnmanagedMemory(nullptr),
17  m_ImportFlags(static_cast<MemorySourceFlags>(MemorySource::Undefined)),
18  m_Imported(false)
19 {
20 
21 }
22 
24  std::shared_ptr<SampleMemoryManager> &memoryManager,
25  MemorySourceFlags importFlags)
26  : m_TensorInfo(tensorInfo),
27  m_MemoryManager(memoryManager),
28  m_Pool(nullptr),
29  m_UnmanagedMemory(nullptr),
30  m_ImportFlags(importFlags),
31  m_Imported(false)
32 {
33 
34 }
35 
37 {
38  if (!m_Pool)
39  {
40  // unmanaged
41  if (!m_Imported)
42  {
43  ::operator delete(m_UnmanagedMemory);
44  }
45  }
46 }
47 
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("SampleTensorHandle::Allocate Trying to allocate a SampleTensorHandle"
70  "that already has allocated memory.");
71  }
72 }
73 
74 const void* SampleTensorHandle::Map(bool /*unused*/) const
75 {
76  return GetPointer();
77 }
78 
79 void* SampleTensorHandle::GetPointer() const
80 {
81  if (m_UnmanagedMemory)
82  {
83  return m_UnmanagedMemory;
84  }
85  else
86  {
87  return m_MemoryManager->GetPointer(m_Pool);
88  }
89 }
90 
91 bool SampleTensorHandle::Import(void* memory, MemorySource source)
92 {
93 
94  if (m_ImportFlags & static_cast<MemorySourceFlags>(source))
95  {
96  if (source == MemorySource::Malloc)
97  {
98  // Check memory alignment
99  constexpr uintptr_t alignment = sizeof(size_t);
100  if (reinterpret_cast<uintptr_t>(memory) % alignment)
101  {
102  if (m_Imported)
103  {
104  m_Imported = false;
105  m_UnmanagedMemory = nullptr;
106  }
107 
108  return false;
109  }
110 
111  // m_UnmanagedMemory not yet allocated.
112  if (!m_Imported && !m_UnmanagedMemory)
113  {
114  m_UnmanagedMemory = memory;
115  m_Imported = true;
116  return true;
117  }
118 
119  // m_UnmanagedMemory initially allocated with Allocate().
120  if (!m_Imported && m_UnmanagedMemory)
121  {
122  return false;
123  }
124 
125  // m_UnmanagedMemory previously imported.
126  if (m_Imported)
127  {
128  m_UnmanagedMemory = memory;
129  return true;
130  }
131  }
132  }
133 
134  return false;
135 }
136 
137 }
virtual const void * Map(bool) const override
Map the tensor data for access.
SampleTensorHandle(const TensorInfo &tensorInfo, std::shared_ptr< SampleMemoryManager > &memoryManager)
DataLayout::NHWC false
unsigned int GetNumBytes() const
Definition: Tensor.cpp:214
unsigned int MemorySourceFlags
Copyright (c) 2020 ARM Limited.
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
virtual void Manage() override
Indicate to the memory manager that this resource is active.
virtual bool Import(void *memory, MemorySource source) override
Import externally allocated memory.