Compute Library
 21.02
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 139 of file CLCompileContext.cpp.

140  : _context(), _device(), _programs_map(), _built_programs_map(), _is_wbsm_supported()
141 {
142 }

◆ CLCompileContext() [2/2]

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

Constructor.

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

Definition at line 144 of file CLCompileContext.cpp.

References arm_compute::get_wbsm_support_info().

145  : _context(), _device(), _programs_map(), _built_programs_map(), _is_wbsm_supported()
146 {
147  _context = std::move(context);
148  _device = CLDevice(device);
149  _is_wbsm_supported = get_wbsm_support_info(device);
150 }
cl::Context & context()
Accessor for the associated CL context.
bool get_wbsm_support_info(const cl::Device &device)
Definition: CLHelpers.cpp:419

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

Referenced by CLKernelLibrary::add_built_program().

295 {
296  _built_programs_map.emplace(built_program_name, program);
297 }

◆ clear_programs_cache()

void clear_programs_cache ( )

Clear the library's cache of binary programs.

Definition at line 299 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::clear_programs_cache().

300 {
301  _programs_map.clear();
302  _built_programs_map.clear();
303 }

◆ context()

cl::Context & context ( )

Accessor for the associated CL context.

Returns
A CL context.

Definition at line 310 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::context().

311 {
312  return _context;
313 }

◆ 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 152 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().

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

327 {
328  GPUTarget _target = get_target_from_device(_device.cl_device());
329  cl::NDRange default_range;
330 
331  switch(_target)
332  {
333  case GPUTarget::MIDGARD:
334  case GPUTarget::T600:
335  case GPUTarget::T700:
336  case GPUTarget::T800:
337  default_range = cl::NDRange(128u, 1);
338  break;
339  default:
340  default_range = cl::NullRange;
341  }
342 
343  return default_range;
344 }
GPUTarget get_target_from_device(const cl::Device &device)
Helper function to get the GPU target from CL device.
Definition: CLHelpers.cpp:221
GPUTarget
Available GPU Targets.
Definition: GPUTarget.h:34
const cl::Device & cl_device() const
Returns the underlying cl device object.
Definition: CLDevice.h:111

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

References ARM_COMPUTE_UNUSED, and CLDevice::supported().

Referenced by CLKernelLibrary::fp16_supported().

272 {
273  return _device.supported("cl_khr_fp16");
274 }
bool supported(const std::string &extension) const override
Check if extensions on a device are supported.
Definition: CLDevice.h:140

◆ get_built_programs()

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

Access the cache of built OpenCL programs.

Definition at line 305 of file CLCompileContext.cpp.

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

306 {
307  return _built_programs_map;
308 }

◆ get_device()

const cl::Device & get_device ( ) const

Gets the CL device for which the programs are created.

Definition at line 315 of file CLCompileContext.cpp.

References CLDevice::cl_device().

Referenced by CLKernelLibrary::get_device().

316 {
317  return _device.cl_device();
318 }
const cl::Device & cl_device() const
Returns the underlying cl device object.
Definition: CLDevice.h:111

◆ get_device_version()

std::string get_device_version ( ) const

Return the device version.

Returns
The content of CL_DEVICE_VERSION

Definition at line 367 of file CLCompileContext.cpp.

References CLDevice::device_version().

Referenced by CLKernelLibrary::get_device_version().

368 {
369  return _device.device_version();
370 }
std::string device_version() const
Returns the device version as a string.
Definition: CLDevice.h:129

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

References CLDevice::compute_units().

Referenced by CLKernelLibrary::get_num_compute_units().

373 {
374  return _device.compute_units();
375 }
size_t compute_units() const
Returns the number of compute units available.
Definition: CLDevice.h:102

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

References CLDevice::supported().

Referenced by CLKernelLibrary::int64_base_atomics_supported().

347 {
348  return _device.supported("cl_khr_int64_base_atomics");
349 }
bool supported(const std::string &extension) const override
Check if extensions on a device are supported.
Definition: CLDevice.h:140

◆ is_wbsm_supported()

bool is_wbsm_supported ( ) const

Definition at line 351 of file CLCompileContext.cpp.

Referenced by CLKernelLibrary::is_wbsm_supported().

352 {
353  return _is_wbsm_supported;
354 }

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

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

Referenced by CLKernelLibrary::max_local_workgroup_size().

357 {
358  size_t result;
359 
360  size_t err = kernel.getWorkGroupInfo(_device.cl_device(), CL_KERNEL_WORK_GROUP_SIZE, &result);
361  ARM_COMPUTE_ERROR_ON_MSG(err != 0, "clGetKernelWorkGroupInfo failed to return the maximum workgroup size for the kernel");
362  ARM_COMPUTE_UNUSED(err);
363 
364  return result;
365 }
#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:111

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

References ARM_COMPUTE_ERROR, build_options, arm_compute::CL20, arm_compute::get_arch_from_target(), CLDevice::supported(), CLDevice::target(), arm_compute::support::cpp11::to_string(), type, and CLDevice::version().

Referenced by CLKernelLibrary::set_context().

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

References arm_compute::get_wbsm_support_info().

Referenced by CLKernelLibrary::set_device().

321 {
322  _device = std::move(device);
323  _is_wbsm_supported = get_wbsm_support_info(device);
324 }
bool get_wbsm_support_info(const cl::Device &device)
Definition: CLHelpers.cpp:419

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