Skip to content

Commit bb959e6

Browse files
rparolinclaude
andcommitted
cuda.core: match headless EGL device to the current CUDA device in test_graphics.py
CUDA_VISIBLE_DEVICES reorders CUDA's device enumeration but has no effect on EGL's, so on headless multi-GPU Linux systems EGL device 0 does not necessarily correspond to CUDA device 0. cuGraphicsGLRegisterBuffer/Image then fail with CUDA_ERROR_INVALID_DEVICE because interop resources must be registered on the CUDA device backing the GL context. Add select_headless_egl_device_for_cuda(), which uses the EGL_NV_device_cuda extension (eglQueryDeviceAttribEXT + EGL_CUDA_DEVICE_NV) to find the EGL device that actually maps to the current CUDA device, and wire it into _configure_pyglet_headless(). Falls back to pyglet's default device when the extension is unavailable or no match is found, so single-GPU and non-NVIDIA-EGL environments are unaffected. Fixes NVIDIA#2864 Co-Authored-By: Claude Sonnet 5 <[email protected]>
1 parent db53e14 commit bb959e6

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

‎cuda_core/tests/test_graphics.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@
1212
import numpy as np
1313
import pyglet
1414
import pytest
15-
from cuda_python_test_helpers.graphics import is_gl_context_unavailable, open_gl_window
15+
from cuda_python_test_helpers.graphics import (
16+
is_gl_context_unavailable,
17+
open_gl_window,
18+
select_headless_egl_device_for_cuda,
19+
)
1620

1721
from cuda.core import (
1822
Buffer,
23+
Device,
1924
GraphicsResource,
2025
)
2126
from cuda.core._utils.cuda_utils import CUDAError
@@ -51,12 +56,20 @@ def _register_gl_image(tex_id, target):
5156

5257

5358
def _configure_pyglet_headless():
54-
"""On headless Linux: enable EGL mode or skip if EGL is absent."""
59+
"""On headless Linux: enable EGL mode, matched to the current CUDA device, or skip if EGL is absent."""
5560
if sys.platform.startswith("linux") and not (os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY")):
5661
if ctypes.util.find_library("EGL") is None:
5762
pytest.skip("No DISPLAY and no EGL runtime available for headless context.")
5863
pyglet.options["headless"] = True
5964

65+
# CUDA_VISIBLE_DEVICES does not reorder EGL's device enumeration, so on
66+
# headless multi-GPU systems EGL device 0 may not be the physical GPU
67+
# backing the currently-selected CUDA device. Pick the
68+
# EGL device that the driver reports as corresponding to it.
69+
egl_device = select_headless_egl_device_for_cuda(Device().device_id)
70+
if egl_device is not None:
71+
pyglet.options["headless_device"] = egl_device
72+
6073

6174
def _allocate_gl_buffer(win, nbytes):
6275
"""Allocate a GL buffer. Caller must have a current GL context.

‎cuda_python_test_helpers/cuda_python_test_helpers/graphics.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,56 @@
1616
"""
1717

1818
import contextlib
19+
import ctypes
20+
21+
22+
def select_headless_egl_device_for_cuda(cuda_device_ordinal: int) -> int | None:
23+
"""Return the pyglet ``headless_device`` index matching *cuda_device_ordinal*.
24+
25+
``CUDA_VISIBLE_DEVICES`` reorders CUDA's device enumeration but has no
26+
effect on EGL's, so on headless multi-GPU systems EGL platform device
27+
``N`` does not necessarily correspond to CUDA device ``N`` (the CUDA
28+
Programming Guide notes ``CUDA_VISIBLE_DEVICES`` does not select the
29+
default EGL device). The ``EGL_NV_device_cuda``
30+
extension (``eglQueryDeviceAttribEXT`` with ``EGL_CUDA_DEVICE_NV``)
31+
reports the CUDA ordinal each EGL device corresponds to, so it can be
32+
used to pick the EGL device that matches the CUDA device already
33+
selected via ``cuda.core``.
34+
35+
Must be called after ``pyglet.options["headless"]`` is set and before a
36+
headless GL context is created. Returns ``None`` if the extension is
37+
unavailable or no EGL device reports *cuda_device_ordinal*; the caller
38+
should then fall back to pyglet's default (device 0).
39+
"""
40+
egl_cuda_device_nv = 0x323A
41+
42+
try:
43+
from pyglet.libs.egl import egl, eglext
44+
from pyglet.libs.egl.lib import link_EGL
45+
46+
egl_query_device_attrib_ext = link_EGL(
47+
"eglQueryDeviceAttribEXT",
48+
egl.EGLBoolean,
49+
[eglext.EGLDeviceEXT, egl.EGLint, ctypes.POINTER(ctypes.c_ssize_t)],
50+
)
51+
52+
num_devices = egl.EGLint()
53+
if not eglext.eglQueryDevicesEXT(0, None, ctypes.byref(num_devices)) or num_devices.value <= 0:
54+
return None
55+
56+
devices = (eglext.EGLDeviceEXT * num_devices.value)()
57+
if not eglext.eglQueryDevicesEXT(num_devices.value, devices, ctypes.byref(num_devices)):
58+
return None
59+
60+
for index in range(num_devices.value):
61+
queried_ordinal = ctypes.c_ssize_t(-1)
62+
found = egl_query_device_attrib_ext(devices[index], egl_cuda_device_nv, ctypes.byref(queried_ordinal))
63+
if found and queried_ordinal.value == cuda_device_ordinal:
64+
return index
65+
except Exception:
66+
return None
67+
68+
return None
1969

2070

2171
def open_gl_window():

0 commit comments

Comments
 (0)