blob: 42ac7f0826f77898f476dc6cc816a853f09d085e [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),
15 m_ImportFlags(static_cast<MemorySourceFlags>(MemorySource::Undefined)),
16 m_Imported(false)
17{
18
19}
20
21RefTensorHandle::RefTensorHandle(const TensorInfo& tensorInfo, std::shared_ptr<RefMemoryManager> &memoryManager,
22 MemorySourceFlags importFlags)
23 : m_TensorInfo(tensorInfo),
24 m_MemoryManager(memoryManager),
25 m_Pool(nullptr),
26 m_UnmanagedMemory(nullptr),
27 m_ImportFlags(importFlags),
28 m_Imported(false)
Matthew Bentham4cefc412019-06-18 16:14:34 +010029{
30
31}
32
33RefTensorHandle::~RefTensorHandle()
34{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010035 if (!m_Pool)
36 {
37 // unmanaged
Ferran Balaguer1cd451c2019-08-22 14:09:44 +010038 if (!m_Imported)
39 {
40 ::operator delete(m_UnmanagedMemory);
41 }
Matthew Bentham7c1603a2019-06-21 17:22:23 +010042 }
43}
44
45void RefTensorHandle::Manage()
46{
47 BOOST_ASSERT_MSG(!m_Pool, "RefTensorHandle::Manage() called twice");
48 BOOST_ASSERT_MSG(!m_UnmanagedMemory, "RefTensorHandle::Manage() called after Allocate()");
49
50 m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
Matthew Bentham4cefc412019-06-18 16:14:34 +010051}
52
53void RefTensorHandle::Allocate()
54{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010055 if (!m_UnmanagedMemory)
Matthew Bentham4cefc412019-06-18 16:14:34 +010056 {
Matthew Bentham7c1603a2019-06-21 17:22:23 +010057 if (!m_Pool)
58 {
59 // unmanaged
60 m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes());
61 }
62 else
63 {
64 m_MemoryManager->Allocate(m_Pool);
65 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010066 }
67 else
68 {
69 throw InvalidArgumentException("RefTensorHandle::Allocate Trying to allocate a RefTensorHandle"
Matthew Bentham7c1603a2019-06-21 17:22:23 +010070 "that already has allocated memory.");
Matthew Bentham4cefc412019-06-18 16:14:34 +010071 }
72}
73
Matthew Bentham7c1603a2019-06-21 17:22:23 +010074const void* RefTensorHandle::Map(bool /*unused*/) const
Matthew Bentham4cefc412019-06-18 16:14:34 +010075{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010076 return GetPointer();
Matthew Bentham4cefc412019-06-18 16:14:34 +010077}
78
Matthew Bentham7c1603a2019-06-21 17:22:23 +010079void* RefTensorHandle::GetPointer() const
Matthew Bentham4cefc412019-06-18 16:14:34 +010080{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010081 if (m_UnmanagedMemory)
82 {
83 return m_UnmanagedMemory;
84 }
85 else
86 {
87 BOOST_ASSERT_MSG(m_Pool, "RefTensorHandle::GetPointer called on unmanaged, unallocated tensor handle");
88 return m_MemoryManager->GetPointer(m_Pool);
89 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010090}
91
Matthew Bentham7c1603a2019-06-21 17:22:23 +010092void RefTensorHandle::CopyOutTo(void* dest) const
93{
94 const void *src = GetPointer();
95 BOOST_ASSERT(src);
96 memcpy(dest, src, m_TensorInfo.GetNumBytes());
97}
98
99void RefTensorHandle::CopyInFrom(const void* src)
100{
101 void *dest = GetPointer();
102 BOOST_ASSERT(dest);
103 memcpy(dest, src, m_TensorInfo.GetNumBytes());
104}
105
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100106bool RefTensorHandle::Import(void* memory, MemorySource source)
107{
108
109 if (m_ImportFlags & static_cast<MemorySourceFlags>(source))
110 {
111 if (source == MemorySource::Malloc)
112 {
113 // Checks the 16 byte memory alignment.
114 if (reinterpret_cast<uint64_t>(memory) % 16)
115 {
Ferran Balaguer1cd451c2019-08-22 14:09:44 +0100116 if (m_Imported)
117 {
118 m_Imported = false;
119 m_UnmanagedMemory = nullptr;
120 }
121
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100122 return false;
123 }
124
125 // m_UnmanagedMemory not yet allocated.
126 if (!m_Imported && !m_UnmanagedMemory)
127 {
128 m_UnmanagedMemory = memory;
129 m_Imported = true;
130 return true;
131 }
132
133 // m_UnmanagedMemory initially allocated with Allocate().
134 if (!m_Imported && m_UnmanagedMemory)
135 {
136 return false;
137 }
138
139 // m_UnmanagedMemory previously imported.
140 if (m_Imported)
141 {
142 m_UnmanagedMemory = memory;
143 return true;
144 }
145 }
146 }
147
148 return false;
149}
150
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100151}