secdet
secdet

Reputation: 308

C Program to get GPU temp

So there is a command in Linux called

nvidia-settings -q=[gpu:0]/GPUCoreTemp

which gives me this output to see that my GPU has a temp of 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.

Is it somehow possible to directly get the temperature in C in a like integer form? Otherwise I would have to read the output and trim it which isn't really fast when I want to continously check the temperature.

Upvotes: 0

Views: 900

Answers (1)

geebert
geebert

Reputation: 290

You can use the Nvidia Manangement Library (nvml) to access that information. I used nvcc to compile this:

#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", 0, nvmlErrorString(result));
    }else{
            printf("%d\n", temp);
    }

    return 0;
}

Upvotes: 2

Related Questions