CUDA(九) 周斌 CUDA程序分析和调试工具

Linux

$ nsight

ncvv编译器

单卡模式(一个GPU)

双卡模式(两个GPU,1个显示,1个调试)

远程调试(连接远程服务器)

查看硬件配置:$ lspci | grep -i nvidia

显示设置放在GPU,专业调试放在另一个GPU

$ nvidia-smi系统管理接口

1、启动Nsight

2、新建project

3、选择平台CUDA的计算能力是不一样的

4、release和debug的选项

5、添加sorce file,自动生成模板


#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>

cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);

__global__ void addKernel(int *c, const int *a, const int *b)
{
    int i = threadIdx.x;
	int m, n;
	m = 0;
	n = 1;
    c[i] = m+n+a[i] + b[i];
}

int main()
{
    const int arraySize = 5;
    const int a[arraySize] = { 1, 2, 3, 4, 5 };
    const int b[arraySize] = { 10, 20, 30, 40, 50 };
    int c[arraySize] = { 0 };

    // Add vectors in parallel.
    cudaError_t cudaStatus = addWithCuda(c, a, b, arraySize);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "addWithCuda failed!");
        return 1;
    }

    printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
        c[0], c[1], c[2], c[3], c[4]);

    // cudaDeviceReset must be called before exiting in order for profiling and
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaStatus = cudaDeviceReset();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceReset failed!");
        return 1;
    }

    return 0;
}

// Helper function for using CUDA to add vectors in parallel.
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size)
{
    int *dev_a = 0;
    int *dev_b = 0;
    int *dev_c = 0;
    cudaError_t cudaStatus;

    // Choose which GPU to run on, change this on a multi-GPU system.
    cudaStatus = cudaSetDevice(0);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
        goto Error;
    }

    // Allocate GPU buffers for three vectors (two input, one output)    .
    cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_a, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    // Copy input vectors from host memory to GPU buffers.
    cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    cudaStatus = cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    // Launch a kernel on the GPU with one thread for each element.
    addKernel<<<1, size>>>(dev_c, dev_a, dev_b);

    // Check for any errors launching the kernel
    cudaStatus = cudaGetLastError();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
        goto Error;
    }
    
    // cudaDeviceSynchronize waits for the kernel to finish, and returns
    // any errors encountered during the launch.
    cudaStatus = cudaDeviceSynchronize();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
        goto Error;
    }

    // Copy output vector from GPU buffer to host memory.
    cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

Error:
    cudaFree(dev_c);
    cudaFree(dev_a);
    cudaFree(dev_b);
    
    return cudaStatus;
}

6、编译

7、配置

8、执行

利用Nsight跟踪程序

1、配置debug

2、启动debug

3、双击打断点

直接进行系统性能分析。

profiler,会得到性能指标,占用率;

使用命令行CUDA GDB调试

1、编译生成a.out:$ nvcc -g -G xx.cu

2、执行: ./a.out

3、代码调试:cuda-gdb a.out

4、查看代码(cuda-gdb) l

5、cuda-gdb的差异:(cuda-gdb) help cuda

6、14行打断点: (cuda-gdb) b 14

7、 直接执行:(cuda-gdb) r

8、观察其他变量:(cuda-gdb) p x

9、执行下一步:(cuda-gdb) n

10、查看另一个线程:(cuda-gdb) cuda thread (2,0,0)

11、执行完剩余代码:(cuda-gdb) c

12、退出:(cuda-gdb) q

13、清屏 : $ clear

如果只有一块GPU的卡,使用nsight有点困难,同时做显示和调试有些冲突,一个GPU推荐使用CUDA-GDB使用命令行,有一些要求就是要停掉占用GPU做图形显示的

$ /etc/init.d/lightdm stop

如果使用远程服务器的GPU,使用SSH登陆上去做服务器调试

$ ssh [email protected]

即使用nsight又使用远程调试

不是很方便

硬件需求:远程服务器+远程cuda-gdbserver+账户密码+网络环境

1、建立cuda工程

2、设置PTX和GPU code版本

3、配置release和debug

4、新建源代码

5、编译

6、执行 (本机上没有CUDA环境)

7、配置服务器环境:Run-->Debug Remote Application-->Debug CUDA Application-->Debug an application on a remote system-->Upload local executable--》remote connection ->new-->ssh only--> host name;connection name; description-->connect-->user ID ;Password 

     a)把本地编译的可执行文件上传到服务器:Upload local executable

     b) 如果在服务器已经有了编译好的可执行文件:Debug remote executable

     c) 不上传,只是在本地执行

7、设置远程服务器

8、选择cuda-gdbserver的路径(默认),远程目录环境,

猜你喜欢

转载自blog.csdn.net/fanre/article/details/83181287