diff --git a/scripts/test.sh b/scripts/test.sh index 065be349b..a4f420de9 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -329,6 +329,8 @@ if [ $PARALLEL_ONLY -eq 0 ]; then batch 4 tests/parallel/test_1106_fac_cover_partition_parallel.py # So is the FAC patch summary, the same on every rank (test_1133). batch 4 tests/parallel/test_1133_fac_patch_summary_parallel.py + # So is the serial reference child's environment (#922). + batch 4 tests/parallel/test_1138_serial_reference_child_env.py # test_106*/test_107* were held back pending "a triage/deselect decision". That # decision is made: the band is not the unit of exclusion. Of the 243 tests in # the deferred bands only 26 are level_3 or tier_c; 55 are level_1 AND tier_a — diff --git a/tests/parallel/serial_reference.py b/tests/parallel/serial_reference.py index 90c366a1b..d88ab8a4e 100644 --- a/tests/parallel/serial_reference.py +++ b/tests/parallel/serial_reference.py @@ -23,7 +23,8 @@ which prints one ``SERIALREF `` line; the payload is broadcast to every rank. The child's environment is scrubbed of the launcher's MPI variables — inherited ``OMPI_*`` / ``PMIX_*`` make the child believe it is a member of the parent's job, and it then hangs -or aborts instead of running as a singleton. +or aborts instead of running as a singleton. The user's Open MPI selections and +parameters for io (and its sub-frameworks), mtl, btl and pml are kept (:func:`_child_env`). The mesh is read from the same gmsh cache the parent uses, so parent and child are the same triangulation by construction; the fingerprint is carried through anyway and @@ -49,6 +50,15 @@ # Open MPI 5's runtime, which is a separate family again from OMPI_. _MPI_ENV_PREFIXES = ("OMPI_", "PMIX_", "PMI_", "MPI_", "HYDRA_", "I_MPI_", "SLURM_", "PRTE_", "PRTERUN_", "OPAL_") +# The user's Open MPI component selections, with their parameters, are not job +# identity, and a singleton runs with them: the child keeps them. A job that sets +# OMPI_MCA_io=^ompio to keep HDF5 off OMPIO otherwise has its reference runs on +# OMPIO regardless (#922). A selection travels with its framework's parameters: +# pml=cm needs its mtl's provider settings, pml=ucx its UCX settings (synonyms under +# opal_common_ucx), and io's sub-frameworks configure OMPIO and ROMIO. Without them +# the child's MPI_Init can abort where the parent's succeeded. +_MPI_ENV_KEPT_FRAMEWORKS = ("io", "fcoll", "fs", "fbtl", "sharedfp", "mtl", "btl", + "pml", "opal_common_ucx") _CACHE = {} @@ -106,16 +116,31 @@ def serial_reference(module_file, kind, timeout=600): return payload +def _kept_mpi_setting(name): + """Is ``name`` one of the user's kept Open MPI framework settings + (``OMPI_MCA_`` or ``OMPI_MCA__``)?""" + return any(name == f"OMPI_MCA_{fw}" or name.startswith(f"OMPI_MCA_{fw}_") + for fw in _MPI_ENV_KEPT_FRAMEWORKS) + + +def _child_env(environ): + """``environ`` for a spawned singleton: the launcher's variables removed, the + user's Open MPI framework selections and parameters kept + (``_MPI_ENV_KEPT_FRAMEWORKS``). Other MCA settings (``osc``, ``coll``, + ``PMIX_MCA_*``, ``PRTE_MCA_*``) and ``OMPI_CC`` are still dropped.""" + return {k: v for k, v in environ.items() + if _kept_mpi_setting(k) or not k.startswith(_MPI_ENV_PREFIXES)} + + def _run_child(module_file, kind, timeout): """Never raises — see :func:`serial_reference`. Returns the payload dict, or a string describing what went wrong.""" name = os.path.basename(module_file) try: - env = {k: v for k, v in os.environ.items() - if not k.startswith(_MPI_ENV_PREFIXES)} proc = subprocess.run( [sys.executable, "-u", os.path.abspath(module_file), kind], - env=env, capture_output=True, text=True, timeout=timeout) + env=_child_env(os.environ), capture_output=True, text=True, + timeout=timeout) except subprocess.TimeoutExpired: return f"serial reference for {name}:{kind} timed out after {timeout}s" except Exception as exc: # noqa: BLE001 - see the docstring diff --git a/tests/parallel/test_0855_mesh_smoothing_parallel.py b/tests/parallel/test_0855_mesh_smoothing_parallel.py index 2f11ca5cf..447dc7d71 100644 --- a/tests/parallel/test_0855_mesh_smoothing_parallel.py +++ b/tests/parallel/test_0855_mesh_smoothing_parallel.py @@ -36,7 +36,7 @@ import underworld3 as uw from underworld3.meshing import smooth_mesh_interior -from serial_reference import _MPI_ENV_PREFIXES +from serial_reference import _child_env pytestmark = [pytest.mark.mpi(min_size=2), pytest.mark.timeout(120)] @@ -254,7 +254,7 @@ def test_parallel_matches_serial_bit_identical(): # launcher's variables are stripped first, or the child's MPI_Init tries to # join THIS mpirun's job and aborts on a descriptor it does not own. # - # The prefix list is shared with serial_reference rather than restated here: + # The scrub is serial_reference's (_child_env) rather than restated here: # a local copy that named only the Open MPI family passed on Open MPI and # failed under MPICH, whose variables are PMI_* (#675). One list, so a # launcher that is missing from it is missing everywhere and gets noticed. @@ -262,10 +262,7 @@ def test_parallel_matches_serial_bit_identical(): if rank == 0: tmpdir = tempfile.mkdtemp(prefix="winslow_ref_") ref_path = os.path.join(tmpdir, "ref.npz") - clean_env = { - k: v for k, v in os.environ.items() - if not k.startswith(_MPI_ENV_PREFIXES) - } + clean_env = _child_env(os.environ) proc = subprocess.run( [sys.executable, "-c", _SERIAL_REFERENCE_SCRIPT, ref_path], diff --git a/tests/parallel/test_1138_serial_reference_child_env.py b/tests/parallel/test_1138_serial_reference_child_env.py new file mode 100644 index 000000000..25fcaf5c5 --- /dev/null +++ b/tests/parallel/test_1138_serial_reference_child_env.py @@ -0,0 +1,72 @@ +"""The serial reference child keeps the user's MPI component selections (#922). + +``serial_reference`` runs a test module's np=1 diagnostic in a spawned Python, and +scrubs the launcher's variables from the child's environment so that the child +runs as a singleton instead of trying to join the parent's job. The scrub dropped +every ``OMPI_*`` variable, which also dropped the user's own Open MPI component +selections. On kaiju, jobs set ``OMPI_MCA_io=^ompio`` to keep HDF5 off OMPIO, and +under load every HDF5 error 76 was in a spawned reference run that never saw it. + +The child now keeps the user's selections and parameters for io (and its +sub-frameworks), mtl, btl and pml, and still drops the launcher's variables. A +selection is kept with its parameters: ``pml=cm`` without its mtl's provider +settings, or ``pml=ucx`` without its UCX settings, can make the child's MPI_Init +abort where the parent's succeeded. The first test checks the scrub. The second +spawns a child through the same launch path; the child initialises MPI, reports +its world size and every launcher-prefixed variable it received. + +This file carries no ``mpi(min_size=2)`` mark: under mpirun the launcher's own +variables are set too, which is the case the scrub exists for. +""" +import pytest + +from serial_reference import _MPI_ENV_PREFIXES, _kept_mpi_setting, _run_child + +pytestmark = [pytest.mark.level_1] + +USER_SELECTIONS = {"OMPI_MCA_io": "^ompio", "OMPI_MCA_mtl": "^ofi", + "OMPI_MCA_mtl_ofi_provider_include": "tcp", + "OMPI_MCA_btl": "self,vader", "OMPI_MCA_pml": "ob1"} +LAUNCHER_VARIABLES = {"OMPI_COMM_WORLD_RANK": "0", "OMPI_MCA_ess_base_jobid": "123", + "OMPI_MCA_orte_hnp_uri": "x", "PMIX_RANK": "0", "PMI_RANK": "0", + "SLURM_JOB_ID": "9", "HYDRA_CONTROL_FD": "3", + "MPI_LOCALRANKID": "0"} + + +@pytest.mark.tier_b +def test_the_child_env_keeps_selections_and_drops_job_identity(): + from serial_reference import _child_env + env = _child_env({**USER_SELECTIONS, **LAUNCHER_VARIABLES, "PATH": "/bin", "HOME": "/h"}) + for name, value in USER_SELECTIONS.items(): + assert env.get(name) == value, f"{name} was not passed to the child" + leaked = sorted(set(LAUNCHER_VARIABLES) & set(env)) + assert not leaked, f"launcher variables passed to the child: {leaked}" + assert env["PATH"] == "/bin" and env["HOME"] == "/h" + + +CHILD = ''' +import json, os, sys +from mpi4py import MPI +prefixes = tuple(sys.argv[1].split(",")) +print("SERIALREF " + json.dumps({ + "size": MPI.COMM_WORLD.Get_size(), + "mpi_env": {k: v for k, v in os.environ.items() if k.startswith(prefixes)}})) +''' + + +@pytest.mark.tier_b +def test_a_spawned_child_receives_the_parents_io_selection(tmp_path, monkeypatch): + for name, value in {**USER_SELECTIONS, "PMIX_RANK": "0"}.items(): + monkeypatch.setenv(name, value) + child = tmp_path / "child.py" + child.write_text(CHILD) + payload = _run_child(str(child), ",".join(_MPI_ENV_PREFIXES), timeout=120) + assert isinstance(payload, dict), payload + assert payload["size"] == 1, f"the child is not a singleton: {payload}" + received = payload["mpi_env"] + for name, value in USER_SELECTIONS.items(): + assert received.get(name) == value, f"the child saw {name}={received.get(name)!r}" + # under mpirun the parent carries the launcher's own variables too: none of + # them may reach the child, only the kept framework settings + unexpected = sorted(k for k in received if not _kept_mpi_setting(k)) + assert not unexpected, f"launcher variables reached the child: {unexpected}" diff --git a/tests/test_1079_diffusion_disk_restart.py b/tests/test_1079_diffusion_disk_restart.py index aa2afb1e7..b7fd4d377 100644 --- a/tests/test_1079_diffusion_disk_restart.py +++ b/tests/test_1079_diffusion_disk_restart.py @@ -10,7 +10,7 @@ import pytest import sympy import underworld3 as uw -from parallel.serial_reference import _MPI_ENV_PREFIXES +from parallel.serial_reference import _child_env pytestmark = [pytest.mark.tier_b] @@ -87,8 +87,7 @@ def test_symbolic_diffusion_fresh_process_disk_restart(tmp_path): ranks = int(os.environ.get("UW_DIFFUSION_TEST_RANKS", "1")) root = Path(__file__).resolve().parents[1] worker = root / "tests/parallel/ptest_1079_diffusion_disk_restart.py" - env = {k: v for k, v in os.environ.items() - if not k.startswith(_MPI_ENV_PREFIXES)} + env = _child_env(os.environ) launcher = [] if ranks == 1 else [str(Path(sys.executable).with_name("mpirun")), "-np", str(ranks)] for phase in ("write", "resume"): command = [sys.executable, str(root / "scripts/mpi_supervisor.py"),