WSL2 安装cuda
CUDA 安装
进去这个nvidia的网站 https://developer.nvidia.com/cuda-downloads 有相关的安装流程
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/12.1.0/local_installers/cuda-repo-wsl-ubuntu-12-1-local_12.1.0-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-12-1-local_12.1.0-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-12-1-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda
没有nvcc
现在运行nvcc --version 会发现没有nvcc
按照上面的装完,默认装在/usr/local/
- 把nvcc添加到环境变量。打开~/.bashrc,添加环境变量
export PATH=$PATH:/usr/local/cuda/bin
保存然后source ~/.bashrc。 - 输入nvcc --version可以看到版本信息
运行nvidia-smi看GPU 信息
编写hello world 测试
#include <iostream>
#include <math.h>
#include <cuda_runtime.h>
using namespace std;
int main() {
int count = 0;
cudaGetDeviceCount(&count);
cout <<"当前计算机包含GPU数为"<< count << endl;
cudaError_t err = cudaGetDeviceCount(&count);
if (err != cudaSuccess)
printf("%s\n", cudaGetErrorString(err));
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 0);
printf("Device Number: %d\n", 0);
cout << "当前设备名字为" << prop.name << endl;
cout << "GPU全局内存总量为" << prop.totalGlobalMem << endl;
cout << "单个线程块中包含的线程数最多为" << prop.maxThreadsPerBlock << endl;
}
vscode + WSL2 debug CUDA
c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/local/bin/nsys",
"cStandard": "c17",
"cppStandard": "c++14",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/test.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath":"/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "CUDA: nvcc 生成活动文件",
"type": "shell",
"command": "nvcc",
"args": [
"-g",
"-o",
"${workspaceFolder}/test.out",
"hello.cu"
],
"problemMatcher": [
"$nvcc"
]
}
]
}
运行task之后 会生成可执行的文件
#include <stdio.h>
// 定义一个CUDA kernel函数,使用__global__关键字修饰
__global__ void hello_from_gpu() {
// 在GPU上输出一句话
printf("Hello world from the GPU!\n");
}
// 主函数
int main() {
// 启动一个block,包含一个thread
hello_from_gpu<<<1, 1>>>();
// 等待GPU执行完所有的任务
cudaDeviceSynchronize();
return 0;
}
#include <stdio.h>
// 定义一个CUDA kernel函数,使用__global__关键字修饰
__global__ void hello_from_gpu() {
// 在GPU上输出一句话
printf("Hello world from the GPU!\n");
}
// 主函数
int main() {
// 启动2个块,每个块有4个线程
hello_from_gpu<<<2, 4>>>();
// 等待GPU执行完所有的任务
cudaDeviceSynchronize();
return 0;
}