Dedicating a GPU for CUDA on Ubuntu 18.04
Jun 27, 2019
3 minute read

Recently, I have been doing some deep learning on Tensorflow and PyTorch by dual booting my PC into Ubuntu 18.04 and training with CUDA drivers.

However, I am using that graphics card to display my desktop as well, and the desktop becomes annoyingly laggy when running training, especially in GPU accelerated applications like Firefox or POLAR. Changing to using the internal graphics would fix this.

In addition, it would be nice to claim back the graphics RAM used by X.Org, by dedicating the GPU to CUDA use only 1.

Credit to Stas Berkman for these instructions on AskUbuntu

Note Ubuntu 18.04 uses X.Org as the display server. Other versions of Ubuntu may be using Wayland, which will have different instructions. If you are not using an Intel processor, you will need different instructions!

Steps

  1. Ensure the internal Intel graphics are enabled in your BIOS. I had to turn it on.
  2. (Optional) set the default output to “internal” or similar in your BIOS so the boot screen appears on the right output
  3. Plug your monitor into the output of the internal graphics. You can have it plugged into both this and the graphics card at once, it is fine 2.
  4. Check the address of your internal Intel card using lspci
1
2
3
4
5
chris@chris-desktop:~$ lspci
00:00.0 Host bridge: Intel Corporation Xeon E3-1200 v5/E3-1500 v5/6th Gen Core Processor Host Bridge/DRAM Registers (rev 07)
00:01.0 PCI bridge: Intel Corporation Xeon E3-1200 v5/E3-1500 v5/6th Gen Core Processor PCIe Controller (x16) (rev 07)
00:02.0 VGA compatible controller: Intel Corporation HD Graphics 530 (rev 06)
00:14.0 USB controller: Intel Corporation 100 Series/C230 Series Chipset Family USB 3.0 xHCI Controller (rev 31)
  1. Edit/create /etc/X11/xorg.conf, ensuring the PCI number matches, see the AskUbuntu post for details.
Section "Device"
    Identifier      "intel"
    Driver          "intel"
    BusId           "PCI:0:2:0"
EndSection

Section "Screen"
    Identifier      "intel"
    Device          "intel"
EndSection
  1. Check your existing code works.

In my case, the code had set the CUDA_VISIBLE_DEVICES environmental variable, which hid my GPU from TensorFlow! Removing this got things working again.

Now when running nvidia-smi I get the following output, showing that the GPU is completely free of X.Org or any other process 🙂.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
chris@chris-desktop:~$ nvidia-smi
Thu Jun 27 11:55:10 2019       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.67       Driver Version: 418.67       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 1070    On   | 00000000:01:00.0 Off |                  N/A |
|  0%   34C    P8     7W / 180W |      1MiB /  8119MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

  1. I have a known issue with CUDA no longer working after suspend/resume, which this might fix, or at least make reloading the kernel module easier. ↩︎

  2. I use both cables so I can still play games on my windows partition ↩︎