WSL2 上的设备内存分配失败

Posted

技术标签:

【中文标题】WSL2 上的设备内存分配失败【英文标题】:Device memory allocation fails on WSL2 【发布时间】:2021-09-16 07:48:30 【问题描述】:

我正在尝试在 WSL2 上运行一个带有 Cuda Thrust 函数的简单 c++ 程序。似乎程序在运行时无法分配设备内存。我一直将 Thrust 与 Microsoft Visual Studio 一起使用,没有出现任何错误。

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;

输出:

    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)

如果我注释 device_vector 行,并改用宿主向量,它运行时不会出错。

附加信息:

GeForce GTX 950M Windows 11 家庭版。构建 22000.51。 WSL2:Ubuntu-18.04 Cuda 编译工具,9.1 版,V9.1.85

【问题讨论】:

WSL2 在一个实际上可能没有你的 GPU 的虚拟机中运行。因此 device_vector 将无法在 GPU 上分配内存 - 它在 WSL2 的上下文中不存在 您的 WSL2 实例是否甚至可以访问 GPU?尝试使用此处的代码检查您的设备属性developer.nvidia.com/blog/… 您没有在 WSL2 上正确设置 CUDA。见here。 【参考方案1】:

在我发问题之前,我已经看过here中的说明,并且都下载并安装了WSL的CUDA驱动程序,并加入了windows Insider程序并升级到了windows 11 build。但它没有工作。

但我也安装了 Ubuntu 18.04。我认为是安装了 Ubuntu 20.4,而不是让程序最终运行没有错误!

所以,这就是我为解决这个问题所做的:

-从 Microsoft 商店安装 Ubuntu 20.04(它要求我先下载并运行 WSL 修复程序,我照做了)

-在 powershell 中,我删除了 Ubuntu 18.04

wsl --unregister Ubuntu-18.04 

将 Ubuntu-18.04 替换为您的发行版名称。你可以通过

在powershell中获得它的名字
wsl --list

-安装了cmake和g++-9(gcc-9已经安装)

-使用here中的说明下载了cuda工具包

然后

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

【讨论】:

以上是关于WSL2 上的设备内存分配失败的主要内容,如果未能解决你的问题,请参考以下文章

分配内存失败:8 (NetBeans)

cudaMemcpy 仍然无法托管设备分配的内存?

页面迁移从CMA(连续内存分配器)区域失败

设备内存上的推力减小结果

#01

堆栈内存有限制吗?