问题描述
所以Linux中有一个命令叫做
nvidia-settings -q=[gpu:0]/GPUCoreTemp
这给了我这个输出,可以看到我的 GPU 的温度为 41°C。
Attribute 'GPUCoreTemp' (devnux:0[gpu:0]): 41.
'GPUCoreTemp' is an integer attribute.
'GPUCoreTemp' is a read-only attribute.
'GPUCoreTemp' can use the following target types: X Screen,GPU.
是否有可能以类似整数形式直接获得以 C 为单位的温度?否则,当我想连续检查温度时,我将不得不读取输出并修剪它,这并不是很快。
解决方法
您可以使用 Nvidia 管理库 (nvml) 来访问该信息。 我用 nvcc 来编译这个:
#include <stdio.h>
#include <nvml.h>
int main(){
nvmlReturn_t result;
unsigned int temp;
nvmlDevice_t device;
result = nvmlInit();
if(NVML_SUCCESS != result){
printf("failed to initialyse nvml \n");
return 1;
}
result = nvmlDeviceGetHandleByIndex(0,&device);
result = nvmlDeviceGetTemperature(device,NVML_TEMPERATURE_GPU,&temp);
if (NVML_SUCCESS != result) {
printf("Failed to get temperature of device %i: %s\n",nvmlErrorString(result));
}else{
printf("%d\n",temp);
}
return 0;
}