Notes: CUDA / OpenCL with Nvidia Optimus under Ubuntu 16.04

NVidia 375 once again changed how OpenCL programs on Ubuntu 16.04 need to be invocated.

  1. Do the bumblebee dance
  2. Check that /etc/OpenCL/vendors/nvidia.icd contains libnvidia-opencl.so.1
  3. Maybe link libOpenCL.so to the expected location to enable standard OpenCL programs like darktable: sudo ln -s /usr/lib/x86_64-linux-gnu/libOpenCL.so.1 /usr/lib/x86_64-linux-gnu/libOpenCL.so
  4. Check that /etc/bumblebee/bumblebee.conf and /etc/modprobe.d/bumblebee.conf both contain the right driver version
  5. Ensure that nvidia-modprobe is installed: sudo apt install nvidia-modprobe
  6. nvidia-modprobe is responsible for loading NVidia kernel drivers but will fail since it checks eg. for nvidia-uvm but the correct name is nvidia-{version}-uvm etc. So add an alias to /etc/modprobe.d/bumblebee.conf: alias nvidia-uvm nvidia_375_uvm

These are the one-time-only steps. There are other obstacles still. NVidia introduced the CUDA Multi Process Service (MPS) which is not started transparently when needed, but has to be started by the user. Once the mps runs you will find that your load path does not contain critical libraries (thanks strace, you really saved my bacon there). This leads to the following little script:

# Start your favorite shell, make sure you use primus, other backends may fail to find libdlfaker.so and librrfaker.so
optirun -b primus fish

# Include NVidia's libraries into your load path
set -x LD_LIBRARY_PATH "$LD_LIBRARY_PATH:/usr/lib/nvidia-375/"

# Start your own mps instance
/usr/lib/nvidia-375/bin/nvidia-cuda-mps-control -d

# Loading uvm's kernel module may or may not be needed, depends on your nvidia-modprobe setup
sudo modprobe nvidia_375_uvm

# Run an OpenCL-enabled program
clinfo

I'm not sure why NVidia drivers still are not supported out-of-the-box after all this time. Since I started to use Cuda and OpenCL a few years ago it seems to become harder with every release to install it properly. And my text terminals still don't work. I wish we had, like, a package management system that could do all these things automatically... /rant