Set Currently Used CUDA Device using C++

Set Currently Used CUDA Device using C++

When working with multiple CUDA capable devices on a system, it is important to be able to specify which device to use for computations. By default, the first available CUDA device is used, but it is often desirable to select a specific device to use. This tutorial shows how to set the currently used CUDA device using C++.

The cudaSetDevice function can be used to specify which CUDA device to use. This function accepts an integer argument that specifies the index of the device. Valid device indexes are 0 to cudaGetDeviceCount() - 1.

In the following example, we set the currently used CUDA device to be the first device (index 0).

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

int main()
{
    int device = 0;
    cudaSetDevice(device);

    return 0;
}

Leave a Comment

Cancel reply

Your email address will not be published.