热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【WSL】[03]windowssubsytemlinux配置和使用在ubuntu赋能NVIDIACUDA的GPU加速功能

前言:WSLVSVM,在NVIDIA的管网已经有了明确的说明:既然WSL的默认设置为在不离开Windows的情况下开发跨平台应用程序&#


前言:

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。


请注意&#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;



2.1 删除GPG key

donkeycar01&#64;DESKTOP-M4INDEG:~$ sudo apt-key del 7fa2af80
[sudo] password for donkeycar01:
OK

 2.2 按照以下步骤&#xff0c;逐一设置正确的&#xff0c;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%[&#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]



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 &#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



 2.2.3

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



2.3.4

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.





3 用CUDA开始搞事情&#xff1a; 


 3.1 做一个编译&#xff1a;



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






参考&#xff1a;

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)





推荐阅读
  • 本文介绍了一种轻巧方便的工具——集算器,通过使用集算器可以将文本日志变成结构化数据,然后可以使用SQL式查询。集算器利用集算语言的优点,将日志内容结构化为数据表结构,SPL支持直接对结构化的文件进行SQL查询,不再需要安装配置第三方数据库软件。本文还详细介绍了具体的实施过程。 ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Webmin远程命令执行漏洞复现及防护方法
    本文介绍了Webmin远程命令执行漏洞CVE-2019-15107的漏洞详情和复现方法,同时提供了防护方法。漏洞存在于Webmin的找回密码页面中,攻击者无需权限即可注入命令并执行任意系统命令。文章还提供了相关参考链接和搭建靶场的步骤。此外,还指出了参考链接中的数据包不准确的问题,并解释了漏洞触发的条件。最后,给出了防护方法以避免受到该漏洞的攻击。 ... [详细]
  • 本文介绍了三种方法来实现在Win7系统中显示桌面的快捷方式,包括使用任务栏快速启动栏、运行命令和自己创建快捷方式的方法。具体操作步骤详细说明,并提供了保存图标的路径,方便以后使用。 ... [详细]
  • 基于dlib的人脸68特征点提取(眨眼张嘴检测)python版本
    文章目录引言开发环境和库流程设计张嘴和闭眼的检测引言(1)利用Dlib官方训练好的模型“shape_predictor_68_face_landmarks.dat”进行68个点标定 ... [详细]
  • RouterOS 5.16软路由安装图解教程
    本文介绍了如何安装RouterOS 5.16软路由系统,包括系统要求、安装步骤和登录方式。同时提供了详细的图解教程,方便读者进行操作。 ... [详细]
  • CEPH LIO iSCSI Gateway及其使用参考文档
    本文介绍了CEPH LIO iSCSI Gateway以及使用该网关的参考文档,包括Ceph Block Device、CEPH ISCSI GATEWAY、USING AN ISCSI GATEWAY等。同时提供了多个参考链接,详细介绍了CEPH LIO iSCSI Gateway的配置和使用方法。 ... [详细]
  • 嵌入式处理器的架构与内核发展历程
    本文主要介绍了嵌入式处理器的架构与内核发展历程,包括不同架构的指令集的变化,以及内核的流水线和结构。通过对ARM架构的分析,可以更好地理解嵌入式处理器的架构与内核的关系。 ... [详细]
  • Vagrant虚拟化工具的安装和使用教程
    本文介绍了Vagrant虚拟化工具的安装和使用教程。首先介绍了安装virtualBox和Vagrant的步骤。然后详细说明了Vagrant的安装和使用方法,包括如何检查安装是否成功。最后介绍了下载虚拟机镜像的步骤,以及Vagrant镜像网站的相关信息。 ... [详细]
  • 微软评估和规划(MAP)的工具包介绍及应用实验手册
    本文介绍了微软评估和规划(MAP)的工具包,该工具包是一个无代理工具,旨在简化和精简通过网络范围内的自动发现和评估IT基础设施在多个方案规划进程。工具包支持库存和使用用于SQL Server和Windows Server迁移评估,以及评估服务器的信息最广泛使用微软的技术。此外,工具包还提供了服务器虚拟化方案,以帮助识别未被充分利用的资源和硬件需要成功巩固服务器使用微软的Hyper - V技术规格。 ... [详细]
  • 本文介绍了在Ubuntu 11.10 x64环境下安装Android开发环境的步骤,并提供了解决常见问题的方法。其中包括安装Eclipse的ADT插件、解决缺少GEF插件的问题以及解决无法找到'userdata.img'文件的问题。此外,还提供了相关插件和系统镜像的下载链接。 ... [详细]
  • PeopleSoft安装镜像版本及导入语言包的方法
    本文介绍了PeopleSoft安装镜像的两个版本,分别是VirtualBox虚拟机版本和NativeOS版本,并详细说明了导入语言包的方法。对于Windows版本,可以通过psdmt.exe登录进入,并使用datamover脚本导入语言包。对于Linux版本,同样可以使用命令行方式执行datamover脚本导入语言包。导入语言包后,可以实现多种语言的登录。参考文献提供了相关链接以供深入了解。 ... [详细]
  • 本文介绍了使用readlink命令获取文件的完整路径的简单方法,并提供了一个示例命令来打印文件的完整路径。共有28种解决方案可供选择。 ... [详细]
author-avatar
c_陈可儿
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有