Compute Library
 23.11
CLTensorAllocator Class Reference

Basic implementation of a CL memory tensor allocator. More...

#include <CLTensorAllocator.h>

Collaboration diagram for CLTensorAllocator:
[legend]

Public Member Functions

 CLTensorAllocator (IMemoryManageable *owner=nullptr, CLRuntimeContext *ctx=nullptr)
 Default constructor. More...
 
 CLTensorAllocator (const CLTensorAllocator &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLTensorAllocatoroperator= (const CLTensorAllocator &)=delete
 Prevent instances of this class from being copy assigned (As this class contains pointers) More...
 
 CLTensorAllocator (CLTensorAllocator &&)=default
 Allow instances of this class to be moved. More...
 
CLTensorAllocatoroperator= (CLTensorAllocator &&)=default
 Allow instances of this class to be moved. More...
 
uint8_t * data ()
 Interface to be implemented by the child class to return the pointer to the mapped data. More...
 
const cl::Buffer & cl_data () const
 Interface to be implemented by the child class to return the pointer to the CL data. More...
 
CLQuantization quantization () const
 Wrapped quantization info data accessor. More...
 
uint8_t * map (cl::CommandQueue &q, bool blocking)
 Enqueue a map operation of the allocated buffer on the given queue. More...
 
void unmap (cl::CommandQueue &q, uint8_t *mapping)
 Enqueue an unmap operation of the allocated buffer on the given queue. More...
 
void allocate () override
 Allocate size specified by TensorInfo of OpenCL memory. More...
 
void free () override
 Free allocated OpenCL memory. More...
 
Status import_memory (cl::Buffer buffer)
 Import an existing memory as a tensor's backing memory. More...
 
void set_associated_memory_group (IMemoryGroup *associated_memory_group)
 Associates the tensor with a memory group. More...
 
- Public Member Functions inherited from ITensorAllocator
 ITensorAllocator ()=default
 Default constructor. More...
 
 ITensorAllocator (const ITensorAllocator &)=default
 Allow instances of this class to be copy constructed. More...
 
ITensorAllocatoroperator= (const ITensorAllocator &)=default
 Allow instances of this class to be copied. More...
 
 ITensorAllocator (ITensorAllocator &&)=default
 Allow instances of this class to be move constructed. More...
 
ITensorAllocatoroperator= (ITensorAllocator &&)=default
 Allow instances of this class to be moved. More...
 
virtual ~ITensorAllocator ()=default
 Default virtual destructor. More...
 
void init (const TensorInfo &input, size_t alignment=0)
 Initialize a tensor based on the passed TensorInfo. More...
 
void soft_init (TensorInfo &input, size_t alignment=0)
 Initialize a tensor based with a reference TensorInfo. More...
 
TensorInfoinfo ()
 Return a reference to the tensor's metadata. More...
 
const TensorInfoinfo () const
 Return a constant reference to the tensor's metadata. More...
 
size_t alignment () const
 Return underlying's tensor buffer alignment. More...
 

Static Public Member Functions

static void set_global_allocator (IAllocator *allocator)
 Sets global allocator that will be used by all CLTensor objects. More...
 

Detailed Description

Basic implementation of a CL memory tensor allocator.

Definition at line 42 of file CLTensorAllocator.h.

Constructor & Destructor Documentation

◆ CLTensorAllocator() [1/3]

CLTensorAllocator ( IMemoryManageable owner = nullptr,
CLRuntimeContext ctx = nullptr 
)

Default constructor.

Parameters
[in]owner(Optional) Owner of the allocator.
[in]ctx(Optional) Runtime context.

Definition at line 111 of file CLTensorAllocator.cpp.

112  : _ctx(ctx), _owner(owner), _associated_memory_group(nullptr), _memory(), _mapping(nullptr), _scale(), _offset()
113 {
114 }

◆ CLTensorAllocator() [2/3]

CLTensorAllocator ( const CLTensorAllocator )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ CLTensorAllocator() [3/3]

Allow instances of this class to be moved.

Member Function Documentation

◆ allocate()

void allocate ( )
overridevirtual

Allocate size specified by TensorInfo of OpenCL memory.

Note
: The tensor must not already be allocated when calling this function.

Implements ITensorAllocator.

Definition at line 131 of file CLTensorAllocator.cpp.

132 {
133  // Allocate tensor backing memory
134  if (_associated_memory_group == nullptr)
135  {
136  // Perform memory allocation
137  if (static_global_cl_allocator != nullptr)
138  {
139  _memory.set_owned_region(static_global_cl_allocator->make_region(info().total_size(), 0));
140  }
141  else
142  {
143  _memory.set_owned_region(allocate_region(info().total_size(), 0));
144  }
145  }
146  else
147  {
148  // Finalize memory management instead
149  _associated_memory_group->finalize_memory(_owner, _memory, info().total_size(), alignment());
150  }
151 
152  // Allocate and fill the quantization parameter arrays
154  {
155  const size_t pad_size = 0;
156  populate_quantization_info(_scale, _offset, info().quantization_info(), pad_size);
157  }
158 
159  // Lock allocator
160  info().set_is_resizable(false);
161 }

References ITensorAllocator::alignment(), arm_compute::test::validation::data_type, IMemoryGroup::finalize_memory(), ITensorAllocator::info(), arm_compute::is_data_type_quantized_per_channel(), TensorInfo::set_is_resizable(), and CLMemory::set_owned_region().

Referenced by CLTensorHandle::allocate(), CLAuxTensorHandler::CLAuxTensorHandler(), arm_compute::test::validation::compute_float_target_in_place(), CLReduceMean::configure(), CLFFT2D::configure(), CLFFT1D::configure(), CLRNNLayer::configure(), CLDepthwiseConvolutionLayer::configure(), CLL2NormalizeLayer::configure(), CLArgMinMaxLayer::configure(), CLReductionOperation::configure(), CLInstanceNormalizationLayer::configure(), CLFFTConvolutionLayer::configure(), CLGenerateProposalsLayer::configure(), CLGEMMDeconvolutionLayer::configure(), CLDirectDeconvolutionLayer::configure(), CLLSTMLayerQuantized::configure(), CLLSTMLayer::configure(), CLQLSTMLayer::configure(), arm_compute::manage_workspace(), CLDepthwiseConvolutionLayer::prepare(), CLGEMMDeconvolutionLayer::prepare(), CLFFTConvolutionLayer::prepare(), CLDirectDeconvolutionLayer::prepare(), CLLSTMLayerQuantized::prepare(), CLQLSTMLayer::prepare(), CLConvertFullyConnectedWeightsManaged::run(), and arm_compute::test::validation::TEST_CASE().

◆ cl_data()

const cl::Buffer & cl_data ( ) const

Interface to be implemented by the child class to return the pointer to the CL data.

Returns
pointer to the CL data.

Definition at line 126 of file CLTensorAllocator.cpp.

127 {
128  return _memory.region() == nullptr ? _empty_buffer : _memory.cl_region()->cl_data();
129 }

References ICLMemoryRegion::cl_data(), CLMemory::cl_region(), and CLMemory::region().

Referenced by CLTensor::cl_buffer().

◆ data()

uint8_t * data ( )

Interface to be implemented by the child class to return the pointer to the mapped data.

Returns
pointer to the mapped data.

Definition at line 121 of file CLTensorAllocator.cpp.

122 {
123  return _mapping;
124 }

◆ free()

void free ( )
overridevirtual

Free allocated OpenCL memory.

Note
The tensor must have been allocated when calling this function.

Implements ITensorAllocator.

Definition at line 163 of file CLTensorAllocator.cpp.

164 {
165  _mapping = nullptr;
166  _memory.set_region(nullptr);
167  clear_quantization_arrays(_scale, _offset);
168  info().set_is_resizable(true);
169 }

References ITensorAllocator::info(), TensorInfo::set_is_resizable(), and CLMemory::set_region().

Referenced by CLTensorHandle::free(), CLFFTConvolutionLayer::prepare(), CLGEMMDeconvolutionLayer::prepare(), CLDirectDeconvolutionLayer::prepare(), CLLSTMLayerQuantized::prepare(), CLConvertFullyConnectedWeightsManaged::release(), CLTensorHandle::release_if_unused(), and arm_compute::test::validation::TEST_CASE().

◆ import_memory()

Status import_memory ( cl::Buffer  buffer)

Import an existing memory as a tensor's backing memory.

Warning
memory should have been created under the same context that Compute Library uses.
memory is expected to be aligned with the device requirements.
tensor shouldn't be memory managed.
ownership of memory is not transferred.
memory must be writable in case of in-place operations
padding should be accounted by the client code.
Note
buffer size will be checked to be compliant with total_size reported by ITensorInfo.
Parameters
[in]bufferBuffer to be used as backing memory
Returns
An error status

Definition at line 171 of file CLTensorAllocator.cpp.

172 {
173  ARM_COMPUTE_RETURN_ERROR_ON(buffer.get() == nullptr);
174  ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_SIZE>() < info().total_size());
175  ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_CONTEXT>().get() != CLScheduler::get().context().get());
176  ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr);
177 
178  _memory.set_owned_region(std::make_unique<CLBufferMemoryRegion>(buffer));
179 
180  info().set_is_resizable(false);
181  return Status{};
182 }

