Skip to content
Working with OpenCL

Working with OpenCL

Working with OpenCL

OpenCL programs are used for performing computations on video cards that support OpenCL 1.1 or higher. Modern video cards contain hundreds of small specialized processors that can simultaneously perform simple mathematical operations with incoming data streams. The OpenCL language organizes parallel computing and provides greater speed for a certain class of tasks.

In some graphic cards working with the double type numbers is disabled by default. This can lead to compilation error 5105. To enable support for the double type numbers, please add the following directive to your OpenCL program: #pragma OPENCL EXTENSION cl_khr_fp64 : enable. However if a graphic card doesn’t support double, enabling this directive won’t be of help.

It is recommended to write the source code for OpenCL in separate CL files, which can later be included in the MQL5 program using the resource variables.

Handling errors in OpenCL programs

To get information about the last error in an OpenCL program, use the CLGetInfoInteger and CLGetInfoStringfunctions that allow getting the error code and text description.

OpenCL last error code: To get the latest OpenCL error, call CLGetInfoInteger, while thehandleparameter is ignored (can be set to zero). Description of errors: https://registry.khronos.org/OpenCL/specs/3.0-unified/html/OpenCL_API.html#CL_SUCCESS.

For an unknown error code, the"unknown OpenCL error N" string is returned where N is an error code. Example:

//--- the first 'handle' parameter is ignored when getting the last error code
intcode= (int)CLGetInfoInteger(0,CL_LAST_ERROR);

Text description of the OpenCL error: To get the latest OpenCL error, call CLGetInfoString. The error code should be passed as thehandleparameter.

Description of errors: https://registry.khronos.org/OpenCL/specs/3.0-unified/html/OpenCL_API.html#CL_SUCCESS. If CL_LAST_ERROR is passed instead of the error code, then the function returns the description of the last error. For example:

//--- get the code of the last OpenCL error
int   code= (int)CLGetInfoInteger(0,CL_LAST_ERROR);
stringdesc;// to get an error text description

//--- use the error code to get an error text description
if(!CLGetInfoString(code,CL_ERROR_DESCRIPTION,desc))
 desc="cannot get OpenCL error description,"+ (string)GetLastError();
Print(desc);

//--- in order to get the description of the last OpenCL error without getting the code first, pass CL_LAST_ERROR
if(!CLGetInfoString(CL_LAST_ERROR,CL_ERROR_DESCRIPTION,desc))
 desc="cannot get OpenCL error description,"+ (string)GetLastError();
Print(desc);;

So far, the name of the internal enumeration is given as an error description. You can find its decoding here: https://registry.khronos.org/OpenCL/specs/3.0-unified/html/OpenCL_API.html#CL_SUCCESS. For example, the CL_INVALID_KERNEL_ARGS value means “Returned when enqueuing a kernel when some kernel arguments have not been set or are invalid.”

Functions for running programs in OpenCL:

FunctionAction
CLHandleTypeReturns the type of an OpenCL handle as a value of the ENUM_OPENCL_HANDLE_TYPE enumeration
CLGetInfoIntegerReturns the value of an integer property for an OpenCL object or device
CLContextCreateCreates an OpenCL context
CLContextFreeRemoves an OpenCL context
CLGetDeviceInfoReceives device property from OpenCL driver
CLProgramCreateCreates an OpenCL program from a source code
CLProgramFreeRemoves an OpenCL program
CLKernelCreateCreates an OpenCL start function
CLKernelFreeRemoves an OpenCL start function
CLSetKernelArgSets a parameter for the OpenCL function
CLSetKernelArgMemSets an OpenCL buffer as a parameter of the OpenCL function
CLSetKernelArgMemLocalSets the local buffer as an argument of the kernel function
CLBufferCreateCreates an OpenCL buffer
CLBufferFreeDeletes an OpenCL buffer
CLBufferWriteWrites an array into an OpenCL buffer
CLBufferReadReads an OpenCL buffer into an array
CLExecuteRuns an OpenCL program
CLExecutionStatusReturns the OpenCL program execution status

See also

OpenCL, Resources

Last updated on