Get Performance State of Nvidia GPU using NVML and C++

Get Performance State of Nvidia GPU using NVML and C++

Understanding the performance state of the NVIDIA GPU is critical for optimizing computational tasks, whether you're running machine learning workloads, gaming, or managing data centers. NVIDIA Management Library (NVML) provides an interface for monitoring and managing NVIDIA GPUs, allowing developers to access vital metrics such as performance state, utilization, temperature, and more. This tutorial shows how to get performance state of Nvidia GPU using NVML and C++.

The code initializes the NVML library to interact with Nvidia GPUs and retrieves the total number of GPUs on the system. It then iterates through each GPU and fetches its name using nvmlDeviceGetName. Additionally, the code retrieves the GPU's current performance state (P-State) using nvmlDeviceGetPerformanceState. The code prints the device number, name, and performance state to the console.

#include <iostream>
#include <nvml.h>

int main()
{
    nvmlInit();

    uint32_t deviceCount;
    nvmlDeviceGetCount(&deviceCount);

    for (uint32_t i = 0; i < deviceCount; ++i) {
        nvmlDevice_t device;
        nvmlDeviceGetHandleByIndex(i, &device);

        char name[NVML_DEVICE_NAME_V2_BUFFER_SIZE];
        nvmlDeviceGetName(device, name, NVML_DEVICE_NAME_V2_BUFFER_SIZE);

        nvmlPstates_t state{};
        nvmlDeviceGetPerformanceState(device, &state);

        std::cout << "Device Number: " << i << std::endl;
        std::cout << " Device Name: " << name << std::endl;
        std::cout << " Performance State: P" << state << std::endl;
    }

    nvmlShutdown();

    return 0;
}

The output will appear similar to this:

Device Number: 0
 Device Name: NVIDIA GeForce RTX 3070 Laptop GPU
 Performance State: P0

In the example output above, P0 indicates the performance state. Performance states range from P0 (highest performance) to P12 (lowest or idle performance).

Leave a Comment

Cancel reply

Your email address will not be published.