References ARM_COMPUTE_RETURN_ERROR_ON, CLScheduler::context(), CLScheduler::get(), ITensorAllocator::info(), TensorInfo::set_is_resizable(), and CLMemory::set_owned_region().

Referenced by CLAuxTensorHandler::CLAuxTensorHandler(), CLFFTConvolutionLayer::run(), and arm_compute::test::validation::TEST_CASE().

◆ map()

uint8_t * map ( cl::CommandQueue &  q,
bool  blocking 
)

Enqueue a map operation of the allocated buffer on the given queue.

Parameters
[in,out]qThe CL command queue to use for the mapping operation.
[in]blockingIf true, then the mapping will be ready to use by the time this method returns, else it is the caller's responsibility to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
Returns
The mapping address.

Definition at line 224 of file CLTensorAllocator.cpp.

225 {
226  ARM_COMPUTE_ERROR_ON(_mapping != nullptr);
227  ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
228  ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() != nullptr);
229 
230  _mapping = reinterpret_cast<uint8_t *>(_memory.cl_region()->map(q, blocking));
231  return _mapping;
232 }

References ARM_COMPUTE_ERROR_ON, IMemoryRegion::buffer(), CLMemory::cl_region(), ICLMemoryRegion::map(), and CLMemory::region().

◆ operator=() [1/2]

