Compute Library
 23.05
CLCompileContext Class Referencefinal

CLCompileContext class. More...

#include <CLCompileContext.h>

Public Member Functions

 CLCompileContext ()
 Constructor. More...
 
 CLCompileContext (cl::Context context, const cl::Device &device)
 Constructor. More...
 
cl::Context & context ()
 Accessor for the associated CL context. More...
 
void set_context (cl::Context context)
 Sets the CL context used to create programs. More...
 
const cl::Device & get_device () const
 Gets the CL device for which the programs are created. More...
 
void set_device (cl::Device device)
 Sets the CL device for which the programs are created. More...
 
Kernel create_kernel (const std::string &kernel_name, const std::string &program_name, const std::string &program_source, const std::string &kernel_path, const StringSet &build_options_set, bool is_binary) const
 Creates an OpenCL kernel. More...
 
void clear_programs_cache ()
 Clear the library's cache of binary programs. More...
 
const std::map< std::string, cl::Program > & get_built_programs () const
 Access the cache of built OpenCL programs. More...
 
void add_built_program (const std::string &built_program_name, const cl::Program &program) const
 Add a new built program to the cache. More...
 
bool fp16_supported () const
 Returns true if FP16 is supported by the CL device. More...
 
cl_uint get_num_compute_units () const
 Return the maximum number of compute units in the device. More...
 
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. More...
 
cl::NDRange default_ndrange () const
 Return the default NDRange for the device. More...
 
std::string get_device_version () const
 Return the device version. More...
 
bool int64_base_atomics_supported () const
 Returns true if int64_base_atomics extension is supported by the CL device. More...
 
bool is_wbsm_supported () const
 
int32_t get_ddk_version () const
 Return the DDK version. More...
 
GPUTarget get_gpu_target () const
 Return the Gpu target of the associated device. More...
 

Detailed Description

CLCompileContext class.

Definition at line 204 of file CLCompileContext.h.

Constructor & Destructor Documentation

◆ CLCompileContext() [1/2]

Constructor.

Definition at line 146 of file CLCompileContext.cpp.

147  : _context(), _device(), _programs_map(), _built_programs_map(), _is_wbsm_supported()
148 {
149 }

◆ CLCompileContext() [2/2]

CLCompileContext ( cl::Context  context,
const cl::Device &  device 
)

Constructor.

Parameters
[in]contextA CL context.
[in]deviceA CL device.

Definition at line 151 of file CLCompileContext.cpp.

References arm_compute::get_wbsm_support_info().

152  : _context(), _device(), _programs_map(), _built_programs_map(), _is_wbsm_supported()
153 {
154  _context = std::move(context);
155  _device = CLDevice(device);
156  _is_wbsm_supported = get_wbsm_support_info(device);
157 }
cl::Context & context()
Accessor for the associated CL context.
bool get_wbsm_support_info(const cl::Device &device)
Definition: CLHelpers.cpp:423

Member Function Documentation

◆ add_built_program()

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

Add a new built program to the cache.

Parameters
[in]built_program_nameName of the program
[in]programBuilt program to add to the cache

Definition at line 306 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::add_built_program().

307 {
308  _built_programs_map.emplace(built_program_name, program);
309 }

◆ clear_programs_cache()

void clear_programs_cache ( )

Clear the library's cache of binary programs.

Definition at line 311 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::clear_programs_cache().

312 {
313  _programs_map.clear();
314  _built_programs_map.clear();
315 }

◆ context()

cl::Context & context ( )

Accessor for the associated CL context.

Returns
A CL context.

Definition at line 322 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::context().

323 {
324  return _context;
325 }

◆ create_kernel()

Kernel create_kernel ( const std::string &  kernel_name,
const std::string &  program_name,
const std::string &  program_source,
const std::string &  kernel_path,
const StringSet &  build_options_set,
bool  is_binary 
) const

Creates an OpenCL kernel.

Parameters
[in]kernel_nameKernel name.
[in]program_nameProgram name.
[in]program_sourceProgram source.
[in]kernel_pathCL kernel path.
[in]build_options_setKernel build options as a set.
[in]is_binaryFlag to indicate if the program source is binary.
Returns
The created kernel.

Definition at line 159 of file CLCompileContext.cpp.

References ARM_COMPUTE_UNUSED, Program::build(), build_options, and CLDevice::cl_device().

