I am going to show you how to build and run a very simple OpenCL program, that will calculate the dot product, since it is my favorite functionality testing algorithm. I enrolled one course about GPGPU and we were told,that learning OpenCL starts with learning CUDA first. This is just because the CUDA runtime API is much more elegant and easier to understand than the CUDA driver API, which is in fact very similar to OpenCL. Although OpenCL can be run on any platform, I prefer CUDA due its support from nVidia (Good documentation and Library base – CuFFT, CuBLAS …).

I will start with describing a standard CUDA cyclus:

  1. Allocate Memory on GPU
  2. Data copy HostToDevice
  3. Set Grid Dimensions
  4. Run Kernel to compute whatever
  5. Data copy DeviceToHost
  6. Clear memory

On the other hand, lets take a look on a standard OpenCL cyclus:

  1. Select Platform
  2. Select Device
  3. Create Context
  4. Create Command Queue
  5. Allocate Memory on the GPU
  6. Load Kernel files
  7. Create a Program
  8. Build the Program
  9. Create Kernel
  10. Set Kernel Arguments
  11. Data copy HostToDevice
  12. Set Grid Dimensions
  13. Run Kernel to compute whatever
  14. Data copy DeviceToHost
  15. Release resources

As you can see, there is plenty of code, that basically “does nothing useful at all”. Its more like a talking to a child and telling him exactly what to do. Although almost the same must be done in case of the CUDA driver API, most programmers tend to use the runtime API for its simplicity. More OpenCL code is a result of its portability. It can be run anywhere, even on the CPU. I will now post the example code, which you can find as a VS2013 project at the bottom of this page. Keep in mind, that to fully understand the OpenCL you will have to lookup the documentation, as you can basically pass plenty of OpenCL parameters with NULL.

And last, the very basic OpenCL Kernel:

Thats it. You can copy and paste the code for yourself, or download the VS2013 Project files HERE. Keep also in mind that OpenCL has to be supported by your GPU, you can easily check with the free GPU-Z program. In case OpenCL is not available, you should probably update your drivers. In case you are still wondering whether to program in OpenCL or CUDA, I suggest you to take the green CUDA road :) 

Share This