Compute Library
 22.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
 

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:410

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

Referenced by CLKernelLibrary::add_built_program().

314 {
315  _built_programs_map.emplace(built_program_name, program);
316 }

◆ clear_programs_cache()

void clear_programs_cache ( )

Clear the library's cache of binary programs.

Definition at line 318 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::clear_programs_cache().

319 {
320  _programs_map.clear();
321  _built_programs_map.clear();
322 }

◆ context()

cl::Context & context ( )

Accessor for the associated CL context.

Returns
A CL context.

Definition at line 329 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::context().

330 {
331  return _context;
332 }

◆ 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 ClCompositeKernel::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 345 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().

346 {
347  GPUTarget _target = get_target_from_device(_device.cl_device());
348  cl::NDRange default_range;
349 
350  switch(_target)
351  {
352  case GPUTarget::MIDGARD:
353  case GPUTarget::T600:
354  case GPUTarget::T700:
355  case GPUTarget::T800:
356  default_range = cl::NDRange(128u, 1);
357  break;
358  default:
359  default_range = cl::NullRange;
360  }
361 
362  return default_range;
363 }
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 290 of file CLCompileContext.cpp.

References ARM_COMPUTE_UNUSED, and CLDevice::supported().

Referenced by CLKernelLibrary::fp16_supported().

291 {
292  return _device.supported("cl_khr_fp16");
293 }
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 324 of file CLCompileContext.cpp.

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

325 {
326  return _built_programs_map;
327 }

◆ get_device()

const cl::Device & get_device ( ) const

Gets the CL device for which the programs are created.

Definition at line 334 of file CLCompileContext.cpp.

References CLDevice::cl_device().

Referenced by CLKernelLibrary::get_device().

335 {
336  return _device.cl_device();
337 }
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 386 of file CLCompileContext.cpp.

References CLDevice::device_version().

Referenced by CLKernelLibrary::get_device_version().

387 {
388  return _device.device_version();
389 }
std::string device_version() const
Returns the device version as a string.
Definition: CLDevice.h:130

◆ 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 391 of file CLCompileContext.cpp.

References CLDevice::compute_units().

Referenced by CLKernelLibrary::get_num_compute_units().

392 {
393  return _device.compute_units();
394 }
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 365 of file CLCompileContext.cpp.

References CLDevice::supported().

Referenced by CLKernelLibrary::int64_base_atomics_supported().

366 {
367  return _device.supported("cl_khr_int64_base_atomics");
368 }
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 370 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::is_wbsm_supported().

371 {
372  return _is_wbsm_supported;
373 }

◆ 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 375 of file CLCompileContext.cpp.

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

Referenced by CLKernelLibrary::max_local_workgroup_size().

376 {
377  size_t result;
378 
379  size_t err = kernel.getWorkGroupInfo(_device.cl_device(), CL_KERNEL_WORK_GROUP_SIZE, &result);
380  ARM_COMPUTE_ERROR_ON_MSG(err != 0, "clGetKernelWorkGroupInfo failed to return the maximum workgroup size for the kernel");
381  ARM_COMPUTE_UNUSED(err);
382 
383  return result;
384 }
#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::CL20, CLDevice::device_version(), arm_compute::get_arch_from_target(), arm_compute::MIDGARD, arm_compute::support::cpp11::stoi(), CLDevice::supported(), CLDevice::target(), arm_compute::support::cpp11::to_string(), type, arm_compute::UNKNOWN, and CLDevice::version().

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

References arm_compute::get_wbsm_support_info().

Referenced by CLKernelLibrary::set_device().

340 {
341  _device = std::move(device);
342  _is_wbsm_supported = get_wbsm_support_info(device);
343 }
bool get_wbsm_support_info(const cl::Device &device)
Definition: CLHelpers.cpp:410

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