OpenCL (Open Computing Language) is a framework for writing programs that execute across heterogeneous platforms consisting of central processing units (CPUs), graphics processing units (GPUs), digital signal processors (DSPs), field-programmable gate arrays (FPGAs) and other processors or hardware accelerators. OpenCL specifies programming languages (based on C99 and C++11) for programming these devices and application programming interfaces (APIs) to control the platform and execute programs on the compute devices. OpenCL provides a standard interface for parallel computing using task- and data-based parallelism.
This is the definition of OpenCL on Wikipedia. In this post, I’d like to do some research about the architecture in a brief view, based on Arch Linux. Arch Linux is chosen because of its clear software source, in which we can find out what exactly in a package for OpenCL components.
Here, we only talk about the type that runs on GPU.
GPGPU
GPGPU is a notion standing General-Purpose computing on Graphics Processing Units. It’s called General Purpose
because through it, we will not use Graphics Processing Units to process graphics data. Instead, we’d like to use them as more general computing devices.
To achieve this, there are several frameworks proposed by different communities, organizations or companies. For example:
- NVIDIA Cuda
- ATI Stream
- OpenCL
NVIDIA Cuda is proprietary and of course supported by NVIDIA. OpenCL is an open standard maintained by the non-profit technology consortium Khronos Group. But in face, the implementations rely on hardware/software providers.
The important stuff of the various implementations is OpenCL Runtime libraries, which are prerequisites of executing a program that uses OpenCL.
OpenCL Runtime libraries
To be clarified, OpenCL Runtime libraries depend on hardware manufacturers. So, we should install the runtime libraries that match our hardwares.
For example, I have an Intel CPU along with integrated Intel GPU, and a NVIDIA GPU beside. So, what I need to use all GPU devices is just to install Intel GPU OpenCL runtime libraries and NVIDIA GPU OpenCL runtime libraries.
If you have an AMD GPU, or other types of devices, like an integrated FPGA(wow, you must be really professional), you should install the libraries provided by their manufacturers, or a compatible version.
On Arch Linux, pacman
is used to install packages. There are not bad OpenCL runtime library packages in the repository:
1 | AMD/ATI |
I’d like to install the one for NVIDIA, so I did
1 | sudo pacman -S opencl-nvidia |
We can see what are in the package from Arch website.
1 | etc/ |
There are some dynamic libraries for OpenCL with NVIDIA prefixes and a configuration file in /etc/OpenCL/vendors
.
The configuration file in etc/OpenCL/vendors/nvidia.icd
should be able to tell someone, that there is NVIDIA OpenCL runtime, and where it is. The file content is:
1 | libnvidia-opencl.so.1 |
If you’d like also to use Intel GPU, install intel-compute-runtime
. Files to be installed is below:
1 | etc/ |
You may notice that, there is nothing like libnvidia-compiler
in Intel OpenCL Runtime. But it’s not true, the fact is that the OpenCL compiler of Intel OpenCL runtime is installed in another library, intel-graphics-compiler. And, this package is a mandatory dependency of intel-compute-runtime
.
You should be able to install other OpenCL runtime if you have other devices.
But it’s not the end, we are not aiming at just “running” an OpenCL program, we aim at developing one.
Develop Environment
Basically, OpenCL is for C/C++ development. To develop, at least we need header files and dynamic libraries.
The most used header file is CL/cl.h
, which can be installed by installing opencl-headers package, which will import other necessary headers as well.
1 | usr/ |
And, the most import library is libOpenCL.so
, which is in ocl-icd
package.
1 | usr/ |
The full name of this package is OpenCL Installable Client Driver. It’s a mechanism to allow developers to build applications against an Installable Client Driver loader (ICD loader) rather than linking their applications against a specific OpenCL implementation. The ICD Loader is responsible for:
- Exporting OpenCL API entry points
- Enumerating OpenCL implementations
- Forwarding OpenCL API calls to the correct implementation
The official implementation of ICD Loader can be found here on GitHub: https://github.com/KhronosGroup/OpenCL-ICD-Loader.
There are other SDKs as well, but we ought not talk about them because they are out of scope:
1 | intel-opencl-sdkAUR: Intel OpenCL SDK (old version, new OpenCL SDKs are included in the INDE and Intel Media Server Studio) |
Besides, for the OpenCL Implementations, we’ve already talked about them. They are runtime libraries along with configuration files in /etc/OpenCL/vendors
.
A utility to show all possible properties on your system, is clinfo
.
Owning headers, OpenCL dynamic libraries and runtime libraries, we can start developing and running OpenCL program on C/C++. As well, there are lots of bindings for other languages, take ease to use them if you want.
In the next post, we will write a first OpenCL program. See you then!