获取NVIDIA显卡的温度
NVIDIA显卡在硬件上有温度传感器,可以感知显卡的运行环境。温度数据的获取,一般是通过调用NVIDIA的SDK的相关函数即可。SDK的下载的网址(https://developer.nvidia.com/nvapi),里面有一些sample code可以参考。获取温度的函数是NvAPI_GPU_GetThermalSettings。参考代码如下:
int main() { NvAPI_Status ret = NVAPI_OK; ret = NvAPI_Initialize(); if(ret != NVAPI_OK) { printf("NvAPI_Initialize() failed = 0x%x", ret); return 1; // Initialization failed } NvPhysicalGpuHandle hPhysicalGpu[NVAPI_MAX_PHYSICAL_GPUS]; for (NvU32 PhysicalGpuIndex = 0; PhysicalGpuIndex < NVAPI_MAX_PHYSICAL_GPUS; PhysicalGpuIndex++) { hPhysicalGpu[PhysicalGpuIndex]=0; } ret = NvAPI_EnumPhysicalGPUs(hPhysicalGpu, &physicalGpuCount); if(ret != NVAPI_OK) { printf("Cannot enumerate GPUs in the system...\n"); getchar(); exit(1); } NV_GPU_THERMAL_SETTINGS currentTemp;//获取温度的数据结构 currentTemp.version = NV_GPU_THERMAL_SETTINGS_VER;//一定要设置,不然调用获取温度函数时候会出错 for(NvU32 GpuIndex = 0; GpuIndex < physicalGpuCount; GpuIndex++) { ret = NvAPI_GPU_GetThermalSettings(hPhysicalGpu[PhysicalGpuIndex], NVAPI_THERMAL_TARGET_ALL,¤tTemp);//获取温度 if(ret == NVAPI_OK) { printf("current temperature=%d\n",currentTemp.sensor[0].currentTemp); } } return 0; }