ArmNN
 22.02
ClImportTensorHandle Class Reference

#include <ClImportTensorHandle.hpp>

Inheritance diagram for ClImportTensorHandle:
IClTensorHandle IAclTensorHandle ITensorHandle

Public Member Functions

 ClImportTensorHandle (const TensorInfo &tensorInfo, MemorySourceFlags importFlags)
 
 ClImportTensorHandle (const TensorInfo &tensorInfo, DataLayout dataLayout, MemorySourceFlags importFlags)
 
arm_compute::CLTensor & GetTensor () override
 
arm_compute::CLTensor const & GetTensor () const override
 
virtual void Allocate () override
 Indicate to the memory manager that this resource is no longer active. More...
 
virtual void Manage () override
 Indicate to the memory manager that this resource is active. More...
 
virtual const void * Map (bool blocking=true) const override
 Map the tensor data for access. More...
 
virtual void Unmap () const override
 Unmap the tensor data. More...
 
virtual ITensorHandleGetParent () const override
 Get the parent tensor if this is a subtensor. More...
 
virtual arm_compute::DataType GetDataType () const override
 
virtual void SetMemoryGroup (const std::shared_ptr< arm_compute::IMemoryGroup > &memoryGroup) override
 
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...
 
void SetImportFlags (MemorySourceFlags importFlags)
 
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...
 
- 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 30 of file ClImportTensorHandle.hpp.

Constructor & Destructor Documentation

◆ ClImportTensorHandle() [1/2]

ClImportTensorHandle ( const TensorInfo tensorInfo,
MemorySourceFlags  importFlags 
)
inline

Definition at line 33 of file ClImportTensorHandle.hpp.

34  : m_ImportFlags(importFlags)
35  {
36  armnn::armcomputetensorutils::BuildArmComputeTensor(m_Tensor, tensorInfo);
37  }

◆ ClImportTensorHandle() [2/2]

ClImportTensorHandle ( const TensorInfo tensorInfo,
DataLayout  dataLayout,
MemorySourceFlags  importFlags 
)
inline

Definition at line 39 of file ClImportTensorHandle.hpp.

42  : m_ImportFlags(importFlags), m_Imported(false)
43  {
44  armnn::armcomputetensorutils::BuildArmComputeTensor(m_Tensor, tensorInfo, dataLayout);
45  }

Member Function Documentation

◆ Allocate()

virtual void Allocate ( )
inlineoverridevirtual

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 49 of file ClImportTensorHandle.hpp.

49 {}

◆ CanBeImported()

virtual bool CanBeImported ( void *  memory,
MemorySource  source 
)
inlineoverridevirtual

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 188 of file ClImportTensorHandle.hpp.

References ARMNN_ASSERT, armnn::error, ClImportTensorHandle::GetDataType(), armnn::Malloc, and ClImportTensorHandle::Map().

189  {
190  if (m_ImportFlags & static_cast<MemorySourceFlags>(source))
191  {
192  if (source == MemorySource::Malloc)
193  {
194  const cl_import_properties_arm importProperties[] =
195  {
196  CL_IMPORT_TYPE_ARM,
197  CL_IMPORT_TYPE_HOST_ARM,
198  0
199  };
200 
201  size_t totalBytes = m_Tensor.info()->total_size();
202 
203  // Round the size of the mapping to match the CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE
204  // This does not change the size of the buffer, only the size of the mapping the buffer is mapped to
205  // We do this to match the behaviour of the Import function later on.
206  auto cachelineAlignment =
207  arm_compute::CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE>();
208  auto roundedSize = totalBytes;
209  if (totalBytes % cachelineAlignment != 0)
210  {
211  roundedSize = cachelineAlignment + totalBytes - (totalBytes % cachelineAlignment);
212  }
213 
214  cl_int error = CL_SUCCESS;
215  cl_mem buffer;
216  buffer = clImportMemoryARM(arm_compute::CLKernelLibrary::get().context().get(),
217  CL_MEM_READ_WRITE, importProperties, memory, roundedSize, &error);
218 
219  // If we fail to map we know the import will not succeed and can return false.
220  // There is no memory to be released if error is not CL_SUCCESS
221  if (error != CL_SUCCESS)
222  {
223  return false;
224  }
225  else
226  {
227  // If import was successful we can release the mapping knowing import will succeed at workload
228  // execution and return true
229  error = clReleaseMemObject(buffer);
230  if (error == CL_SUCCESS)
231  {
232  return true;
233  }
234  else
235  {
236  // If we couldn't release the mapping this constitutes a memory leak and throw an exception
237  throw MemoryImportException("ClImportTensorHandle::Failed to unmap cl_mem buffer: "
238  + std::to_string(error));
239  }
240  }
241  }
242  }
243  else
244  {
245  throw MemoryImportException("ClImportTensorHandle::Incorrect import flag");
246  }
247  return false;
248  }

◆ GetDataType()

virtual arm_compute::DataType GetDataType ( ) const
inlineoverridevirtual

Implements IClTensorHandle.

Definition at line 62 of file ClImportTensorHandle.hpp.

Referenced by ClImportTensorHandle::CanBeImported().

63  {
64  return m_Tensor.info()->data_type();
65  }

◆ GetImportFlags()

MemorySourceFlags GetImportFlags ( ) const
inlineoverridevirtual

Get flags describing supported import sources.

Reimplemented from ITensorHandle.

Definition at line 87 of file ClImportTensorHandle.hpp.

88  {
89  return m_ImportFlags;
90  }

◆ 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 60 of file ClImportTensorHandle.hpp.

60 { return nullptr; }

◆ 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 77 of file ClImportTensorHandle.hpp.

78  {
79  return armcomputetensorutils::GetShape(m_Tensor.info()->tensor_shape());
80  }

