The parts of any OpenCL code

When you have confusing OpenCL code(s), split it into steps:

  1. Allocate and initialize host memory

// Usually using malloc
  1. Get platform and device id + setup

// Example
cl_platform_id platform_id;
cl_device_id device_id = NULL;
clGetPlatformIDs(1, &platform_id, NULL);
clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, NULL);
  1. Create context

// Example
cl_context context = NULL;
context = clCreateContext(NULL, 1, &device_id, NULL, NULL,  &_err);
  1. Create command queue

// Example
clCreateCommandQueue(context, device_id, CL_QUEUE_PROFILING_ENABLE, &_err);
  1. Create memory buffers

//Use 
clCreateBuffer()
  1. Copy host variables to device

//Use 
clEnqueueWriteBuffer()
  1. Create program from kernel

//Use
clCreateProgramWithBinary()  or clCreateProgram()
  1. Build program and create opencl kernel

//Use
clBuildProgram()
  1. Set Global and local work size

  2. a. Set args kernel

//Use
 clSetKernelArg()
  1. b. Invoke kernel

//Use
clEnqueueNDRangeKernel()
  1. Copy the termination variable or results back

//Use
clEnqueueReadBuffer()