Get Currently Used CUDA Device using C++

Get Currently Used CUDA Device using C++

When working with CUDA capable devices, it is important to ensure that the application is utilizing the correct device. In some scenarios, you may need to programmatically determine which device is currently being used by your application. This tutorial demonstrates how to get the currently used CUDA device using C++.

The cudaGetDevice function can be used to determine which CUDA device is currently being used. In the following example, we declare an integer variable which is passed to the function as a pointer. The function sets the value of the variable to the index of the currently used CUDA device.

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

int main()
{
    int device;
    cudaGetDevice(&device);
    std::cout << "Currently used CUDA device: " << device << std::endl;

    return 0;
}

Here's an example of the output:

Currently used CUDA device: 0

In this case, the output indicates that a device which index is 0 is currently being used.

Leave a Comment

Cancel reply

Your email address will not be published.