VScode编译调试debug,gpu的cuda程序,Nsight

进行下面操作的前提是,我们的环境已经能跑简单的CUDA程序了。

一、安装Nsight

二、创建launch.json文件

bash 复制代码
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "CUDA C++: Launch",
            "type": "cuda-gdb",
            "request": "launch",
            "program": "${fileDirname}/main",
            "preLaunchTask": "mynvcc",
            "args": ["1024"]  // 示例:传递向量大小作为参数
        }
    ]
}

三、创建task.json文件

bash 复制代码
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "mynvcc",
            "type": "shell",
            "command": "nvcc",
            "args": [
                "-g",
                "-G",
                "-o",
                "${fileDirname}/main",
                "${file}",
                "-I", "/usr/local/cuda/include",
                "-L", "/usr/local/cuda/lib64",
                "-l", "cudart",
                "-D_MWAITXINTRIN_H_INCLUDED"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": ["$gcc"]
        }
    ]
}

四、创建main.cu

注意:名称一定是main.cu,和上面的json文件中的main对应。

cpp 复制代码
#include <cuda.h> 
#include <iostream> 
#include <vector> 
using namespace std;

// Add A and B vector on the GPU. Results stored into C
__global__
void addKernel(int n, float* A, float* B, float* C)
{
  int i = blockIdx.x*blockDim.x + threadIdx.x;
  if(i < n) C[i] = A[i] + B[i];
}

// Add A and B vector. Results stored into C
int add(int n, float* h_A, float* h_B, float* h_C)
{
  int size = n*sizeof(float);

  // Allocate memory on device and copy data
  float* d_A;
  cudaMalloc((void**)&d_A, size);
  cudaMemcpy(d_A, h_A, size, cudaMemcpyHostToDevice);

  float* d_B;
  cudaMalloc((void**)&d_B, size);
  cudaMemcpy(d_B, h_B, size, cudaMemcpyHostToDevice);

  float* d_C;
  cudaMalloc((void**)&d_C, size);

  // launch Kernel
  cout << "Running 256 threads on " << ceil(n/256.0f) << " blocks -> " << 256*ceil(n/256.0f) << endl;
  addKernel<<<ceil(n/256.0f),256>>>(n, d_A, d_B, d_C);

  // Transfer results back to host
  cudaMemcpy(h_C, d_C, size, cudaMemcpyDeviceToHost);

  // Free device memory
  cudaFree(d_A);
  cudaFree(d_B);
  cudaFree(d_C);

  return 0;
}

// C = A + B on a GPU, where A is a vector of 1.0f and B a vector of 2.0f
// The main function takes one argument, the size of the vectors
int main(int argc, char* argv[])
{
  int n = atoi(argv[1]);

  vector<float> h_A(n, 1.0f);
  vector<float> h_B(n, 2.0f);
  vector<float> h_C(n);

  add(n, h_A.data(), h_B.data(), h_C.data());

  for(auto& c : h_C) {
    if(fabs(c-3.0f) > 0.00001f) {
      cout << "Error!" << endl;
      return 1;
    }
  }

  cout << "The program completed successfully" << endl;

  return 0;
}

五、编译main.cu

bash 复制代码
nvcc -g -G -o main main.cu

六、开始调试

进入main.cu文件,打上断点,按F5,开始debug调试。

按F5后可能会出现警告,点击 无论如何继续,能调试就行,先别管乱起八糟的。

相关推荐
witton2 天前
MacOS中在目录或者文件右键菜单中添加“使用VSCode打开”
vscode·macos·文件·目录·服务·automator·右键菜单
码上暴富2 天前
Cursor / VS Code 自定义文件颜色
前端·vscode
2601_962381582 天前
VSCode如何配置LlamaIndex RAG(检索增强生成)应用开发环境
vscode·开发环境·rag·llamaindex·检索增强生成
北漂燕郊杨哥3 天前
VS Code 安装微信小程序 MCP 介绍
vscode·微信小程序·小程序·mcp
YZJenny3 天前
在服务器上安装使用code-server(Web 版 VS Code)
ide·vscode·编辑器
Eloudy3 天前
Isaac ROS 3.2 编译
gpu
Eloudy3 天前
GXF 报告 - 构建依赖分析,NvSci 裁剪,公网裸机构建与测试
gpu
salestina3 天前
vscode迁移扩展目录
ide·vscode·编辑器
sunoo-2294 天前
【Linux 系统编程】学习第七天:线程属性 | 互斥锁 | 死锁 | 信号量 核心知识点 + 踩坑实战
linux·c语言·笔记·vscode·学习
铁手飞鹰4 天前
VSCode Python .embed 嵌入式环境黄色波浪线问题
ide·vscode·python