blob: cce992c947ec4c0a821c9424b93a7bb1149a73c5 [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{
104 const void *src = GetPointer();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100105 ARMNN_ASSERT(src);
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100106 memcpy(dest, src, m_TensorInfo.GetNumBytes());
107}
108
109void RefTensorHandle::CopyInFrom(const void* src)
110{
111 void *dest = GetPointer();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100112 ARMNN_ASSERT(dest);
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100113 memcpy(dest, src, m_TensorInfo.GetNumBytes());
114}
115
Matthew Benthamc30abd82022-11-23 12:11:32 +0000116MemorySourceFlags RefTensorHandle::GetImportFlags() const
117{
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000118 return static_cast<MemorySourceFlags>(MemorySource::Malloc);
Matthew Benthamc30abd82022-11-23 12:11:32 +0000119}
120
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100121bool RefTensorHandle::Import(void* memory, MemorySource source)
122{
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000123 if (source == MemorySource::Malloc)
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100124 {
Matthew Benthamc30abd82022-11-23 12:11:32 +0000125 // Check memory alignment
126 if(!CanBeImported(memory, source))
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100127 {
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000128 m_ImportedMemory = nullptr;
Matthew Benthamc30abd82022-11-23 12:11:32 +0000129 return false;
130 }
131
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000132 m_ImportedMemory = memory;
133 return true;
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100134 }
135
136 return false;
137}
138
Nikhil Raj53e06592022-01-05 16:04:08 +0000139bool RefTensorHandle::CanBeImported(void *memory, MemorySource source)
140{
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000141 if (source == MemorySource::Malloc)
Nikhil Raj53e06592022-01-05 16:04:08 +0000142 {
Matthew Benthamc30abd82022-11-23 12:11:32 +0000143 uintptr_t alignment = GetDataTypeSize(m_TensorInfo.GetDataType());
144 if (reinterpret_cast<uintptr_t>(memory) % alignment)
Nikhil Raj53e06592022-01-05 16:04:08 +0000145 {
Matthew Benthamc30abd82022-11-23 12:11:32 +0000146 return false;
Nikhil Raj53e06592022-01-05 16:04:08 +0000147 }
Matthew Benthamc30abd82022-11-23 12:11:32 +0000148 return true;
Nikhil Raj53e06592022-01-05 16:04:08 +0000149 }
150 return false;
151}
152
Mike Kelly4cc341c2023-07-07 15:43:06 +0100153std::shared_ptr<ITensorHandle> RefTensorHandle::DecorateTensorHandle(const TensorInfo& tensorInfo)
154{
155 auto decorated = std::make_shared<RefTensorHandleDecorator>(tensorInfo, *this);
156 m_Decorated.emplace_back(decorated);
157 return decorated;
158}
159
160RefTensorHandleDecorator::RefTensorHandleDecorator(const TensorInfo& tensorInfo, const RefTensorHandle& parent)
161: RefTensorHandle(tensorInfo)
162, m_TensorInfo(tensorInfo)
163, m_Parent(parent)
164{
165}
166
167void RefTensorHandleDecorator::Manage()
168{
169}
170
171void RefTensorHandleDecorator::Allocate()
172{
173}
174
175const void* RefTensorHandleDecorator::Map(bool unused) const
176{
177 return m_Parent.Map(unused);
178}
179
180MemorySourceFlags RefTensorHandleDecorator::GetImportFlags() const
181{
182 return static_cast<MemorySourceFlags>(MemorySource::Malloc);
183}
184
185bool RefTensorHandleDecorator::Import(void*, MemorySource )
186{
187 return false;
188}
189
190bool RefTensorHandleDecorator::CanBeImported(void* , MemorySource)
191{
192 return false;
193}
194
195std::shared_ptr<ITensorHandle> RefTensorHandleDecorator::DecorateTensorHandle(const TensorInfo&)
196{
197 return nullptr;
198}
199
200
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100201}