When a computer has multiple CUDA-capable GPUs, each GPU is assigned a device ID
. By default, CUDA kernels execute on device ID 0
. You can use cudaSetDevice(int device)
to select a different device.
Let's say I have two GPUs in my machine: a GTX 480 and a GTX 670. How does CUDA decide which GPU is device ID 0
and which GPU is device ID 1
?
Ideas for how CUDA might assign device IDs (just brainstorming):
Motivation: I'm working on some HPC algorithms, and I'm benchmarking and autotuning them for several GPUs. My processor has enough PCIe lanes to drive cudaMemcpys to 3 GPUs at full bandwidth. So, instead of constantly swapping GPUs in and out of my machine, I'm planning to just keep 3 GPUs in my computer. I'd like to be able to predict what will happen when I add or replace some GPUs in the computer.
or CUDA Driver API cuDeviceGetByPCIBusId cuDeviceGetPCIBusId . But IMO the most reliable way to know which device is which would be to use NVML or nvidia-smi to get each device's unique identifier (UUID) using nvmlDeviceGetUUID and then match it do CUDA device with pciBusId using nvmlDeviceGetPciInfo .
To run multiple instances of a single-GPU application on different GPUs you could use CUDA environment variable CUDA_VISIBLE_DEVICES. The variable restricts execution to a specific set of devices. To use it, just set CUDA_VISIBLE_DEVICES to a comma-separated list of GPU IDs.
CUDA_VISIBLE_DEVICES is used by the CUDA driver to decide what devices should be visible to CUDA.
The canonical way to select a device in the runtime API is using cudaSetDevice . That will configure the runtime to perform lazy context establishment on the nominated device. Prior to CUDA 4.0, this call didn't actually establish a context, it just told the runtime which GPU to try and use.
Set the environment variable CUDA_DEVICE_ORDER
as:
export CUDA_DEVICE_ORDER=PCI_BUS_ID
Then the GPU IDs will be ordered by pci bus IDs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With