blob: 80554aad3cb783f92956cad4174b529aef0600e3 [file] [log] [blame]
Narumol Prangnawarat867eba52020-02-03 12:29:56 +00001//
2// Copyright © 2020 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "SampleTensorHandle.hpp"
7
Jan Eilersc84e45d2020-08-19 14:14:36 +01008namespace sdb // sample dynamic backend
Narumol Prangnawarat867eba52020-02-03 12:29:56 +00009{
10
Jan Eilersc84e45d2020-08-19 14:14:36 +010011SampleTensorHandle::SampleTensorHandle(const armnn::TensorInfo &tensorInfo,
Narumol Prangnawarat867eba52020-02-03 12:29:56 +000012 std::shared_ptr<SampleMemoryManager> &memoryManager)
13 : m_TensorInfo(tensorInfo),
14 m_MemoryManager(memoryManager),
15 m_Pool(nullptr),
16 m_UnmanagedMemory(nullptr),
Jan Eilersc84e45d2020-08-19 14:14:36 +010017 m_ImportFlags(static_cast<armnn::MemorySourceFlags>(armnn::MemorySource::Undefined)),
Narumol Prangnawarat867eba52020-02-03 12:29:56 +000018 m_Imported(false)
19{
20
21}
22
Jan Eilersc84e45d2020-08-19 14:14:36 +010023SampleTensorHandle::SampleTensorHandle(const armnn::TensorInfo& tensorInfo,
24 armnn::MemorySourceFlags importFlags)
Narumol Prangnawarat867eba52020-02-03 12:29:56 +000025 : m_TensorInfo(tensorInfo),
Narumol Prangnawarat0739fee2020-08-11 11:24:25 +010026 m_MemoryManager(nullptr),
Narumol Prangnawarat867eba52020-02-03 12:29:56 +000027 m_Pool(nullptr),
28 m_UnmanagedMemory(nullptr),
29 m_ImportFlags(importFlags),
Narumol Prangnawarat0739fee2020-08-11 11:24:25 +010030 m_Imported(true)
Narumol Prangnawarat867eba52020-02-03 12:29:56 +000031{
32
33}
34
35SampleTensorHandle::~SampleTensorHandle()
36{
37 if (!m_Pool)
38 {
39 // unmanaged
40 if (!m_Imported)
41 {
42 ::operator delete(m_UnmanagedMemory);
43 }
44 }
45}
46
47void SampleTensorHandle::Manage()
48{
49 m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
50}
51
52void SampleTensorHandle::Allocate()
53{
54 if (!m_UnmanagedMemory)
55 {
56 if (!m_Pool)
57 {
58 // unmanaged
59 m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes());
60 }
61 else
62 {
63 m_MemoryManager->Allocate(m_Pool);
64 }
65 }
66 else
67 {
Jan Eilersc84e45d2020-08-19 14:14:36 +010068 throw armnn::InvalidArgumentException("SampleTensorHandle::Allocate Trying to allocate a "
69 "SampleTensorHandle that already has allocated "
70 "memory.");
Narumol Prangnawarat867eba52020-02-03 12:29:56 +000071 }
72}
73
74const void* SampleTensorHandle::Map(bool /*unused*/) const
75{
76 return GetPointer();
77}
78
79void* SampleTensorHandle::GetPointer() const
80{
81 if (m_UnmanagedMemory)
82 {
83 return m_UnmanagedMemory;
84 }
85 else
86 {
87 return m_MemoryManager->GetPointer(m_Pool);
88 }
89}
90
Jan Eilersc84e45d2020-08-19 14:14:36 +010091bool SampleTensorHandle::Import(void* memory, armnn::MemorySource source)
Narumol Prangnawarat867eba52020-02-03 12:29:56 +000092{
93
Jan Eilersc84e45d2020-08-19 14:14:36 +010094 if (m_ImportFlags & static_cast<armnn::MemorySourceFlags>(source))
Narumol Prangnawarat867eba52020-02-03 12:29:56 +000095 {
Jan Eilersc84e45d2020-08-19 14:14:36 +010096 if (source == armnn::MemorySource::Malloc)
Narumol Prangnawarat867eba52020-02-03 12:29:56 +000097 {
98 // Check memory alignment
99 constexpr uintptr_t alignment = sizeof(size_t);
100 if (reinterpret_cast<uintptr_t>(memory) % alignment)
101 {
102 if (m_Imported)
103 {
104 m_Imported = false;
105 m_UnmanagedMemory = nullptr;
106 }
107
108 return false;
109 }
110
111 // m_UnmanagedMemory not yet allocated.
112 if (!m_Imported && !m_UnmanagedMemory)
113 {
114 m_UnmanagedMemory = memory;
115 m_Imported = true;
116 return true;
117 }
118
119 // m_UnmanagedMemory initially allocated with Allocate().
120 if (!m_Imported && m_UnmanagedMemory)
121 {
122 return false;
123 }
124
125 // m_UnmanagedMemory previously imported.
126 if (m_Imported)
127 {
128 m_UnmanagedMemory = memory;
129 return true;
130 }
131 }
132 }
133
134 return false;
135}
136
Narumol Prangnawarat0739fee2020-08-11 11:24:25 +0100137void SampleTensorHandle::CopyOutTo(void* dest) const
138{
David Monahan6a1d5062023-08-29 09:10:50 +0100139 const void* src = GetPointer();
140 if (dest == nullptr)
141 {
142 throw armnn::Exception("SampleTensorHandle:CopyOutTo: Destination Ptr is null");
143 }
144 if (src == nullptr)
145 {
146 throw armnn::Exception("SampleTensorHandle:CopyOutTo: Source Ptr is null");
147 }
Narumol Prangnawarat0739fee2020-08-11 11:24:25 +0100148 memcpy(dest, src, m_TensorInfo.GetNumBytes());
149}
150
151void SampleTensorHandle::CopyInFrom(const void* src)
152{
David Monahan6a1d5062023-08-29 09:10:50 +0100153 void* dest = GetPointer();
154 if (src == nullptr)
155 {
156 throw armnn::Exception("SampleTensorHandle:CopyInFrom: Source Ptr is null");
157 }
158 if (dest == nullptr)
159 {
160 throw armnn::Exception("SampleTensorHandle:CopyInFrom: Destination Ptr is null");
161 }
Narumol Prangnawarat0739fee2020-08-11 11:24:25 +0100162 memcpy(dest, src, m_TensorInfo.GetNumBytes());
163}
164
Jan Eilersc84e45d2020-08-19 14:14:36 +0100165} // namespace sdb