blob: 07f497c54ec30e45701dd4f9e16d49d9d1202464 [file] [log] [blame]
Matthew Bentham4cefc412019-06-18 16:14:34 +01001//
Mike Kelly4cc341c2023-07-07 15:43:06 +01002// Copyright © 2019-2023 Arm Ltd. All rights reserved.
Matthew Bentham4cefc412019-06-18 16:14:34 +01003// SPDX-License-Identifier: MIT
4//
Mike Kelly4cc341c2023-07-07 15:43:06 +01005
Matthew Bentham4cefc412019-06-18 16:14:34 +01006#include "RefTensorHandle.hpp"
7
8namespace armnn
9{
10
Mike Kelly4cc341c2023-07-07 15:43:06 +010011RefTensorHandle::RefTensorHandle(const TensorInfo& tensorInfo, std::shared_ptr<RefMemoryManager>& memoryManager):
Matthew Bentham4cefc412019-06-18 16:14:34 +010012 m_TensorInfo(tensorInfo),
Matthew Bentham7c1603a2019-06-21 17:22:23 +010013 m_MemoryManager(memoryManager),
14 m_Pool(nullptr),
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010015 m_UnmanagedMemory(nullptr),
Mike Kelly4cc341c2023-07-07 15:43:06 +010016 m_ImportedMemory(nullptr),
17 m_Decorated()
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010018{
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010019}
20
Matthew Benthamc30abd82022-11-23 12:11:32 +000021RefTensorHandle::RefTensorHandle(const TensorInfo& tensorInfo)
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010022 : m_TensorInfo(tensorInfo),
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010023 m_Pool(nullptr),
24 m_UnmanagedMemory(nullptr),
Mike Kelly4cc341c2023-07-07 15:43:06 +010025 m_ImportedMemory(nullptr),
26 m_Decorated()
Matthew Bentham4cefc412019-06-18 16:14:34 +010027{
Mike Kelly4cc341c2023-07-07 15:43:06 +010028}
Matthew Bentham4cefc412019-06-18 16:14:34 +010029
Mike Kelly4cc341c2023-07-07 15:43:06 +010030RefTensorHandle::RefTensorHandle(const TensorInfo& tensorInfo, const RefTensorHandle& parent)
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{
Matthew Bentham4cefc412019-06-18 16:14:34 +010038}
39
40RefTensorHandle::~RefTensorHandle()
41{
Matthew Bentham6b5f6742022-11-23 18:17:48 +000042 ::operator delete(m_UnmanagedMemory);
Matthew Bentham7c1603a2019-06-21 17:22:23 +010043}
44
45void RefTensorHandle::Manage()
46{
Matthew Bentham6b5f6742022-11-23 18:17:48 +000047 ARMNN_ASSERT_MSG(!m_Pool, "RefTensorHandle::Manage() called twice");
48 ARMNN_ASSERT_MSG(!m_UnmanagedMemory, "RefTensorHandle::Manage() called after Allocate()");
Matthew Bentham7c1603a2019-06-21 17:22:23 +010049
Matthew Bentham6b5f6742022-11-23 18:17:48 +000050 if (m_MemoryManager)
51 {
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010052 m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
53 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010054}
55
56void RefTensorHandle::Allocate()
57{
Matthew Bentham6b5f6742022-11-23 18:17:48 +000058 if (!m_UnmanagedMemory)
Matthew Bentham4cefc412019-06-18 16:14:34 +010059 {
Matthew Bentham6b5f6742022-11-23 18:17:48 +000060 if (!m_Pool)
Matthew Bentham7c1603a2019-06-21 17:22:23 +010061 {
Matthew Bentham6b5f6742022-11-23 18:17:48 +000062 // unmanaged
63 m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes());
Matthew Bentham7c1603a2019-06-21 17:22:23 +010064 }
65 else
66 {
Matthew Bentham6b5f6742022-11-23 18:17:48 +000067 m_MemoryManager->Allocate(m_Pool);
Matthew Bentham7c1603a2019-06-21 17:22:23 +010068 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010069 }
Matthew Bentham6b5f6742022-11-23 18:17:48 +000070 else
71 {
72 throw InvalidArgumentException("RefTensorHandle::Allocate Trying to allocate a RefTensorHandle"
73 "that already has allocated memory.");
74 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010075}
76
Matthew Bentham7c1603a2019-06-21 17:22:23 +010077const void* RefTensorHandle::Map(bool /*unused*/) const
Matthew Bentham4cefc412019-06-18 16:14:34 +010078{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010079 return GetPointer();
Matthew Bentham4cefc412019-06-18 16:14:34 +010080}
81
Matthew Bentham7c1603a2019-06-21 17:22:23 +010082void* RefTensorHandle::GetPointer() const
Matthew Bentham4cefc412019-06-18 16:14:34 +010083{
Matthew Bentham6b5f6742022-11-23 18:17:48 +000084 if (m_ImportedMemory)
85 {
86 return m_ImportedMemory;
87 }
88 else if (m_UnmanagedMemory)
Matthew Bentham7c1603a2019-06-21 17:22:23 +010089 {
90 return m_UnmanagedMemory;
91 }
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010092 else if (m_Pool)
93 {
94 return m_MemoryManager->GetPointer(m_Pool);
95 }
Matthew Bentham7c1603a2019-06-21 17:22:23 +010096 else
97 {
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010098 throw NullPointerException("RefTensorHandle::GetPointer called on unmanaged, unallocated tensor handle");
Matthew Bentham7c1603a2019-06-21 17:22:23 +010099 }
Matthew Bentham4cefc412019-06-18 16:14:34 +0100100}
101
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100102void RefTensorHandle::CopyOutTo(void* dest) const
103{
David Monahan6a1d5062023-08-29 09:10:50 +0100104 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());
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100114}
115
116void RefTensorHandle::CopyInFrom(const void* src)
117{
David Monahan6a1d5062023-08-29 09:10:50 +0100118 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());
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100128}
129
Matthew Benthamc30abd82022-11-23 12:11:32 +0000130MemorySourceFlags RefTensorHandle::GetImportFlags() const
131{
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000132 return static_cast<MemorySourceFlags>(MemorySource::Malloc);
Matthew Benthamc30abd82022-11-23 12:11:32 +0000133}
134
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100135bool RefTensorHandle::Import(void* memory, MemorySource source)
136{
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000137 if (source == MemorySource::Malloc)
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100138 {
Matthew Benthamc30abd82022-11-23 12:11:32 +0000139 // Check memory alignment
140 if(!CanBeImported(memory, source))
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100141 {
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000142 m_ImportedMemory = nullptr;
Matthew Benthamc30abd82022-11-23 12:11:32 +0000143 return false;
144 }
145
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000146 m_ImportedMemory = memory;
147 return true;
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100148 }
149
150 return false;
151}
152
Nikhil Raj53e06592022-01-05 16:04:08 +0000153bool RefTensorHandle::CanBeImported(void *memory, MemorySource source)
154{
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000155 if (source == MemorySource::Malloc)
Nikhil Raj53e06592022-01-05 16:04:08 +0000156 {
Matthew Benthamc30abd82022-11-23 12:11:32 +0000157 uintptr_t alignment = GetDataTypeSize(m_TensorInfo.GetDataType());
158 if (reinterpret_cast<uintptr_t>(memory) % alignment)
Nikhil Raj53e06592022-01-05 16:04:08 +0000159 {
Matthew Benthamc30abd82022-11-23 12:11:32 +0000160 return false;
Nikhil Raj53e06592022-01-05 16:04:08 +0000161 }
Matthew Benthamc30abd82022-11-23 12:11:32 +0000162 return true;
Nikhil Raj53e06592022-01-05 16:04:08 +0000163 }
164 return false;
165}
166
Mike Kelly4cc341c2023-07-07 15:43:06 +0100167std::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
174RefTensorHandleDecorator::RefTensorHandleDecorator(const TensorInfo& tensorInfo, const RefTensorHandle& parent)
175: RefTensorHandle(tensorInfo)
176, m_TensorInfo(tensorInfo)
177, m_Parent(parent)
178{
179}
180
181void RefTensorHandleDecorator::Manage()
182{
183}
184
185void RefTensorHandleDecorator::Allocate()
186{
187}
188
189const void* RefTensorHandleDecorator::Map(bool unused) const
190{
191 return m_Parent.Map(unused);
192}
193
194MemorySourceFlags RefTensorHandleDecorator::GetImportFlags() const
195{
196 return static_cast<MemorySourceFlags>(MemorySource::Malloc);
197}
198
199bool RefTensorHandleDecorator::Import(void*, MemorySource )
200{
201 return false;
202}
203
204bool RefTensorHandleDecorator::CanBeImported(void* , MemorySource)
205{
206 return false;
207}
208
209std::shared_ptr<ITensorHandle> RefTensorHandleDecorator::DecorateTensorHandle(const TensorInfo&)
210{
211 return nullptr;
212}
213
214
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100215}