ArmNN
 23.05
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_ImportedMemory(nullptr)
16 {
17 
18 }
19 
21  : m_TensorInfo(tensorInfo),
22  m_Pool(nullptr),
23  m_UnmanagedMemory(nullptr),
24  m_ImportedMemory(nullptr)
25 {
26 
27 }
28 
30 {
31  ::operator delete(m_UnmanagedMemory);
32 }
33 
35 {
36  ARMNN_ASSERT_MSG(!m_Pool, "RefTensorHandle::Manage() called twice");
37  ARMNN_ASSERT_MSG(!m_UnmanagedMemory, "RefTensorHandle::Manage() called after Allocate()");
38 
39  if (m_MemoryManager)
40  {
41  m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
42  }
43 }
44 
46 {
47  if (!m_UnmanagedMemory)
48  {
49  if (!m_Pool)
50  {
51  // unmanaged
52  m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes());
53  }
54  else
55  {
56  m_MemoryManager->Allocate(m_Pool);
57  }
58  }
59  else
60  {
61  throw InvalidArgumentException("RefTensorHandle::Allocate Trying to allocate a RefTensorHandle"
62  "that already has allocated memory.");
63  }
64 }
65 
66 const void* RefTensorHandle::Map(bool /*unused*/) const
67 {
68  return GetPointer();
69 }
70 
71 void* RefTensorHandle::GetPointer() const
72 {
73  if (m_ImportedMemory)
74  {
75  return m_ImportedMemory;
76  }
77  else if (m_UnmanagedMemory)
78  {
79  return m_UnmanagedMemory;
80  }
81  else if (m_Pool)
82  {
83  return m_MemoryManager->GetPointer(m_Pool);
84  }
85  else
86  {
87  throw NullPointerException("RefTensorHandle::GetPointer called on unmanaged, unallocated tensor handle");
88  }
89 }
90 
91 void RefTensorHandle::CopyOutTo(void* dest) const
92 {
93  const void *src = GetPointer();
94  ARMNN_ASSERT(src);
95  memcpy(dest, src, m_TensorInfo.GetNumBytes());
96 }
97 
98 void RefTensorHandle::CopyInFrom(const void* src)
99 {
100  void *dest = GetPointer();
101  ARMNN_ASSERT(dest);
102  memcpy(dest, src, m_TensorInfo.GetNumBytes());
103 }
104 
106 {
107  return static_cast<MemorySourceFlags>(MemorySource::Malloc);
108 }
109 
110 bool RefTensorHandle::Import(void* memory, MemorySource source)
111 {
112  if (source == MemorySource::Malloc)
113  {
114  // Check memory alignment
115  if(!CanBeImported(memory, source))
116  {
117  m_ImportedMemory = nullptr;
118  return false;
119  }
120 
121  m_ImportedMemory = memory;
122  return true;
123  }
124 
125  return false;
126 }
127 
129 {
130  if (source == MemorySource::Malloc)
131  {
132  uintptr_t alignment = GetDataTypeSize(m_TensorInfo.GetDataType());
133  if (reinterpret_cast<uintptr_t>(memory) % alignment)
134  {
135  return false;
136  }
137  return true;
138  }
139  return false;
140 }
141 
142 }
armnn::RefTensorHandle::GetImportFlags
virtual MemorySourceFlags GetImportFlags() const override
Get flags describing supported import sources.
Definition: RefTensorHandle.cpp:105
armnn::MemorySource::Malloc
@ Malloc
armnn::GetDataTypeSize
constexpr unsigned int GetDataTypeSize(DataType dataType)
Definition: TypesUtils.hpp:169
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::RefTensorHandle::RefTensorHandle
RefTensorHandle(const TensorInfo &tensorInfo, std::shared_ptr< RefMemoryManager > &memoryManager)
Definition: RefTensorHandle.cpp:10
armnn::RefTensorHandle::Import
virtual bool Import(void *memory, MemorySource source) override
Import externally allocated memory.
Definition: RefTensorHandle.cpp:110
armnn::MemorySource
MemorySource
Define the Memory Source to reduce copies.
Definition: Types.hpp:241
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::RefTensorHandle::Manage
virtual void Manage() override
Indicate to the memory manager that this resource is active.
Definition: RefTensorHandle.cpp:34
ARMNN_ASSERT_MSG
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
armnn::RefTensorHandle::Map
virtual const void * Map(bool) const override
Map the tensor data for access.
Definition: RefTensorHandle.cpp:66
RefTensorHandle.hpp
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
armnn::TensorInfo::GetNumBytes
unsigned int GetNumBytes() const
Definition: Tensor.cpp:427
armnn::RefTensorHandle::CanBeImported
virtual bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.
Definition: RefTensorHandle.cpp:128
armnn::RefTensorHandle::~RefTensorHandle
~RefTensorHandle()
Definition: RefTensorHandle.cpp:29
armnn::MemorySourceFlags
unsigned int MemorySourceFlags
Definition: MemorySources.hpp:15
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::TensorInfo::GetDataType
DataType GetDataType() const
Definition: Tensor.hpp:198
armnn::RefTensorHandle::Allocate
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
Definition: RefTensorHandle.cpp:45