ONE - On-device Neural Engine
Loading...
Searching...
No Matches
arm_compute::CLKernelLibraryEx Class Reference

Class to build OpenCL kernels added from nnfw. More...

#include <CLKernelLibraryEx.h>

Public Member Functions

 CLKernelLibraryEx (const CLKernelLibraryEx &)=delete
 Prevent instances of this class from being copied.
 
const CLKernelLibraryExoperator= (const CLKernelLibraryEx &)=delete
 Prevent instances of this class from being copied.
 
void init (std::string kernel_path, cl::Context context, cl::Device device)
 Initialise the kernel library.
 
void set_kernel_path (const std::string &kernel_path)
 Set the path that the kernels reside in.
 
std::string get_kernel_path ()
 Get the path that the kernels reside in.
 
std::string get_program_source (const std::string &program_name)
 Get the source of the selected program.
 
void set_context (cl::Context context)
 Set the CL context used to create programs.
 
cl::Context & context ()
 Return associated CL context.
 
void set_device (cl::Device device)
 Set the CL device for which the programs are created.
 
cl::Device & get_device ()
 Gets the CL device for which the programs are created.
 
std::string get_device_version ()
 Return the device version.
 
Kernel create_kernel (const std::string &kernel_name, const StringSet &build_options_set={}) const
 Create a kernel from the kernel library.
 
size_t max_local_workgroup_size (const cl::Kernel &kernel) const
 Find the maximum number of local work items in a workgroup can be supported for the kernel.
 
cl::NDRange default_ndrange () const
 Return the default NDRange for the device.
 
void clear_programs_cache ()
 Clear the library's cache of binary programs.
 
const std::map< std::string, cl::Program > & get_built_programs () const
 Access the cache of built OpenCL programs.
 
void add_built_program (const std::string &built_program_name, cl::Program program)
 Add a new built program to the cache.
 
bool fp16_supported () const
 Returns true if FP16 is supported by the CL device.
 
bool int64_base_atomics_supported () const
 Returns true if int64_base_atomics extension is supported by the CL device.
 

Static Public Member Functions

static CLKernelLibraryExget ()
 Get the KernelLibrary singleton.
 

Detailed Description

Class to build OpenCL kernels added from nnfw.

Definition at line 64 of file CLKernelLibraryEx.h.

Constructor & Destructor Documentation

◆ CLKernelLibraryEx()

arm_compute::CLKernelLibraryEx::CLKernelLibraryEx ( const CLKernelLibraryEx )
delete

Prevent instances of this class from being copied.

Member Function Documentation

◆ add_built_program()

void CLKernelLibraryEx::add_built_program ( const std::string &  built_program_name,
cl::Program  program 
)

Add a new built program to the cache.

Parameters
[in]built_program_nameName of the program
[in]programBuilt program to add to the cache
Returns
N/A

Definition at line 261 of file CLKernelLibrary.cpp.

263{
264 _built_programs_map.emplace(built_program_name, program);
265}

◆ clear_programs_cache()

void arm_compute::CLKernelLibraryEx::clear_programs_cache ( )
inline

Clear the library's cache of binary programs.

Returns
N/A

Definition at line 205 of file CLKernelLibraryEx.h.

206 {
207 _programs_map.clear();
208 _built_programs_map.clear();
209 }

◆ context()

cl::Context & arm_compute::CLKernelLibraryEx::context ( )
inline

Return associated CL context.

Returns
A CL context.

Definition at line 158 of file CLKernelLibraryEx.h.

158{ return _context; }

Referenced by init(), and set_context().

◆ create_kernel()

Kernel CLKernelLibraryEx::create_kernel ( const std::string &  kernel_name,
const StringSet &  build_options_set = {} 
) const

Create a kernel from the kernel library.

Parameters
[in]kernel_nameKernel name.
[in]build_options_setKernel build options as a set.
Returns
The created kernel.

Definition at line 201 of file CLKernelLibrary.cpp.

