CTL  0.6.1
Computed Tomography Library
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
CTL::OpenCLFunctions Class Reference

The OpenCLFunctions class provides additional OpenCL C functions. More...

#include <openclfunctions.h>

Classes

class  OpenCLFunction
 Representation of a single OpenCL C function. More...
 

Public Types

using OpenCLFunctionGen = OpenCLFunction(*)()
 

Public Member Functions

 OpenCLFunctions (std::initializer_list< OpenCLFunctionGen > functionList)
 
 OpenCLFunctions (OpenCLFunctionGen function)
 
const std::vector< OpenCLFunction > & functions () const
 
std::string declarations () const
 
std::string declarationsAndDefinitions () const
 

Static Public Member Functions

static OpenCLFunction atomic_addf_g ()
 
static OpenCLFunction atomic_addf_l ()
 
static OpenCLFunction write_bufferf ()
 

Private Attributes

std::vector< OpenCLFunction_functions
 The list of OpenCL C functions.
 

Detailed Description

The OpenCLFunctions class provides additional OpenCL C functions.

Instances of the ClFileLoader class represent collections of OpenCL C functions, which may be used in an OpenCL program and in particular in the implementation of OpenCL kernel code. Such an instance can be passed to the OCL::OpenCLConfig::addKernel function in order to equip a kernel with the according additional OpenCL functions. By this approach, additional functions are made available to the OpenCL programmer that are not standardized in OpenCL C.

The functions that are available are represented by a list of static member functions of the OpenCLFunctions class. This list may be extended during the development of the CTL toolkit. An instance of a OpenCLFunctions object is created using the constructor, eg.

Here, myFunctions represents a set consisting of the two functions write_bufferf and atomic_addf_l. This object can be passed when creating a kernel:

std::string kernelSourceCode = ClFileLoader("path_to/myKernel.cl").loadSourceCode();
OCL::OpenCLConfig::instance().addKernel("myKernelFunctionName", myFunctions, kernelSourceCode);

Note that by using this approach, it is is also possible that the internal implementation of a function provided by OpenCLFunctions uses other functions from OpenCLFunctions, as long as all required functions are passed to the addKernel function (all function declarations will be prefixed to the kernel source code, followed by all definitions).

Constructor & Destructor Documentation

◆ OpenCLFunctions() [1/2]

CTL::OpenCLFunctions::OpenCLFunctions ( std::initializer_list< OpenCLFunctionGen >  functionList)

Constructs a OpenCLFunctions instance from an std::initializer_list functionList. An example use case is given by

As this constructor is implicit, the right-hand side of the aboves code can be also directly passed to the OpenCLConfig::addKernel function.

The input type std::initializer_list has OpenCLFunctionGen as template argument. This helper class defines a function type (generator function) with the signature of the static functions provided by the OpenCLFunctions class, which in turn, when invoked, return the actual OpenCLFunction object. The invocation of the generator functions in functionList is performed in this constructor and the returned OpenCLFunction objects are stored internally.

◆ OpenCLFunctions() [2/2]

CTL::OpenCLFunctions::OpenCLFunctions ( OpenCLFunctionGen  function)

Constructs a OpenCLFunctions instance that contains a single function. An example use case is given by

As this constructor is implicit, the right-hand side of the aboves code can be also directly passed to the OpenCLConfig::addKernel function.

The input type OpenCLFunctionGen defines a function type (generator function) with the signature of the static functions provided by the OpenCLFunctions class, which in turn, when invoked, return the actual OpenCLFunction object. The invocation of the generator function is performed in this constructor and the returned OpenCLFunction object is stored internally.

An instance of OpenCLFunctions may be also constructed from a custom function using a lambda expression:

const auto myFun = OpenCLFunctions([]() -> OpenCLFunctions::OpenCLFunction
{
return
R"clSrc(void myFun(){
// do something
}
)clSrc";
});

Member Function Documentation

◆ atomic_addf_g()

OpenCLFunctions::OpenCLFunction CTL::OpenCLFunctions::atomic_addf_g ( )
static

inline void atomic_addf_g(volatile global float* addr, float val)

This function is supposed to extend the capabilities of the built-in function atomic_add. The built-in function works only with integer types, whereas this function works with float. In an atomic manner it does the following: It reads the 32-bit value floating point value stored at location pointed by addr. It computes the sum with val and stores back the result to the location pointed by addr.

Internally, the implementation uses atomic_cmpxchg in a loop. Depending on the simultaneous write accesses to the location addr (by other workers), the function may take several iterations to terminate.

The _g in the function name indicates that it can only be used with an addr pointing to global qualified memory. For local memory, see atomic_addf_l.

Note that this function does not return anything in contrast to the OpenCL built-in function.

◆ atomic_addf_l()

OpenCLFunctions::OpenCLFunction CTL::OpenCLFunctions::atomic_addf_l ( )
static

inline void atomic_addf_l(volatile local float* addr, float val)

This function is supposed to extend the capabilities of the built-in function atomic_add. The built-in function works only with integer types, whereas this function works with float. In an atomic manner it does the following: It reads the 32-bit value floating point value stored at location pointed by addr. It computes the sum with val and stores back the result to the location pointed by addr.

Internally, the implementation uses atomic_cmpxchg in a loop. Depending on the simultaneous write accesses to the location addr (by other workers), the function may take several iterations to terminate.

The _l in the function name indicates that it can only be used with an addr pointing to local qualified memory. For global memory, see atomic_addf_g.

Note that this function does not return anything in contrast to the OpenCL built-in function.

◆ declarations()

std::string CTL::OpenCLFunctions::declarations ( ) const

Returns all declarations of the internally handled OpenCLFunctions as a single string.

◆ declarationsAndDefinitions()

std::string CTL::OpenCLFunctions::declarationsAndDefinitions ( ) const

Returns all declarations of the internally handled OpenCLFunctions followed by all definitions of these functions as a single string.

◆ functions()

const std::vector< OpenCLFunctions::OpenCLFunction > & CTL::OpenCLFunctions::functions ( ) const

Returns the list of the internally stored OpenCLFunctions.

◆ write_bufferf()

OpenCLFunctions::OpenCLFunction CTL::OpenCLFunctions::write_bufferf ( )
static

void write_bufferf(global float* buffer, int4 coord, float color, image3d_t image)

This function imitates the behavior of the function write_imagef for buffers of vectorized 3d float images instead of image3d_t. Unlike write_imagef, the extension cl_khr_3d_image_writes does not need to be implemented for write_bufferf.

Note that no bounds checking is performed on coord.

In contrast to write_imagef, the color argument is a single float value.

The extents of the 3d image are inherited from the last argument image.


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