blob: dbfa3749458982b657c114bb6ebd2e95bb7a4ff5 [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),
Matthew Bentham6b5f6742022-11-23 18:17:48 +000015 m_ImportedMemory(nullptr)
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010016{
17
18}
19
Matthew Benthamc30abd82022-11-23 12:11:32 +000020RefTensorHandle::RefTensorHandle(const TensorInfo& tensorInfo)
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010021 : m_TensorInfo(tensorInfo),
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010022 m_Pool(nullptr),
23 m_UnmanagedMemory(nullptr),
Matthew Bentham6b5f6742022-11-23 18:17:48 +000024 m_ImportedMemory(nullptr)
Matthew Bentham4cefc412019-06-18 16:14:34 +010025{
26
27}
28
29RefTensorHandle::~RefTensorHandle()
30{
Matthew Bentham6b5f6742022-11-23 18:17:48 +000031 ::operator delete(m_UnmanagedMemory);
Matthew Bentham7c1603a2019-06-21 17:22:23 +010032}
33
34void RefTensorHandle::Manage()
35{
Matthew Bentham6b5f6742022-11-23 18:17:48 +000036 ARMNN_ASSERT_MSG(!m_Pool, "RefTensorHandle::Manage() called twice");
37 ARMNN_ASSERT_MSG(!m_UnmanagedMemory, "RefTensorHandle::Manage() called after Allocate()");
Matthew Bentham7c1603a2019-06-21 17:22:23 +010038
Matthew Bentham6b5f6742022-11-23 18:17:48 +000039 if (m_MemoryManager)
40 {
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010041 m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
42 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010043}
44
45void RefTensorHandle::Allocate()
46{
Matthew Bentham6b5f6742022-11-23 18:17:48 +000047 if (!m_UnmanagedMemory)
Matthew Bentham4cefc412019-06-18 16:14:34 +010048 {
Matthew Bentham6b5f6742022-11-23 18:17:48 +000049 if (!m_Pool)
Matthew Bentham7c1603a2019-06-21 17:22:23 +010050 {
Matthew Bentham6b5f6742022-11-23 18:17:48 +000051 // unmanaged
52 m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes());
Matthew Bentham7c1603a2019-06-21 17:22:23 +010053 }
54 else
55 {
Matthew Bentham6b5f6742022-11-23 18:17:48 +000056 m_MemoryManager->Allocate(m_Pool);
Matthew Bentham7c1603a2019-06-21 17:22:23 +010057 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010058 }
Matthew Bentham6b5f6742022-11-23 18:17:48 +000059 else
60 {
61 throw InvalidArgumentException("RefTensorHandle::Allocate Trying to allocate a RefTensorHandle"
62 "that already has allocated memory.");
63 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010064}
65
Matthew Bentham7c1603a2019-06-21 17:22:23 +010066const void* RefTensorHandle::Map(bool /*unused*/) const
Matthew Bentham4cefc412019-06-18 16:14:34 +010067{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010068 return GetPointer();
Matthew Bentham4cefc412019-06-18 16:14:34 +010069}
70
Matthew Bentham7c1603a2019-06-21 17:22:23 +010071void* RefTensorHandle::GetPointer() const
Matthew Bentham4cefc412019-06-18 16:14:34 +010072{
Matthew Bentham6b5f6742022-11-23 18:17:48 +000073 if (m_ImportedMemory)
74 {
75 return m_ImportedMemory;
76 }
77 else if (m_UnmanagedMemory)
Matthew Bentham7c1603a2019-06-21 17:22:23 +010078 {
79 return m_UnmanagedMemory;
80 }
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010081 else if (m_Pool)
82 {
83 return m_MemoryManager->GetPointer(m_Pool);
84 }
Matthew Bentham7c1603a2019-06-21 17:22:23 +010085 else
86 {
Narumol Prangnawarat3b90af62020-06-26 11:00:21 +010087 throw NullPointerException("RefTensorHandle::GetPointer called on unmanaged, unallocated tensor handle");
Matthew Bentham7c1603a2019-06-21 17:22:23 +010088 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010089}
90
Matthew Bentham7c1603a2019-06-21 17:22:23 +010091void RefTensorHandle::CopyOutTo(void* dest) const
92{
93 const void *src = GetPointer();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010094 ARMNN_ASSERT(src);
Matthew Bentham7c1603a2019-06-21 17:22:23 +010095 memcpy(dest, src, m_TensorInfo.GetNumBytes());
96}
97
98void RefTensorHandle::CopyInFrom(const void* src)
99{
100 void *dest = GetPointer();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100101 ARMNN_ASSERT(dest);
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100102 memcpy(dest, src, m_TensorInfo.GetNumBytes());
103}
104
Matthew Benthamc30abd82022-11-23 12:11:32 +0000105MemorySourceFlags RefTensorHandle::GetImportFlags() const
106{
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000107 return static_cast<MemorySourceFlags>(MemorySource::Malloc);
Matthew Benthamc30abd82022-11-23 12:11:32 +0000108}
109
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100110bool RefTensorHandle::Import(void* memory, MemorySource source)
111{
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000112 if (source == MemorySource::Malloc)
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100113 {
Matthew Benthamc30abd82022-11-23 12:11:32 +0000114 // Check memory alignment
115 if(!CanBeImported(memory, source))
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100116 {
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000117 m_ImportedMemory = nullptr;
Matthew Benthamc30abd82022-11-23 12:11:32 +0000118 return false;
119 }
120
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000121 m_ImportedMemory = memory;
122 return true;
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100123 }
124
125 return false;
126}
127
Nikhil Raj53e06592022-01-05 16:04:08 +0000128bool RefTensorHandle::CanBeImported(void *memory, MemorySource source)
129{
Matthew Bentham6b5f6742022-11-23 18:17:48 +0000130 if (source == MemorySource::Malloc)
Nikhil Raj53e06592022-01-05 16:04:08 +0000131 {
Matthew Benthamc30abd82022-11-23 12:11:32 +0000132 uintptr_t alignment = GetDataTypeSize(m_TensorInfo.GetDataType());
133 if (reinterpret_cast<uintptr_t>(memory) % alignment)
Nikhil Raj53e06592022-01-05 16:04:08 +0000134 {
Matthew Benthamc30abd82022-11-23 12:11:32 +0000135 return false;
Nikhil Raj53e06592022-01-05 16:04:08 +0000136 }
Matthew Benthamc30abd82022-11-23 12:11:32 +0000137 return true;
Nikhil Raj53e06592022-01-05 16:04:08 +0000138 }
139 return false;
140}
141
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100142}