From 2a1840aaddac01b5726458bbd128472d0c108cdd Mon Sep 17 00:00:00 2001 From: Lester Hedges Date: Fri, 20 Nov 2026 13:19:33 +0000 Subject: [PATCH] Backport fix from PR #301. [ci skip] --- src/somd2/runner/_repex.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/somd2/runner/_repex.py b/src/somd2/runner/_repex.py index 6f8371c..4d9b23e 100644 --- a/src/somd2/runner/_repex.py +++ b/src/somd2/runner/_repex.py @@ -1086,7 +1086,10 @@ def _check_device_memory(device=0): the visible set, whereas pynvml and pyopencl enumerate all devices on the machine, so the two only agree when the visible set starts at zero and is contiguous. CUDA_VISIBLE_DEVICES entries may be - either an index or a UUID. + either an index or a UUID. In containerized or cgroup-isolated + environments where only a subset of GPU device nodes is mounted, + OpenCL may expose fewer devices than the physical index and will + fall back to pynvml. """ device = str(device).strip() @@ -1117,11 +1120,11 @@ def _check_device_memory(device=0): ocl_device = all_devices[device_index] vendor = ocl_device.vendor else: - msg = f"Device index {device_index} out of range. Found {len(all_devices)} GPU(s)." - _logger.error(msg) - raise IndexError(msg) - except IndexError: - raise + msg = ( + f"Device index {device_index} out of range for OpenCL " + f"(found {len(all_devices)} GPU(s)); falling back to pynvml." + ) + _logger.warning(msg) except Exception: _logger.warning( "Could not query GPU platform via OpenCL; falling back to pynvml for NVIDIA detection." @@ -1133,13 +1136,15 @@ def _check_device_memory(device=0): import pynvml pynvml.nvmlInit() - if is_uuid: - handle = pynvml.nvmlDeviceGetHandleByUUID(device.encode()) - else: - handle = pynvml.nvmlDeviceGetHandleByIndex(device_index) - memory = pynvml.nvmlDeviceGetMemoryInfo(handle) - pynvml.nvmlShutdown() - return (memory.used, memory.free, memory.total) + try: + if is_uuid: + handle = pynvml.nvmlDeviceGetHandleByUUID(device.encode()) + else: + handle = pynvml.nvmlDeviceGetHandleByIndex(device_index) + memory = pynvml.nvmlDeviceGetMemoryInfo(handle) + return (memory.used, memory.free, memory.total) + finally: + pynvml.nvmlShutdown() except Exception as e: if vendor is None: msg = f"Could not get GPU memory info for device {device} via OpenCL or pynvml: {e}"