Twitter

CUDA: getting started on WSL

I have always preferred command line and vi finding it more efficient so after the CUDA: getting started on Windows, let's have a look at how to get started with CUDA on WSL (Windows Subsystem for Linux) which I personally use a lot when I work on my own PC (and my own GPU which is far cheaper than in any cloud for my personal training :)) in combination with Windows Terminal. Keep in mind that you can find NVidia official installation documentation to install CUDA on any platform here.

To start with, let's head to the CUDA toolkit download page which will be nice enough to generate the installation instructions for you (specific to WSL):
$ wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
$ sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
$ wget https://developer.download.nvidia.com/compute/cuda/12.4.1/local_installers/cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb
$ sudo dpkg -i cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb                       <== this one should be executed after the next one
$ sudo cp /var/cuda-repo-wsl-ubuntu-12-4-local/cuda-*-keyring.gpg /usr/share/keyrings/  <== this one should be executed before the previous one
$ sudo apt-get update
$ sudo apt-get -y install cuda-toolkit-12-4
As mentioned, I found that 2 lines of the instructions need to be switched; we'll see that later. Let's start by upgrading your system, it is always better to start with an up to date system:
$ sudo apt-get update && sudo apt-get upgrade
. . .
$
You may face the below issue when updating your WSL system:
Err:4 http://security.ubuntu.com/ubuntu jammy-security InRelease
Temporary failure resolving 'security.ubuntu.com'
This is due to the DNS incorrectly set up at WSL level; you can easily fix it using google DNS (this issue needs to be fixed before continuing, you cannot ignore it):
$ echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf > /dev/null
$
Now let's run the installation instructions:
$ wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
--2024-04-15 13:27:23--  https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
Resolving developer.download.nvidia.com (developer.download.nvidia.com)... 152.199.20.126
Connecting to developer.download.nvidia.com (developer.download.nvidia.com)|152.199.20.126|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 190 [application/octet-stream]
Saving to: ‘cuda-wsl-ubuntu.pin’

cuda-wsl-ubuntu.pin                                100%[===============================================================================================================>]     190  --.-KB/s    in 0s
$ sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
$ wget https://developer.download.nvidia.com/compute/cuda/12.4.1/local_installers/cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb
--2024-04-15 13:10:46--  https://developer.download.nvidia.com/compute/cuda/12.4.1/local_installers/cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb
Resolving developer.download.nvidia.com (developer.download.nvidia.com)... 152.199.20.126
Connecting to developer.download.nvidia.com (developer.download.nvidia.com)|152.199.20.126|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3057903050 (2.8G) [application/x-deb]
Saving to: ‘cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb’

cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb 100%[===============================================================================================================>]   2.85G  26.7MB/s    in 1m 52s

2024-04-15 13:12:39 (26.0 MB/s) - ‘cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb’ saved [3057903050/3057903050]
$
As mentioned in the official instruction list above the next one will fail:
$ sudo dpkg -i cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb
Selecting previously unselected package cuda-repo-wsl-ubuntu-12-4-local.
(Reading database ... 24227 files and directories currently installed.)
Preparing to unpack cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb ...
Unpacking cuda-repo-wsl-ubuntu-12-4-local (12.4.1-1) ...
Setting up cuda-repo-wsl-ubuntu-12-4-local (12.4.1-1) ...

The public cuda-repo-wsl-ubuntu-12-4-local GPG key does not appear to be installed.
To install the key, run this command:
sudo cp /var/cuda-repo-wsl-ubuntu-12-4-local/cuda-38AF289B-keyring.gpg /usr/share/keyrings/
$
And it fails clearly requiring that the next instruction line to be run before (thus my remark into the list of instructions, strange they missed that but this is what happend on my system); so let's re order those two and finish the installation:
$ sudo cp /var/cuda-repo-wsl-ubuntu-12-4-local/cuda-*-keyring.gpg /usr/share/keyrings/
$ sudo dpkg -i cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb
(Reading database ... 24313 files and directories currently installed.)
Preparing to unpack cuda-repo-wsl-ubuntu-12-4-local_12.4.1-1_amd64.deb ...
Unpacking cuda-repo-wsl-ubuntu-12-4-local (12.4.1-1) over (12.4.1-1) ...
Setting up cuda-repo-wsl-ubuntu-12-4-local (12.4.1-1) ...
$ sudo apt-get -y install cuda-toolkit-12-4
Reading package lists... Done
Building dependency tree... Done
. . .
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...

done.
done.
Setting up at-spi2-core (2.44.0-3) ...
$
And done!

