How to make OpenCL work on 14.10 + Nvidia 331.89 drivers?

Proprietary drivers 331.89 do not give me OpenCL. (tried both tested/updates) for a while I had version 304.123 that worked - except I wanted the current drivers, and now are unable to downgrade.

Anyway - I could downgrade by workaround, but I wish 331.89 worked.

I used "darktable -d opencl" as test alos added symbolic link to darktable, but that does not help with 331.89


Solution 1:

I too was confounded by this perplexing problem until I found a series of forum posts bout Virtual Lighttable in which the participants do some debugging. It turns out that the NVIDIA drivers need a custom modprobe rule that is not installed by default with either nvidia-331 or the the opencl drivers. These rules are provided in the nvidia-modprobe package.

Here is a list of all of the packages you will need to get OpenCL working on Ubuntu 14.10 with NVIDIA drivers:

sudo apt-get install nvidia-331 nvidia-331-uvm nvidia-opencl-dev nvidia-modprobe

EDIT: In case anyone encounters a similar problem on Ubuntu 15.04 and the NVIDIA 346.59 drivers, the command to fix the issue is nearly identical:

sudo apt-get install nvidia-346 nvidia-346-uvm nvidia-opencl-dev nvidia-modprobe

Solution 2:

Ubuntu 20.04 install

Things got much better now. Find available driver versions:

apt-cache search nvidia-driver

Install the latest one listed + opencl:

sudo apt install nvidia-driver-435 nvidia-opencl-dev

You can also search under:

software-properties-gtk

in the "Additional Drivers" tab for the latest driver.

Ubuntu 15.10 install

sudo apt-get install nvidia-352 nvidia-352-dev nvidia-prime nvidia-modprobe nvidia-opencl-dev
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

Test it out

Compile and run:

gcc -o main main.c -lOpenCL
./main

Here's a minimal test program:

main.c

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#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.

Notes

  • find your GPU model: How do I find out the model of my graphics card?
  • test that the driver is working: How do I check if Ubuntu is using my NVIDIA graphics card?
  • 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.

Tested on:

  • Lenovo ThinkPad T430 with NVIDIA NVS 5400M
  • Lenovo ThinkPad W540 with NVIDIA Quadro K1100M
  • Lenovo ThinkPad P51 with an NVIDIA Quadro M1200 + Ubuntu 21.10 + driver nvidia-driver-470

Related: https://stackoverflow.com/questions/7542808/how-to-compile-opencl-on-ubuntu/33483311#33483311