Using OpenCL on the CS Linux Machines

Almost all of the machines in our classrooms and labs (though not the machines in the two “computational clusters”, DIAS and Pandora) have graphics cards that support GPGPU (General Purpose computing using Graphics Processing Units): Machines in CSI 270A and CSI 270L have NVIDIA cards; other machines have AMD cards. On these machines we install proprietary drivers and toolkits that support OpenCL. (For AMD, that currently means a proprietary driver; for NVIDIA, it means CUDA, which is a programming environment for GPGPU in its own right but also supports OpenCL.) Where possible, we also install a driver from Intel that allows running OpenCL programs using the CPU rather than a GPU. This note describes how to develop OpenCL programs in C using one of these toolkits. (Likely similar instructions apply to C++, but I have not tested that.)


The first thing to do is to confirm that the machine you want to use has the right combination of hardware and software. Probably the easiest way to do this is with the command opencl-info, which lists available OpenCL “platforms” and “devices”. (Note that for machines with AMD cards, if you are logged in remotely this command will only list the CPU. This does not happen if you are logged in from the console, but apparently non-root users cannot get access to the GPU if logged in remotely.)

Next, for some platforms you will need to enable access to the appropriate toolkit. For AMD cards, nothing extra is required; for NVIDIA cards, use the command

module load cuda-latest
(See my notes on the “Modules package” for more about the module command.)

Compiling OpenCL programs


If the computer you're using has an AMD graphics card, just compile your program with gcc as usual, but add to the command the following flags:

-L/opt/amdgpu-pro/lib64 -lrt -lOpenCL


If the computer you're using has an NVIDIA graphics card, you'll need to use a different compiler, namely nvcc. This command isn't in the standard search path, but it (and its man page) will be accessible if you first do a

module load cuda-latest
as mentioned previously. You'll need to pass it some additional flags:
-L/lib64/lib64 -L/usr/lib64 -lOpenCL
nvcc works by calling a system compiler (one of the GCC compilers); if you need or want to pass it additional flags, you can do so with the -Xcompiler flag, e.g.,
-Xcompiler "-std=c99"
nvcc seems to be aimed more at compiling C++, so you may get some warnings if you use it to compile straight C, but the compile should work.

Running OpenCL programs

To execute the compiled program, it should be enough to just type the name of the executable (but see “Caveat” below about AMD). If you've set things up correctly, and you have access to the GPU (as discussed below), all should be well (assuming no bugs in the program itself!). Note, however, that some published examples make assumptions about the number of OpenCL “platforms” and “devices” that are not true in our environment. So they may need modification in order to work.

Caveat (AMD):

Useful links and other information