ArmNN
 22.02
MockTensorHandle Class Reference

#include <MockTensorHandle.hpp>

Inheritance diagram for MockTensorHandle:
ITensorHandle

Public Member Functions

 MockTensorHandle (const TensorInfo &tensorInfo, std::shared_ptr< MockMemoryManager > &memoryManager)
 
 MockTensorHandle (const TensorInfo &tensorInfo, MemorySourceFlags importFlags)
 
 ~MockTensorHandle () override
 
void Manage () override
 Indicate to the memory manager that this resource is active. More...
 
void Allocate () override
 Indicate to the memory manager that this resource is no longer active. More...
 
ITensorHandleGetParent () const override
 Get the parent tensor if this is a subtensor. More...
 
const void * Map (bool) const override
 Map the tensor data for access. More...
 
void Unmap () const override
 Unmap the tensor data. More...
 
TensorShape GetStrides () const override
 Get the strides for each dimension ordered from largest to smallest where the smallest value is the same as the size of a single element in the tensor. More...
 
TensorShape GetShape () const override
 Get the number of elements for each dimension ordered from slowest iterating dimension to fastest iterating dimension. More...
 
const TensorInfoGetTensorInfo () const
 
MemorySourceFlags GetImportFlags () const override
 Get flags describing supported import sources. More...
 
bool Import (void *memory, MemorySource source) override
 Import externally allocated memory. More...
 
bool CanBeImported (void *memory, MemorySource source) override
 Implementations must determine if this memory block can be imported. More...
 
- Public Member Functions inherited from ITensorHandle
virtual ~ITensorHandle ()
 
void * Map (bool blocking=true)
 Map the tensor data for access. More...
 
void Unmap ()
 Unmap the tensor data that was previously mapped with call to Map(). More...
 
virtual void Unimport ()
 Unimport externally allocated memory. More...
 

Detailed Description

Definition at line 14 of file MockTensorHandle.hpp.

Constructor & Destructor Documentation

◆ MockTensorHandle() [1/2]

MockTensorHandle ( const TensorInfo tensorInfo,
std::shared_ptr< MockMemoryManager > &  memoryManager 
)

Definition at line 11 of file MockTensorHandle.cpp.

Referenced by MockTensorHandle::GetImportFlags().

12  : m_TensorInfo(tensorInfo)
13  , m_MemoryManager(memoryManager)
14  , m_Pool(nullptr)
15  , m_UnmanagedMemory(nullptr)
16  , m_ImportFlags(static_cast<MemorySourceFlags>(MemorySource::Undefined))
17  , m_Imported(false)
18  , m_IsImportEnabled(false)
19 {}

◆ MockTensorHandle() [2/2]

MockTensorHandle ( const TensorInfo tensorInfo,
MemorySourceFlags  importFlags 
)

Definition at line 21 of file MockTensorHandle.cpp.

22  : m_TensorInfo(tensorInfo)
23  , m_Pool(nullptr)
24  , m_UnmanagedMemory(nullptr)
25  , m_ImportFlags(importFlags)
26  , m_Imported(false)
27  , m_IsImportEnabled(true)
28 {}

◆ ~MockTensorHandle()

~MockTensorHandle ( )
override

Definition at line 30 of file MockTensorHandle.cpp.

31 {
32  if (!m_Pool)
33  {
34  // unmanaged
35  if (!m_Imported)
36  {
37  ::operator delete(m_UnmanagedMemory);
38  }
39  }
40 }

Member Function Documentation

◆ Allocate()

void Allocate ( )
overridevirtual

Indicate to the memory manager that this resource is no longer active.

This is used to compute overlapping lifetimes of resources.

Implements ITensorHandle.

Definition at line 53 of file MockTensorHandle.cpp.

References TensorInfo::GetNumBytes().

54 {
55  // If import is enabled, do not allocate the tensor
56  if (!m_IsImportEnabled)
57  {
58 
59  if (!m_UnmanagedMemory)
60  {
61  if (!m_Pool)
62  {
63  // unmanaged
64  m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes());
65  }
66  else
67  {
68  m_MemoryManager->Allocate(m_Pool);
69  }
70  }
71  else
72  {
73  throw InvalidArgumentException("MockTensorHandle::Allocate Trying to allocate a MockTensorHandle"
74  "that already has allocated memory.");
75  }
76  }
77 }
unsigned int GetNumBytes() const
Definition: Tensor.cpp:429

◆ CanBeImported()

bool CanBeImported ( void *  memory,
MemorySource  source 
)
overridevirtual

Implementations must determine if this memory block can be imported.

This might be based on alignment or memory source type.

Returns
true if this memory can be imported.
false by default, cannot be imported.

Reimplemented from ITensorHandle.

Definition at line 158 of file MockTensorHandle.cpp.

References TensorInfo::GetDataType(), armnn::GetDataTypeSize(), and armnn::Malloc.

Referenced by MockTensorHandle::GetImportFlags(), and MockTensorHandle::Import().

159 {
160  if (m_ImportFlags & static_cast<MemorySourceFlags>(source))
161  {
162  if (m_IsImportEnabled && source == MemorySource::Malloc)
163  {
164  uintptr_t alignment = GetDataTypeSize(m_TensorInfo.GetDataType());
165  if (reinterpret_cast<uintptr_t>(memory) % alignment)
166  {
167  return false;
168  }
169 
170  return true;
171  }
172  }
173  return false;
174 }
DataType GetDataType() const
Definition: Tensor.hpp:198
constexpr unsigned int GetDataTypeSize(DataType dataType)
Definition: TypesUtils.hpp:151