203{
204 // Find which program contains the kernel
205 auto kernel_program_it = _kernel_program_map.find(kernel_name);
206
207 if (_kernel_program_map.end() == kernel_program_it)
208 {
209 ARM_COMPUTE_ERROR_VAR("Kernel %s not found in the CLKernelLibrary", kernel_name.c_str());
210 }
211 std::string concat_str;
212
213 if (fp16_supported())
214 {
215 concat_str += " -DARM_COMPUTE_OPENCL_FP16_ENABLED=1 ";
216 }
217
218 if (get_cl_version(_device) == CLVersion::CL20)
219 {
220 concat_str += " -cl-std=CL2.0 ";
221 }
222 else if (arm_non_uniform_workgroup_supported(_device))
223 {
224 concat_str += " -cl-arm-non-uniform-work-group-size ";
225 }
226 else
227 {
228 ARM_COMPUTE_ERROR("Non uniform workgroup size is not supported!!");
229 }
230
231 // Check if the program has been built before with same build options.
232 const std::string program_name = kernel_program_it->second;
233 const std::string build_options = stringify_set(build_options_set) + concat_str;
234
235 const std::string built_program_name = program_name + "_" + build_options;
236 auto built_program_it = _built_programs_map.find(built_program_name);
237
238 cl::Program cl_program;
239
240 if (_built_programs_map.end() != built_program_it)
241 {
242 // If program has been built, retrieve to create kernel from it
243 cl_program = built_program_it->second;
244 }
245 else
246 {
247 // Get program
248 Program program = load_program(program_name);
249
250 // Build program
251 cl_program = program.build(build_options);
252
253 // Add built program to internal map
254 _built_programs_map.emplace(built_program_name, cl_program);
255 }
256
257 // Create and return kernel
258 return Kernel(kernel_name, cl_program);
259}
bool fp16_supported() const
Returns true if FP16 is supported by the CL device.

References fp16_supported().

Referenced by arm_compute::CLPadLayerKernelEx::configure(), arm_compute::CLGEMMMatrixAccumulateBiasesKernel::configure(), arm_compute::CLMemsetKernel::configure(), arm_compute::CLGatherExKernel::configure(), arm_compute::CLQuantizationSymmetricKernel::configure(), arm_compute::CLMultiplyScaleFactorKernel::configure(), arm_compute::CLCastBoolKernel::configure(), arm_compute::CLNegKernel::configure(), arm_compute::CLScaleFactorSymm8Kernel::configure(), arm_compute::CLEmbeddingLookupKernel::configure(), arm_compute::CLReduceOperationKernel::configure(), arm_compute::CLBinaryLogicalOpKernel::configure(), arm_compute::CLHashtableLookupKernel::configure(), and arm_compute::CLInstanceNormalizationLayerKernelEx::configure().

◆ default_ndrange()

cl::NDRange CLKernelLibraryEx::default_ndrange ( ) const

Return the default NDRange for the device.

Returns
default NDRangeof the device

Definition at line 363 of file CLKernelLibrary.cpp.

364{
365 // GPUTarget _target = get_target_from_device(_device);
366 cl::Device device = cl::Device::getDefault();
367 GPUTarget _target = get_target_from_device(device);
368 cl::NDRange default_range;
369
370 switch (_target)
371 {
372 case GPUTarget::MIDGARD:
373 case GPUTarget::T600:
374 case GPUTarget::T700:
375 case GPUTarget::T800:
376 default_range = cl::NDRange(128u, 1);
377 break;
378 default:
379 default_range = cl::NullRange;
380 }
381
382 return default_range;
383}

◆ fp16_supported()

bool CLKernelLibraryEx::fp16_supported ( ) const

Returns true if FP16 is supported by the CL device.

Returns
true if the CL device supports FP16

Definition at line 267 of file CLKernelLibrary.cpp.

267{ return ::fp16_supported(_device); }

Referenced by create_kernel().

◆ get()

◆ get_built_programs()

const std::map< std::string, cl::Program > & arm_compute::CLKernelLibraryEx::get_built_programs ( ) const
inline

Access the cache of built OpenCL programs.

Returns
program map data structure of which key is name of kernel and value is kerel source name. (*.cl)

Definition at line 216 of file CLKernelLibraryEx.h.

217 {
218 return _built_programs_map;
219 }

◆ get_device()

cl::Device & arm_compute::CLKernelLibraryEx::get_device ( )
inline

Gets the CL device for which the programs are created.

Returns
A CL device.

Definition at line 171 of file CLKernelLibraryEx.h.

171{ return _device; }

◆ get_device_version()

std::string CLKernelLibraryEx::get_device_version ( )

Return the device version.

Returns
The content of CL_DEVICE_VERSION

Definition at line 385 of file CLKernelLibrary.cpp.

385{ return _device.getInfo<CL_DEVICE_VERSION>(); }

