3
votes

What is the CUDA driver's API equivalent for the runtime API function cudaSetDevice?

I was looking into the driver API and cannot find an equivalent function. What I can do is

cuDeviceGet(&cuDevice, device_no);
cuCtxCreate(&cuContext, 0, cuDevice);

which is not equivalent since beside setting the device it also creates a context. The runtime API cudaSetDevice does not create a context per se. In the runtime API the CUDA context is created implicitly with the first CUDA call that requires state on the device.

Background for this question: CUDA-aware MPI (MVAPICH2 1.8/9) initialization requires the CUDA device to be set before MPI_init is called. Using the CUDA runtime API this can be done with

cudaSetDevice(device_no);
MPI_init();

However, I don't want to use the call to the CUDA runtime since the rest of my application is purely using the driver API and I'd like to avoid linking also to the runtime.

What's wrong in creating the context already before MPI is initialized? In principle nothing. Just wondering if there is an equivalent call in the driver API.

1
Thinking of it again. I think there is no equivalent call, since in the driver API the device is not set. Instead it's as I posted: One creates a handle to a device and creates a context (implicitly setting the device). With this MVAPICH2 is happy. - ritter
In other words: Using the driver API one has to create the context when using CUDA-aware MPI. - ritter
In the runtime API, as of CUDA 4.0, cudaSetDevice does create a context, if one is not already in existence on the device in question. - talonmies

1 Answers

5
votes

You can find information about this in the Programming Guide Appendix about the Driver API, but the short version is this:

  • cuCtxCreate acts as the first cudaSetDevice call (that is it creates a context on the driver context stack)
  • The cuCtxPushCurrent() and cuCtxPopCurrent() pair (or cuCtxSetCurrent depending on which API version you are using) acts as any subsequent cudaSetDevice call (that is it pushes or selects a previously created context to be the active context for all subsequent API calls until the context is popped off the driver context stack or deselected)