前段时间做英伟达硬解得时候,显卡总是莫名挂掉,后来发现是因为显卡温度过高掉了。这几天找到CUDA中有NVML工具可以查看显卡信息,nvidia-smi也是基于这个工具包。
使用的CUDA版本为CUDA 8.0 。
1.给程序添加NVML
安装CUDA之后可以找到如下:
图1.NVML的例子
这里面包含的是NVML的一个例子。我的系统是64位的,可以找到NVML的lib和头文件如下:
图2.NVML的lib文件
图3.NVML头文件
在工程中包含NVML。我是新建的CUDA 8.0 Runtime工程,因为NVML包含在CUDA中,建CUDA 8.0 Runtime工程可以省去CUDA的配置工作,工程建立方法参见VS2013 VC++的.cpp文件调用CUDA的.cu文件中的函数
,CUDA 8.0为默认安装,系统为win10 64位。
在程序中直接包含NVML的头文件和lib文件即可:
#include "nvml.h"
#pragma comment(lib,"nvml.lib")
注意64位系统应该建立x64工程,因为在安装的CUDA中没有win32的nvml.lib。
2.NVML查询显卡信息
常用函数:
·nvmlInit()函数初始化NVML;
·nvmlDeviceGetCount(unsigned int *deviceCount)函数可以获得显卡数;
·nvmlDeviceGetHandleByIndex(unsigned int index, nvmlDevice_t *device)获取设备;
·nvmlDeviceGetName(nvmlDevice_t device, char *name, unsigned int length)查询设备的名称;
·nvmlDeviceGetPciInfo(nvmlDevice_t device, nvmlPciInfo_t *pci)获取PCI信息,对这个函数的重要性,例子中是这么说的
// pci.busId is very useful to know which device physically you're talking to
// Using PCI identifier you can also match nvmlDevice handle to CUDA device.
·nvmlDeviceGetComputeMode(nvmlDevice_t device, nvmlComputeMode_t *mode)得到显卡当前所处的模式,模式由以下:
typedef enum nvmlComputeMode_enum
{
NVML_COMPUTEMODE_DEFAULT = 0, //!< Default compute mode -- multiple contexts per device
NVML_COMPUTEMODE_EXCLUSIVE_THREAD = 1, //!< Support Removed
NVML_COMPUTEMODE_PROHIBITED = 2, //!< Compute-prohibited mode -- no contexts per device
NVML_COMPUTEMODE_EXCLUSIVE_PROCESS = 3, //!< Compute-exclusive-process mode -- only one context per device, usable from multiple threads at a time
// Keep this last
NVML_COMPUTEMODE_COUNT
} nvmlComputeMode_t;
·nvmlDeviceSetComputeMode(nvmlDevice_t device, nvmlComputeMode_t mode)可以修改显卡的模式;
·nvmlDeviceGetTemperatureThreshold(nvmlDevice_t device, nvmlTemperatureThresholds_t thresholdType, unsigned int *temp)查询温度阈值,具体有两种:
typedef enum nvmlTemperatureThresholds_enum
{
NVML_TEMPERATURE_THRESHOLD_SHUTDOWN = 0, // Temperature at which the GPU will shut down for HW protection
NVML_TEMPERATURE_THRESHOLD_SLOWDOWN = 1, // Temperature at which the GPU will begin slowdown
// Kee