Nvidia Optimus, Bumblebee and CUDA on Kbuntu 15.10

I decided to write this post after experiencing a chain of weird events with setting up CUDA with ubuntu (Kubuntu,etc)!

I’ve used CUDA on OpenCV with Archlinux in 2014-2015 and it wasn’t too hard to get to work. But the story with Ubuntu is completely different 😛

First path is nvidia developer repo. That have own perils but you get latest CUDA version. (7.5). Second path is Ubuntu provided way which is much safer but not the latest (6.5).

Option 1 CUDA through nvidia repo. ( nvidia proprietary drivers + nvidia-primus)

Follow the guide at http://docs.nvidia.com/cuda/cuda-getting-started-guide-for-linux/#axzz43DrWGyFP

They’ll replace ubuntu driver with their version (352.79 instead of 352.68 in my case)

Warning 1 :Do not reboot right now, otherwise you may come to a black screen and will have to boot from a live cd and chroot !! From my experience you have to run gpu-manager manually (explained below)

Warning 2: nvidia-prime will set nvidia chipset as the default** This doesn’t work very well and consume power (caused kde to crash with multiple monitors, strange font sizes, etc).

  • Use nvidia-settings and set intel as the primary chipset.
  • Login from the command line (alt + ctrl +f1);
  • Run the following commands, first will stop the DM (sddm for kubuntu, kde dropped KDM since kde 5) second will run gpu-manager that’ll go through the configuration.
  1. sudo systemctl stop sddm
  2. sudo gpu-manager
  • Observe the output of gpu-manager. Now you can reboot and see the results.
  • Make sure everything works fine (multiple monitors, etc).

Option 2 CUDA through Ubuntu Repo (nvidia proprietary + bumblebee or nvidia-primus)

Install nvidia drivers. Easiest path is using the “driver manager” software of ubuntu/ kubuntu. In kubuntu its accessible in System settings.

Note "Driver Management" icon in hardware section.
Note “Driver Management” icon in hardware section.
Choose the nvidia proprietary driver. (352 recommended)
Choose the nvidia proprietary driver. (352 recommended)

Next install the following packages: nvidia-cuda nvidia-cuda-toolkit

sudo apt-get install nvidia-cuda-dev nvidia-cuda-toolkit

Now run “nvcc -V” and see if the compiler runs.

Getting CUDA to work with cmake and gcc

I prefer to use cmake script for OpenCV projects so I’ll explain that method. Other options are easily found on internet.

If you observe closely, the compatibility matrix shown at nvidia website, maximum supported gcc version at this time is 4.9 with CUDA 7.5 Now the issue is Ubuntu 15.10 have gcc 5+.

So the first fix had to be install gcc 4.9 and point nvcc to gcc 4.9. In cMake scripts, the following declarations worked for me. In addition I had to specify some more info. Some people suggest editing nvcc.profile but I didn’t bother, I was already using cmake for the opencv projects!

set(CUDA_TOOLKIT_ROOT_DIR "/usr/local/cuda")
set(CUDA_HOST_COMPILER "/usr/bin/gcc-4.9")
set(CUDA_CUDART_LIBRARY "${CUDA_TOOLKIT_ROOT_DIR}/lib64/libcudart.so")
find_package(CUDA 7.5)

The first two lines found via opencv cmake config and the third had to be added after cmake complained “missing CUDA_CUDART_LIBRARY”.

Now everything should work fine;

If you get “invalid device ordinal” when running CUDA apps, the reason is the driver seems not to load properly on resuming from sleep. Dmesg will show this as “gpu falling off the bus”. Currently I couldn’t find a fix for the matter, I guess editing the config of nvidia-primus or bumblebee may help.

CUDA devicequery

Some history without dates 😛

Initially the only solution for graphics switching and keeping the nvidia chipset from overheating or acting strange required some hacking of ACPI calls. Then this project called Bumblebee emerged (http://bumblebee-project.org/). Thanks for the bumblebee daemon, it was possible to have proper power management. Some time later, they released a kernel module called “bbswitch”. This module make life even easier by automatically enabling power management.

Since the beginning I went with bumblebee + nvidia propriatery drivers, so as usual I went that way with ubuntu.

By now, Nvidia has released their version of optimus switching for Linux, the package is known as nvidia-prime. In past, the only alternative was bumblebee. I’m yet to see which works better, but I’m not much concerned as the only use of nvidia in linux is for CUDA based stuff. On windows however, it sees enough action ;).