James Paul Turner
James Paul Turner

Reputation: 851

Do CUDA device IDs change when debugging?

I've noticed that, on a host with two working CUDA SM_2.x devices, the first of which is running the display, calling cudaSetDevice(1) in the debugger throws CUDA error 10 (invalid device). It works fine when executed outside of the debugger, however. I also note that the device which normally has ID 1 has device ID 0 inside the debugger.

Are my suspicions confirmed that device ID 0 is assigned only to the first available device, rather than the device installed in the first PCIe slot?

If so, is there a way of ensuring that e.g. cudaSetDevice(1) always selects the same device, irrespective of how CUDA assigns device IDs?

Upvotes: 1

Views: 123

Answers (1)

talonmies
talonmies

Reputation: 72352

The really short answer is, no, there is no way to do this. Having said that, hardcoding a fixed device id is never the correct thing to do. You want to either:

  1. Select an id from the list of available devices which the API returns for you (and there are a number of very helpful APIs to let you get the device you want), or
  2. You don't use any explicit device selection at all in your code and rely on appropriate driver compute mode settings and/or the CUDA_VISIBLE_DEVICES environment setting to have the driver automatically select a suitable valid device ID for you.

Which you choose will probably dictated by the environment in which your code ends up being deployed.

Upvotes: 1

Related Questions