blob: eccdc2654244d27247912fe521ee10d599a303ae [file] [log] [blame]
Matthew Bentham4cefc412019-06-18 16:14:34 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#include "RefTensorHandle.hpp"
6
7namespace armnn
8{
9
Matthew Bentham7c1603a2019-06-21 17:22:23 +010010RefTensorHandle::RefTensorHandle(const TensorInfo &tensorInfo, std::shared_ptr<RefMemoryManager> &memoryManager):
Matthew Bentham4cefc412019-06-18 16:14:34 +010011 m_TensorInfo(tensorInfo),
Matthew Bentham7c1603a2019-06-21 17:22:23 +010012 m_MemoryManager(memoryManager),
13 m_Pool(nullptr),
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010014 m_UnmanagedMemory(nullptr),
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010015 m_Imported(false),
16 m_IsImportEnabled(false)
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010017{
18
19}
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),
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010025 m_Imported(false),
26 m_IsImportEnabled(true)
Matthew Bentham4cefc412019-06-18 16:14:34 +010027{
28
29}
30
31RefTensorHandle::~RefTensorHandle()
32{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010033 if (!m_Pool)
34 {
35 // unmanaged
Ferran Balaguer1cd451c2019-08-22 14:09:44 +010036 if (!m_Imported)
37 {
38 ::operator delete(m_UnmanagedMemory);
39 }
Matthew Bentham7c1603a2019-06-21 17:22:23 +010040 }
41}
42
43void RefTensorHandle::Manage()
44{
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010045 if (!m_IsImportEnabled)
46 {
47 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
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010050 m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
51 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010052}
53
54void RefTensorHandle::Allocate()
55{
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010056 // If import is enabled, do not allocate the tensor
57 if (!m_IsImportEnabled)
Matthew Bentham4cefc412019-06-18 16:14:34 +010058 {
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010059
60 if (!m_UnmanagedMemory)
Matthew Bentham7c1603a2019-06-21 17:22:23 +010061 {
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010062 if (!m_Pool)
63 {
64 // unmanaged
65 m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes());
66 }
67 else
68 {
69 m_MemoryManager->Allocate(m_Pool);
70 }
Matthew Bentham7c1603a2019-06-21 17:22:23 +010071 }
72 else
73 {
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010074 throw InvalidArgumentException("RefTensorHandle::Allocate Trying to allocate a RefTensorHandle"
75 "that already has allocated memory.");
Matthew Bentham7c1603a2019-06-21 17:22:23 +010076 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010077 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010078}
79
Matthew Bentham7c1603a2019-06-21 17:22:23 +010080const void* RefTensorHandle::Map(bool /*unused*/) const
Matthew Bentham4cefc412019-06-18 16:14:34 +010081{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010082 return GetPointer();
Matthew Bentham4cefc412019-06-18 16:14:34 +010083}
84
Matthew Bentham7c1603a2019-06-21 17:22:23 +010085void* RefTensorHandle::GetPointer() const
Matthew Bentham4cefc412019-06-18 16:14:34 +010086{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010087 if (m_UnmanagedMemory)
88 {
89 return m_UnmanagedMemory;
90 }
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010091 else if (m_Pool)
92 {
93 return m_MemoryManager->GetPointer(m_Pool);
94 }
Matthew Bentham7c1603a2019-06-21 17:22:23 +010095 else
96 {
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010097 throw NullPointerException("RefTensorHandle::GetPointer called on unmanaged, unallocated tensor handle");
Matthew Bentham7c1603a2019-06-21 17:22:23 +010098 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010099}
100
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100101void RefTensorHandle::CopyOutTo(void* dest) const
102{
103 const void *src = GetPointer();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100104 ARMNN_ASSERT(src);
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100105 memcpy(dest, src, m_TensorInfo.GetNumBytes());
106}
107
108void RefTensorHandle::CopyInFrom(const void* src)
109{
110 void *dest = GetPointer();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100111 ARMNN_ASSERT(dest);
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100112 memcpy(dest, src, m_TensorInfo.GetNumBytes());
113}
114
Matthew Benthamc30abd82022-11-23 12:11:32 +0000115MemorySourceFlags RefTensorHandle::GetImportFlags() const
116{
117 if (m_IsImportEnabled)
118 {
119 return static_cast<MemorySourceFlags>(MemorySource::Malloc);
120 }
121 else
122 {
123 return static_cast<MemorySourceFlags>(MemorySource::Undefined);
124 }
125}
126
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100127bool RefTensorHandle::Import(void* memory, MemorySource source)
128{
Matthew Benthamc30abd82022-11-23 12:11:32 +0000129 if (m_IsImportEnabled && source == MemorySource::Malloc)
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100130 {
Matthew Benthamc30abd82022-11-23 12:11:32 +0000131 // Check memory alignment
132 if(!CanBeImported(memory, source))
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100133 {
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100134 if (m_Imported)
135 {
Matthew Benthamc30abd82022-11-23 12:11:32 +0000136 m_Imported = false;
137 m_UnmanagedMemory = nullptr;
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100138 }
Matthew Benthamc30abd82022-11-23 12:11:32 +0000139 return false;
140 }
141
142 // m_UnmanagedMemory not yet allocated.
143 if (!m_Imported && !m_UnmanagedMemory)
144 {
145 m_UnmanagedMemory = memory;
146 m_Imported = true;
147 return true;
148 }
149
150 // m_UnmanagedMemory initially allocated with Allocate().
151 if (!m_Imported && m_UnmanagedMemory)
152 {
153 return false;
154 }
155
156 // m_UnmanagedMemory previously imported.
157 if (m_Imported)
158 {
159 m_UnmanagedMemory = memory;
160 return true;
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100161 }
162 }
163
164 return false;
165}
166
Nikhil Raj53e06592022-01-05 16:04:08 +0000167bool RefTensorHandle::CanBeImported(void *memory, MemorySource source)
168{
Matthew Benthamc30abd82022-11-23 12:11:32 +0000169 if (m_IsImportEnabled && source == MemorySource::Malloc)
Nikhil Raj53e06592022-01-05 16:04:08 +0000170 {
Matthew Benthamc30abd82022-11-23 12:11:32 +0000171 uintptr_t alignment = GetDataTypeSize(m_TensorInfo.GetDataType());
172 if (reinterpret_cast<uintptr_t>(memory) % alignment)
Nikhil Raj53e06592022-01-05 16:04:08 +0000173 {
Matthew Benthamc30abd82022-11-23 12:11:32 +0000174 return false;
Nikhil Raj53e06592022-01-05 16:04:08 +0000175 }
Matthew Benthamc30abd82022-11-23 12:11:32 +0000176 return true;
Nikhil Raj53e06592022-01-05 16:04:08 +0000177 }
178 return false;
179}
180
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100181}