◆ get_kernel_path()

std::string arm_compute::CLKernelLibraryEx::get_kernel_path ( )
inline

Get the path that the kernels reside in.

Returns
the path of kernel files

Definition at line 116 of file CLKernelLibraryEx.h.

116{ return _kernel_path; };

◆ get_program_source()

std::string CLKernelLibraryEx::get_program_source ( const std::string &  program_name)

Get the source of the selected program.

Parameters
[in]program_nameProgram name.
Returns
Source of the selected program.

Definition at line 338 of file CLKernelLibrary.cpp.

339{
340 const auto program_source_it = _program_source_map.find(program_name);
341
342 if (program_source_it == _program_source_map.end())
343 {
344 ARM_COMPUTE_ERROR_VAR("Embedded program for %s does not exist.", program_name.c_str());
345 }
346
347 return program_source_it->second;
348}

◆ init()

void arm_compute::CLKernelLibraryEx::init ( std::string  kernel_path,
cl::Context  context,
cl::Device  device 
)
inline

Initialise the kernel library.

Parameters
[in]kernel_pathPath of the directory from which kernel sources are loaded.
[in]contextCL context used to create programs.
[in]deviceCL device for which the programs are created.
Returns
N/A

Definition at line 98 of file CLKernelLibraryEx.h.

99 {
100 _kernel_path = std::move(kernel_path);
101 _context = std::move(context);
102 _device = std::move(device);
103 }
cl::Context & context()
Return associated CL context.

References context().

Referenced by onert::backend::acl_cl::Config::initialize().

◆ int64_base_atomics_supported()

bool CLKernelLibraryEx::int64_base_atomics_supported ( ) const

Returns true if int64_base_atomics extension is supported by the CL device.

Returns
true if the CL device supports int64_base_atomics extension

Definition at line 269 of file CLKernelLibrary.cpp.

270{
271 return device_supports_extension(_device, "cl_khr_int64_base_atomics");
272}

◆ max_local_workgroup_size()

size_t CLKernelLibraryEx::max_local_workgroup_size ( const cl::Kernel &  kernel) const

Find the maximum number of local work items in a workgroup can be supported for the kernel.

Parameters
[in]kernelkernel object

Definition at line 350 of file CLKernelLibrary.cpp.

351{
352 size_t result;
353
354 size_t err = kernel.getWorkGroupInfo(_device, CL_KERNEL_WORK_GROUP_SIZE, &result);
355 ARM_COMPUTE_ERROR_ON_MSG(
356 err != 0,
357 "clGetKernelWorkGroupInfo failed to return the maximum workgroup size for the kernel");
358 ARM_COMPUTE_UNUSED(err);
359
360 return result;
361}
result
Definition infer.py:103

◆ operator=()

const CLKernelLibraryEx & arm_compute::CLKernelLibraryEx::operator= ( const CLKernelLibraryEx )
delete

Prevent instances of this class from being copied.

◆ set_context()

void arm_compute::CLKernelLibraryEx::set_context ( cl::Context  context)
inline

Set the CL context used to create programs.

Note
Setting the context also resets the device to the first one available in the new context.
Parameters
[in]contextA CL context.
Returns
N/A

Definition at line 132 of file CLKernelLibraryEx.h.

133 {
134 _context = std::move(context);
135 if (_context.get() == nullptr)
136 {
137 _device = cl::Device();
138 }
139 else
140 {
141 const auto cl_devices = _context.getInfo<CL_CONTEXT_DEVICES>();
142
143 if (cl_devices.empty())
144 {
145 _device = cl::Device();
146 }
147 else
148 {
149 _device = cl_devices[0];
150 }
151 }
152 }

References context().

◆ set_device()

void arm_compute::CLKernelLibraryEx::set_device ( cl::Device  device)
inline

Set the CL device for which the programs are created.

Parameters
[in]deviceA CL device.
Returns
N/A

Definition at line 165 of file CLKernelLibraryEx.h.

165{ _device = std::move(device); }

◆ set_kernel_path()

void arm_compute::CLKernelLibraryEx::set_kernel_path ( const std::string &  kernel_path)
inline

Set the path that the kernels reside in.

Parameters
[in]kernel_pathPath of the directory from which kernel sources are loaded.
Returns
N/A

Definition at line 110 of file CLKernelLibraryEx.h.

110{ _kernel_path = kernel_path; };

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