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[]);
int deviceCount = ;
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 == ) {
printf("There are no available device(s) that support CUDA\n");
} else {
printf("Detected %d CUDA Capable device(s)\n", deviceCount);
}
int dev, driverVersion = , runtimeVersion = ;
dev =;
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/, (driverVersion%)/,runtimeVersion/, (runtimeVersion%)/);
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,)),(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[],
deviceProp.maxTexture2D[],
deviceProp.maxTexture3D[], deviceProp.maxTexture3D[],
deviceProp.maxTexture3D[]);
printf(" Max Layered Texture Size (dim) x layers 1D=(%d) x %d, 2D=(%d,%d) x %d\n",
deviceProp.maxTexture1DLayered[], deviceProp.maxTexture1DLayered[],
deviceProp.maxTexture2DLayered[], deviceProp.maxTexture2DLayered[],
deviceProp.maxTexture2DLayered[]);
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[],
deviceProp.maxThreadsDim[],
deviceProp.maxThreadsDim[]);
printf(" Maximum sizes of each dimension of a grid: %d x %d x %d\n",
deviceProp.maxGridSize[],
deviceProp.maxGridSize[],
deviceProp.maxGridSize[]);
printf(" Maximum memory pitch: %lu bytes\n", deviceProp.memPitch);
exit(EXIT_SUCCESS);
}

编译运行:

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

输出:

./checkDeviceInfor Starting...
Detected CUDA Capable device(s)
Device : "Tesla M2070"
CUDA Driver Version / Runtime Version 5.5 / 5.5
CUDA Capability Major/Minor version number: 2.0
Total amount of global memory: 5.25 MBytes ( bytes)
GPU Clock rate: MHz (1.15 GHz)
Memory Clock rate: Mhz
Memory Bus Width: -bit
L2 Cache Size: bytes
Max Texture Dimension Size (x,y,z) 1D=(), 2D=(,), 3D=(,,)
Max Layered Texture Size (dim) x layers 1D=() x , 2D=(,) x
Total amount of constant memory: bytes
Total amount of shared memory per block: bytes
Total number of registers available per block:
Warp size:
Maximum number of threads per multiprocessor:
Maximum number of threads per block:
Maximum sizes of each dimension of a block: x x
Maximum sizes of each dimension of a grid: x x
Maximum memory pitch: bytes

决定最佳GPU

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