CLTensorAllocator& operator= ( CLTensorAllocator &&  )
default

Allow instances of this class to be moved.

◆ operator=() [2/2]

CLTensorAllocator& operator= ( const CLTensorAllocator )
delete

Prevent instances of this class from being copy assigned (As this class contains pointers)

◆ quantization()

CLQuantization quantization ( ) const

Wrapped quantization info data accessor.

Returns
A wrapped quantization info object.

Definition at line 116 of file CLTensorAllocator.cpp.

117 {
118  return {&_scale, &_offset};
119 }

Referenced by CLTensor::quantization().

◆ set_associated_memory_group()

void set_associated_memory_group ( IMemoryGroup associated_memory_group)

Associates the tensor with a memory group.

Parameters
[in]associated_memory_groupMemory group to associate the tensor with

Definition at line 184 of file CLTensorAllocator.cpp.

185 {
186  ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
187  ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr && _associated_memory_group != associated_memory_group);
188  ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.cl_region()->cl_data().get() != nullptr);
189 
190  _associated_memory_group = associated_memory_group;
191 }

References ARM_COMPUTE_ERROR_ON, ICLMemoryRegion::cl_data(), CLMemory::cl_region(), and CLMemory::region().

Referenced by CLTensor::associate_memory_group(), and arm_compute::test::validation::TEST_CASE().

◆ set_global_allocator()

void set_global_allocator ( IAllocator allocator)
static

Sets global allocator that will be used by all CLTensor objects.

