Get Nvidia GPU UUID using NVML and C++

Get Nvidia GPU UUID using NVML and C++

When working with NVIDIA GPUs, especially in environments with multiple GPUs, identifying each GPU uniquely is important for effective resource management, debugging, and optimization. NVIDIA Management Library (NVML) provides a powerful API for interacting with NVIDIA GPUs, offering a range of features to monitor and manage GPU resources. NVML provides the ability to retrieve a universally unique identifier (UUID) of the GPU. This tutorial shows how to get Nvidia GPU UUID using NVML and C++.

The code initializes the NVML library and retrieves the number of NVIDIA GPUs available on the system. For each GPU, it obtains the GPU's name with nvmlDeviceGetName and its UUID using nvmlDeviceGetUUID. This information is printed to the console. Finally, the code shuts down the NVML library to release resources.

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

        char uuid[NVML_DEVICE_UUID_V2_BUFFER_SIZE];
        nvmlDeviceGetUUID(device, uuid, NVML_DEVICE_UUID_V2_BUFFER_SIZE);

        std::cout << "Device Number: " << i << std::endl;
        std::cout << " Device Name: " << name << std::endl;
        std::cout << " UUID: " << uuid << std::endl;
    }

    nvmlShutdown();

    return 0;
}

An example of the output displayed in the console is:

Device Number: 0
 Device Name: NVIDIA GeForce RTX 3070 Laptop GPU
 UUID: GPU-316d7af0-0cb8-2f31-2828-fe24c2f4a5e9

Leave a Comment

Cancel reply

Your email address will not be published.