ArmNN
 23.11
RefTensorHandle Class Reference

#include <RefTensorHandle.hpp>

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

Public Member Functions

 RefTensorHandle (const TensorInfo &tensorInfo, std::shared_ptr< RefMemoryManager > &memoryManager)
 
 RefTensorHandle (const TensorInfo &tensorInfo)
 
 RefTensorHandle (const TensorInfo &tensorInfo, const RefTensorHandle &parent)
 
 ~RefTensorHandle ()
 
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 std::shared_ptr< ITensorHandleDecorateTensorHandle (const TensorInfo &tensorInfo) override
 Returns a decorated version of this TensorHandle allowing us to override the TensorInfo for it. 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...
 

Detailed Description

Definition at line 17 of file RefTensorHandle.hpp.

Constructor & Destructor Documentation

◆ RefTensorHandle() [1/3]

RefTensorHandle ( const TensorInfo tensorInfo,
std::shared_ptr< RefMemoryManager > &  memoryManager 
)

Definition at line 11 of file RefTensorHandle.cpp.

11  :
12  m_TensorInfo(tensorInfo),
13  m_MemoryManager(memoryManager),
14  m_Pool(nullptr),
15  m_UnmanagedMemory(nullptr),
16  m_ImportedMemory(nullptr),
17  m_Decorated()
18 {
19 }

◆ RefTensorHandle() [2/3]

RefTensorHandle ( const TensorInfo tensorInfo)

Definition at line 21 of file RefTensorHandle.cpp.

22  : m_TensorInfo(tensorInfo),
23  m_Pool(nullptr),
24  m_UnmanagedMemory(nullptr),
25  m_ImportedMemory(nullptr),
26  m_Decorated()
27 {
28 }

◆ RefTensorHandle() [3/3]

RefTensorHandle ( const TensorInfo tensorInfo,
const RefTensorHandle parent 
)

Definition at line 30 of file RefTensorHandle.cpp.

31  : m_TensorInfo(tensorInfo),
32  m_MemoryManager(parent.m_MemoryManager),
33  m_Pool(parent.m_Pool),
34  m_UnmanagedMemory(parent.m_UnmanagedMemory),
35  m_ImportedMemory(parent.m_ImportedMemory),
36  m_Decorated()
37 {
38 }

◆ ~RefTensorHandle()

Definition at line 40 of file RefTensorHandle.cpp.

41 {
42  ::operator delete(m_UnmanagedMemory);
43 }

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.

Reimplemented in RefTensorHandleDecorator.

Definition at line 56 of file RefTensorHandle.cpp.

57 {
58  if (!m_UnmanagedMemory)
59  {
60  if (!m_Pool)
61  {
62  // unmanaged
63  m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes());
64  }
65  else
66  {
67  m_MemoryManager->Allocate(m_Pool);
68  }
69  }
70  else
71  {
72  throw InvalidArgumentException("RefTensorHandle::Allocate Trying to allocate a RefTensorHandle"
73  "that already has allocated memory.");
74  }
75 }

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.

Reimplemented in RefTensorHandleDecorator.

Definition at line 153 of file RefTensorHandle.cpp.

154 {
155  if (source == MemorySource::Malloc)
156  {
157  uintptr_t alignment = GetDataTypeSize(m_TensorInfo.GetDataType());
158  if (reinterpret_cast<uintptr_t>(memory) % alignment)
159  {
160  return false;
161  }
162  return true;
163  }
164  return false;
165 }

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

Referenced by RefTensorHandle::Import().

◆ DecorateTensorHandle()

std::shared_ptr< ITensorHandle > DecorateTensorHandle ( const TensorInfo tensorInfo)
overridevirtual

Returns a decorated version of this TensorHandle allowing us to override the TensorInfo for it.

Parameters
tensorInfothe overidden TensorInfo.

Reimplemented from ITensorHandle.

Reimplemented in RefTensorHandleDecorator.

Definition at line 167 of file RefTensorHandle.cpp.

168 {
169  auto decorated = std::make_shared<RefTensorHandleDecorator>(tensorInfo, *this);
170  m_Decorated.emplace_back(decorated);
171  return decorated;
172 }

◆ GetImportFlags()

MemorySourceFlags GetImportFlags ( ) const
overridevirtual

Get flags describing supported import sources.

Reimplemented from ITensorHandle.

Reimplemented in RefTensorHandleDecorator.

Definition at line 130 of file RefTensorHandle.cpp.

131 {
132  return static_cast<MemorySourceFlags>(MemorySource::Malloc);
133 }

References armnn::Malloc.

◆ 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.

Reimplemented in RefTensorHandleDecorator.

Definition at line 32 of file RefTensorHandle.hpp.

33  {
34  return nullptr;
35  }

◆ 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.

Reimplemented in RefTensorHandleDecorator.

Definition at line 48 of file RefTensorHandle.hpp.

49  {
50  return m_TensorInfo.GetShape();
51  }

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.

Reimplemented in RefTensorHandleDecorator.

Definition at line 43 of file RefTensorHandle.hpp.

44  {
45  return GetUnpaddedTensorStrides(m_TensorInfo);
46  }

References armnn::GetUnpaddedTensorStrides().

◆ GetTensorInfo()

const TensorInfo& GetTensorInfo ( ) const
inline

Definition at line 53 of file RefTensorHandle.hpp.

54  {
55  return m_TensorInfo;
56  }

◆ 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.

Reimplemented in RefTensorHandleDecorator.

Definition at line 135 of file RefTensorHandle.cpp.

136 {
137  if (source == MemorySource::Malloc)
138  {
139  // Check memory alignment
140  if(!CanBeImported(memory, source))
141  {
142  m_ImportedMemory = nullptr;
143  return false;
144  }
145 
146  m_ImportedMemory = memory;
147  return true;
148  }
149 
150  return false;
151 }

References RefTensorHandle::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.

Reimplemented in RefTensorHandleDecorator.

Definition at line 45 of file RefTensorHandle.cpp.

46 {
47  ARMNN_ASSERT_MSG(!m_Pool, "RefTensorHandle::Manage() called twice");
48  ARMNN_ASSERT_MSG(!m_UnmanagedMemory, "RefTensorHandle::Manage() called after Allocate()");
49 
50  if (m_MemoryManager)
51  {
52  m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
53  }
54 }

References ARMNN_ASSERT_MSG, 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.

Reimplemented in RefTensorHandleDecorator.

Definition at line 77 of file RefTensorHandle.cpp.

78 {
79  return GetPointer();
80 }

Referenced by RefTensorHandleDecorator::Map().

◆ Unmap()

virtual void Unmap ( ) const
inlineoverridevirtual

Unmap the tensor data.

Implements ITensorHandle.

Reimplemented in RefTensorHandleDecorator.

Definition at line 40 of file RefTensorHandle.hpp.

41  {}

The documentation for this class was generated from the following files:
armnn::MemorySource::Malloc
@ Malloc
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_ASSERT_MSG
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
armnn::GetDataTypeSize
constexpr unsigned int GetDataTypeSize(DataType dataType)
Definition: TypesUtils.hpp:182
armnn::TensorInfo::GetDataType
DataType GetDataType() const
Definition: Tensor.hpp:198
armnn::RefTensorHandle::Map
virtual const void * Map(bool) const override
Map the tensor data for access.
Definition: RefTensorHandle.cpp:77
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
armnn::RefTensorHandle::CanBeImported
virtual bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.
Definition: RefTensorHandle.cpp:153