Nvidia T4 and Matlab on Ubuntu Linux

I setup a PowerEdge server with a Nvidia T4 GPU. The OS is Ubuntu 20.04. User complained that the Matlab benchmark is very slow. The reason is that Matlab is using software OpenGL rendering. It seems like an easy task, but I ended up spent countless hours to find a solution which works for me.

Install Nvidia Driver

There are a lot of ways to install Nvidia driver on Ubuntu. What works for me is using the default GUI. To install it, click on Show Applications and then Additional Drivers. Select nvidia-driver-465 and click on Apply Changes. Note that by the time you read this article, the version will be different. I’d suggest selecting the most recent one.

Reboot the computer after driver installation.

Run this command to check the driver installation:
sudo nvidia-smi

You will see the output like this:

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 465.19.01    Driver Version: 465.19.01    CUDA Version: 11.3     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA Tesla T4     On   | 00000000:3B:00.0 Off |                    0 |
| N/A   31C    P8     9W /  70W |     47MiB / 15109MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      4537      G   /usr/lib/xorg/Xorg                 39MiB |
|    0   N/A  N/A      6290      G   /usr/bin/gnome-shell                6MiB |
+-----------------------------------------------------------------------------+

Note that you should see some processes under Processes section. I you don’t see any processes, you need to reinstall the driver.

Nvidia T4 does not have a display output. We need to generate a custom xorg.conf for headless configuration. [reference]

Run the following command to get the bus ID of the GPU:
sudo nvidia-xconfig --query-gpu-info

Excerpt of the output:

GPU #0:
  Name      : NVIDIA Tesla T4
  UUID      : GPU-xxxxxxxx
  PCI BusID : PCI:59:0:0

Run the following command to generate a xorg.conf. Remember to substitute the busid on your computer.

sudo nvidia-xconfig -a --allow-empty-initial-configuration --virtual=1920x1080 --busid PCI:59:0:0

You might need to edite the /etc/X11/xorg.conf file, if you are using driver 440 or above. Add this line under Device and Screen section.
Option "HardDPMS" "false"

Install VirtualGL

You need VirtualGL if you want to run Matlab in a remote X11 session.

Download the deb package from SourceForge.

Run this command to install VirtualGL.
sudo dpkg -i virtualgl*.deb

Run this command to configure VirtualGL.
sudo vglserver_config

For my machine, I choose not restricting 3D X server access, not restricting frambuffer device access, and enabling XTEST extension.

Reboot the computer.

Matlab Configuration

If Matlab is using software OpenGL rendering, you need to configure it to use hardware OpenGL rendering.

Type the following command in Matlab to use the hardware OpenGL rendering.
opengl save hardware

Close Matlab and run Matlab again. If you see an error message from Matlab about hardware OpenGL error. You can try to follow the instructions in this post to fix the problem.

Invoke Matlab using vglrun

To run Matlab during a remote X11 connection, run this command:
vglrun matlab

Note that there are a lot of command options to fine tune the performance. I found this combination works best for me.
vglrun +sync +xcb -c rgb matlab

If you only need to run Matlab locally, you don’t have to do this. To make sure Matlab is using the correct rendering device. Type the following command in Matlab:
opengl info

This is the output on my computer.

                          Version: '4.6.0 NVIDIA 465.19.01'
                           Vendor: 'NVIDIA Corporation'
                         Renderer: 'NVIDIA Tesla T4/PCIe/SSE2'
                   MaxTextureSize: 32768
                           Visual: 'Visual 0x21, (RGBA 32 bits (8 8 8 8), Z depth 16 bits, Hardware acceleration, Double buffer, Antialias 8 samples)'
                         Software: 'false'
             HardwareSupportLevel: 'full'
        SupportsGraphicsSmoothing: 1
    SupportsDepthPeelTransparency: 1
       SupportsAlignVertexCenters: 1
                       Extensions: {400×1 cell}
               MaxFrameBufferSize: 32768


This post may contain affiliated links. When you click on the link and purchase a product, we receive a small commision to keep us running. Thanks.