1

I started to learn OpenCl. I read these links:

https://en.wikipedia.org/wiki/OpenCL
https://github.com/KhronosGroup/OpenCL-Guide/blob/main/chapters/os_tooling.md
https://www.khronos.org/opencl/

but I did not understand well that OpenCl is a library by including header file in source code or it is a compiler by using OpenCl C Compiler?!

saharsa
  • 467
  • 1
  • 7
  • 24

2 Answers2

0

It is both a library and a compiler.

The OpenCL C/C++ bindings that you include as header files and that you link against are a library. These provide the necessary functions and commands in C/C++ to control the device (GPU).

For the device, you write OpenCL C code. This language is not C or C++, but rather based on C99 and has some specific limitations (for example only 1D arrays) as well as extensions (math and vector functionality).

The OpenCL compiler sits in between the C/C++ bindings and the OpenCL C part. Using the C/C++ bindings, you run the compiler at runtime of the executable with the command clBuildProgram (C bindings) or program.build(...) (C++). It then at runtime once compiles the OpenCL C code to the device-specific assembly, which is different for every vendor. With an Nvidia GPU, you can for example look at the Compiler PTX assembly for the device.

ProjectPhysX
  • 4,535
  • 2
  • 14
  • 34
0

If you know OpenGL then OpenCL works on the same principle.

Disclaimer: Self-learned knowledge ahead. I wanted to learn OpenCL and found the OpenCL term as confusing as you do. So I did some painful research until I got my first OpenCL hello-world program working.

Overview

OpenCL is an open standard - i.e. just API specification which targets heterogeneous computing hardware in particular.

The standard comprises a set of documents available here. It is up to the manufacturers to implement the standard for their devices and make OpenCL available e.g. through GPU drivers to users. Perhaps in the form of a shared library.

It is also up to the manufacturers to provide tools for developer to make applications using OpenCL.

Here's where it gets complicated.

SDKs

Manufacturers provide SDKs - software packages that contain everything the said developer needs. (See the link above). But they are specific for each - e.g. NVIDIA SDK won't work without their gpu.

ICD Loader

Because of SDKs being tied to a signle vendor, the most portable(IMHO) solution is to use what is known as Khronos' ICD loader. It is kind of "meta-driver" that will, during run-time, search for other ICDs present in the system by AMD, Intel, NVIDIA, and others; then forward them calls from our application. So, as a developer, we can develop against this generic driver and use clGetPlatformIDs to fetch the available platforms and devices. It is availble as libOpenCL.so, at least on Linux, and we should link against it.

Counterpart for OpenGL's libOpenGL, well almost, because the vast majority of OpenGL(1.1+) is present in the form of extensions and must be loaded separately with e.g. GLAD. In that sense, GLAD is very similar to the ICD loader.

Again, it does not contain any actual "computing" code, only stub implementations of the API which forward everything to the chosen platform's ICD.

Headers

We are still missing the headers, thankfully Khronos organization releases C headers and also C++ bindings. But nothing is stopping you from writing them yourself based on the official API documents. It would just be really tedious and error-prone.

Here we can find yet another parallel with OpenGL because the headers are also just the consequence of the Standard and GLAD generates them directly from its XML version! How cool is that?!

Summary

To write a simple OpenCL application we need to:

  1. Download an ICD from the device's manufactures - e.g. up-to-date GPU drivers is enough.
  2. Download the headers and place them in some folder.
  3. Download, build, and install an ICD loader. It will likely need the headers too.
  4. Include the headers, use API in them, and link against the ICD loader.

For Debian, maybe Ubuntu and others there is a simpler approach:

  1. Download the drivers... look for <vendor>-opencl-icd, the drivers on Linux are usually not as monolithic as on Windows and might span many packages.
  2. Install ocl-icd-opencl-dev which contains C, C++ headers + the loader.
  3. Use the headers and link the library.
Quimby
  • 17,735
  • 4
  • 35
  • 55