int numDevices = ;
cudaGetDeviceCount(&numDevices);
if (numDevices > ) {
int maxMultiprocessors = , maxDevice = ;
for (int device=; 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 : Tesla M2070 (UUID: GPU-68df8aec-e85c--2b81-0c9e689a43a7)
GPU : Tesla M2070 (UUID: GPU-382f23c1--01e2--ff9628930b70)

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

$nvidia-smi –q –i 

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

MEMORY

UTILIZATION

ECC

TEMPERATURE

POWER

CLOCK

COMPUTE

PIDS

PERFORMANCE

SUPPORTED_CLOCKS

PAGE_RETIREMENT

ACCOUNTING

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

$nvidia-smi –q –i  –d    MEMORY | tail –n
Memory Usage
Total : MB
Used : MB
Free : 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.

代码下载:CodeSamples.zip

CUDA ---- device管理的更多相关文章

  1. [转] HTML5+规范:device(管理设备信息)

    http://blog.csdn.net/qq_27626333/article/details/51815310 Device模块管理设备信息,用于获取手机设备的相关信息,如IMEI.IMSI.型号 ...

  2. BEP 7:CUDA外部内存管理插件(上)

    BEP 7:CUDA外部内存管理插件(上) 背景和目标 在CUDA阵列接口使得能够共享不同的Python之间的数据库的访问CUDA设备.但是,每个库都与其它库区别对待.例如: Numba在内部管理内存 ...

  3. 【CUDA 基础】4.2 内存管理

    title: [CUDA 基础]4.2 内存管理 categories: - CUDA - Freshman tags: - CUDA内存管理 - CUDA内存分配和释放 - CUDA内存传输 - 固 ...

  4. Caffe + Ubuntu 14.04 64bit + CUDA 6.5 配置说明

    本文安装显卡驱动的方式已经过时, 最新安装说明请参考发布在Gist上的这篇文章,如有任何疑问,仍然欢迎在本文下留言 :P (本文档使用同一块NVIDIA显卡进行显示与计算, 如分别使用不同的显卡进行显 ...

  5. CUDA C Best Practices Guide 在线教程学习笔记 Part 2

    10. 执行配置优化 ● 一个 SM中,占用率 = 活动线程束的数量 / 最大可能活动线程束的数量.后者保存在设备属性的  maxThreadsPerMultiProcessor  分量中(GTX10 ...

  6. Caffe + Ubuntu 14.04 64bit + CUDA 6.5 配置说明2

    1. 安装build-essentials 安装开发所需要的一些基本包 sudo apt-get install build-essential 2. 安装NVIDIA驱动 (3.4.0) 2.1 准 ...

  7. Caffe使用: Ubuntu 14.04(x64) 从cuda 7.0 升级到 cuda8.0

    由于之前已经在Ubuntu 14.04 x64上面安装cuda7.0+caffe, 并且已经配置好,caffe也已经跑通. 但是最近需要使用Torch,而Torch对cuda的要求是8.0,因此决定对 ...

  8. Ubuntu14.04 64bit下Caffe + CUDA 6.5安装详细步骤

    不多说,直接上干货! 笔者花了很长时间才装完,主要是cuda安装和opencv安装比较费劲,cuda找不到32位的安装包只好重装64位的ubuntu系统,opencv 也是尝试了很久才解决,这里建议用 ...

  9. Caffe+UbuntuKylin14.04_X64+CUDA 6.5配置

    在编译Caffe的漫长过程中,经过了一个又一个坑,掉进去再爬出来,挺有趣的.对比原文有修改! LInux下配置安装:(本文档使用同一块NVIDIA显卡进行显示与计算, 如分别使用不同的显卡进行显示和计 ...

随机推荐

  1. 前端性能优化:Add Expires headers

    前端性能优化:Add Expires headers Expires headers 是什么? Expires headers:直接翻译是过期头.Expires headers 告诉浏览器是否应该从服 ...

  2. Android Exception Type "share_dialog_title" is not translated in en, zh-rTW strings

    异常出现的场景:打包Android项目时出现 解决办法: Eclipse > Preference > Android > Lint Error Checking搜索Messages ...

  3. HDU 2647 Reward(拓扑排序,vector实现邻接表)

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  4. 3-(基础入门篇)稍微了解一下(需要知道的关于Lua的一些基本的知识)

      http://www.cnblogs.com/yangfengwu/p/8948935.html 基础教程源码链接如果失效,请在淘宝介绍中下载,由于链接很容易失效,如果失效请联系卖家,谢谢 htt ...

  5. C#数组、js数组、json

    C#数组 参考地址C#之数组 什么是数组?数组是一种数据结构,包含同一个类型的多个元素.数组的声明:int[] myIntArray; 注:声明数组时,方括号 [] 必须跟在类型后面,而不是变量名后面 ...

  6. php判断一个数组是否为另一个数组子集的方法

    原文地址http://www.jbxue.com/article/14703.html // 快速的判断$a数组是否是$b数组的子集  $a = array(135,138);  $b = array ...

  7. Spring Data JPA、MyBatis还有Hibernate有什么区别

    原文:https://www.imooc.com/article/19754?block_id=tuijian_wz Spring Data JPA.MyBatis还有Hibernate有什么区别 2 ...

  8. 【转】基于Ubuntu Server16.04 安装Odoo11

    使用 非 root 用户 进行下面的测试: 本文使用 有sudo 权限的 odoo 用户进行测试()如果是 阿里云,可以先创建 odoo 用户 sudo adduser odoo 2:给root 权限 ...

  9. python 回溯法 记录

    一直不是太理解回溯法,这几天集中学习了一下,记录如下. 回溯法有"通用的解题法"之称. 1.定义:  也叫试探法,它是一种系统地搜索问题的解的方法. 2.基本思想:  从一条路往前 ...

  10. Caffe 深度学习框架上手教程

    Caffe 深度学习框架上手教程   blink 15年1月   Caffe (CNN, deep learning) 介绍 Caffe -----------Convolution Architec ...