Running Stan on the GPU with OpenCL on WSL: Seeking Assistance

@WardBrian Thank you for your assistance.

After your reply, I installed Ubuntu 24.04 on WSL2 and performed the following steps, including installing CUDA for WSL2. In my previous environment, Ubuntu 22.04, I had been experimenting with running large language models (LLMs) locally, which resulted in multiple CUDA installations and a convoluted PATHs. Therefore, I installed Ubuntu 24.04 afresh.

I managed to get cmdstanr running on Ubuntu 24.04 as well. However, the performance remains slower than I expected:

  1. Moved the folder from home/<user_name> on Ubuntu 22.04 to /mnt/c/Users/<user_name>/wsl-init.
  2. Installed Ubuntu 24.04 on WSL2.
  3. Moved the folder from /mnt/c/Users/<user_name>/wsl-init to home/<user_name> on Ubuntu 22.04.
  4. Installed the latest NVIDIA Driver from NVIDIA’s website.
  5. Verified that libcuda.so is only located in /usr/lib/wsl/lib/libcuda.so using find /usr/ -name libcuda.so.
  6. Followed the CUDA on WSL guide:
    1. Removed the existing key: sudo apt-key del 7fa2af80.
    2. Executed the following commands as per the CUDA 12.1.1 installation guide:
      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.1.1/local_installers/cuda-repo-wsl-ubuntu-12-1-local_12.1.1-1_amd64.deb
      sudo dpkg -i cuda-repo-wsl-ubuntu-12-1-local_12.1.1-1_amd64.deb
      sudo cp /var/cuda-repo-wsl-ubuntu-12-1-local/cuda-*-keyring.gpg /usr/share/keyrings/
      sudo apt-get update
      sudo apt-get -y install cuda
      
  7. Set the PATH:
    echo 'export PATH=/usr/local/cuda-12.1/bin${PATH:+:${PATH}}' >> ~/.bashrc
    echo 'export LD_LIBRARY_PATH=/usr/local/cuda-12.1/lib64:${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}' >> ~/.bashrc
    
    source ~/.bashrc
    
  8. Configured WSL not to inherit the Windows PATH:
    1. Edited /etc/wsl.conf to add:
      [interop]
      appendWindowsPath = false
      
    2. Executed the following in Windows PowerShell:
      wsl.exe --shutdown
      
    3. Reboot Ubuntu 24.04
  9. Verified that libcuda.so exists in /usr/lib/wsl/lib/libcuda.so and /usr/local/cuda-12.1/targets/x86_64-linux/lib/stubs/libcuda.so.
  10. Installed essential build tools:
    sudo apt -y install build-essential gcc g++ make libtool texinfo dpkg-dev pkg-config gfortran
    
  11. Installed OpenBLAS following the OpenBLAS Wiki because I also wanted to install it:
    sudo apt update
    sudo apt install libopenblas-dev
    
  12. Attempted GPU model fitting in R, but encountered clGetPlatformIDs CL_PLATFORM_NOT_FOUND_KHR error. Fixed it by installing libpocl-dev:
    sudo apt install libpocl-dev
    
  13. Tried GPU model fitting in R again. All 4 chains finished successfully with a Mean chain execution time of 293.8 seconds and a Total execution time of 297.9 seconds. However, the performance is still comparable to using CPUs.
1 Like