# C++ api for reading GPU temperatures?

**URL:** <https://forums.developer.nvidia.com/t/c-api-for-reading-gpu-temperatures/40614>\
**Category:** Linux\
**Created:** [December 11, 2015, 12:18am UTC](https://forums.developer.nvidia.com/t/c-api-for-reading-gpu-temperatures/40614 "2015-12-11T00:18:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kev717](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kev717](https://forums.developer.nvidia.com/u/kev717)\
**Post date:** [December 11, 2015, 12:18am UTC](https://forums.developer.nvidia.com/t/c-api-for-reading-gpu-temperatures/40614/1 "2015-12-11T00:18:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![blackout24](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@blackout24](https://forums.developer.nvidia.com/u/blackout24)\
**Post date:** [December 11, 2015, 12:39am UTC](https://forums.developer.nvidia.com/t/c-api-for-reading-gpu-temperatures/40614/2 "2015-12-11T00:39:23Z")

</div>

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

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

```

and get it from there.

---

<div class="post-metadata">

**Author:** ![kev717](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kev717](https://forums.developer.nvidia.com/u/kev717)\
**Post date:** [December 11, 2015, 1:02am UTC](https://forums.developer.nvidia.com/t/c-api-for-reading-gpu-temperatures/40614/3 "2015-12-11T01:02:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![blackout24](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@blackout24](https://forums.developer.nvidia.com/u/blackout24)\
**Post date:** [December 11, 2015, 1:11am UTC](https://forums.developer.nvidia.com/t/c-api-for-reading-gpu-temperatures/40614/4 "2015-12-11T01:11:45Z")

</div>

> [@](#):
>
> 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

> **[NVIDIA Management Library (NVML)](https://developer.nvidia.com/management-library-nvml)**

---

<div class="post-metadata">

**Author:** ![kev717](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kev717](https://forums.developer.nvidia.com/u/kev717)\
**Post date:** [December 11, 2015, 1:15am UTC](https://forums.developer.nvidia.com/t/c-api-for-reading-gpu-temperatures/40614/5 "2015-12-11T01:15:22Z")

</div>

That should be helpful. Thanks.

---

<div class="post-metadata">

**Author:** ![Gusar](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Gusar](https://forums.developer.nvidia.com/u/Gusar)\
**Post date:** [December 11, 2015, 5:44pm UTC](https://forums.developer.nvidia.com/t/c-api-for-reading-gpu-temperatures/40614/6 "2015-12-11T17:44:24Z")

</div>

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]](http://cgit.freedesktop.org/~aplattner/nvidia-settings/%5B/url%5D)

---

<div class="post-metadata">

**Author:** ![blackout24](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@blackout24](https://forums.developer.nvidia.com/u/blackout24)\
**Post date:** [December 11, 2015, 8:09pm UTC](https://forums.developer.nvidia.com/t/c-api-for-reading-gpu-temperatures/40614/7 "2015-12-11T20:09:50Z")

</div>

Much easier to just use nvml.

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

```

---

<div class="post-metadata">

**Author:** ![leonamleite](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@leonamleite](https://forums.developer.nvidia.com/u/leonamleite)\
**Post date:** [April 18, 2019, 1:06pm UTC](https://forums.developer.nvidia.com/t/c-api-for-reading-gpu-temperatures/40614/8 "2019-04-18T13:06:03Z")

</div>

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