ArmNN
 24.08
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 sdb // sample dynamic backend
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<armnn::MemorySourceFlags>(armnn::MemorySource::Undefined)),
18  m_Imported(false)
19 {
20 
21 }
22 
24  armnn::MemorySourceFlags importFlags)
25  : m_TensorInfo(tensorInfo),
26  m_MemoryManager(nullptr),
27  m_Pool(nullptr),
28  m_UnmanagedMemory(nullptr),
29  m_ImportFlags(importFlags),
30  m_Imported(true)
31 {
32 
33 }
34 
36 {
37  if (!m_Pool)
38  {
39  // unmanaged
40  if (!m_Imported)
41  {
42  ::operator delete(m_UnmanagedMemory);
43  }
44  }
45 }
46 
48 {
49  m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
50 }
51 
53 {
54  if (!m_UnmanagedMemory)
55  {
56  if (!m_Pool)
57  {
58  // unmanaged
59  m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes());
60  }
61  else
62  {
63  m_MemoryManager->Allocate(m_Pool);
64  }
65  }
66  else
67  {
68  throw armnn::InvalidArgumentException("SampleTensorHandle::Allocate Trying to allocate a "
69  "SampleTensorHandle that already has allocated "
70  "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 
92 {
93 
94  if (m_ImportFlags & static_cast<armnn::MemorySourceFlags>(source))
95  {
96  if (source == armnn::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 void SampleTensorHandle::CopyOutTo(void* dest) const
138 {
139  const void* src = GetPointer();
140  if (dest == nullptr)
141  {
142  throw armnn::Exception("SampleTensorHandle:CopyOutTo: Destination Ptr is null");
143  }
144  if (src == nullptr)
145  {
146  throw armnn::Exception("SampleTensorHandle:CopyOutTo: Source Ptr is null");
147  }
148  memcpy(dest, src, m_TensorInfo.GetNumBytes());
149 }
150 
151 void SampleTensorHandle::CopyInFrom(const void* src)
152 {
153  void* dest = GetPointer();
154  if (src == nullptr)
155  {
156  throw armnn::Exception("SampleTensorHandle:CopyInFrom: Source Ptr is null");
157  }
158  if (dest == nullptr)
159  {
160  throw armnn::Exception("SampleTensorHandle:CopyInFrom: Destination Ptr is null");
161  }
162  memcpy(dest, src, m_TensorInfo.GetNumBytes());
163 }
164 
165 } // namespace sdb
armnn::MemorySource::Malloc
@ Malloc
sdb::SampleTensorHandle::~SampleTensorHandle
~SampleTensorHandle()
Definition: SampleTensorHandle.cpp:35
armnn::TensorInfo::GetNumBytes
unsigned int GetNumBytes() const
Definition: Tensor.cpp:427
SampleTensorHandle.hpp
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::MemorySourceFlags
unsigned int MemorySourceFlags
Definition: MemorySources.hpp:15
sdb::SampleTensorHandle::Allocate
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
Definition: SampleTensorHandle.cpp:52
sdb
Definition: SampleDynamicAdditionWorkload.cpp:11
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::Exception
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
sdb::SampleTensorHandle::Manage
virtual void Manage() override
Indicate to the memory manager that this resource is active.
Definition: SampleTensorHandle.cpp:47
sdb::SampleTensorHandle::Map
virtual const void * Map(bool) const override
Map the tensor data for access.
Definition: SampleTensorHandle.cpp:74
armnn::MemorySource
MemorySource
Define the Memory Source to reduce copies.
Definition: Types.hpp:244
sdb::SampleTensorHandle::Import
virtual bool Import(void *memory, armnn::MemorySource source) override
Import externally allocated memory.
Definition: SampleTensorHandle.cpp:91
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
sdb::SampleTensorHandle::SampleTensorHandle
SampleTensorHandle(const armnn::TensorInfo &tensorInfo, std::shared_ptr< SampleMemoryManager > &memoryManager)
Definition: SampleTensorHandle.cpp:11