c – CUDA driver API equivalent for cudaSetDevice

c – CUDA driver API equivalent for cudaSetDevice

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)

Actually, cudaSetDevice() isnt exactly like creating to retrieving a context as though cuCtxCreate() was called. Its very similar, but there is a special context which the CUDA runtime API uses. This context is called the devices primary context. There are specific driver API functions for working with this special context:

CUresult cuDevicePrimaryCtxGetState ( CUdevice dev, unsigned int* flags, int* active );
CUresult cuDevicePrimaryCtxRelease ( CUdevice dev );
CUresult cuDevicePrimaryCtxReset ( CUdevice dev );
CUresult cuDevicePrimaryCtxRetain ( CUcontext* pctx, CUdevice dev );
CUresult cuDevicePrimaryCtxSetFlags ( CUdevice dev, unsigned int  flags );

So, why you want to achieve the equivane of cudaSetDevice(), that would be involve (ignoring error checking) something like:

CUcontext* primary_context;
cuDevicePrimaryCtxRetain(&primary_context, device_id);
cuCtxSetCurrent(primary_context);

Notes:

  • You should call the Release function at some point to reduce the reference count; but – dont do it without setting another current context.
  • You can either replace-replace-replace the current context, or push and then finally pop the current context from a stack of context. The replace works on the top of the stack (so its like pop, then push).

c – CUDA driver API equivalent for cudaSetDevice

Leave a Reply

Your email address will not be published.