Setting Up WSL for GPU Compute

5 minute read

Published:

This post explains how to prepare and set up Windows Subsystem Linux (WSL) for GPU compute to run CUDA, OpenCL and oneAPI. In addition, it explains how to set up TornadoVM, but after installing the required SDKs and drivers, one can install any tool supported by WSL, such as PyTorch.

Configure WSL

This tutorial assumes WSL is already installed. If this is not the case, on Windows 11 is as easy as running the following command in PowerShell in admin mode:

wsl --install 

This command installs, by default, the latest LTS version of Ubuntu. At the time of writing this post, the latest version is Ubuntu 24.04 LTS.

For more details about WSL configuration and installation, follow the official documentation: https://learn.microsoft.com/en-us/windows/wsl/install.

Set-up CUDA in WSL

If you have an NVIDIA GPU, chances are that you have already installed the NVIDIA driver for Windows PC. If not, the first step is to install (or update) the NVIDIA driver for Windows 11. The NVIDIA driver for Windows also includes support for WSL. Thus there is no need to install the NVIDIA driver within our WSL terminal. We only need the CUDA SDK.

## Update the system
sudo apt-get update
sudo apt-get dist-upgrade 

Download and install CUDA for WSL

Use the NVIDIA documentation to obtain the latest instructions: https://docs.nvidia.com/cuda/wsl-user-guide/index.html

The following commands install CUDA Toolkit 12.6 for WSL:

sudo apt-key del 7fa2af80

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.6.3/local_installers/cuda-repo-wsl-ubuntu-12-6-local_12.6.3-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-12-6-local_12.6.3-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-12-6-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-6

Update ~/.bashrc file to include the following content:

export C_INCLUDE_PATH=/usr/local/cuda/include
export CPLUS_INCLUDE_PATH=/usr/local/cuda/include
export LD_LIBRARY_PATH=/usr/local/cuda/lib64
export PATH=/usr/local/cuda/bin/:$PATH

And open a new terminal. So now, CUDA is ready to run on our NVIDIA GPUs within WSL!

Compiling and Running CUDA Samples

$ git clone https://github.com/NVIDIA/cuda-samples.git 
$ cd cuda-samples/Samples/1_Utilities/deviceQuery
$ make 
$ ./deviceQuery 
./deviceQuery Starting...

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

Detected 1 CUDA Capable device(s)

Device 0: "NVIDIA GeForce RTX 3070"
  CUDA Driver Version / Runtime Version          12.7 / 12.6
  CUDA Capability Major/Minor version number:    8.6
  Total amount of global memory:                 8192 MBytes (8589410304 bytes)
  (046) Multiprocessors, (128) CUDA Cores/MP:    5888 CUDA Cores
  GPU Max Clock rate:                            1725 MHz (1.73 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:        102400 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  1536
  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 1 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 / 1 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 12.7, CUDA Runtime Version = 12.6, NumDevs = 1
Result = PASS

Note that, after this point, we can install PyTorch and unsloth and create fine tune LLM models in our system (if you have a powerful enough GPU, and plenty of VRAM!).

Intel Compute Runtime: Enabling OpenCL and Level Zero

Additionally, you can install the Intel Compute Runtime to get access to OpenCL and Level Zero for iGPUs, ARC GPUs and CPUs.

Go to https://github.com/intel/compute-runtime/releases/ and download the latest release. In my case, at the time of writing this post, the latest version is 24.48.31907.7.

mkdir -p ~/bin/neo
cd ~/bin/neo
wget https://github.com/intel/intel-graphics-compiler/releases/download/v2.2.3/intel-igc-core-2_2.2.3+18220_amd64.deb
wget https://github.com/intel/intel-graphics-compiler/releases/download/v2.2.3/intel-igc-opencl-2_2.2.3+18220_amd64.deb
wget https://github.com/intel/compute-runtime/releases/download/24.48.31907.7/intel-level-zero-gpu-dbgsym_1.6.31907.7_amd64.ddeb
wget https://github.com/intel/compute-runtime/releases/download/24.48.31907.7/intel-level-zero-gpu_1.6.31907.7_amd64.deb
wget https://github.com/intel/compute-runtime/releases/download/24.48.31907.7/intel-opencl-icd-dbgsym_24.48.31907.7_amd64.ddeb
wget https://github.com/intel/compute-runtime/releases/download/24.48.31907.7/intel-opencl-icd_24.48.31907.7_amd64.deb
wget https://github.com/intel/compute-runtime/releases/download/24.48.31907.7/libigdgmm12_22.5.4_amd64.deb

Verify CheckSums:

wget https://github.com/intel/compute-runtime/releases/download/24.48.31907.7/ww48.sum
sha256sum -c ww48.sum

Install Intel Compute Runtime packages:

sudo dpkg -i *.deb

To run OpenCL, we need to do one more step for Ubuntu-based distributions:

sudo ln -s /usr/lib/x86_64-linux-gnu/libOpenCL.so.1 /usr/lib/x86_64-linux-gnu/libOpenCL.so 

Ready! Let’s set up TornadoVM

Configuring TornadoVM for WSL

Install Python venv module

sudo apt install python3-venv 

And set up a new Python environment as follows:

python3 -m venv ~/bin/venv 
source ~/bin/venv/bin/activate 

Finally, let’s configure TornadoVM:

cd ~/
git clone  git clone https://github.com/beehive-lab/TornadoVM.git tornado
cd tornado 

## Install OpenCL only 
./bin/tornadovm-installer --jdk jdk21 --backend=opencl

## Install OpenCL and PTX 
./bin/tornadovm-installer --jdk jdk21 --backend=opencl,ptx

## Install All backends:
./bin/tornadovm-installer --jdk jdk21 --backend=opencl,ptx,spirv

source setvars.sh

Run tests

make tests
tornado --devices

Conclusions

By following the outlined steps, readers can successfully configure WSL, install CUDA for NVIDIA GPUs, and set up the Intel Compute Runtime for OpenCL and Level Zero support on compatible hardware. Furthermore, the guide has demonstrated how to install TornadoVM. Importantly, the guide has emphasized that once the necessary SDKs and drivers are in place, users can readily install other GPU-accelerated tools like PyTorch, opening up a vast landscape of possibilities for AI, deep learning, and scientific computing within the convenient and flexible WSL framework. Happy coding!