blob: 59ccec6bace00255fb3638554ab5797d1529ddf2 [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
38 ::operator delete(m_UnmanagedMemory);
39 }
40}
41
42void RefTensorHandle::Manage()
43{
44 BOOST_ASSERT_MSG(!m_Pool, "RefTensorHandle::Manage() called twice");
45 BOOST_ASSERT_MSG(!m_UnmanagedMemory, "RefTensorHandle::Manage() called after Allocate()");
46
47 m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
Matthew Bentham4cefc412019-06-18 16:14:34 +010048}
49
50void RefTensorHandle::Allocate()
51{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010052 if (!m_UnmanagedMemory)
Matthew Bentham4cefc412019-06-18 16:14:34 +010053 {
Matthew Bentham7c1603a2019-06-21 17:22:23 +010054 if (!m_Pool)
55 {
56 // unmanaged
57 m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes());
58 }
59 else
60 {
61 m_MemoryManager->Allocate(m_Pool);
62 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010063 }
64 else
65 {
66 throw InvalidArgumentException("RefTensorHandle::Allocate Trying to allocate a RefTensorHandle"
Matthew Bentham7c1603a2019-06-21 17:22:23 +010067 "that already has allocated memory.");
Matthew Bentham4cefc412019-06-18 16:14:34 +010068 }
69}
70
Matthew Bentham7c1603a2019-06-21 17:22:23 +010071const void* RefTensorHandle::Map(bool /*unused*/) const
Matthew Bentham4cefc412019-06-18 16:14:34 +010072{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010073 return GetPointer();
Matthew Bentham4cefc412019-06-18 16:14:34 +010074}
75
Matthew Bentham7c1603a2019-06-21 17:22:23 +010076void* RefTensorHandle::GetPointer() const
Matthew Bentham4cefc412019-06-18 16:14:34 +010077{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010078 if (m_UnmanagedMemory)
79 {
80 return m_UnmanagedMemory;
81 }
82 else
83 {
84 BOOST_ASSERT_MSG(m_Pool, "RefTensorHandle::GetPointer called on unmanaged, unallocated tensor handle");
85 return m_MemoryManager->GetPointer(m_Pool);
86 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010087}
88
Matthew Bentham7c1603a2019-06-21 17:22:23 +010089void RefTensorHandle::CopyOutTo(void* dest) const
90{
91 const void *src = GetPointer();
92 BOOST_ASSERT(src);
93 memcpy(dest, src, m_TensorInfo.GetNumBytes());
94}
95
96void RefTensorHandle::CopyInFrom(const void* src)
97{
98 void *dest = GetPointer();
99 BOOST_ASSERT(dest);
100 memcpy(dest, src, m_TensorInfo.GetNumBytes());
101}
102
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100103bool RefTensorHandle::Import(void* memory, MemorySource source)
104{
105
106 if (m_ImportFlags & static_cast<MemorySourceFlags>(source))
107 {
108 if (source == MemorySource::Malloc)
109 {
110 // Checks the 16 byte memory alignment.
111 if (reinterpret_cast<uint64_t>(memory) % 16)
112 {
113 return false;
114 }
115
116 // m_UnmanagedMemory not yet allocated.
117 if (!m_Imported && !m_UnmanagedMemory)
118 {
119 m_UnmanagedMemory = memory;
120 m_Imported = true;
121 return true;
122 }
123
124 // m_UnmanagedMemory initially allocated with Allocate().
125 if (!m_Imported && m_UnmanagedMemory)
126 {
127 return false;
128 }
129
130 // m_UnmanagedMemory previously imported.
131 if (m_Imported)
132 {
133 m_UnmanagedMemory = memory;
134 return true;
135 }
136 }
137 }
138
139 return false;
140}
141
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100142}