Parameters
[in]allocatorAllocator to be used as a global allocator

Definition at line 193 of file CLTensorAllocator.cpp.

194 {
195  static_global_cl_allocator = allocator;
196 }

References arm_compute::test::validation::allocator().

Referenced by arm_compute::test::validation::TEST_CASE().

◆ unmap()

void unmap ( cl::CommandQueue &  q,
uint8_t *  mapping 
)

Enqueue an unmap operation of the allocated buffer on the given queue.

Note
This method simply enqueue the unmap operation, it is the caller's responsibility to flush the queue and make sure the unmap is finished before the memory is accessed by the device.
Parameters
[in,out]qThe CL command queue to use for the mapping operation.
[in]mappingThe cpu mapping to unmap.

Definition at line 234 of file CLTensorAllocator.cpp.

235 {
236  ARM_COMPUTE_ERROR_ON(_mapping == nullptr);
237  ARM_COMPUTE_ERROR_ON(_mapping != mapping);
238  ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
239  ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() == nullptr);
240  ARM_COMPUTE_UNUSED(mapping);
241 
242  _memory.cl_region()->unmap(q);
243  _mapping = nullptr;
244 }

References ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_UNUSED, IMemoryRegion::buffer(), CLMemory::cl_region(), CLMemory::region(), and ICLMemoryRegion::unmap().


The documentation for this class was generated from the following files:
arm_compute::CLMemory::cl_region
ICLMemoryRegion * cl_region()
OpenCL Region accessor.
Definition: CLMemory.cpp:47
arm_compute::CLMemory::set_region
void set_region(IMemoryRegion *region) final
Sets a memory region.
Definition: CLMemory.cpp:67
arm_compute::CLMemory::set_owned_region
void set_owned_region(std::unique_ptr< IMemoryRegion > region) final
Sets a memory region.
Definition: CLMemory.cpp:74
arm_compute::ICLMemoryRegion::cl_data
const cl::Buffer & cl_data() const
Returns the underlying CL buffer.
Definition: CLMemoryRegion.cpp:36
arm_compute::CLMemory::region
IMemoryRegion * region() final
Region accessor.
Definition: CLMemory.cpp:57
arm_compute::ICLMemoryRegion::unmap
virtual void unmap(cl::CommandQueue &q)=0
Enqueue an unmap operation of the allocated buffer on the given queue.
arm_compute::ITensorAllocator::alignment
size_t alignment() const
Return underlying's tensor buffer alignment.
Definition: ITensorAllocator.cpp:56
ARM_COMPUTE_ERROR_ON
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
arm_compute::ITensorAllocator::info
TensorInfo & info()
Return a reference to the tensor's metadata.
Definition: ITensorAllocator.cpp:46
ARM_COMPUTE_RETURN_ERROR_ON
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:298
arm_compute::is_data_type_quantized_per_channel
bool is_data_type_quantized_per_channel(DataType dt)
Check if a given data type is of per channel type.
Definition: DataTypeUtils.h:401
arm_compute::IMemoryGroup::finalize_memory
virtual void finalize_memory(IMemoryManageable *obj, IMemory &obj_memory, size_t size, size_t alignment)=0
Finalizes memory for a given object.
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:151
arm_compute::TensorInfo::set_is_resizable
ITensorInfo & set_is_resizable(bool is_resizable) override
Set the flag whether the tensor size can be changed.
Definition: TensorInfo.h:286
arm_compute::CLScheduler::get
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:112
arm_compute::test::validation::data_type
data_type
Definition: Cast.cpp:222
arm_compute::IMemoryRegion::buffer
virtual void * buffer()=0
Returns the pointer to the allocated data.
arm_compute::ICLMemoryRegion::map
virtual void * map(cl::CommandQueue &q, bool blocking)=0
Enqueue a map operation of the allocated buffer on the given queue.
arm_compute::CLScheduler::context
cl::Context & context()
Accessor for the associated CL context.
Definition: CLScheduler.cpp:33
arm_compute::test::validation::allocator
input allocator() -> allocate()