ArmNN
 25.11
Loading...
Searching...
No Matches
RefTensorHandle.cpp
Go to the documentation of this file.
1//
2// Copyright © 2019-2024 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "RefTensorHandle.hpp"
7
8namespace armnn
9{
10
11RefTensorHandle::RefTensorHandle(const TensorInfo& tensorInfo, std::shared_ptr<RefMemoryManager>& memoryManager):
12 m_TensorInfo(tensorInfo),
13 m_MemoryManager(memoryManager),
14 m_Pool(nullptr),
15 m_UnmanagedMemory(nullptr),
16 m_ImportedMemory(nullptr),
17 m_Decorated()
18{
19}
20
22 : m_TensorInfo(tensorInfo),
23 m_Pool(nullptr),
24 m_UnmanagedMemory(nullptr),
25 m_ImportedMemory(nullptr),
26 m_Decorated()
27{
28}
29
31 : m_TensorInfo(tensorInfo),
32 m_MemoryManager(parent.m_MemoryManager),
33 m_Pool(parent.m_Pool),
34 m_UnmanagedMemory(parent.m_UnmanagedMemory),
35 m_ImportedMemory(parent.m_ImportedMemory),
36 m_Decorated()
37{
38}
39
41{
42 ::operator delete(m_UnmanagedMemory);
43}
44
46{
47 ARMNN_THROW_MSG_IF_FALSE(!m_Pool, RuntimeException, "RefTensorHandle::Manage() called twice");
48 ARMNN_THROW_MSG_IF_FALSE(!m_UnmanagedMemory, RuntimeException, "RefTensorHandle::Manage() called after Allocate()");
49
50 if (m_MemoryManager)
51 {
52 m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
53 }
54}
55
57{
58 if (!m_UnmanagedMemory)
59 {
60 if (!m_Pool)
61 {
62 // unmanaged
63 m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes());
64 }
65 else
66 {
67 m_MemoryManager->Allocate(m_Pool);
68 }
69 }
70 else
71 {
72 throw InvalidArgumentException("RefTensorHandle::Allocate Trying to allocate a RefTensorHandle"
73 "that already has allocated memory.");
74 }
75}
76
77const void* RefTensorHandle::Map(bool /*unused*/) const
78{
79 return GetPointer();
80}
81
82void* RefTensorHandle::GetPointer() const
83{
84 if (m_ImportedMemory)
85 {
86 return m_ImportedMemory;
87 }
88 else if (m_UnmanagedMemory)
89 {
90 return m_UnmanagedMemory;
91 }
92 else if (m_Pool)
93 {
94 return m_MemoryManager->GetPointer(m_Pool);
95 }
96 else
97 {
98 throw NullPointerException("RefTensorHandle::GetPointer called on unmanaged, unallocated tensor handle");
99 }
100}
101
102void RefTensorHandle::CopyOutTo(void* dest) const
103{
104 const void* src = GetPointer();
105 if (src == nullptr)
106 {
107 throw NullPointerException("TensorHandle::CopyOutTo called with a null src pointer");
108 }
109 if (dest == nullptr)
110 {
111 throw NullPointerException("TensorHandle::CopyOutTo called with a null dest pointer");
112 }
113 memcpy(dest, src, GetTensorInfo().GetNumBytes());
114}
115
116void RefTensorHandle::CopyInFrom(const void* src)
117{
118 void* dest = GetPointer();
119 if (dest == nullptr)
120 {
121 throw NullPointerException("RefTensorHandle::CopyInFrom called with a null dest pointer");
122 }
123 if (src == nullptr)
124 {
125 throw NullPointerException("RefTensorHandle::CopyInFrom called with a null src pointer");
126 }
127 memcpy(dest, src, GetTensorInfo().GetNumBytes());
128}
129
134
135bool RefTensorHandle::Import(void* memory, MemorySource source)
136{
137 if (source == MemorySource::Malloc)
138 {
139 // Check memory alignment
140 if(!CanBeImported(memory, source))
141 {
142 m_ImportedMemory = nullptr;
143 return false;
144 }
145
146 m_ImportedMemory = memory;
147 return true;
148 }
149
150 return false;
151}
152
154{
155 if (source == MemorySource::Malloc)
156 {
157 uintptr_t alignment = GetDataTypeSize(m_TensorInfo.GetDataType());
158 if (reinterpret_cast<uintptr_t>(memory) % alignment)
159 {
160 return false;
161 }
162 return true;
163 }
164 return false;
165}
166
167std::shared_ptr<ITensorHandle> RefTensorHandle::DecorateTensorHandle(const TensorInfo& tensorInfo)
168{
169 auto decorated = std::make_shared<RefTensorHandleDecorator>(tensorInfo, *this);
170 m_Decorated.emplace_back(decorated);
171 return decorated;
172}
173
175: RefTensorHandle(tensorInfo)
176, m_TensorInfo(tensorInfo)
177, m_Parent(parent)
178{
179}
180
184
188
189const void* RefTensorHandleDecorator::Map(bool unused) const
190{
191 return m_Parent.Map(unused);
192}
193
198
200{
201 return false;
202}
203
205{
206 return false;
207}
208
209std::shared_ptr<ITensorHandle> RefTensorHandleDecorator::DecorateTensorHandle(const TensorInfo&)
210{
211 return nullptr;
212}
213
214
215}
#define ARMNN_THROW_MSG_IF_FALSE(_cond, _except, _str)
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
virtual std::shared_ptr< ITensorHandle > DecorateTensorHandle(const TensorInfo &tensorInfo) override
Returns a decorated version of this TensorHandle allowing us to override the TensorInfo for it.
virtual MemorySourceFlags GetImportFlags() const override
Get flags describing supported import sources.
RefTensorHandleDecorator(const TensorInfo &tensorInfo, const RefTensorHandle &parent)
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.
virtual const void * Map(bool) const override
Map the tensor data for access.
virtual bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.
const TensorInfo & GetTensorInfo() const
RefTensorHandle(const TensorInfo &tensorInfo, std::shared_ptr< RefMemoryManager > &memoryManager)
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
virtual std::shared_ptr< ITensorHandle > DecorateTensorHandle(const TensorInfo &tensorInfo) override
Returns a decorated version of this TensorHandle allowing us to override the TensorInfo for it.
virtual MemorySourceFlags GetImportFlags() const override
Get flags describing supported import sources.
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.
virtual const void * Map(bool) const override
Map the tensor data for access.
virtual bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.
Copyright (c) 2021 ARM Limited and Contributors.
MemorySource
Define the Memory Source to reduce copies.
Definition Types.hpp:246
unsigned int MemorySourceFlags
constexpr unsigned int GetDataTypeSize(DataType dataType)