CUDA 学习

1 篇文章 0 订阅

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/
在这里插入图片描述

  1. 把nvcc添加到环境变量。打开~/.bashrc,添加环境变量export PATH=$PATH:/usr/local/cuda/bin 保存然后source ~/.bashrc。
  2. 输入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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值