​CUDA学习笔记(四)device管理

news/2024/7/3 1:44:12

本篇博文转载于https://www.cnblogs.com/1024incn/tag/CUDA/,仅用于学习。

device管理

NVIDIA提供了集中凡是来查询和管理GPU device,掌握GPU信息查询很重要,因为这可以帮助你设置kernel的执行配置。

本博文将主要介绍下面两方面内容:

  • CUDA runtime API function
  • NVIDIA系统管理命令行

使用runtime API来查询GPU信息

你可以使用下面的function来查询所有关于GPU device 的信息:

cudaError_t cudaGetDeviceProperties(cudaDeviceProp *prop, int device);

GPU的信息放在cudaDeviceProp这个结构体中。

代码

#include <cuda_runtime.h>#include <stdio.h>int main(int argc, char **argv) {      printf("%s Starting...\n", argv[0]);
    int deviceCount = 0;
    cudaError_t error_id = cudaGetDeviceCount(&deviceCount);
    if (error_id != cudaSuccess) {
        printf("cudaGetDeviceCount returned %d\n-> %s\n",
        (int)error_id, cudaGetErrorString(error_id));
        printf("Result = FAIL\n");
        exit(EXIT_FAILURE);
    }
    if (deviceCount == 0) {
        printf("There are no available device(s) that support CUDA\n");
    } else {
        printf("Detected %d CUDA Capable device(s)\n", deviceCount);
    }
    int dev, driverVersion = 0, runtimeVersion = 0;
    dev =0;
    cudaSetDevice(dev);
    cudaDeviceProp deviceProp;
    cudaGetDeviceProperties(&deviceProp, dev);
    printf("Device %d: \"%s\"\n", dev, deviceProp.name);
    cudaDriverGetVersion(&driverVersion);
    cudaRuntimeGetVersion(&runtimeVersion);
    printf(" CUDA Driver Version / Runtime Version %d.%d / %d.%d\n",driverVersion/1000, (driverVersion%100)/10,runtimeVersion/1000, (runtimeVersion%100)/10);
    printf(" CUDA Capability Major/Minor version number: %d.%d\n",deviceProp.major, deviceProp.minor);
    printf(" Total amount of global memory: %.2f MBytes (%llu bytes)\n",(float)deviceProp.totalGlobalMem/(pow(1024.0,3)),(unsigned long long) deviceProp.totalGlobalMem);
    printf(" GPU Clock rate: %.0f MHz (%0.2f GHz)\n",deviceProp.clockRate * 1e-3f, deviceProp.clockRate * 1e-6f);
    printf(" Memory Clock rate: %.0f Mhz\n",deviceProp.memoryClockRate * 1e-3f);
    printf(" Memory Bus Width: %d-bit\n",deviceProp.memoryBusWidth);
    if (deviceProp.l2CacheSize) {
        printf(" L2 Cache Size: %d bytes\n",
        deviceProp.l2CacheSize);
    }
    printf(" Max Texture Dimension Size (x,y,z) 1D=(%d), 2D=(%d,%d), 3D=(%d,%d,%d)\n",
    deviceProp.maxTexture1D , deviceProp.maxTexture2D[0],
    deviceProp.maxTexture2D[1],
    deviceProp.maxTexture3D[0], deviceProp.maxTexture3D[1],
    deviceProp.maxTexture3D[2]);
    printf(" Max Layered Texture Size (dim) x layers 1D=(%d) x %d, 2D=(%d,%d) x %d\n",
    deviceProp.maxTexture1DLayered[0], deviceProp.maxTexture1DLayered[1],
    deviceProp.maxTexture2DLayered[0], deviceProp.maxTexture2DLayered[1],
    deviceProp.maxTexture2DLayered[2]);
    printf(" Total amount of constant memory: %lu bytes\n",deviceProp.totalConstMem);
    printf(" Total amount of shared memory per block: %lu bytes\n",deviceProp.sharedMemPerBlock);
    printf(" Total number of registers available per block: %d\n",deviceProp.regsPerBlock);
    printf(" Warp size: %d\n", deviceProp.warpSize);
    printf(" Maximum number of threads per multiprocessor: %d\n",deviceProp.maxThreadsPerMultiProcessor);
    printf(" Maximum number of threads per block: %d\n",deviceProp.maxThreadsPerBlock);
    printf(" Maximum sizes of each dimension of a block: %d x %d x %d\n",
    deviceProp.maxThreadsDim[0],
    deviceProp.maxThreadsDim[1],
    deviceProp.maxThreadsDim[2]);
    printf(" Maximum sizes of each dimension of a grid: %d x %d x %d\n",
    deviceProp.maxGridSize[0],
    deviceProp.maxGridSize[1],
    deviceProp.maxGridSize[2]);
    printf(" Maximum memory pitch: %lu bytes\n", deviceProp.memPitch);
    exit(EXIT_SUCCESS);
}

编译运行:

$ nvcc checkDeviceInfor.cu -o checkDeviceInfor
$ ./checkDeviceInfor

决定最佳GPU

对于支持多GPU的系统,是需要从中选择一个来作为我们的device的,抉择出最佳计算性能GPU的一种方法就是由其拥有的处理器数量决定,可以用下面的代码来选择最佳GPU。

