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.
I've recently used similar process on a clean build on linux, setting up OpenCL with an NVIDIA card.
Steps I took:
1 - install the NVIDIA driver.
2 - install the CUDA tool kit - (follwing the steps in the guide, there are many ways to do it, but I used the .deb installer, guide can be found here: http://docs.nvidia.com/cuda/cuda-getting-started-guide-for-linux/)
3 - using apt-get install the OpenCL headers. Command: sudo apt-get install opencl-headers
Using the : CL/opencl.h
header I was able to compile C/C++ code using gcc/g++ and the flag: -lOpenCL
Explaination of steps
1 - Self explanatory
2 - The CUDA toolkit also installs the OpenCL library (libOpencl.so) but not the header (at least not on my system)
3 - hence the header can be installed with apt-get. The header files get stored in the /usr/include/CL directory
Things that worked for me in Ubuntu 16.04
I have installed openCL on:
SandyBridge CPU: cpu only
IvyBridge GPU
Nvidia GTX 950
install packets
Generic ubuntu packages for OpenCL
Basic installation
sudo apt install ocl-icd-libopencl1
sudo apt install opencl-headers
sudo apt install clinfo
Package that allows to compile OpenCL code (1.2 I think)
Needed to link and compile
sudo apt install ocl-icd-opencl-dev
For Intel GT core
Package that enables runnig openCL on Intel GT, IvyBridge and up
sudo apt install beignet
For SandyBridge Intel CPU and possible others
Download this file OpenCL™ Runtime 16.1.1 for Intel® Core™ and Intel® Xeon® Processors for Ubuntu* (64-bit) On https://software.intel.com/en-us/articles/opencl-drivers#latest_linux_SDK_release
Install packages for turning rpm to deb
sudo apt-get install -y rpm alien libnuma1
Untar downloaded file
tar -xvf opencl_runtime_16.1.1_x64_ubuntu_6.4.0.25.tgz
cd opencl_runtime_16.1.1_x64_ubuntu_6.4.0.25/rpm/
Turn rpm files to deb
fakeroot alien --to-deb opencl-1.2-base-6.4.0.25-1.x86_64.rpm
fakeroot alien --to-deb opencl-1.2-intel-cpu-6.4.0.25-1.x86_64.rpm
Install .deb packages
sudo dpkg -i opencl-1.2-base_6.4.0.25-2_amd64.deb
sudo dpkg -i opencl-1.2-intel-cpu_6.4.0.25-2_amd64.deb
Touch local config file
sudo touch /etc/ld.so.conf.d/intelOpenCL.conf
Open the file
sudo vim /etc/ld.so.conf.d/intelOpenCL.conf
and add the line
/opt/intel/opencl-1.2-6.4.0.25/lib64/clinfo
Create a vendors dir and add intel.icd
sudo mkdir -p /etc/OpenCL/vendors
sudo ln /opt/intel/opencl-1.2-6.4.0.25/etc/intel64.icd /etc/OpenCL/vendors/intel64.icd
sudo ldconfig
test if this worked
clinfo should list your devices Dowload this file
https://codeload.github.com/hpc12/tools/tar.gz/master
Run this code to make sure everything works
tar xzvf tools-master.tar.gz
cd tools-master
make
./print-devices
./cl-demo 1000 10
This should print out GOOD in the end
For Nvidia
install nvidia drivers (I used 370), this should include all the runtime dirvers
Ubuntu 20.04 with an NVIDIA Quadro M1200, Lenovo P51
The software integration got a lot better since I had last tried, so I will do an update.
First, at least for graphics, I needed to tweak some BIOS settings as mentioned at, not sure needed for OpenCL: https://askubuntu.com/questions/973605/ubuntu-17-10-boot-stuck-at-message-started-nvidia-persistence-daemon-after-ins/976578#976578
Then, I find and install the latest driver available:
apt-cache search nvidia-driver
sudo apt install nvidia-driver-435 nvidia-opencl-dev
You can also search under:
software-properties-gtk
in the "Additional Drivers" tab.
Now I can compile and run the following test program:
main.c
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#define CL_TARGET_OPENCL_VERSION 220
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#include <CL/cl.h>
int main() {
cl_command_queue command_queue;
cl_context context;
cl_device_id device;
cl_int input = 1;
cl_int kernel_result = 0;
cl_kernel kernel;
cl_mem buffer;
cl_platform_id platform;
cl_program program;
const char *source = "__kernel void increment(int in, __global int* out) { out[0] = in + 1; }";
clGetPlatformIDs(1, &platform, NULL);
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &device, NULL);
context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL);
command_queue = clCreateCommandQueue(context, device, 0, NULL);
buffer = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, sizeof(cl_int), NULL, NULL);
program = clCreateProgramWithSource(context, 1, &source, NULL, NULL);
clBuildProgram(program, 1, &device, "", NULL, NULL);
kernel = clCreateKernel(program, "increment", NULL);
clSetKernelArg(kernel, 0, sizeof(cl_int), &input);
clSetKernelArg(kernel, 1, sizeof(cl_mem), &buffer);
clEnqueueTask(command_queue, kernel, 0, NULL, NULL);
clFlush(command_queue);
clFinish(command_queue);
clEnqueueReadBuffer(command_queue, buffer, CL_TRUE, 0, sizeof (cl_int), &kernel_result, 0, NULL, NULL);
assert(kernel_result == 2);
return EXIT_SUCCESS;
}
GitHub upstream
with:
gcc -ggdb3 -O0 -Wall -Wextra -pthread -std=c11 \
-pedantic-errors -o main.out main.c -lm -pthread -lOpenCL
./main.out
Notes:
- find your GPU model: https://askubuntu.com/questions/72766/how-do-i-find-out-the-model-of-my-graphics-card
- test that the driver is working: https://askubuntu.com/questions/68028/how-do-i-check-if-ubuntu-is-using-my-nvidia-graphics-card
- similar answer for CUDA: https://askubuntu.com/questions/917356/how-to-verify-cuda-installation-in-16-04/1215237#1215237
Ubuntu 15.10 with an NVIDIA NVS 5400M, Lenovo T430
sudo apt-get install nvidia-352 nvidia-352-dev nvidia-prime nvidia-modprobe
sudo ln -s /usr/include/nvidia-352/GL /usr/local/include
sudo ln -s /usr/lib/x86_64-linux-gnu/libOpenCL.so.1 /usr/local/lib/libOpenCL.so
Then use the header as:
#include <CL/cl.h>
And compile with:
gcc -o main main.c -lOpenCL
Notes:
- do not install the
nvidia-current
package. It is old. Eitherapt-cache search nvidia
and get the latest one, or usesoftware-properties-gtk
"Additional Drivers" tab.
I really recommend upgrading to 15.10 to get this to work: I had never managed before.