Compute Library
 21.08
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 202 of file CLCompileContext.h.

Constructor & Destructor Documentation

◆ CLCompileContext() [1/2]

Constructor.

Definition at line 141 of file CLCompileContext.cpp.

142  : _context(), _device(), _programs_map(), _built_programs_map(), _is_wbsm_supported()
143 {
144 }

◆ CLCompileContext() [2/2]

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

Constructor.

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

Definition at line 146 of file CLCompileContext.cpp.

References arm_compute::get_wbsm_support_info().

147  : _context(), _device(), _programs_map(), _built_programs_map(), _is_wbsm_supported()
148 {
149  _context = std::move(context);
150  _device = CLDevice(device);
151  _is_wbsm_supported = get_wbsm_support_info(device);
152 }
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 308 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::add_built_program().

309 {
310  _built_programs_map.emplace(built_program_name, program);
311 }

◆ clear_programs_cache()

void clear_programs_cache ( )

Clear the library's cache of binary programs.

Definition at line 313 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::clear_programs_cache().

314 {
315  _programs_map.clear();
316  _built_programs_map.clear();
317 }

◆ context()

cl::Context & context ( )

Accessor for the associated CL context.

Returns
A CL context.

Definition at line 324 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::context().

325 {
326  return _context;
327 }

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

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

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

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

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

References ARM_COMPUTE_UNUSED, and CLDevice::supported().

Referenced by CLKernelLibrary::fp16_supported().

286 {
287  return _device.supported("cl_khr_fp16");
288 }
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 319 of file CLCompileContext.cpp.

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

320 {
321  return _built_programs_map;
322 }

◆ get_device()

const cl::Device & get_device ( ) const

Gets the CL device for which the programs are created.

Definition at line 329 of file CLCompileContext.cpp.

References CLDevice::cl_device().

Referenced by CLKernelLibrary::get_device().

330 {
331  return _device.cl_device();
332 }
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 381 of file CLCompileContext.cpp.

References CLDevice::device_version().

Referenced by CLKernelLibrary::get_device_version().

382 {
383  return _device.device_version();
384 }
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 386 of file CLCompileContext.cpp.

References CLDevice::compute_units().

Referenced by CLKernelLibrary::get_num_compute_units().

387 {
388  return _device.compute_units();
389 }
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 360 of file CLCompileContext.cpp.

References CLDevice::supported().

Referenced by CLKernelLibrary::int64_base_atomics_supported().

361 {
362  return _device.supported("cl_khr_int64_base_atomics");
363 }
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 365 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::is_wbsm_supported().

366 {
367  return _is_wbsm_supported;
368 }

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

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

Referenced by CLKernelLibrary::max_local_workgroup_size().

371 {
372  size_t result;
373 
374  size_t err = kernel.getWorkGroupInfo(_device.cl_device(), CL_KERNEL_WORK_GROUP_SIZE, &result);
375  ARM_COMPUTE_ERROR_ON_MSG(err != 0, "clGetKernelWorkGroupInfo failed to return the maximum workgroup size for the kernel");
376  ARM_COMPUTE_UNUSED(err);
377 
378  return result;
379 }
#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 213 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().

214 {
215  _context = std::move(context);
216  if(_context.get() != nullptr)
217  {
218  const auto cl_devices = _context.getInfo<CL_CONTEXT_DEVICES>();
219 
220  if(!cl_devices.empty())
221  {
222  _device = CLDevice(cl_devices[0]);
223  }
224  }
225 }
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 334 of file CLCompileContext.cpp.

References arm_compute::get_wbsm_support_info().

Referenced by CLKernelLibrary::set_device().

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

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