0

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.

2
  • 1
    ... I didnt saw any examples for this need. Plenty of examples here Commented Jun 24, 2015 at 13:10
  • Duplicate of this Commented Jun 24, 2015 at 13:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.