最简单的方式:如何在wsl2上配置CDUA开发环境
step0:序言这篇文章可以帮助你以一个最为简单的方式迈出CUDA的第一步,从此一入CUDA深似海,从此头发是路人。
前提:你需要在Windows 11上:
[*]电脑中有nvidia显卡以及驱动,由于wsl cuda不支持maxwell gpu架构,所以需要10系以上的显卡
[*]安装wsl2,配置ubuntu镜像并确定能够进入。
此处使用:
[*]操作系统镜像为:ubuntu 22.04.3 LTS
[*]安装cuda版本:12.8.1
注意,在wsl上配置cuda与在普通Linux上配置有程序上的不同,务必注意!
step1:前期准备
由于此处使用的ubuntu 22.04.x版本,我们需要做一些前期配置
可以从这个链接中查询cuda 12.8.1和操作系统,内核,gcc,glibc版本的对应关系:https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html
理论上来说其他版本也不一定不支持,但是出于少给自己找麻烦的需求,按照官方教程更新版本
确认操作系统和工具链版本:
lsb_release -a # 确认操作系统版本gcc -v # 确认gcc版本cmake --version # 确认cmake版本ldd --version # 确认glibc版本安装gcc12.3.0:
sudo apt install gcc-12 g++-12 -ysudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 100sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-12 100gcc --version # 确认gcc版本安装cmake(截至本文撰写cmake latest version为3.22.1):
sudo apt install cmakecmake --version至此,先期工具链已经配置完成。
step2:安装CUDA toolkit
CUDA toolkit是开发过程中必备的工具链,本阶段我们需要安装并配置。
参考官方教程:
[*]https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0&target_type=deb_local
[*]https://docs.nvidia.com/cuda/wsl-user-guide/index.html#getting-started-with-cuda-on-wsl
既有的运行用的CUDA和编译开发用的CUDA toolkit完全是两个东西,不要搞混。
严格按照以下代码执行,顺序不要改变:
sudo apt-key del 7fa2af80wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pinsudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600wget https://developer.download.nvidia.com/compute/cuda/12.8.1/local_installers/cuda-repo-wsl-ubuntu-12-8-local_12.8.1-1_amd64.debsudo dpkg -i cuda-repo-wsl-ubuntu-12-8-local_12.8.1-1_amd64.debsudo cp /var/cuda-repo-wsl-ubuntu-12-8-local/cuda-*-keyring.gpg /usr/share/keyrings/sudo apt-get updatesudo apt-get -y install cuda-toolkit-12-8sudo apt-get install cudastep3:尝试编译并运行sample项目
nvidia官方提供了一个sample项目,用于初学者快速上手CUDA:
执行如下代码,从github中clone并构建
git clone https://github.com/NVIDIA/cuda-samples.gitcd cuda-samples/mkdir build && cd buildexport PATH=/usr/local/cuda-12.8/bin/:$PATHcmake ..也可以进入某一个具体项目路径下编译并运行,此处以vectorAdd为例:
cd Samples/0_Introduction/vectorAddcmake .make./vectorAdd如果一切顺利,你将会看到如下输出:
~/cuda-samples/Samples/0_Introduction/vectorAdd$ ./vectorAdd Copy input data from the host memory to the CUDA deviceCUDA kernel launch with 196 blocks of 256 threadsCopy output data from the CUDA device to the host memoryTest PASSEDDone至此,已经完成了所有配置,你应该能够在自己的wsl中自由编译并运行CUDA程序了。
下一期我们开始正式进入CUDA的世界,从CUDA kernel开始,压榨GPU的每一寸性能,在这个过程中你会深切感受到算力的强大。
以上,感谢阅读,如果你认为这篇文章有帮助,关注雪豹喵,关注雪豹谢谢喵。
页:
[1]