Get Compute Capability of Nvidia GPU using NVML and C++

Get Compute Capability of Nvidia GPU using NVML and C++

When working with Nvidia GPUs, knowing the compute capability is crucial, as it determines which features and optimizations you can leverage in CUDA programming. NVML provides a way to retrieve Nvidia GPU hardware information. This tutorial demonstrates how to get compute capability of Nvidia GPU using NVML and C++.

The code initializes the NVML library and retrieves the number of NVIDIA GPUs available on the system. For each detected GPU, it obtains a device handle, retrieves the GPU name with nvmlDeviceGetName, and fetches the compute capability using nvmlDeviceGetCudaComputeCapability. The code then prints this information. Finally, shutdown is called to clean up NVML resources before exiting.

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

        int major, minor;
        nvmlDeviceGetCudaComputeCapability(device, &major, &minor);

        std::cout << "Device Number: " << i << std::endl;
        std::cout << " Device Name: " << name << std::endl;
        std::cout << " Compute Capability: " << major << "." << minor << std::endl;
    }

    nvmlShutdown();

    return 0;
}

A sample output displayed in the console:

Device Number: 0
 Device Name: NVIDIA GeForce RTX 3070 Laptop GPU
 Compute Capability: 8.6

Leave a Comment

Cancel reply

Your email address will not be published.