15

Question: What is needed headers and drivers are needed and where would I get them for compiling open CL on ubuntu using gcc/g++?


Info: for a while now I've been stumbling around trying to figure out how to install open CL on my desktop and if possible my netbook. There are a couple tutorials out there that I've tried but none seem to work. Also, they all just give a step by step with out really explaining why for the what, or even worse they are specific to a particular IDE so you have to learn the IDE to be able to do anything.

So I have an NVIDA GX465 in my desktop and integrated graphics in my netbook. my priority is of course my desktop, the netbook is just a convenience for development purposes(both run ubuntu 11.04 and will be running 11.10 as soon as it comes out). Can some one spell out for me what exactly is needed to get it so I can actually compile code and have it run. and if you could also explain what each piece does so that I can understand it's importance.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Narcolapser
  • 5,895
  • 15
  • 46
  • 56

4 Answers4

13

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.

aland
  • 4,829
  • 2
  • 24
  • 42
virtuallinux
  • 996
  • 8
  • 13
  • AMD APP works on any CPU with SSE3 support (not only AMD's ones), and there is Intel OpenCL SDK (SSE4 support is necessary). But they're barely useful if you are targeting GPUs: there are too much architecture differences between CPU and GPU to be able to draw any conclusions from performance of your kernel on CPU. – aland Sep 25 '11 at 12:59
  • @Virtuallinux what exactly do i set LD_LIBRARY_PATH to? It didn't say in the installation process. I tried setting it to "/usr/local/cuda/include/CL/" and then compiling with "gcc -lOpenCL cltest.c -o cltest" and got "fatal error: OpenCL/opencl.h: No such file or directory:" any advice? – Narcolapser Sep 26 '11 at 00:22
  • @aland oh I don't expect any kind of performance, but it really isn't a matter of performance it is a matter of I always have my netbook, not always at desktop. so it is just handy to be able to work away from my desktop, just work on a program here and there. but when I finish testing and run the program for real, yea, i won't use my netbook there, that will be my desktop. – Narcolapser Sep 26 '11 at 00:24
  • *tears of joy* I've been trying to get openCL to work now since April. I've been wanting to code on my graphics card since 2 Februarys ago. I am so pleased that I've finally gotten a program to compile. thank you very very much! (and now i understand what i'm doing, not just following a walk through too!) – Narcolapser Sep 27 '11 at 19:28
  • [Ciro Santilli](https://stackoverflow.com/a/33483311/2809027)'s [solution](https://stackoverflow.com/a/33483311/2809027) is *dramatically* superior for this question's specific combination of Nvidia GPU and Debian-based Linux distro (e.g., Mint, Ubuntu), which requires creating several non-trivial symlinks *not* addressed by this now-outdated answer. – Cecil Curry Jul 01 '17 at 01:59
6

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:

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. Either apt-cache search nvidia and get the latest one, or use software-properties-gtk "Additional Drivers" tab.

I really recommend upgrading to 15.10 to get this to work: I had never managed before.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • Is `nvidia-prime` and `nvidia-modprobe` necessary? I am on 15.04, and `nvidia-modprobe` caused the next boot to fail to start Xorg. – Gambhiro Nov 26 '15 at 08:36
  • @Nyiti I don't understand it profoundly. Without modprobe, it worked at first, but stopped working one day suddenly. I am running it right now with the above specs just fine: do try to upgrade to 15.10: it seems that there has been great progress on the area lately. – Ciro Santilli OurBigBook.com Nov 26 '15 at 08:53
  • that is for C not C++ – Mr. Developerdude Sep 03 '16 at 01:12
  • @LennartRolland sure, but C++ was not very preeminently requested by OP (quick mention to `g++`), and the official API is C. But if you get C++ working as well with this setup, tell me how :-) – Ciro Santilli OurBigBook.com Sep 03 '16 at 08:26
  • **Excellent,** but tragically insufficient. The above instructions should be edited to *also* symlink the Nvidia-specific OpenCL headers: `sudo ln -s /usr/include/nvidia-352/CL /usr/local/include`. Nonetheless, this answer remains the authoritative solution for setting up an Nvidia developer environment on Debian-based Linux distros (e.g., Mint, Ubuntu). Ideally, installing the `nvidia-*-dev` package would automatically create these symlinks for you. Of course, it doesn't. `` – Cecil Curry Jul 01 '17 at 01:45
5

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

amanusk
  • 232
  • 2
  • 5
2

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

Liang
  • 383
  • 4
  • 13