WSL VS VM,在NVIDIA的管网已经有了明确的说明:
既然WSL 的默认设置为在不离开 Windows 的情况下开发跨平台应用程序,那么, WSL 中启用 GPU 加速,提供了对硬件的直接访问为必然。这对GPU加速的AI / ML训练提供了支持,并能够开发和测试基于技术构建的应用程序,例如OpenVINO,OpenGL和CUDA,这些技术针对Ubuntu,同时停留在Windows上搞事情。
What you will learn:
- How to install a Windows graphical device driver compatible with WSL2
- How to install the NVIDIA CUDA toolkit for WSL 2 on Ubuntu
- How to compile and run a sample CUDA application on Ubuntu on WSL2
What you will need:
- A Windows 10 version 21H2 or newer physical machine equipped with an NVIDIA graphics card and administrative permission to be able to install device drivers
- Ubuntu on WSL2 previously installed
- Familiarity with Linux command line utilities and interacting with Ubuntu on WSL2
【案,笔者的GPU是NVIDIA的,具体参照】
You will need to be on Windows 10 Build 19044+ or Windows 11 to access this feature.
GPU in Windows Subsystem for Linux (WSL) | NVIDIA Developer
WSL 上的 NVIDIA CUDA 驱动程序将 NVIDIA CUDA 和 AI 与无处不在的 Microsoft Windows 平台结合在一起,为众多行业细分和应用领域提供机器学习功能。
开发人员现在可以使用目前可用的 NVIDIA 驱动程序在 Microsoft Windows WSL 环境中利用 NVIDIA 软件堆栈。
NVIDIA 提供的 NVIDIA Windows GeForce 或 Quadro 生產 (x86) 驅動程序隨著 CUDA 和 DirectML 支援 WSL,可以從下面下載。
【案,笔者的硬件选择如下,按这个下载】然后具体的配置,按这个链接:CUDA on WSL User Guide (nvidia.com)
Normally, CUDA toolkit for Linux will have the device driver for the GPU packaged with it. On WSL 2, the CUDA driver used is part of the Windows driver installed on the system, and, therefore, care must be taken not to install this Linux driver as previously mentioned.
案,CUDA的linux的工具包将包括GPU的驱动包,要注意,在WSL2,CUDA的驱动是用了系统windows的驱动,也就是,你只要装适合你的windows驱动就好了,不需要安装linux的驱动
谁说intel的GPU就不能用呢?Intel® Graphics – Windows* DCH Drivers
【案,现在准备再WSL的ubuntu中装CUDA了,注意,安装要按照Ubuntu的官网来搞,笔者,是按照ubuntu的WSL配置官网搞的】
以下命令将在 Ubuntu 22.04 AMD64 体系结构上安装 WSL 特定的 CUDA 工具包版本 11.6。
请注意&#xff0c;旧版本的 CUDA &#xff08;<&#61;10&#xff09; 不支持 WSL 2。
另请注意&#xff0c;尝试直接从 Ubuntu 存储库&#xff08;“cuda”、“cuda-11-0”或“cuda-drivers”&#xff09;安装 CUDA 工具包包将尝试安装 Linux NVIDIA 图形驱动程序&#xff0c;这不是您在 WSL 2 上想要的。
因此&#xff0c;首先删除旧的GPG密钥&#xff1a;
donkeycar01&#64;DESKTOP-M4INDEG:~$ sudo apt-key del 7fa2af80
[sudo] password for donkeycar01:
OK
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
--2022-12-30 16:09:54-- 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.39.144
Connecting to developer.download.nvidia.com (developer.download.nvidia.com)|152.199.39.144|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://developer.download.nvidia.cn/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin [following]
--2022-12-30 16:09:55-- https://developer.download.nvidia.cn/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
Resolving developer.download.nvidia.cn (developer.download.nvidia.cn)... 36.153.62.130, 111.2.178.66, 111.2.178.68, ...
Connecting to developer.download.nvidia.cn (developer.download.nvidia.cn)|36.153.62.130|: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%[&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;>] 190 --.-KB/s in 0s
2022-12-30 16:09:55 (27.0 MB/s) - ‘cuda-wsl-ubuntu.pin’ saved [190/190]
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/3bf863cc.pub
Executing: /tmp/apt-key-gpghome.XjsnF3HvcZ/gpg.1.sh --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/3bf863cc.pub
gpg: requesting key from &#39;https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/3bf863cc.pub&#39;
gpg: key A4B469963BF863CC: public key "cudatools" imported
gpg: Total number processed: 1
gpg: imported: 1
sudo add-apt-repository &#39;deb https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/ /&#39;
Hit:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal InRelease
Get:2 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates InRelease [114 kB]
Get:4 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-backports InRelease [108 kB]
Get:3 https://developer.download.nvidia.cn/compute/cuda/repos/wsl-ubuntu/x86_64 InRelease [1581 B]
Get:5 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security InRelease [114 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:7 http://archive.ubuntu.com/ubuntu focal InRelease
Get:8 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:9 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 DEP-11 Metadata [275 kB]
Get:10 https://developer.download.nvidia.cn/compute/cuda/repos/wsl-ubuntu/x86_64 Packages [243 kB]
Get:11 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/universe amd64 DEP-11 Metadata [407 kB]
Get:12 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [940 B]
Get:13 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-backports/main amd64 DEP-11 Metadata [7992 B]
Get:14 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-backports/universe amd64 DEP-11 Metadata [30.5 kB]
Get:15 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security/main amd64 DEP-11 Metadata [40.7 kB]
Get:16 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security/universe amd64 DEP-11 Metadata [94.1 kB]
Get:17 https://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security/multiverse amd64 DEP-11 Metadata [940 B]
Get:18 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [40.7 kB]
Get:19 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:20 http://security.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [94.3 kB]
Get:21 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 DEP-11 Metadata [940 B]
Get:22 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [274 kB]
Get:23 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [407 kB]
Get:24 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [944 B]
Get:25 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 DEP-11 Metadata [7964 B]
Get:26 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [30.5 kB]
Fetched 2631 kB in 5s (507 kB/s)
Reading package lists... Done
sudo apt-get update
sudo apt-get -y install cuda
【案&#xff0c;讲真的&#xff0c;NVIDIA的官网速度真的快啊】
donkeycar01&#64;DESKTOP-M4INDEG:~$ sudo apt-get -y install cuda
Reading package lists... Done
Building dependency tree
Reading state information... Done
cuda is already the newest version (12.0.0-1).
The following packages were automatically installed and are no longer required:
libfprint-2-tod1 libfwupdplugin1 libxmlb1
Use &#39;sudo apt autoremove&#39; to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
NVidia 在 GitHub 上提供了一个开源存储库&#xff0c;其中包含示例&#xff0c;供 CUDA 开发人员探索 CUDA 工具包中可用的功能。构建其中一个是测试 CUDA 安装的好方法。让我们选择最简单的一个来验证我们的安装是否有效。在cuda-samples repository:下载这些案例&#xff0c;然后&#xff0c;找一个编一下看看
新建一个你的工作目录&#xff1a;
donkeycar01&#64;DESKTOP-M4INDEG:~$ mkdir CUDA_Sample
donkeycar01&#64;DESKTOP-M4INDEG:~$ cd CUDA_Sample/
git clone https://github.com/NVIDIA/cuda-samples.git
cd ~/CUDA_Sample/cuda-samples/Samples/1_Utilities/deviceQuery
make
/usr/local/cuda/bin/nvcc -ccbin g&#43;&#43; -I../../../Common -m64 --threads 0 --std&#61;c&#43;&#43;11 -gencode arch&#61;compute_50,code&#61;sm_50 -gencode arch&#61;compute_52,code&#61;sm_52 -gencode arch&#61;compute_60,code&#61;sm_60 -gencode arch&#61;compute_61,code&#61;sm_61 -gencode arch&#61;compute_70,code&#61;sm_70 -gencode arch&#61;compute_75,code&#61;sm_75 -gencode arch&#61;compute_80,code&#61;sm_80 -gencode arch&#61;compute_86,code&#61;sm_86 -gencode arch&#61;compute_90,code&#61;sm_90 -gencode arch&#61;compute_90,code&#61;compute_90 -o deviceQuery.o -c deviceQuery.cpp
/usr/local/cuda/bin/nvcc -ccbin g&#43;&#43; -m64 -gencode arch&#61;compute_50,code&#61;sm_50 -gencode arch&#61;compute_52,code&#61;sm_52 -gencode arch&#61;compute_60,code&#61;sm_60 -gencode arch&#61;compute_61,code&#61;sm_61 -gencode arch&#61;compute_70,code&#61;sm_70 -gencode arch&#61;compute_75,code&#61;sm_75 -gencode arch&#61;compute_80,code&#61;sm_80 -gencode arch&#61;compute_86,code&#61;sm_86 -gencode arch&#61;compute_90,code&#61;sm_90 -gencode arch&#61;compute_90,code&#61;compute_90 -o deviceQuery deviceQuery.o
mkdir -p ../../../bin/x86_64/linux/release
cp deviceQuery ../../../bin/x86_64/linux/release
然后&#xff0c;关键时刻到了&#xff1a;用query程序查看你的硬件配置&#xff1a;如果能顺利查看&#xff0c;说明&#xff0c;你的CUDA配置成功了。
./deviceQuery
./deviceQuery Starting...
CUDA Device Query (Runtime API) version (CUDART static linking)
Detected 1 CUDA Capable device(s)
Device 0: "NVIDIA GeForce GTX 1050 Ti"
CUDA Driver Version / Runtime Version 12.0 / 12.0
CUDA Capability Major/Minor version number: 6.1
Total amount of global memory: 2048 MBytes (2147352576 bytes)
(006) Multiprocessors, (128) CUDA Cores/MP: 768 CUDA Cores
GPU Max Clock rate: 1620 MHz (1.62 GHz)
Memory Clock rate: 3504 Mhz
Memory Bus Width: 128-bit
L2 Cache Size: 1048576 bytes
Maximum Texture Dimension Size (x,y,z) 1D&#61;(131072), 2D&#61;(131072, 65536), 3D&#61;(16384, 16384, 16384)
Maximum Layered 1D Texture Size, (num) layers 1D&#61;(32768), 2048 layers
Maximum Layered 2D Texture Size, (num) layers 2D&#61;(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: 98304 bytes
Total number of registers available per block: 65536
Warp size: 32
Maximum number of threads per multiprocessor: 2048
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 / 2 / 0
Compute Mode:
deviceQuery, CUDA Driver &#61; CUDART, CUDA Driver Version &#61; 12.0, CUDA Runtime Version &#61; 12.0, NumDevs &#61; 1
Result &#61; PASS
Enabling GPU acceleration on Ubuntu on WSL2 with the NVIDIA CUDA Platform | Ubuntu
Run Linux GUI apps with WSL | Microsoft Learn
CUDA on WSL User Guide (nvidia.com)