ArmNN
 24.08
TosaRefTensorHandle Class Reference

#include <TosaRefTensorHandle.hpp>

Inheritance diagram for TosaRefTensorHandle:
[legend]
Collaboration diagram for TosaRefTensorHandle:
[legend]

Public Member Functions

 TosaRefTensorHandle (const TensorInfo &tensorInfo, std::shared_ptr< TosaRefMemoryManager > &memoryManager)
 
 TosaRefTensorHandle (const TensorInfo &tensorInfo, MemorySourceFlags importFlags)
 
 ~TosaRefTensorHandle ()
 
virtual void Manage () override
 Indicate to the memory manager that this resource is active. More...
 
virtual void Allocate () override
 Indicate to the memory manager that this resource is no longer active. More...
 
virtual ITensorHandleGetParent () const override
 Get the parent tensor if this is a subtensor. More...
 
virtual const void * Map (bool) const override
 Map the tensor data for access. More...
 
virtual 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
 
virtual MemorySourceFlags GetImportFlags () const override
 Get flags describing supported import sources. More...
 
virtual bool Import (void *memory, MemorySource source) override
 Import externally allocated memory. More...
 
virtual bool CanBeImported (void *memory, MemorySource source) override
 Implementations must determine if this memory block can be imported. More...
 
virtual const void * Map (bool blocking=true) const=0
 Map the tensor data for access. More...
 
void * Map (bool blocking=true)
 Map the tensor data for access. 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...
 
virtual std::shared_ptr< ITensorHandleDecorateTensorHandle (const TensorInfo &tensorInfo)
 Returns a decorated version of this TensorHandle allowing us to override the TensorInfo for it. More...
 

Detailed Description

Definition at line 15 of file TosaRefTensorHandle.hpp.

Constructor & Destructor Documentation

◆ TosaRefTensorHandle() [1/2]

TosaRefTensorHandle ( const TensorInfo tensorInfo,
std::shared_ptr< TosaRefMemoryManager > &  memoryManager 
)

Definition at line 10 of file TosaRefTensorHandle.cpp.

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 {}

◆ TosaRefTensorHandle() [2/2]

TosaRefTensorHandle ( const TensorInfo tensorInfo,
MemorySourceFlags  importFlags 
)

Definition at line 21 of file TosaRefTensorHandle.cpp.

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

◆ ~TosaRefTensorHandle()

Definition at line 31 of file TosaRefTensorHandle.cpp.

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

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 54 of file TosaRefTensorHandle.cpp.

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

References TensorInfo::GetNumBytes().

◆ 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 172 of file TosaRefTensorHandle.cpp.

173 {
174  if (m_ImportFlags & static_cast<MemorySourceFlags>(source))
175  {
176  if (m_IsImportEnabled && source == MemorySource::Malloc)
177  {
178  uintptr_t alignment = GetDataTypeSize(m_TensorInfo.GetDataType());
179  if (reinterpret_cast<uintptr_t>(memory) % alignment)
180  {
181  return false;
182  }
183  return true;
184  }
185  }
186  return false;
187 }

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

Referenced by TosaRefTensorHandle::Import().

◆ GetImportFlags()

virtual MemorySourceFlags GetImportFlags ( ) const
inlineoverridevirtual

Get flags describing supported import sources.

Reimplemented from ITensorHandle.

Definition at line 54 of file TosaRefTensorHandle.hpp.

55  {
56  return m_ImportFlags;
57  }

◆ GetParent()

virtual 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 28 of file TosaRefTensorHandle.hpp.

29  {
30  return nullptr;
31  }

◆ 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 44 of file TosaRefTensorHandle.hpp.

45  {
46  return m_TensorInfo.GetShape();
47  }

References TensorInfo::GetShape().

◆ 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 39 of file TosaRefTensorHandle.hpp.

40  {
41  return GetUnpaddedTensorStrides(m_TensorInfo);
42  }

References armnn::GetUnpaddedTensorStrides().

