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 (std::string_view 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 (std::string_view 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 230 of file CLKernelLibrary.cpp.

232{
233 _built_programs_map.emplace(built_program_name, program);
234}

◆ 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 170 of file CLKernelLibrary.cpp.

172{
173 // Find which program contains the kernel
174 auto kernel_program_it = _kernel_program_map.find(kernel_name);
175
176 if (_kernel_program_map.end() == kernel_program_it)
177 {
178 ARM_COMPUTE_ERROR_VAR("Kernel %s not found in the CLKernelLibrary", kernel_name.c_str());
179 }
180 std::string concat_str;
181
182 if (fp16_supported())
183 {
184 concat_str += " -DARM_COMPUTE_OPENCL_FP16_ENABLED=1 ";
185 }
186
187 if (get_cl_version(_device) == CLVersion::CL20)
188 {
189 concat_str += " -cl-std=CL2.0 ";
190 }
191 else if (arm_non_uniform_workgroup_supported(_device))
192 {
193 concat_str += " -cl-arm-non-uniform-work-group-size ";
194 }
195 else
196 {
197 ARM_COMPUTE_ERROR("Non uniform workgroup size is not supported!!");
198 }
199
200 // Check if the program has been built before with same build options.
201 const std::string program_name = kernel_program_it->second;
202 const std::string build_options = stringify_set(build_options_set) + concat_str;
203
204 const std::string built_program_name = program_name + "_" + build_options;
205 auto built_program_it = _built_programs_map.find(built_program_name);
206
207 cl::Program cl_program;
208
209 if (_built_programs_map.end() != built_program_it)
210 {
211 // If program has been built, retrieve to create kernel from it
212 cl_program = built_program_it->second;
213 }
214 else
215 {
216 // Get program
217 Program program = load_program(program_name);
218
219 // Build program
220 cl_program = program.build(build_options);
221
222 // Add built program to internal map
223 _built_programs_map.emplace(built_program_name, cl_program);
224 }
225
226 // Create and return kernel
227 return Kernel(kernel_name, cl_program);
228}
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::CLScaleFactorSymm8Kernel::configure(), arm_compute::CLReduceOperationKernel::configure(), and arm_compute::CLHashtableLookupKernel::configure().

◆ default_ndrange()

cl::NDRange CLKernelLibraryEx::default_ndrange ( ) const

Return the default NDRange for the device.

Returns
default NDRangeof the device

Definition at line 332 of file CLKernelLibrary.cpp.

333{
334 // GPUTarget _target = get_target_from_device(_device);
335 cl::Device device = cl::Device::getDefault();
336 GPUTarget _target = get_target_from_device(device);
337 cl::NDRange default_range;
338
339 switch (_target)
340 {
341 case GPUTarget::MIDGARD:
342 case GPUTarget::T600:
343 case GPUTarget::T700:
344 case GPUTarget::T800:
345 default_range = cl::NDRange(128u, 1);
346 break;
347 default:
348 default_range = cl::NullRange;
349 }
350
351 return default_range;
352}

◆ 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 236 of file CLKernelLibrary.cpp.

236{ 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 354 of file CLKernelLibrary.cpp.

354{ 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 ( std::string_view  program_name)

Get the source of the selected program.

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

Definition at line 307 of file CLKernelLibrary.cpp.

308{
309 const auto program_source_it = _program_source_map.find(program_name);
310
311 if (program_source_it == _program_source_map.end())
312 {
313 ARM_COMPUTE_ERROR_VAR("Embedded program for %s does not exist.", program_name.data());
314 }
315
316 return program_source_it->second;
317}

◆ 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 238 of file CLKernelLibrary.cpp.

239{
240 return device_supports_extension(_device, "cl_khr_int64_base_atomics");
241}

◆ 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 319 of file CLKernelLibrary.cpp.

320{
321 size_t result;
322
323 size_t err = kernel.getWorkGroupInfo(_device, CL_KERNEL_WORK_GROUP_SIZE, &result);
324 ARM_COMPUTE_ERROR_ON_MSG(
325 err != 0,
326 "clGetKernelWorkGroupInfo failed to return the maximum workgroup size for the kernel");
327 ARM_COMPUTE_UNUSED(err);
328
329 return result;
330}
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 ( std::string_view  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: