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

Detailed Description

CLCompileContext class.

Definition at line 194 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()
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.

145  : _context(), _device(), _programs_map(), _built_programs_map()
146 {
147  _context = std::move(context);
148  _device = CLDevice(device);
149 }
cl::Context & context()
Accessor for the associated CL context.

References CLCompileContext::context().

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

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

Referenced by CLKernelLibrary::add_built_program().

◆ clear_programs_cache()

void clear_programs_cache ( )

Clear the library's cache of binary programs.

Definition at line 298 of file CLCompileContext.cpp.

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

Referenced by CLKernelLibrary::clear_programs_cache().

◆ context()

cl::Context & context ( )

Accessor for the associated CL context.

Returns
A CL context.

Definition at line 309 of file CLCompileContext.cpp.

310 {
311  return _context;
312 }

Referenced by CLCompileContext::CLCompileContext(), CLKernelLibrary::context(), and CLCompileContext::set_context().

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

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

References Program::build(), build_options, and kernel_name.

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

◆ default_ndrange()

cl::NDRange default_ndrange ( ) const

Return the default NDRange for the device.

Definition at line 324 of file CLCompileContext.cpp.

325 {
326  GPUTarget _target = get_target_from_device(_device.cl_device());
327  cl::NDRange default_range;
328 
329  switch(_target)
330  {
331  case GPUTarget::MIDGARD:
332  case GPUTarget::T600:
333  case GPUTarget::T700:
334  case GPUTarget::T800:
335  default_range = cl::NDRange(128u, 1);
336  break;
337  default:
338  default_range = cl::NullRange;
339  }
340 
341  return default_range;
342 }
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

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

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

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

References CLDevice::supported().

Referenced by CLKernelLibrary::fp16_supported().

◆ get_built_programs()

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

Access the cache of built OpenCL programs.

Definition at line 304 of file CLCompileContext.cpp.

305 {
306  return _built_programs_map;
307 }

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

◆ get_device()

const cl::Device & get_device ( ) const

Gets the CL device for which the programs are created.

Definition at line 314 of file CLCompileContext.cpp.

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

References CLDevice::cl_device().

Referenced by CLKernelLibrary::get_device().

◆ get_device_version()

std::string get_device_version ( ) const

Return the device version.

Returns
The content of CL_DEVICE_VERSION

Definition at line 360 of file CLCompileContext.cpp.

361 {
362  return _device.device_version();
363 }
std::string device_version() const
Returns the device version as a string.
Definition: CLDevice.h:129

References CLDevice::device_version().

Referenced by CLKernelLibrary::get_device_version().

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

366 {
367  return _device.compute_units();
368 }
size_t compute_units() const
Returns the number of compute units available.
Definition: CLDevice.h:102

References CLDevice::compute_units().

Referenced by CLKernelLibrary::get_num_compute_units().

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

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

References CLDevice::supported().

Referenced by CLKernelLibrary::int64_base_atomics_supported().

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

350 {
351  size_t result;
352 
353  size_t err = kernel.getWorkGroupInfo(_device.cl_device(), CL_KERNEL_WORK_GROUP_SIZE, &result);
354  ARM_COMPUTE_ERROR_ON_MSG(err != 0, "clGetKernelWorkGroupInfo failed to return the maximum workgroup size for the kernel");
355  ARM_COMPUTE_UNUSED(err);
356 
357  return result;
358 }
#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

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

Referenced by CLKernelLibrary::max_local_workgroup_size().

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

211 {
212  _context = std::move(context);
213  if(_context.get() != nullptr)
214  {
215  const auto cl_devices = _context.getInfo<CL_CONTEXT_DEVICES>();
216 
217  if(!cl_devices.empty())
218  {
219  _device = CLDevice(cl_devices[0]);
220  }
221  }
222 }
cl::Context & context()
Accessor for the associated CL context.

References CLCompileContext::context().

Referenced by CLKernelLibrary::set_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 319 of file CLCompileContext.cpp.

320 {
321  _device = std::move(device);
322 }

Referenced by CLKernelLibrary::set_device().


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