3.2.cuda运行时API-第一个运行时程序,hello-cuda

前言

杜老师推出的 tensorRT从零起步高性能部署 课程,之前有看过一遍,但是没有做笔记,很多东西也忘了。这次重新撸一遍,顺便记记笔记。

本次课程学习精简 CUDA 教程-第一个运行时程序,hello-cuda

课程大纲可看下面的思维导图

在这里插入图片描述

1. hello-cuda

我们来开始写第一个 runtime API 的程序

根据 Driver API 的错误代码处理,同样我们也提出了 runtime API 的错误处理

#define checkRuntime(op)  __check_cuda_runtime((op), #op, __FILE__, __LINE__)

bool __check_cuda_runtime(cudaError_t code, const char* op, const char* file, int line){
    
    
    if(code != cudaSuccess){
    
        
        const char* err_name = cudaGetErrorName(code);    
        const char* err_message = cudaGetErrorString(code);  
        printf("runtime error %s:%d  %s failed. \n  code = %s, message = %s\n", file, line, op, err_name, err_message);   
        return false;
    }
    return true;
}

第一个完整的调用 runtime API 的程序示例代码如下:


// CUDA运行时头文件
#include <cuda_runtime.h>

// CUDA驱动头文件
#include <cuda.h>
#include <stdio.h>
#include <string.h>

#define checkRuntime(op)  __check_cuda_runtime((op), #op, __FILE__, __LINE__)

bool __check_cuda_runtime(cudaError_t code, const char* op, const char* file, int line){
    
    
    if(code != cudaSuccess){
    
        
        const char* err_name = cudaGetErrorName(code);    
        const char* err_message = cudaGetErrorString(code);  
        printf("runtime error %s:%d  %s failed. \n  code = %s, message = %s\n", file, line, op, err_name, err_message);   
        return false;
    }
    return true;
}

int main(){
    
    

    CUcontext context = nullptr;
    cuCtxGetCurrent(&context);
    printf("Current context = %p,当前无context\n", context);

    // cuda runtime是以cuda为基准开发的运行时库
    // cuda runtime所使用的CUcontext是基于cuDevicePrimaryCtxRetain函数获取的
    // 即,cuDevicePrimaryCtxRetain会为每个设备关联一个context,通过cuDevicePrimaryCtxRetain函数可以获取到
    // 而context初始化的时机是懒加载模式,即当你调用一个runtime api时,会触发创建动作
    // 也因此,避免了cu驱动级别的init和destroy操作。使得api的调用更加容易
    int device_count = 0;
    checkRuntime(cudaGetDeviceCount(&device_count));
    printf("device_count = %d\n", device_count);

    // 取而代之,是使用setdevice来控制当前上下文,当你要使用不同设备时
    // 使用不同的device id
    // 注意,context是线程内作用的,其他线程不相关的, 一个线程一个context stack
    int device_id = 0;
    printf("set current device to : %d,这个API依赖CUcontext,触发创建并设置\n", device_id);
    checkRuntime(cudaSetDevice(device_id));

    // 注意,是由于set device函数是“第一个执行的需要context的函数”,所以他会执行cuDevicePrimaryCtxRetain
    // 并设置当前context,这一切都是默认执行的。注意:cudaGetDeviceCount是一个不需要context的函数
    // 你可以认为绝大部分runtime api都是需要context的,所以第一个执行的cuda runtime函数,会创建context并设置上下文
    cuCtxGetCurrent(&context);
    printf("SetDevice after, Current context = %p,获取当前context\n", context);

    int current_device = 0;
    checkRuntime(cudaGetDevice(&current_device));
    printf("current_device = %d\n", current_device);
    return 0;
}

运行效果如下:

在这里插入图片描述

图1-1 第一个运行时程序

上述代码展示了使用 CUDA Runtime API 进行 device 和 context 的管理操作。通过调用相应的 API 函数,可以获取设备信息、设置当前设备和获取当前上下文等操作,而无需手动进行驱动初始化和销毁等操作。这样简化了代码,使得使用 CUDA Runtime API 更加方便和易于操作。

对于本节的知识点,你需要掌握:(form 杜老师)

  1. CUDA Runtime 是封装了 CUDA Driver 的高级别更友好的 API
  2. cudaruntime 需要引入 cudart 这个 so 文件
  3. 上下文管理:使用 cuDevicePrimaryCtxRetain 为每个设备设置 context,不再手工管理 context,并且不提供直接管理 context 的 API;任何依赖 CUcontext 的 API 被调用时,会触发 CUcontext 的创建和对设备的绑定;此后任何 API 调用时,会以设备 id 为基准,调取绑定好的 CUcontext;因此被称为懒加载模式,避免了手动维护 CUContext 的麻烦
  4. cuda 的状态返回值,都是 cudaError_t 类型,通过 check 宏捕获状态并处理是一种通用方式。官方案例采用宏,而非这里的函数加宏,函数加宏具有更加好的便利性

CUDA的在线文档地址:

https://developer.nvidia.com/cuda-toolkit-archive

总结

本次课程演示了 runtime API 的使用,当有需要 context 的 API 调用时,runtime API 会自动创建管理 context 无需我们手动操作管理,同时我们也在 runtime API 中添加了和 driver API 一样的错误处理,方便我们调试和排查问题。
text 的 API 调用时,runtime API 会自动创建管理 context 无需我们手动操作管理,同时我们也在 runtime API 中添加了和 driver API 一样的错误处理,方便我们调试和排查问题。

猜你喜欢

转载自blog.csdn.net/qq_40672115/article/details/131606151