blob: fb4eddcb55d7bcd8650dda421df17b5a5b9f5c16 [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
8namespace armnn
9{
10
11SampleTensorHandle::SampleTensorHandle(const TensorInfo &tensorInfo,
12 std::shared_ptr<SampleMemoryManager> &memoryManager)
13 : m_TensorInfo(tensorInfo),
14 m_MemoryManager(memoryManager),
15 m_Pool(nullptr),
16 m_UnmanagedMemory(nullptr),
17 m_ImportFlags(static_cast<MemorySourceFlags>(MemorySource::Undefined)),
18 m_Imported(false)
19{
20
21}
22
23SampleTensorHandle::SampleTensorHandle(const TensorInfo& tensorInfo,
Narumol Prangnawarat867eba52020-02-03 12:29:56 +000024 MemorySourceFlags importFlags)
25 : 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 {
68 throw InvalidArgumentException("SampleTensorHandle::Allocate Trying to allocate a SampleTensorHandle"
69 "that already has allocated memory.");
70 }
71}
72
73const void* SampleTensorHandle::Map(bool /*unused*/) const
74{
75 return GetPointer();
76}
77
78void* SampleTensorHandle::GetPointer() const
79{
80 if (m_UnmanagedMemory)
81 {
82 return m_UnmanagedMemory;
83 }
84 else
85 {
86 return m_MemoryManager->GetPointer(m_Pool);
87 }
88}
89
90bool SampleTensorHandle::Import(void* memory, MemorySource source)
91{
92
93 if (m_ImportFlags & static_cast<MemorySourceFlags>(source))
94 {
95 if (source == MemorySource::Malloc)
96 {
97 // Check memory alignment
98 constexpr uintptr_t alignment = sizeof(size_t);
99 if (reinterpret_cast<uintptr_t>(memory) % alignment)
100 {
101 if (m_Imported)
102 {
103 m_Imported = false;
104 m_UnmanagedMemory = nullptr;
105 }
106
107 return false;
108 }
109
110 // m_UnmanagedMemory not yet allocated.
111 if (!m_Imported && !m_UnmanagedMemory)
112 {
113 m_UnmanagedMemory = memory;
114 m_Imported = true;
115 return true;
116 }
117
118 // m_UnmanagedMemory initially allocated with Allocate().
119 if (!m_Imported && m_UnmanagedMemory)
120 {
121 return false;
122 }
123
124 // m_UnmanagedMemory previously imported.
125 if (m_Imported)
126 {
127 m_UnmanagedMemory = memory;
128 return true;
129 }
130 }
131 }
132
133 return false;
134}
135
Narumol Prangnawarat0739fee2020-08-11 11:24:25 +0100136void SampleTensorHandle::CopyOutTo(void* dest) const
137{
138 const void *src = GetPointer();
139 ARMNN_ASSERT(src);
140 memcpy(dest, src, m_TensorInfo.GetNumBytes());
141}
142
143void SampleTensorHandle::CopyInFrom(const void* src)
144{
145 void *dest = GetPointer();
146 ARMNN_ASSERT(dest);
147 memcpy(dest, src, m_TensorInfo.GetNumBytes());
148}
149
Narumol Prangnawarat867eba52020-02-03 12:29:56 +0000150}