How to compile OpenCL on Ubuntu?

To compile and run OpenCL code under Linux, you’ll need four things:

1) An NVIDIA Driver which supports OpenCL. The drivers packaged with Ubuntu are somewhat
old, but they should still work just fine. Unless you have explicit need for current
drivers, you should stick with the ones packaged with Ubuntu. To be clear, these are
the same drivers installed through the restricted drivers manager. OpenCL libaries are shipped with driver, so to just run OpenCL programs driver should be enough.

2) The CUDA toolkit. This includes the headers necessary to compile OpenCL code. Install this to the default location.

3) The GPU Computing SDK (optional). This includes various NVIDIA specific support tools, as well as OpenCL code samples.

All three of these items may be found at http://developer.nvidia.com/cuda-toolkit-40.

4) OpenCL C++ bindings (optional). Strangely, they are not included with CUDA Toolkit, but in case you use C++, they could make your code much more redable. You can download them from http://www.khronos.org/registry/cl/api/1.1/cl.hpp, and just put it in /usr/local/cuda/include/CL an you desktop.

Once these are installed, you’ll need to perform a few more steps to be able to compile and run OpenCL outside of the NVIDIA SDK.

1) The CUDA toolkit will have included the OpenCL headers (Listed at http://www.khronos.org/registry/cl/), likely they are in the directory /usr/local/cuda/include/CL. To make these headers available system wide, you should link this directory into /usr/include/, such that they may be accessed as /usr/include/CL/[headerfilename]. Instead of creating a symlink, you could add /usr/local/cuda/include to your C_INCLUDE_PATH and CPLUS_INCLUDE_PATH environment variables, but this would last for only currest session.

2) Make sure that the OpenCL library (libOpenCL.so) is present in /usr/lib. This should have been put in place by the driver, so you shouldn’t have to do anything.

You’re ready to write code. Make sure to include CL/cl.h (or CL/cl.hpp if you’d like to use C++ version of API) in any C(++) program which makes OpenCL API calls. When you compile, make sure to link against the OpenCL library (pass gcc the -lOpenCL flag).

As far as your netbook, integrated graphics don’t generally support OpenCL. In theory, AMD’s APP Acceleration supports running OpenCL on the CPU, but it’s not clear that it actually works.

Leave a Comment