Referenced by ClKernelRuntime::configure(), CLKernelLibrary::create_kernel(), arm_compute::create_kernel(), and arm_compute::test::validation::TEST_CASE().

161 {
162  const std::string build_options = generate_build_options(build_options_set, kernel_path);
163  const std::string built_program_name = program_name + "_" + build_options;
164  auto built_program_it = _built_programs_map.find(built_program_name);
165  cl::Program cl_program;
166 
167  if(_built_programs_map.end() != built_program_it)
168  {
169  // If program has been built, retrieve to create kernel from it
170  cl_program = built_program_it->second;
171  }
172  else
173  {
174  Program program = load_program(program_name, program_source, is_binary);
175 
176  // Build program
177  cl_program = program.build(build_options);
178 
179  // Add built program to internal map
180  _built_programs_map.emplace(built_program_name, cl_program);
181  }
182 
183  // Create and return kernel
184  return Kernel(kernel_name, cl_program);
185 }
std::set< std::string > build_options
std::string kernel_name

◆ default_ndrange()

cl::NDRange default_ndrange ( ) const

Return the default NDRange for the device.

Definition at line 338 of file CLCompileContext.cpp.

References CLDevice::cl_device(), arm_compute::get_target_from_device(), arm_compute::MIDGARD, arm_compute::T600, arm_compute::T700, and arm_compute::T800.

Referenced by CLKernelLibrary::default_ndrange().

339 {
340  GPUTarget _target = get_target_from_device(_device.cl_device());
341  cl::NDRange default_range;
342 
343  switch(_target)
344  {
345  case GPUTarget::MIDGARD:
346  case GPUTarget::T600:
347  case GPUTarget::T700:
348  case GPUTarget::T800:
349  default_range = cl::NDRange(128u, 1);
350  break;
351  default:
352  default_range = cl::NullRange;
353  }
354 
355  return default_range;
356 }
GPUTarget get_target_from_device(const cl::Device &device)
Helper function to get the GPU target from CL device.
Definition: CLHelpers.cpp:223
GPUTarget
Available GPU Targets.
Definition: GPUTarget.h:34
const cl::Device & cl_device() const
Returns the underlying cl device object.
Definition: CLDevice.h:112

◆ fp16_supported()

bool fp16_supported ( ) const

Returns true if FP16 is supported by the CL device.

Returns
true if the CL device supports FP16

Definition at line 283 of file CLCompileContext.cpp.

References ARM_COMPUTE_UNUSED, and CLDevice::supported().

Referenced by CLKernelLibrary::fp16_supported().

284 {
285  return _device.supported("cl_khr_fp16");
286 }
bool supported(const std::string &extension) const override
Check if extensions on a device are supported.
Definition: CLDevice.h:141

◆ get_built_programs()

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

Access the cache of built OpenCL programs.

Definition at line 317 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::get_built_programs(), and arm_compute::test::validation::TEST_CASE().

318 {
319  return _built_programs_map;
320 }

◆ get_ddk_version()

int32_t get_ddk_version ( ) const

Return the DDK version.

If the DDK version cannot be detected, return -1.

Returns
The DDK version.

Definition at line 389 of file CLCompileContext.cpp.

References CLDevice::device_version(), and arm_compute::support::cpp11::stoi().

Referenced by ClTransposedConvolutionKernel::configure(), ClIndirectConv2dKernel::configure(), ClDirectConv2dKernel::configure(), and CLCompileContext::set_context().

390 {
391  const std::string device_version = _device.device_version();
392  const std::regex ddk_regex("r([0-9]*)p[0-9]");
393  std::smatch ddk_match;
394 
395  if(std::regex_search(device_version, ddk_match, ddk_regex))
396  {
397  return std::stoi(ddk_match[1]);
398  }
399 
400  return -1;
401 }
std::string device_version() const
Returns the device version as a string.
Definition: CLDevice.h:130
int stoi(const std::string &str, std::size_t *pos=0, NumericBase base=NumericBase::BASE_10)
Convert string values to integer.
Definition: StringSupport.h:55

◆ get_device()

const cl::Device & get_device ( ) const

Gets the CL device for which the programs are created.

Definition at line 327 of file CLCompileContext.cpp.

References CLDevice::cl_device().

Referenced by CLKernelLibrary::get_device().