Before being able to test CUDA, we need to set our environment paths; indeed, you'll find nvcc (NVidia CUDA Compiler) in /usr/local:
$ locate nvcc
. . .
/usr/local/cuda-12.4/bin/__nvcc_device_query
/usr/local/cuda-12.4/bin/nvcc
/usr/local/cuda-12.4/bin/nvcc.profile
. . .
$
Set the environment up (you may want to add these env varfiables into your .profile or .bash_profile) and verify nvcc is happy to be here:
$ export PATH=/usr/local/cuda-12.4/bin:${PATH}
$ export LD_LIBRARY_PATH=/usr/local/cuda-12.4/lib64:${LD_LIBRARY_PATH}
$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Thu_Mar_28_02:18:24_PDT_2024
Cuda compilation tools, release 12.4, V12.4.131
Build cuda_12.4.r12.4/compiler.34097967_0
$
NVidia is nice enough to share this git repository with plenty of code already written; let's clone it:
$ git clone https://github.com/NVIDIA/cuda-samples
Cloning into 'cuda-samples'...
remote: Enumerating objects: 18291, done.
remote: Counting objects: 100% (3706/3706), done.
remote: Compressing objects: 100% (587/587), done.
remote: Total 18291 (delta 3362), reused 3157 (delta 3119), pack-reused 14585
Receiving objects: 100% (18291/18291), 133.19 MiB | 12.68 MiB/s, done.
Resolving deltas: 100% (16022/16022), done.
Updating files: 100% (4012/4012), done.
$
Head over to the deviceQuery sample:
$ cd cuda-samples/Samples/1_Utilities/deviceQuery
$ ls -ltr
total 76
-rwxrwxrwx 1 fred fred 12620 Apr 15 15:45 Makefile
-rwxrwxrwx 1 fred fred  2154 Apr 15 15:45 NsightEclipse.xml
-rwxrwxrwx 1 fred fred  3639 Apr 15 15:45 README.md
-rwxrwxrwx 1 fred fred 13921 Apr 15 15:45 deviceQuery.cpp
-rwxrwxrwx 1 fred fred   851 Apr 15 15:45 deviceQuery_vs2017.sln
-rwxrwxrwx 1 fred fred  5199 Apr 15 15:45 deviceQuery_vs2017.vcxproj
-rwxrwxrwx 1 fred fred   851 Apr 15 15:45 deviceQuery_vs2019.sln
-rwxrwxrwx 1 fred fred  4784 Apr 15 15:45 deviceQuery_vs2019.vcxproj
-rwxrwxrwx 1 fred fred   851 Apr 15 15:45 deviceQuery_vs2022.sln
-rwxrwxrwx 1 fred fred  4784 Apr 15 15:45 deviceQuery_vs2022.vcxproj
$
Let's compile the deviceQuery sample:
$ make
/usr/local/cuda/bin/nvcc -ccbin g++ -I../../../Common -m64 --threads 0 --std=c++11 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_89,code=sm_89 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 -o deviceQuery.o -c deviceQuery.cpp
/usr/local/cuda/bin/nvcc -ccbin g++ -m64 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_89,code=sm_89 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 -o deviceQuery deviceQuery.o
mkdir -p ../../../bin/x86_64/linux/release
cp deviceQuery ../../../bin/x86_64/linux/release
$ ls -ltr
total 1084
-rwxrwxrwx 1 fred fred   12620 Apr 15 15:45 Makefile
-rwxrwxrwx 1 fred fred    2154 Apr 15 15:45 NsightEclipse.xml
-rwxrwxrwx 1 fred fred    3639 Apr 15 15:45 README.md
-rwxrwxrwx 1 fred fred   13921 Apr 15 15:45 deviceQuery.cpp
-rwxrwxrwx 1 fred fred     851 Apr 15 15:45 deviceQuery_vs2017.sln
-rwxrwxrwx 1 fred fred    5199 Apr 15 15:45 deviceQuery_vs2017.vcxproj
-rwxrwxrwx 1 fred fred     851 Apr 15 15:45 deviceQuery_vs2019.sln
-rwxrwxrwx 1 fred fred    4784 Apr 15 15:45 deviceQuery_vs2019.vcxproj
-rwxrwxrwx 1 fred fred     851 Apr 15 15:45 deviceQuery_vs2022.sln
-rwxrwxrwx 1 fred fred    4784 Apr 15 15:45 deviceQuery_vs2022.vcxproj
-rwxrwxrwx 1 fred fred   17072 Apr 15 15:49 deviceQuery.o
-rwxrwxrwx 1 fred fred 1010752 Apr 15 15:49 deviceQuery
$
And run the sample:
$ ./deviceQuery
./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "NVIDIA GeForce RTX 2070"
  CUDA Driver Version / Runtime Version          12.4 / 12.4
  CUDA Capability Major/Minor version number:    7.5
  Total amount of global memory:                 8192 MBytes (8589475840 bytes)
  (036) Multiprocessors, (064) CUDA Cores/MP:    2304 CUDA Cores
  GPU Max Clock rate:                            1620 MHz (1.62 GHz)
  Memory Clock rate:                             7001 Mhz
  Memory Bus Width:                              256-bit
  L2 Cache Size:                                 4194304 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
  Maximum Layered 1D Texture Size, (num) layers  1D=(32768), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(32768, 32768), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total shared memory per multiprocessor:        65536 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  1024
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 6 copy engine(s)
  Run time limit on kernels:                     Yes
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Disabled
  Device supports Unified Addressing (UVA):      Yes
  Device supports Managed Memory:                Yes
  Device supports Compute Preemption:            Yes
  Supports Cooperative Kernel Launch:            Yes
  Supports MultiDevice Co-op Kernel Launch:      No
  Device PCI Domain ID / Bus ID / location ID:   0 / 7 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 12.4, CUDA Runtime Version = 12.4, NumDevs = 1
Result = PASS
$
You are now good to go, you have a working CUDA environment on your WSL system!

No comments:

Post a Comment

CUDA: Getting started on Google Colab

While getting started with CUDA on Windows or on WSL (same on Linux) requires to install some stuff, it is not the case when using Google...