Search code examples
c++cudathrustwindows-subsystem-for-linux

Device memory allocation fails on WSL2


I am trying to run a simple c++ program, with Cuda Thrust functions, on WSL2. It seems that program fails in runtime to allocate device memory. I use Thrust with Microsoft visual studio all the time, and I don’t get any errors.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(proj LANGUAGES CXX CUDA)
 
add_executable(proj
proj.cu
)

proj.cu:

    #include<thrust/host_vector.h>
#include<thrust/device_vector.h>
#include<thrust/fill.h>

int main()
{
    //thrust::host_vector<int> h_vec(10);
    //thrust::fill(h_vec.begin(), h_vec.end(), 1);
    thrust::device_vector<int> d_vec(10);
    //thrust::fill(d_vec.begin(), d_vec.end(), 1);
    
    
    return 1;
}

output:

    terminate called after throwing an instance of 'thrust::system::system_error'
  what():  get_max_shared_memory_per_block :failed to cudaGetDevice: unknown error
Aborted (core dumped)

If I comment the device_vector line, and use the host vector instead, it runs with no errors.

Additional info:

  • GeForce GTX 950M
  • Windows 11 Home. build 22000.51.
  • WSL2: Ubuntu-18.04
  • Cuda compilation tools, release 9.1, V9.1.85

Solution

  • Before I post the question, I had already seen the instructions in here , and both downloaded and installed the CUDA driver for WSL, and joined the windows insider program and upgraded to windows 11 build. It did not work though.

    But I also had Ubuntu 18.04 installed. I think it was installing Ubuntu 20.4 instead that made the program run without errors in the end!

    So, this is what I did to solve this:

    -installed Ubuntu 20.04 from Microsoft store (It asked me to download and run a WSL fix first, which I did)

    -In powershell, I removed Ubuntu 18.04

    wsl --unregister Ubuntu-18.04 
    
    

    Replace Ubuntu-18.04 with the name of your distribution. You can get its name in powershell by

    wsl --list
    

    -installed cmake and g++-9 (gcc-9 was already installed)

    -downloaded the cuda toolkit using the instructions in here

    Then

    cmake -DCMAKE_CUDA_COMPILER=/usr/local/cuda-11.0/bin/nvcc  .. 
    make
    ./proj