Compute Library
 21.05
CLBufferAllocator Class Referencefinal

Default OpenCL cl buffer allocator implementation. More...

#include <CLBufferAllocator.h>

Collaboration diagram for CLBufferAllocator:
[legend]

Public Member Functions

 CLBufferAllocator (CLCoreRuntimeContext *ctx=nullptr)
 Default constructor. More...
 
 CLBufferAllocator (const CLBufferAllocator &)=default
 Default copy constructor. More...
 
CLBufferAllocatoroperator= (const CLBufferAllocator &)=default
 Default copy assignment operator. More...
 
void * allocate (size_t size, size_t alignment) override
 Interface to be implemented by the child class to allocate bytes. More...
 
void free (void *ptr) override
 Interface to be implemented by the child class to free the allocated tensor. More...
 
std::unique_ptr< IMemoryRegionmake_region (size_t size, size_t alignment) override
 Create self-managed memory region. More...
 
- Public Member Functions inherited from IAllocator
virtual ~IAllocator ()=default
 Default virtual destructor. More...
 

Detailed Description

Default OpenCL cl buffer allocator implementation.

Definition at line 35 of file CLBufferAllocator.h.

Constructor & Destructor Documentation

◆ CLBufferAllocator() [1/2]

Default constructor.

Parameters
[in]ctxA runtime context.

Definition at line 36 of file CLBufferAllocator.cpp.

37  : _ctx(ctx)
38 {
39 }

◆ CLBufferAllocator() [2/2]

CLBufferAllocator ( const CLBufferAllocator )
default

Default copy constructor.

Member Function Documentation

◆ allocate()

void * allocate ( size_t  size,
size_t  alignment 
)
overridevirtual

Interface to be implemented by the child class to allocate bytes.

Parameters
[in]sizeSize to allocate
[in]alignmentAlignment that the returned pointer should comply with
Returns
A pointer to the allocated memory

Implements IAllocator.

Definition at line 41 of file CLBufferAllocator.cpp.

42 {
43  ARM_COMPUTE_UNUSED(alignment);
44  cl_mem buf;
45  if(_ctx == nullptr)
46  {
47  buf = clCreateBuffer(CLScheduler::get().context().get(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size, nullptr, nullptr);
48  }
49  else
50  {
51  buf = clCreateBuffer(_ctx->context().get(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size, nullptr, nullptr);
52  }
53  return static_cast<void *>(buf);
54 }
cl::Context context()
OpenCL context accessor.
static CLScheduler & get()
Access the scheduler singleton.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
cl_mem clCreateBuffer(cl_context context, cl_mem_flags flags, size_t size, void *host_ptr, cl_int *errcode_ret)
Definition: OpenCL.cpp:759

References ARM_COMPUTE_UNUSED, clCreateBuffer(), CLCoreRuntimeContext::context(), and CLScheduler::get().

◆ free()

void free ( void *  ptr)
overridevirtual

Interface to be implemented by the child class to free the allocated tensor.

Implements IAllocator.

Definition at line 56 of file CLBufferAllocator.cpp.

57 {
58  ARM_COMPUTE_ERROR_ON(ptr == nullptr);
59  clReleaseMemObject(static_cast<cl_mem>(ptr));
60 }
#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
cl_int clReleaseMemObject(cl_mem memobj)
Definition: OpenCL.cpp:427

References ARM_COMPUTE_ERROR_ON, and clReleaseMemObject().

◆ make_region()

std::unique_ptr< IMemoryRegion > make_region ( size_t  size,
size_t  alignment 
)
overridevirtual

Create self-managed memory region.

Parameters
[in]sizeSize of the memory region
[in]alignmentAlignment of the memory region
Returns
The memory region object

Implements IAllocator.

Definition at line 62 of file CLBufferAllocator.cpp.

63 {
64  ARM_COMPUTE_UNUSED(alignment);
65  return std::make_unique<CLBufferMemoryRegion>(_ctx, CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size);
66 }
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152

References ARM_COMPUTE_UNUSED.

◆ operator=()

CLBufferAllocator& operator= ( const CLBufferAllocator )
default

Default copy assignment operator.


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