◆ GetTensorInfo()

const TensorInfo& GetTensorInfo ( ) const
inline

Definition at line 49 of file TosaRefTensorHandle.hpp.

50  {
51  return m_TensorInfo;
52  }

◆ 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 129 of file TosaRefTensorHandle.cpp.

130 {
131  if (m_ImportFlags & static_cast<MemorySourceFlags>(source))
132  {
133  if (m_IsImportEnabled && source == MemorySource::Malloc)
134  {
135  // Check memory alignment
136  if(!CanBeImported(memory, source))
137  {
138  if (m_Imported)
139  {
140  m_Imported = false;
141  m_UnmanagedMemory = nullptr;
142  }
143  return false;
144  }
145 
146  // m_UnmanagedMemory not yet allocated.
147  if (!m_Imported && !m_UnmanagedMemory)
148  {
149  m_UnmanagedMemory = memory;
150  m_Imported = true;
151  return true;
152  }
153 
154  // m_UnmanagedMemory initially allocated with Allocate().
155  if (!m_Imported && m_UnmanagedMemory)
156  {
157  return false;
158  }
159 
160  // m_UnmanagedMemory previously imported.
161  if (m_Imported)
162  {
163  m_UnmanagedMemory = memory;
164  return true;
165  }
166  }
167  }
168 
169  return false;
170 }

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

◆ 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 43 of file TosaRefTensorHandle.cpp.

44 {
45  if (!m_IsImportEnabled)
46  {
47  ARMNN_THROW_MSG_IF_FALSE(!m_Pool, RuntimeException, "TosaRefTensorHandle::Manage() called twice");
48  ARMNN_THROW_MSG_IF_FALSE(!m_UnmanagedMemory, RuntimeException,
49  "TosaRefTensorHandle::Manage() called after Allocate()");
50  m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
51  }
52 }

References ARMNN_THROW_MSG_IF_FALSE, and TensorInfo::GetNumBytes().

◆ Map() [1/3]

void* Map
inline

Map the tensor data for access.

Must be paired with call to Unmap().

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.

Definition at line 44 of file ITensorHandle.hpp.

45  {
46  return const_cast<void*>(static_cast<const ITensorHandle*>(this)->Map(blocking));
47  }

◆ Map() [2/3]

virtual const void* Map

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.

◆ Map() [3/3]

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 80 of file TosaRefTensorHandle.cpp.

81 {
82  return GetPointer();
83 }

◆ Unmap()

virtual void Unmap ( ) const
inlineoverridevirtual

Unmap the tensor data.

Implements ITensorHandle.

Definition at line 36 of file TosaRefTensorHandle.hpp.

37  {}

The documentation for this class was generated from the following files:
armnn::MemorySource::Malloc
@ Malloc
armnn::TosaRefTensorHandle::Map
virtual const void * Map(bool) const override
Map the tensor data for access.
Definition: TosaRefTensorHandle.cpp:80
armnn::TensorInfo::GetNumBytes
unsigned int GetNumBytes() const
Definition: Tensor.cpp:427
armnn::MemorySourceFlags
unsigned int MemorySourceFlags
Definition: MemorySources.hpp:15
armnn::GetUnpaddedTensorStrides
TensorShape GetUnpaddedTensorStrides(const TensorInfo &tensorInfo)
Definition: TensorHandle.cpp:15
armnn::TosaRefTensorHandle::CanBeImported
virtual bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.
Definition: TosaRefTensorHandle.cpp:172
armnn::MemorySource::Undefined
@ Undefined
armnn::GetDataTypeSize
constexpr unsigned int GetDataTypeSize(DataType dataType)
Definition: TypesUtils.hpp:182
ARMNN_THROW_MSG_IF_FALSE
#define ARMNN_THROW_MSG_IF_FALSE(_cond, _except, _str)
Definition: Exceptions.hpp:206
armnn::TensorInfo::GetDataType
DataType GetDataType() const
Definition: Tensor.hpp:200
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193