ArmNN
 20.02
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 {
18 
19 }
20 
21 RefTensorHandle::RefTensorHandle(const TensorInfo& tensorInfo, std::shared_ptr<RefMemoryManager> &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  BOOST_ASSERT_MSG(!m_Pool, "RefTensorHandle::Manage() called twice");
48  BOOST_ASSERT_MSG(!m_UnmanagedMemory, "RefTensorHandle::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("RefTensorHandle::Allocate Trying to allocate a RefTensorHandle"
70  "that already has allocated memory.");
71  }
72 }
73 
74 const void* RefTensorHandle::Map(bool /*unused*/) const
75 {
76  return GetPointer();
77 }
78 
79 void* RefTensorHandle::GetPointer() const
80 {
81  if (m_UnmanagedMemory)
82  {
83  return m_UnmanagedMemory;
84  }
85  else
86  {
87  BOOST_ASSERT_MSG(m_Pool, "RefTensorHandle::GetPointer called on unmanaged, unallocated tensor handle");
88  return m_MemoryManager->GetPointer(m_Pool);
89  }
90 }
91 
92 void RefTensorHandle::CopyOutTo(void* dest) const
93 {
94  const void *src = GetPointer();
95  BOOST_ASSERT(src);
96  memcpy(dest, src, m_TensorInfo.GetNumBytes());
97 }
98 
99 void RefTensorHandle::CopyInFrom(const void* src)
100 {
101  void *dest = GetPointer();
102  BOOST_ASSERT(dest);
103  memcpy(dest, src, m_TensorInfo.GetNumBytes());
104 }
105 
106 bool RefTensorHandle::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 }
unsigned int GetNumBytes() const
Definition: Tensor.cpp:213
virtual const void * Map(bool) const override
Map the tensor data for access.
unsigned int MemorySourceFlags
Copyright (c) 2020 ARM Limited.
DataLayout::NHWC false
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
RefTensorHandle(const TensorInfo &tensorInfo, std::shared_ptr< RefMemoryManager > &memoryManager)
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.