CUDA and OpenGL Interoperability: Use device that runs the current GL context


I’m running an application in a VR environment, where each render node has two graphics cards.
The application uses CUDA to compute some geometry that is then rendered with OpenGL.

I’d like to let CUDA use the same device that runs the current OpenGL context because I need CUDA to share data with it.

However, cudaGLSetGLDevice() requires a device number, and as far as I can see, there is no way to tell which of the available devices runs the current OpenGL context. Omitting cudaGLSetGLDevice() does not seem to work either.

Is there a way to make this work?


You can use the following function from the cutil.h header (included in the SDK):


cudaGLSetGLDevice( cutGetMaxGflopsDeviceId() );

cudaSetDevice( cutGetMaxGflopsDeviceId() );


So both contexts will be processed on the same graphics card.

Yes, but I would like to run one thread (or process) that renders for screen A on graphics card 1, shared by CUDA and OpenGL, and one thread (or process) that renders for screen B on graphics card 2, also shared by CUDA and OpenGL.

The OpenGL context can be distinguished by different X11 display names (:0.0 and :1.0).

The question is, how can CUDA find out on which card the current thread (or process) has its OpenGL context,

and then use that same card?