◆ 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 72 of file ClImportTensorHandle.hpp.

73  {
74  return armcomputetensorutils::GetStrides(m_Tensor.info()->strides_in_bytes());
75  }

◆ GetTensor() [1/2]

arm_compute::CLTensor& GetTensor ( )
inlineoverridevirtual

Implements IClTensorHandle.

Definition at line 47 of file ClImportTensorHandle.hpp.

47 { return m_Tensor; }

◆ GetTensor() [2/2]

arm_compute::CLTensor const& GetTensor ( ) const
inlineoverridevirtual

Implements IClTensorHandle.

Definition at line 48 of file ClImportTensorHandle.hpp.

48 { return m_Tensor; }

◆ Import()

virtual bool Import ( void *  memory,
MemorySource  source 
)
inlineoverridevirtual

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 92 of file ClImportTensorHandle.hpp.

References armnn::DmaBuf, armnn::DmaBufProtected, armnn::Gralloc, and armnn::Malloc.

93  {
94  if (m_ImportFlags & static_cast<MemorySourceFlags>(source))
95  {
96  if (source == MemorySource::Malloc)
97  {
98  const cl_import_properties_arm importProperties[] =
99  {
100  CL_IMPORT_TYPE_ARM,
101  CL_IMPORT_TYPE_HOST_ARM,
102  0
103  };
104 
105  return ClImport(importProperties, memory);
106  }
107  if (source == MemorySource::DmaBuf)
108  {
109  const cl_import_properties_arm importProperties[] =
110  {
111  CL_IMPORT_TYPE_ARM,
112  CL_IMPORT_TYPE_DMA_BUF_ARM,
113  CL_IMPORT_DMA_BUF_DATA_CONSISTENCY_WITH_HOST_ARM,
114  CL_TRUE,
115  0
116  };
117 
118  return ClImport(importProperties, memory);
119 
120  }
121  if (source == MemorySource::DmaBufProtected)
122  {
123  const cl_import_properties_arm importProperties[] =
124  {
125  CL_IMPORT_TYPE_ARM,
126  CL_IMPORT_TYPE_DMA_BUF_ARM,
127  CL_IMPORT_TYPE_PROTECTED_ARM,
128  CL_TRUE,
129  0
130  };
131 
132  return ClImport(importProperties, memory, true);
133 
134  }
135  // Case for importing memory allocated by OpenCl externally directly into the tensor
136  else if (source == MemorySource::Gralloc)
137  {
138  // m_Tensor not yet Allocated
139  if (!m_Imported && !m_Tensor.buffer())
140  {
141  // Importing memory allocated by OpenCl into the tensor directly.
142  arm_compute::Status status =
143  m_Tensor.allocator()->import_memory(cl::Buffer(static_cast<cl_mem>(memory)));
144  m_Imported = bool(status);
145  if (!m_Imported)
146  {
147  throw MemoryImportException(status.error_description());
148  }
149  return m_Imported;
150  }
151 
152  // m_Tensor.buffer() initially allocated with Allocate().
153  else if (!m_Imported && m_Tensor.buffer())
154  {
155  throw MemoryImportException(
156  "ClImportTensorHandle::Import Attempting to import on an already allocated tensor");
157  }
158 
159  // m_Tensor.buffer() previously imported.
160  else if (m_Imported)
161  {
162  // Importing memory allocated by OpenCl into the tensor directly.
163  arm_compute::Status status =
164  m_Tensor.allocator()->import_memory(cl::Buffer(static_cast<cl_mem>(memory)));
165  m_Imported = bool(status);
166  if (!m_Imported)
167  {
168  throw MemoryImportException(status.error_description());
169  }
170  return m_Imported;
171  }
172  else
173  {
174  throw MemoryImportException("ClImportTensorHandle::Failed to Import Gralloc Memory");
175  }
176  }
177  else
178  {
179  throw MemoryImportException("ClImportTensorHandle::Import flag is not supported");
180  }
181  }
182  else
183  {
184  throw MemoryImportException("ClImportTensorHandle::Incorrect import flag");
185  }
186  }
Status
enumeration
Definition: Types.hpp:29

◆ Manage()

virtual void Manage ( )
inlineoverridevirtual

Indicate to the memory manager that this resource is active.

This is used to compute overlapping lifetimes of resources.

Implements ITensorHandle.

Definition at line 50 of file ClImportTensorHandle.hpp.

50 {}

◆ Map()

virtual const void* Map ( bool  blocking = true) const
inlineoverridevirtual

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 52 of file ClImportTensorHandle.hpp.

References armnn::IgnoreUnused().

Referenced by ClImportTensorHandle::CanBeImported().

53  {
54  IgnoreUnused(blocking);
55  return static_cast<const void*>(m_Tensor.buffer() + m_Tensor.info()->offset_first_element_in_bytes());
56  }
void IgnoreUnused(Ts &&...)

◆ SetImportFlags()

void SetImportFlags ( MemorySourceFlags  importFlags)
inline

Definition at line 82 of file ClImportTensorHandle.hpp.

83  {
84  m_ImportFlags = importFlags;
85  }

◆ SetMemoryGroup()

virtual void SetMemoryGroup ( const std::shared_ptr< arm_compute::IMemoryGroup > &  memoryGroup)
inlineoverridevirtual

Implements IClTensorHandle.

Definition at line 67 of file ClImportTensorHandle.hpp.

References armnn::IgnoreUnused().

68  {
69  IgnoreUnused(memoryGroup);
70  }
void IgnoreUnused(Ts &&...)

◆ Unmap()

virtual void Unmap ( ) const
inlineoverridevirtual

Unmap the tensor data.

Implements ITensorHandle.

Definition at line 58 of file ClImportTensorHandle.hpp.

58 {}

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