I recently wrote a C++ program to switch on an air conditioner when my computer temperature exceeded a certain threshold. After getting it to work with the main core components (CPU, motherboard, etc.), I started looking at the NVIDIA GPU and found that there don’t seem to be any APIs for getting temperature sensor data from the device (unless I use nouveau drivers)
I’ve tried looking up xnvctrl, but I can’t find a library reference or source code for it.
Does anybody know how I can pass the temperature data from the GPU to my program on Linux?
This would technically work in a C++ program if I was to use the popen function, although I was hoping there was an actual library for a more general approach
#include <stdio.h>
#include <nvidia/gdk/nvml.h>
int main()
{
nvmlReturn_t result;
unsigned int temp;
// First initialize NVML library
result = nvmlInit();
if (NVML_SUCCESS != result)
{
printf("Failed to initialize NVML: %s\n", nvmlErrorString(result));
printf("Press ENTER to continue...\n");
getchar();
return 1;
}
nvmlDevice_t device;
result = nvmlDeviceGetHandleByIndex(0, &device);
if (NVML_SUCCESS != result)
{
printf("Failed to get handle for device %i: %s\n", 0, nvmlErrorString(result));
goto Error;
}
result = nvmlDeviceGetTemperature(device, NVML_TEMPERATURE_GPU, &temp);
if (NVML_SUCCESS != result) {
printf("Failed to get temperature of device %i: %s\n", 0, nvmlErrorString(result));
}
printf("%d\n", temp);
result = nvmlShutdown();
if (NVML_SUCCESS != result)
printf("Failed to shutdown NVML: %s\n", nvmlErrorString(result));
return 0;
Error:
result = nvmlShutdown();
if (NVML_SUCCESS != result)
printf("Failed to shutdown NVML: %s\n", nvmlErrorString(result));
printf("Press ENTER to continue...\n");
getchar();
return 1;
}