ArmNN
 24.02
SampleTensorHandle Class Reference

#include <SampleTensorHandle.hpp>

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

Public Member Functions

 SampleTensorHandle (const armnn::TensorInfo &tensorInfo, std::shared_ptr< SampleMemoryManager > &memoryManager)
 
 SampleTensorHandle (const armnn::TensorInfo &tensorInfo, armnn::MemorySourceFlags importFlags)
 
 ~SampleTensorHandle ()
 
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 ITensorHandle * GetParent () 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...
 
armnn::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...
 
armnn::TensorShape GetShape () const override
 Get the number of elements for each dimension ordered from slowest iterating dimension to fastest iterating dimension. More...
 
const armnn::TensorInfoGetTensorInfo () const
 
virtual armnn::MemorySourceFlags GetImportFlags () const override
 Get flags describing supported import sources. More...
 
virtual bool Import (void *memory, armnn::MemorySource source) override
 Import externally allocated memory. 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 bool CanBeImported (void *memory, MemorySource source)
 Implementations must determine if this memory block can be imported. 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 SampleTensorHandle.hpp.

Constructor & Destructor Documentation

◆ SampleTensorHandle() [1/2]

SampleTensorHandle ( const armnn::TensorInfo tensorInfo,
std::shared_ptr< SampleMemoryManager > &  memoryManager 
)

Definition at line 11 of file SampleTensorHandle.cpp.

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 }

◆ SampleTensorHandle() [2/2]

SampleTensorHandle ( const armnn::TensorInfo tensorInfo,
armnn::MemorySourceFlags  importFlags 
)

Definition at line 23 of file SampleTensorHandle.cpp.

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 }

◆ ~SampleTensorHandle()

Definition at line 35 of file SampleTensorHandle.cpp.

36 {
37  if (!m_Pool)
38  {
39  // unmanaged
40  if (!m_Imported)
41  {
42  ::operator delete(m_UnmanagedMemory);
43  }
44  }
45 }

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 52 of file SampleTensorHandle.cpp.

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 }

References TensorInfo::GetNumBytes().

◆ GetImportFlags()

virtual armnn::MemorySourceFlags GetImportFlags ( ) const
inlineoverridevirtual

Get flags describing supported import sources.

Reimplemented from ITensorHandle.

Definition at line 54 of file SampleTensorHandle.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 SampleTensorHandle.hpp.

29  {
30  return nullptr;
31  }

◆ GetShape()

armnn::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 SampleTensorHandle.hpp.

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

References TensorInfo::GetShape().

◆ GetStrides()

armnn::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 SampleTensorHandle.hpp.

40  {
41  return GetUnpaddedTensorStrides(m_TensorInfo);
42  }

References armnn::GetUnpaddedTensorStrides().

◆ GetTensorInfo()

const armnn::TensorInfo& GetTensorInfo ( ) const
inline

Definition at line 49 of file SampleTensorHandle.hpp.

50  {
51  return m_TensorInfo;
52  }

Referenced by sdb::GetTensorInfo().

◆ Import()

bool Import ( void *  memory,
armnn::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 91 of file SampleTensorHandle.cpp.

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 }

References 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 47 of file SampleTensorHandle.cpp.

48 {
49  m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
50 }

References TensorInfo::GetNumBytes().

◆ 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 74 of file SampleTensorHandle.cpp.

75 {
76  return GetPointer();
77 }

◆ Unmap()

virtual void Unmap ( ) const
inlineoverridevirtual

Unmap the tensor data.

Implements ITensorHandle.

Definition at line 36 of file SampleTensorHandle.hpp.

37  {}

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::MemorySource::Undefined
@ Undefined
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193