Multi GPU CUDA & OpenGL interop

I have a machine with two Fermi class GPUs.
I want to create a CUDA & OpenGL context for each device.

Then I use OpenGL to render to FBO and pass to CUDA for compression.

I have two cards so to do in parallel I need 2 OpenGL contexts and 2 CUDA contexts.

The problem is how to figure out which CUDA device ID matches with the corresponding X Server Display number so I ensure I link the correct contexts together.

On Windows NVIDIA provide the OpenGL extension WGL_NV_gpu_affinity and the CUDA function cudaWGLGetDevice. However there does not appear to be any Linux equivalent.

You can get the GPU’s PCI ID from CUDA API. So how can you get the PCI ID of an X Display from inside program? eg “:0” and “:1”.

I note that OpenCL has support for identifying GL devices for CL devices via clGetGLContextInfoKHR, so I may be forced to use OpenCL 1.0, but I would rather use CUDA until CL 1.1 comes out (with thread safety).

You can get an X screen’s PCI id from the NVCtrl API, which is part of the nvidia-settings codebase.

ftp://download.nvidia.com/XFree86/nvidia-settings/

It’s similar to NVAPI, but for Linux.