Azil
Azil

Reputation: 467

How to integrate CPP applications with OpenCL code

I'm devloping on Linux & CPP (Using Eclipse SDK). I'm novice at OpenCL (GPU Programming) I want to execute some of my code on the GPU (rewrite some functions with openCL and run them on the GPU).

I'm liitle bit confuse - If I will write some code (.cl files) how can I call them from my cpp application ? I didnt saw any examples for this need.

Upvotes: 0

Views: 850

Answers (1)

Käptn Freiversuch
Käptn Freiversuch

Reputation: 268

There are two parts of code if you want to use opencl.

A. The Kernel code. consists of 1 to many kernel functions which perform your calculations on a device.

B. The Host code normal c/c++ code. what happens here:

  1. pick a device for the kernel to be computed on (gpu/cpu/igpu/xeon phi/...) in opencl you got a set of platforms which can contain several different devices. So you pick a platform AND a device. example: platform: intel cpu+gpu opencl 1.2 device: cpu OR IGPU

  2. build your kernel

    const char * code = load_program_source("kernel.cl"); cl_program program = clCreateProgramWithSource(context, 1, (const char **)&code, NULL, &err); errWrapper("clCreateProgramWithSource", err);

  3. create buffer for memory transfers to device:

    cl_mem devInput1 = clCreateBuffer(context, CL_MEM_READ_ONLY, variable1* sizeof(int), NULL, &err);

  4. transfer to device

    errWrapper("setKernel", clSetKernelArg(countKeyCardinality, 0, sizeof (cl_mem), &devInput1));

  5. launch kernel

    errWrapper("clEnqueueNDRangeKernel", clEnqueueNDRangeKernel(command_queue, kernel_function1, 1, NULL, &tasksize, NULL, 0, NULL, NULL));

  6. wait for termination clFinish(command_queue)

  7. Fetch your result from the device using

    clEnqueueReadBuffer

  8. Proceed with your c++ code using the result created by opencl calculations.

Thats the basic idea of using opencl in your code. Better start doing a full opencl tutorial. (just google it, you will drown in opencl tutorials)

concepts you should be familar with: opencl host api command queue kernel arguments. work group local size local memory global memory cl_mem object

Debugging opencl is possible but painfull. I would suggest doing debug with NORMAL C Code and port it to opencl if it works.

The main source for all commands is the offical API documentation, which can be found here: opencl 1.2 api

edit: you do not need a special IDE to code opencl.

Upvotes: 1

Related Questions