【WSL】[03] windows subsytem linux 配置和使用 - 在ubuntu赋能NVIDIA CUDA的GPU加速功能

前言:

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的,具体参照】


 1 安装支持WSL的GPU驱动:(WSL,显然需要的是windows版本)

  • You will need to be on Windows 10 Build 19044+ or Windows 11 to access this feature.

1.1 Nvidia

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的驱动

 1.2 intel 

谁说intel的GPU就不能用呢?Intel® Graphics – Windows* DCH Drivers


2 在ubuntu中安装CUDA Toolkit

【案,现在准备再WSL的ubuntu中装CUDA了,注意,安装要按照Ubuntu的官网来搞,笔者,是按照ubuntu的WSL配置官网搞的】

以下命令将在 Ubuntu 22.04 AMD64 体系结构上安装 WSL 特定的 CUDA 工具包版本 11.6。

请注意,旧版本的 CUDA (<=10) 不支持 WSL 2。

另请注意,尝试直接从 Ubuntu 存储库(“cuda”、“cuda-11-0”或“cuda-drivers”)安装 CUDA 工具包包将尝试安装 Linux NVIDIA 图形驱动程序,这不是您在 WSL 2 上想要的。

因此,首先删除旧的GPG密钥:

2.1 删除GPG key

donkeycar01@DESKTOP-M4INDEG:~$ sudo apt-key del 7fa2af80
[sudo] password for donkeycar01:
OK

 2.2 按照以下步骤,逐一设置正确的,WSL的安装包

2.2.1 

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%[=================================================>]     190  --.-KB/s    in 0s

2022-12-30 16:09:55 (27.0 MB/s) - ‘cuda-wsl-ubuntu.pin’ saved [190/190]

2.2.2

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 'https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/3bf863cc.pub'
gpg: key A4B469963BF863CC: public key "cudatools <[email protected]>" imported
gpg: Total number processed: 1
gpg:               imported: 1

 2.2.3

sudo add-apt-repository 'deb https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/ /'

 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

2.3.4

sudo apt-get update
sudo apt-get -y install cuda

 

 【案,讲真的,NVIDIA的官网速度真的快啊】

donkeycar01@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 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.


3 用CUDA开始搞事情: 

 3.1 做一个编译:

NVidia 在 GitHub 上提供了一个开源存储库,其中包含示例,供 CUDA 开发人员探索 CUDA 工具包中可用的功能。构建其中一个是测试 CUDA 安装的好方法。让我们选择最简单的一个来验证我们的安装是否有效。在cuda-samples repository:下载这些案例,然后,找一个编一下看看

新建一个你的工作目录:

donkeycar01@DESKTOP-M4INDEG:~$ mkdir CUDA_Sample
donkeycar01@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++ -I../../../Common  -m64    --threads 0 --std=c++11 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 -o deviceQuery.o -c deviceQuery.cpp
/usr/local/cuda/bin/nvcc -ccbin g++   -m64      -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90 -o deviceQuery deviceQuery.o
mkdir -p ../../../bin/x86_64/linux/release
cp deviceQuery ../../../bin/x86_64/linux/release

 然后,关键时刻到了:用query程序查看你的硬件配置:如果能顺利查看,说明,你的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=(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:        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:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 12.0, CUDA Runtime Version = 12.0, NumDevs = 1
Result = 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)

猜你喜欢

转载自blog.csdn.net/yellow_hill/article/details/128495763