Get CUDA Device UUID using C++

Get CUDA Device UUID using C++

When working with GPUs that supports CUDA, it can be helpful to identify devices uniquely, especially in multi-GPU setups. The UUID (Universally Unique Identifier) of a CUDA device serves as a unique identifier for each GPU. This tutorial demonstrates how to get CUDA device UUID using C++.

This code begins by using the cudaGetDeviceCount function to determine the number of CUDA devices on the system, storing the count in the deviceCount variable. For each device, a cudaDeviceProp structure is declared to hold its properties, which are populated using the cudaGetDeviceProperties function. Among these properties, the unique GPU identifier (UUID) is formatted into a readable string. The code then prints each device's index, name, and UUID.

#include <iostream>
#include <iomanip>
#include <cuda_runtime.h>

std::string uuidToString(cudaUUID_t uuid)
{
    std::ostringstream oss;
    oss << "GPU-" << std::hex << std::setfill('0');

    for (int i = 0; i < sizeof(uuid.bytes); ++i) {
        oss << std::setw(2) << (unsigned int) (unsigned char) uuid.bytes[i];
        if (i == 3 || i == 5 || i == 7 || i == 9) {
            oss << '-';
        }
    }

    return oss.str();
}

int main()
{
    int deviceCount;
    cudaGetDeviceCount(&deviceCount);

    for (int i = 0; i < deviceCount; ++i) {
        cudaDeviceProp prop{};
        cudaGetDeviceProperties(&prop, i);

        std::cout << "Device Number: " << i << std::endl;
        std::cout << " Device Name: " << prop.name << std::endl;
        std::cout << " UUID: " << uuidToString(prop.uuid) << std::endl;
    }

    return 0;
}

Here's an example of the output you could see in the console:

Device Number: 0
 Device Name: NVIDIA GeForce RTX 3070 Laptop GPU
 UUID: GPU-6b2d19d5-f7cb-6e3a-b13f-3ca8f1f47e3e

Leave a Comment

Cancel reply

Your email address will not be published.