◆ GetImportFlags()

MemorySourceFlags GetImportFlags ( ) const
inlineoverridevirtual

Get flags describing supported import sources.

Reimplemented from ITensorHandle.

Definition at line 53 of file MockTensorHandle.hpp.

References MockTensorHandle::CanBeImported(), MockTensorHandle::Import(), and MockTensorHandle::MockTensorHandle().

54  {
55  return m_ImportFlags;
56  }

◆ GetParent()

ITensorHandle* GetParent ( ) const
inlineoverridevirtual

Get the parent tensor if this is a subtensor.

Returns
a pointer to the parent tensor. Otherwise nullptr if not a subtensor.

Implements ITensorHandle.

Definition at line 27 of file MockTensorHandle.hpp.

References MockTensorHandle::Map(), and ITensorHandle::Map().

28  {
29  return nullptr;
30  }

◆ GetShape()

TensorShape GetShape ( ) const
inlineoverridevirtual

Get the number of elements for each dimension ordered from slowest iterating dimension to fastest iterating dimension.

Returns
a TensorShape filled with the number of elements for each dimension.

Implements ITensorHandle.

Definition at line 43 of file MockTensorHandle.hpp.

References TensorInfo::GetShape().

44  {
45  return m_TensorInfo.GetShape();
46  }
const TensorShape & GetShape() const
Definition: Tensor.hpp:191

◆ GetStrides()

TensorShape GetStrides ( ) const
inlineoverridevirtual

Get the strides for each dimension ordered from largest to smallest where the smallest value is the same as the size of a single element in the tensor.

Returns
a TensorShape filled with the strides for each dimension

Implements ITensorHandle.

Definition at line 38 of file MockTensorHandle.hpp.

References armnn::GetUnpaddedTensorStrides().

39  {
40  return GetUnpaddedTensorStrides(m_TensorInfo);
41  }
TensorShape GetUnpaddedTensorStrides(const TensorInfo &tensorInfo)

◆ GetTensorInfo()

const TensorInfo& GetTensorInfo ( ) const
inline

Definition at line 48 of file MockTensorHandle.hpp.

49  {
50  return m_TensorInfo;
51  }

◆ Import()

bool Import ( void *  memory,
MemorySource  source 
)
overridevirtual

Import externally allocated memory.

Parameters
memorybase address of the memory being imported.
sourcesource of the allocation for the memory being imported.
Returns
true on success or false on failure

Reimplemented from ITensorHandle.

Definition at line 114 of file MockTensorHandle.cpp.

References MockTensorHandle::CanBeImported(), and armnn::Malloc.

Referenced by MockTensorHandle::GetImportFlags().

115 {
116  if (m_ImportFlags & static_cast<MemorySourceFlags>(source))
117  {
118  if (m_IsImportEnabled && source == MemorySource::Malloc)
119  {
120  // Check memory alignment
121  if (!CanBeImported(memory, source))
122  {
123  if (m_Imported)
124  {
125  m_Imported = false;
126  m_UnmanagedMemory = nullptr;
127  }
128 
129  return false;
130  }
131 
132  // m_UnmanagedMemory not yet allocated.
133  if (!m_Imported && !m_UnmanagedMemory)
134  {
135  m_UnmanagedMemory = memory;
136  m_Imported = true;
137  return true;
138  }
139 
140  // m_UnmanagedMemory initially allocated with Allocate().
141  if (!m_Imported && m_UnmanagedMemory)
142  {
143  return false;
144  }
145 
146  // m_UnmanagedMemory previously imported.
147  if (m_Imported)
148  {
149  m_UnmanagedMemory = memory;
150  return true;
151  }
152  }
153  }
154 
155  return false;
156 }
bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.

◆ Manage()

void Manage ( )
overridevirtual

Indicate to the memory manager that this resource is active.

This is used to compute overlapping lifetimes of resources.

Implements ITensorHandle.

Definition at line 42 of file MockTensorHandle.cpp.

References ARMNN_ASSERT_MSG, and TensorInfo::GetNumBytes().

43 {
44  if (!m_IsImportEnabled)
45  {
46  ARMNN_ASSERT_MSG(!m_Pool, "MockTensorHandle::Manage() called twice");
47  ARMNN_ASSERT_MSG(!m_UnmanagedMemory, "MockTensorHandle::Manage() called after Allocate()");
48 
49  m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
50  }
51 }
unsigned int GetNumBytes() const
Definition: Tensor.cpp:429
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15

◆ Map()

const void * Map ( bool  blocking) const
overridevirtual

Map the tensor data for access.

Parameters
blockinghint to block the calling thread until all other accesses are complete. (backend dependent)
Returns
pointer to the first element of the mapped data.

Implements ITensorHandle.

Definition at line 79 of file MockTensorHandle.cpp.

References ARMNN_ASSERT, and TensorInfo::GetNumBytes().

Referenced by MockTensorHandle::GetParent().

80 {
81  return GetPointer();
82 }

◆ Unmap()

void Unmap ( ) const
inlineoverridevirtual

Unmap the tensor data.

Implements ITensorHandle.

Definition at line 35 of file MockTensorHandle.hpp.

36  {}

The documentation for this class was generated from the following files: