Take the 2-minute tour ×

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):

  • descending order of compute capability
  • PCI slot number
  • date/time when the device was added to system (device that was just added to computer is higher ID number)

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.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

CUDA picks the fastest device as device 0. So when you swap GPUs in and out the ordering might change completely. It might be better to pick GPUs based on their PCI bus id using:

cudaError_t cudaDeviceGetByPCIBusId ( int* device, char* pciBusId )
   Returns a handle to a compute device.

cudaError_t cudaDeviceGetPCIBusId ( char* pciBusId, int  len, int  device )
   Returns a PCI Bus Id string for the device.

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.

share|improve this answer
By "fastest" do you mean in terms of clock speed? –  solvingPuzzles Dec 9 '12 at 10:06
Some heuristics are used to estimate the theoretical speed of the GPU. They take into account e.g. chip architecture, clock speed, driver model (on windows TCC is preffered). –  Przemyslaw Zych Dec 9 '12 at 16:08
At the moment, I have 3 CUDA-capable GPUs in my machine: a GTX680, a GTX9800 (an ancient, slow GPU that I just use for graphics), and a C2050. Oddly, the GTX9800 gets a lower number than the C2050... strange. –  solvingPuzzles Dec 26 '12 at 5:53
Only GPU with index 0 is the fastest. Rest of indexes are not sorted by speed. Does GTX 9800 has index 0? If not then everything is working as expected. –  Przemyslaw Zych Dec 26 '12 at 7:43
Nope, the GTX9800 doesn't have index 0. It makes more sense now. –  solvingPuzzles Dec 26 '12 at 7:57

The CUDA Support/Choosing a GPU suggest that

when running a CUDA program on a machine with multiple GPUs, by default CUDA kernels will execute on whichever GPU is installed in the primary graphics card slot.

Also, the discussion at No GPU selected, code working properly, how's this possible? suggests that CUDA does not map the "best" card to device 0 in general.


Today I have installed a PC with a Tesla C2050 card for computation and a 8084 GS card for visualization switching their position between the first two PCI-E slots. I have used deviceQuery and noticed that GPU 0 is always that in the first PCI slot and GPU 1 is always that in the second PCI slot. I do not know if this is a general statement, but it is a proof that for my system GPUs are numbered not according to their "power", but according to their positions.

share|improve this answer
I agree. I've had cases where a machine has a modern GTX6xx Kepler and an ancient G80, and device 0 is the G80. The opposite has happened to me too. The "order of PCIe slots" explanation sounds reasonable. I haven't paid much attention to the PCIe slot order that I used, other than trying to reserve PCIe_3 slots for PCIe_3-compatible GPUs. –  solvingPuzzles Sep 23 '13 at 2:40

Your Answer