328 {
329  return _device.cl_device();
330 }
const cl::Device & cl_device() const
Returns the underlying cl device object.
Definition: CLDevice.h:112

◆ get_device_version()

std::string get_device_version ( ) const

Return the device version.

Returns
The content of CL_DEVICE_VERSION

Definition at line 379 of file CLCompileContext.cpp.

References CLDevice::device_version().

Referenced by CLKernelLibrary::get_device_version().

380 {
381  return _device.device_version();
382 }
std::string device_version() const
Returns the device version as a string.
Definition: CLDevice.h:130

◆ get_gpu_target()

GPUTarget get_gpu_target ( ) const

Return the Gpu target of the associated device.

Returns
GPUTarget

Definition at line 402 of file CLCompileContext.cpp.

References CLDevice::target().

Referenced by GpuWorkloadContext::gpu_target().

403 {
404  return _device.target();
405 }
const GPUTarget & target() const
Returns the GPU target of the cl device.
Definition: CLDevice.h:94

◆ get_num_compute_units()

cl_uint get_num_compute_units ( ) const

Return the maximum number of compute units in the device.

Returns
The content of CL_DEVICE_MAX_COMPUTE_UNITS

Definition at line 384 of file CLCompileContext.cpp.

References CLDevice::compute_units().

Referenced by CLKernelLibrary::get_num_compute_units().

385 {
386  return _device.compute_units();
387 }
size_t compute_units() const
Returns the number of compute units available.
Definition: CLDevice.h:103

◆ int64_base_atomics_supported()

bool 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 358 of file CLCompileContext.cpp.

References CLDevice::supported().

Referenced by CLKernelLibrary::int64_base_atomics_supported().

359 {
360  return _device.supported("cl_khr_int64_base_atomics");
361 }
bool supported(const std::string &extension) const override
Check if extensions on a device are supported.
Definition: CLDevice.h:141

◆ is_wbsm_supported()

bool is_wbsm_supported ( ) const

Definition at line 363 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::is_wbsm_supported().

364 {
365  return _is_wbsm_supported;
366 }

◆ max_local_workgroup_size()

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.

Definition at line 368 of file CLCompileContext.cpp.

References ARM_COMPUTE_ERROR_ON_MSG, ARM_COMPUTE_UNUSED, and CLDevice::cl_device().

Referenced by CLKernelLibrary::max_local_workgroup_size().

369 {
370  size_t result;
371 
372  size_t err = kernel.getWorkGroupInfo(_device.cl_device(), CL_KERNEL_WORK_GROUP_SIZE, &result);
373  ARM_COMPUTE_ERROR_ON_MSG(err != 0, "clGetKernelWorkGroupInfo failed to return the maximum workgroup size for the kernel");
374  ARM_COMPUTE_UNUSED(err);
375 
376  return result;
377 }
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
const cl::Device & cl_device() const
Returns the underlying cl device object.
Definition: CLDevice.h:112

◆ set_context()

void set_context ( cl::Context  context)

Sets 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.

Definition at line 218 of file CLCompileContext.cpp.

References ARM_COMPUTE_ERROR, build_options, arm_compute::get_arch_from_target(), CLCompileContext::get_ddk_version(), CLDevice::is_non_uniform_workgroup_supported(), arm_compute::MIDGARD, CLDevice::supported(), CLDevice::target(), arm_compute::support::cpp11::to_string(), type, and arm_compute::UNKNOWN.

Referenced by CLKernelLibrary::set_context().

219 {
220  _context = std::move(context);
221  if(_context.get() != nullptr)
222  {
223  const auto cl_devices = _context.getInfo<CL_CONTEXT_DEVICES>();
224 
225  if(!cl_devices.empty())
226  {
227  _device = CLDevice(cl_devices[0]);
228  }
229  }
230 }
cl::Context & context()
Accessor for the associated CL context.

◆ set_device()

void set_device ( cl::Device  device)

Sets the CL device for which the programs are created.

Parameters
[in]deviceA CL device.

Definition at line 332 of file CLCompileContext.cpp.

References arm_compute::get_wbsm_support_info().

Referenced by CLKernelLibrary::set_device().

333 {
334  _device = std::move(device);
335  _is_wbsm_supported = get_wbsm_support_info(device);
336 }
bool get_wbsm_support_info(const cl::Device &device)
Definition: CLHelpers.cpp:423

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