File size: 1,400 Bytes
06f9561
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Resolve EGL's own device enumeration before running a MuJoCo renderer.

CUDA ordinals are not EGL ordinals. CPU/Mesa containers can expose a single
EGL device despite having several CUDA GPUs. Never pass CUDA=3 blindly to EGL.
"""
import os
from pathlib import Path
import runpy
import sys


def selected_device(count, requested):
    if count < 1:
        raise RuntimeError("No EGL rendering device available")
    if count == 1:
        return "0"
    if requested is None:
        return None
    value = int(requested)
    if not 0 <= value < count:
        raise ValueError(f"EGL device {value} outside available 0..{count-1}")
    return str(value)


def main():
    from mujoco.egl import egl_ext as EGL
    count = len(EGL.eglQueryDevicesEXT())
    requested = os.environ.get("HFD_RENDER_EGL_DEVICE_ID", os.environ.get("MUJOCO_EGL_DEVICE_ID"))
    selected = selected_device(count, requested)
    if selected is not None:
        os.environ["MUJOCO_EGL_DEVICE_ID"] = selected
    print(f"[render] EGL devices={count} selected={selected or 'auto'} "
          f"(CUDA allocation={os.environ.get('CUDA_VISIBLE_DEVICES', 'all')}; independent enumerations)", flush=True)
    target = Path(sys.argv[1]).resolve()
    sys.argv = [str(target), *sys.argv[2:]]
    sys.path.insert(0, str(target.parent))
    runpy.run_path(str(target), run_name="__main__")


if __name__ == "__main__":
    main()