C++ api for reading GPU temperatures?

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?

Probably the easiest way is to use “nvidia-smi” and apply some awk grep and sed magic.
Something like:

nvidia-smi | grep '[0-9][0-9]C' | awk '{print $3}' | sed 's/C//'

and get it from there.

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

1 Like

That should be helpful. Thanks.

The nvidia-settings source code contains the source of libxnvctrl. Source tarballs are at [url]ftp://download.nvidia.com/XFree86/nvidia-settings/[/url], the git repository is at [url]http://cgit.freedesktop.org/~aplattner/nvidia-settings/[/url]

Much easier to just use nvml.

#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;
}

Anyone know how to tell, why the CPU Core sensor type changes from Clock to Temperature?