ArmNN
 20.02
RefTensorHandle Class Reference

#include <RefTensorHandle.hpp>

Inheritance diagram for RefTensorHandle:
ITensorHandle

Public Member Functions

 RefTensorHandle (const TensorInfo &tensorInfo, std::shared_ptr< RefMemoryManager > &memoryManager)
 
 RefTensorHandle (const TensorInfo &tensorInfo, std::shared_ptr< RefMemoryManager > &memoryManager, MemorySourceFlags importFlags)
 
 ~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...
 
- 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...
 

Detailed Description

Definition at line 15 of file RefTensorHandle.hpp.

Constructor & Destructor Documentation

◆ RefTensorHandle() [1/2]

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

Definition at line 10 of file RefTensorHandle.cpp.

Referenced by RefTensorHandle::GetImportFlags().

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

◆ RefTensorHandle() [2/2]

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

Definition at line 21 of file RefTensorHandle.cpp.

23  : m_TensorInfo(tensorInfo),
24  m_MemoryManager(memoryManager),
25  m_Pool(nullptr),
26  m_UnmanagedMemory(nullptr),
27  m_ImportFlags(importFlags),
28  m_Imported(false)
29 {
30 
31 }

◆ ~RefTensorHandle()

Definition at line 33 of file RefTensorHandle.cpp.

34 {
35  if (!m_Pool)
36  {
37  // unmanaged
38  if (!m_Imported)
39  {
40  ::operator delete(m_UnmanagedMemory);
41  }
42  }
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.

Definition at line 53 of file RefTensorHandle.cpp.

References TensorInfo::GetNumBytes().

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

◆ GetImportFlags()

virtual MemorySourceFlags GetImportFlags ( ) const
inlineoverridevirtual

Get flags describing supported import sources.

Reimplemented from ITensorHandle.

Definition at line 55 of file RefTensorHandle.hpp.

References RefTensorHandle::Import(), and RefTensorHandle::RefTensorHandle().

56  {
57  return m_ImportFlags;
58  }

◆ 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 29 of file RefTensorHandle.hpp.

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

30  {
31  return nullptr;
32  }

◆ 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 45 of file RefTensorHandle.hpp.

References TensorInfo::GetShape().

46  {
47  return m_TensorInfo.GetShape();
48  }
const TensorShape & GetShape() const
Definition: Tensor.hpp:88

◆ 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 40 of file RefTensorHandle.hpp.

References armnn::GetUnpaddedTensorStrides().

41  {
42  return GetUnpaddedTensorStrides(m_TensorInfo);
43  }
TensorShape GetUnpaddedTensorStrides(const TensorInfo &tensorInfo)

◆ GetTensorInfo()

const TensorInfo& GetTensorInfo ( ) const
inline

Definition at line 50 of file RefTensorHandle.hpp.

Referenced by armnn::GetTensorInfo().

51  {
52  return m_TensorInfo;
53  }

◆ 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 106 of file RefTensorHandle.cpp.

References armnn::Malloc.

Referenced by RefTensorHandle::GetImportFlags().

107 {
108 
109  if (m_ImportFlags & static_cast<MemorySourceFlags>(source))
110  {
111  if (source == MemorySource::Malloc)
112  {
113  // Check memory alignment
114  constexpr uintptr_t alignment = sizeof(size_t);
115  if (reinterpret_cast<uintptr_t>(memory) % alignment)
116  {
117  if (m_Imported)
118  {
119  m_Imported = false;
120  m_UnmanagedMemory = nullptr;
121  }
122 
123  return false;
124  }
125 
126  // m_UnmanagedMemory not yet allocated.
127  if (!m_Imported && !m_UnmanagedMemory)
128  {
129  m_UnmanagedMemory = memory;
130  m_Imported = true;
131  return true;
132  }
133 
134  // m_UnmanagedMemory initially allocated with Allocate().
135  if (!m_Imported && m_UnmanagedMemory)
136  {
137  return false;
138  }
139 
140  // m_UnmanagedMemory previously imported.
141  if (m_Imported)
142  {
143  m_UnmanagedMemory = memory;
144  return true;
145  }
146  }
147  }
148 
149  return false;
150 }

◆ 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 45 of file RefTensorHandle.cpp.

References TensorInfo::GetNumBytes().

46 {
47  BOOST_ASSERT_MSG(!m_Pool, "RefTensorHandle::Manage() called twice");
48  BOOST_ASSERT_MSG(!m_UnmanagedMemory, "RefTensorHandle::Manage() called after Allocate()");
49 
50  m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
51 }
unsigned int GetNumBytes() const
Definition: Tensor.cpp:213

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

References TensorInfo::GetNumBytes().

Referenced by RefTensorHandle::GetParent().

75 {
76  return GetPointer();
77 }

◆ Unmap()

virtual void Unmap ( ) const
inlineoverridevirtual

Unmap the tensor data.

Implements ITensorHandle.

Definition at line 37 of file RefTensorHandle.hpp.

38  {}

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