int numDevices = 0;
cudaGetDeviceCount(&numDevices);
if (numDevices > 1) {
    int maxMultiprocessors = 0, maxDevice = 0;
    for (int device=0; device<numDevices; device++) {
        cudaDeviceProp props;
        cudaGetDeviceProperties(&props, device);
        if (maxMultiprocessors < props.multiProcessorCount) {
            maxMultiprocessors = props.multiProcessorCount;
            maxDevice = device;
        }
    }
    cudaSetDevice(maxDevice);
}

使用nvidia-smi来查询GPU信息

nvidia-smi是一个命令行工具,可以帮助你管理操作GPU device,并且允许你查询和更改device状态。

nvidia-smi用处很多,比如,下面的指令:

$ nvidia-smi -L
GPU 0: Tesla M2070 (UUID: GPU-68df8aec-e85c-9934-2b81-0c9e689a43a7)
GPU 1: Tesla M2070 (UUID: GPU-382f23c1-5160-01e2-3291-ff9628930b70)

然后可以使用下面的命令来查询GPU 0 的详细信息:

$nvidia-smi –q –i 0

下面是该命令的一些参数,可以精简nvidia-smi的显示信息:

MEMORY

UTILIZATION

ECC

TEMPERATURE

POWER

CLOCK

COMPUTE

PIDS

PERFORMANCE

SUPPORTED_CLOCKS

PAGE_RETIREMENT

ACCOUNTING

比如,显示只device memory的信息:

$nvidia-smi –q –i 0 –d    MEMORY | tail –n 5
Memory Usage
Total : 5375 MB
Used : 9 MB
Free : 5366 MB

设置device

对于多GPU系统,使用nvidia-smi可以查看各GPU属性,每个GPU从0开始依次标注,使用环境变量CUDA_VISIBLE_DEVICES可以指定GPU而不用修改application。

可以设置环境变量CUDA_VISIBLE_DEVICES-2来屏蔽其他GPU,这样只有GPU2能被使用。当然也可以使用CUDA_VISIBLE_DEVICES-2,3来设置多个GPU,他们的device ID分别为0和1.


http://lihuaxi.xjx100.cn/news/1704042.html

相关文章

关于专业课如何考到120+的几个问题

最近&#xff0c;有许多同学咨询我关于专业课的相关问题&#xff0c;对于一些共性问题&#xff0c;现总结在此&#xff0c;希望能够帮助到各位。如果你还有其他疑问&#xff0c;请私聊我&#xff0c;Thanks&#xff01; 补充&#xff1a;专业课想考到130的同学请私聊我&#x…

Mingw快捷安装教程 并完美解决出现的下载错误:The file has been downloaded incorrectly

安装c语言编译器的时候&#xff0c;老是出现The file has been downloaded incorrectly&#xff0c;真的让人 直接去官网拿压缩包&#xff1a;https://sourceforge.net/projects/mingw-w64/files/ &#xff08;往下拉找到那个x86_64-win32-seh的链接&#xff0c;点击后会自动…

RDD行动算子和血缘关系

wordCount分布式运行 将wordCount进行打包上传&#xff0c;path使用args参数传参启动hdfs和yarn提交任务 [atguiguhadoop102 spark-yarn]$ bin/spark-submit \ --class com.atguigu.spark.WordCount \ --master yarn \ ./WordCount.jar \ /input \ 集群模式&#xff1a;clien…

Spring 解决获取请求参数的乱码问题

解决获取请求参数的乱码问题 解决获取请求参数的乱码问题&#xff0c;可以使用SpringMVC提供的编码过滤器CharacterEncodingFilter&#xff0c;但是必须在web.xml中进行注册 <!--配置springMVC的编码过滤器--> <filter><filter-name>CharacterEncodingFilt…

LRC2024:Lightroom Classic 2024 (WinMac)

Lightroom Classic是一款由Adobe公司开发的数字图像处理和管理工具。它以强大的照片调整、处理、管理和分享功能而著称&#xff0c;被认为是专业摄影师的必备利器。 主要特性如下&#xff1a; 增强的校正工具&#xff1a;Lightroom Classic提供了丰富的照片校正工具&#xff0…

linux 查看内核版本 发行版本

Linux 查看当前系统的内核与发行版本信息_linux内核版本怎么看-CSDN博客

高项.项目管理经验、理念、教训

一、项目管理的一些经验 管项目重在管理&#xff0c;而不是死抠无关紧要的技术细节等等。 真正的团队一定是11>2&#xff0c;要把重心放在凝聚团队协力&#xff0c;共同完成目标上。 项目的推进永远都是不确定性的&#xff0c;真正考验项目经理的是不断出现的需求变更和状…

内存中的计量单位

二进制位 数据传输大多是以“位”&#xff08;bit&#xff0c;又名“比特”&#xff09;为单位&#xff0c;一个位就代表一个 0 或者 1&#xff08;即二进制&#xff09; 位是数据存储的最小单位 电流通过机器设备时候必然会产生电压&#xff0c;人们设定了规则&#xff0c;取…