xref: /aosp_15_r20/external/armnn/src/dynamic/sample/SampleTensorHandle.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2020 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "SampleTensorHandle.hpp"
7 
8 namespace sdb // sample dynamic backend
9 {
10 
SampleTensorHandle(const armnn::TensorInfo & tensorInfo,std::shared_ptr<SampleMemoryManager> & memoryManager)11 SampleTensorHandle::SampleTensorHandle(const armnn::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<armnn::MemorySourceFlags>(armnn::MemorySource::Undefined)),
18       m_Imported(false)
19 {
20 
21 }
22 
SampleTensorHandle(const armnn::TensorInfo & tensorInfo,armnn::MemorySourceFlags importFlags)23 SampleTensorHandle::SampleTensorHandle(const armnn::TensorInfo& tensorInfo,
24                                        armnn::MemorySourceFlags importFlags)
25     : m_TensorInfo(tensorInfo),
26       m_MemoryManager(nullptr),
27       m_Pool(nullptr),
28       m_UnmanagedMemory(nullptr),
29       m_ImportFlags(importFlags),
30       m_Imported(true)
31 {
32 
33 }
34 
~SampleTensorHandle()35 SampleTensorHandle::~SampleTensorHandle()
36 {
37     if (!m_Pool)
38     {
39         // unmanaged
40         if (!m_Imported)
41         {
42             ::operator delete(m_UnmanagedMemory);
43         }
44     }
45 }
46 
Manage()47 void SampleTensorHandle::Manage()
48 {
49     m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
50 }
51 
Allocate()52 void 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 armnn::InvalidArgumentException("SampleTensorHandle::Allocate Trying to allocate a "
69                                               "SampleTensorHandle that already has allocated "
70                                               "memory.");
71     }
72 }
73 
Map(bool) const74 const void* SampleTensorHandle::Map(bool /*unused*/) const
75 {
76     return GetPointer();
77 }
78 
GetPointer() const79 void* 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 
Import(void * memory,armnn::MemorySource source)91 bool SampleTensorHandle::Import(void* memory, armnn::MemorySource source)
92 {
93 
94     if (m_ImportFlags & static_cast<armnn::MemorySourceFlags>(source))
95     {
96         if (source == armnn::MemorySource::Malloc)
97         {
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 
CopyOutTo(void * dest) const137 void SampleTensorHandle::CopyOutTo(void* dest) const
138 {
139     const void *src = GetPointer();
140     ARMNN_ASSERT(src);
141     memcpy(dest, src, m_TensorInfo.GetNumBytes());
142 }
143 
CopyInFrom(const void * src)144 void SampleTensorHandle::CopyInFrom(const void* src)
145 {
146     void *dest = GetPointer();
147     ARMNN_ASSERT(dest);
148     memcpy(dest, src, m_TensorInfo.GetNumBytes());
149 }
150 
151 } // namespace sdb
152