From 4b674d38b101d391960c82ed1d207a3f0d1e0061 Mon Sep 17 00:00:00 2001 From: lmoresi Date: Fri, 20 Nov 2026 08:56:02 +1100 Subject: [PATCH 1/3] An SLCN history record takes one route on every rank, an empty rank included In parallel the semi-Lagrangian history records the current field by copying the tracked variable's nodal data. On a rank with no nodes the copy's reshape(0, -1) raised (NumPy cannot infer -1 from zero elements), the exception sent that rank alone to evaluate psi_fn, and the two routes enter different collectives: an SLCN step hung at np 3 with an empty rank (found in the review of #1234). The column counts now come from the arrays' trailing shape, and the ranks agree on the route with a logical AND. Underworld development team with AI support from Claude Code --- scripts/test.sh | 3 ++ src/underworld3/systems/ddt.py | 12 ++++- ...236_slcn_step_on_an_empty_rank_parallel.py | 51 +++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 tests/parallel/test_1236_slcn_step_on_an_empty_rank_parallel.py diff --git a/scripts/test.sh b/scripts/test.sh index 71ebf2a5f..a812a07f7 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -620,6 +620,9 @@ if [ $PARALLEL_RANKS -gt 0 ] && shard_owns 4; then # onto both ranks and the test skips, so it runs at np 3 always. OMPI_MCA_rmaps_base_oversubscribe=1 $SUPERVISE mpirun -n 3 python -m pytest --with-mpi \ tests/parallel/test_1069_boundary_label_on_some_ranks_parallel.py || status=1 + # An SLCN step with an empty rank: the fixture empties a rank from np 3 only. + OMPI_MCA_rmaps_base_oversubscribe=1 $SUPERVISE mpirun -n 3 python -m pytest --with-mpi \ + tests/parallel/test_1236_slcn_step_on_an_empty_rank_parallel.py || status=1 # A free surface with a rank without cells: the shell partition empties a rank # from np 3 (at np 2 it would leave one rank holding every cell), so np 3 always. OMPI_MCA_rmaps_base_oversubscribe=1 $SUPERVISE mpirun -n 3 python -m pytest --with-mpi \ diff --git a/src/underworld3/systems/ddt.py b/src/underworld3/systems/ddt.py index 8059128d4..a14df0537 100644 --- a/src/underworld3/systems/ddt.py +++ b/src/underworld3/systems/ddt.py @@ -3555,10 +3555,14 @@ def _record_psi_star_from_field_data(self): # layout falls back to evaluate. if var.vtype not in (uw.VarType.SCALAR, uw.VarType.VECTOR): return None + # Columns from the trailing shape, not reshape(n, -1): a rank with + # no nodes holds zero rows, and NumPy cannot infer -1 from zero + # elements. The ValueError sent that rank alone down the evaluate + # route, and the step hung (review of #1234). vflat = np.asarray(var.data) - vflat = vflat.reshape(vflat.shape[0], -1) + vflat = vflat.reshape(vflat.shape[0], int(np.prod(vflat.shape[1:]))) store = np.asarray(self.psi_star[0].data) - if vflat.shape[0] != store.shape[0] or store.reshape(store.shape[0], -1).shape[1] != 1: + if vflat.shape[0] != store.shape[0] or int(np.prod(store.shape[1:])) != 1: return None return np.array(vflat[:, comp]).reshape(-1, 1, 1) except Exception: @@ -3777,6 +3781,10 @@ def _record_current_field_into_history( # psi_fn (those fall back to evaluate). _direct = (self._record_psi_star_from_field_data() if (uw.mpi.size > 1 or oldframe_active) else None) + # One route on every rank: the routes differ in their collectives. + if uw.mpi.size > 1 and not uw.mpi.comm.allreduce(_direct is not None, + op=uw.MPI.LAND): + _direct = None if _direct is not None: eval_result = _direct else: diff --git a/tests/parallel/test_1236_slcn_step_on_an_empty_rank_parallel.py b/tests/parallel/test_1236_slcn_step_on_an_empty_rank_parallel.py new file mode 100644 index 000000000..77ad533d1 --- /dev/null +++ b/tests/parallel/test_1236_slcn_step_on_an_empty_rank_parallel.py @@ -0,0 +1,51 @@ +"""An SLCN step with a rank that holds no cells (review of #1234). + +In parallel the semi-Lagrangian history records the current field by copying +the tracked variable's nodal data. On a rank with no nodes the copy's +``reshape(0, -1)`` raised, the rank fell back to evaluating ``psi_fn``, and the +two routes enter different collectives: the step hung at np 3 with an empty +rank. The ranks now agree on the route, and the copy reads its column count +from the array's shape. + +The field x + y with that value on the boundary, no flow and unit diffusivity +is a steady solution. In parallel the record is the nodal copy, so the field +is held to round-off. Serially it is not: the serial record writes each node +the value at a point nudged towards its cell centroid, which moved this field +by 7.5e-5 in two steps (measured serially on the same fixture). That is why +the test runs from np 3 only. +""" + +import numpy as np +import pytest + +import underworld3 as uw +from empty_rank_fixtures import (ALL_BOUNDARIES_VALUE, cells_along_a_side_leaving_one_rank_empty, + cells_per_rank, right_triangle_of_cells) + +pytestmark = [pytest.mark.level_1, pytest.mark.tier_b, pytest.mark.timeout(300)] + + +@pytest.mark.skipif(uw.mpi.size < 3, reason="at np 2 the fixture's one cell is redistributed " + "onto both ranks, so no rank is empty") +def test_an_slcn_step_with_an_empty_rank_completes(): + size = uw.mpi.size + dm = right_triangle_of_cells(cells_along_a_side_leaving_one_rank_empty(size), + max(size - 1, 1)) + dm.createLabel("All_Boundaries") + dm.markBoundaryFaces("All_Boundaries", ALL_BOUNDARIES_VALUE) # collective + mesh = uw.discretisation.Mesh(dm, qdegree=3) + assert 0 in cells_per_rank(mesh) + x, y = mesh.X + T = uw.discretisation.MeshVariable("T1236", mesh, 1, degree=2) + X = np.asarray(T.coords) + T.array[:, 0, 0] = X[:, 0] + X[:, 1] if X.size else np.empty(0) + V = uw.discretisation.MeshVariable("V1236", mesh, 2, degree=1) # no flow + adv = uw.systems.AdvDiffusionSLCN(mesh, u_Field=T, V_fn=V.sym, order=1) + adv.constitutive_model = uw.constitutive_models.DiffusionModel + adv.constitutive_model.Parameters.diffusivity = 1.0 + adv.add_dirichlet_bc([x + y], "All_Boundaries") + adv.tolerance = 1.0e-10 + for _ in range(2): + adv.solve(timestep=0.01) + error = float(np.abs(np.asarray(T.array).ravel() - (X[:, 0] + X[:, 1])).max()) if X.size else 0.0 + assert uw.mpi.comm.allreduce(error, op=uw.MPI.MAX) < 1.0e-9 From 12f13bbc1909cc676c85687d1ec198966bd0336b Mon Sep 17 00:00:00 2001 From: lmoresi Date: Fri, 20 Nov 2026 08:57:56 +1100 Subject: [PATCH 2/3] The empty-rank SLCN test compares with the same steps on a partition without an empty rank Both runs take the parallel route, so the comparison does not depend on the serial record's centroid nudge; a loose check holds x + y. Underworld development team with AI support from Claude Code --- ...236_slcn_step_on_an_empty_rank_parallel.py | 49 +++++++++++++------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/tests/parallel/test_1236_slcn_step_on_an_empty_rank_parallel.py b/tests/parallel/test_1236_slcn_step_on_an_empty_rank_parallel.py index 77ad533d1..d1c4d1650 100644 --- a/tests/parallel/test_1236_slcn_step_on_an_empty_rank_parallel.py +++ b/tests/parallel/test_1236_slcn_step_on_an_empty_rank_parallel.py @@ -7,12 +7,12 @@ rank. The ranks now agree on the route, and the copy reads its column count from the array's shape. -The field x + y with that value on the boundary, no flow and unit diffusivity -is a steady solution. In parallel the record is the nodal copy, so the field -is held to round-off. Serially it is not: the serial record writes each node -the value at a point nudged towards its cell centroid, which moved this field -by 7.5e-5 in two steps (measured serially on the same fixture). That is why -the test runs from np 3 only. +Two SLCN steps of the field x + y, with that value on the boundary, no flow +and unit diffusivity, on the triangle with one rank empty, are compared with +the same steps on the same triangle shared among every rank. Both take the +parallel route, so they differ by the partition's round-off only. A loose +check against x + y holds the answer, whatever the record's centroid nudge +does (serially it moved this field by 7.5e-5 in two steps). """ import numpy as np @@ -25,21 +25,19 @@ pytestmark = [pytest.mark.level_1, pytest.mark.tier_b, pytest.mark.timeout(300)] -@pytest.mark.skipif(uw.mpi.size < 3, reason="at np 2 the fixture's one cell is redistributed " - "onto both ranks, so no rank is empty") -def test_an_slcn_step_with_an_empty_rank_completes(): +def _two_steps(ranks_with_cells, tag): + """The field after two steps, and its node coordinates, on this rank.""" size = uw.mpi.size dm = right_triangle_of_cells(cells_along_a_side_leaving_one_rank_empty(size), - max(size - 1, 1)) + ranks_with_cells) dm.createLabel("All_Boundaries") dm.markBoundaryFaces("All_Boundaries", ALL_BOUNDARIES_VALUE) # collective mesh = uw.discretisation.Mesh(dm, qdegree=3) - assert 0 in cells_per_rank(mesh) x, y = mesh.X - T = uw.discretisation.MeshVariable("T1236", mesh, 1, degree=2) + T = uw.discretisation.MeshVariable(f"T1236{tag}", mesh, 1, degree=2) X = np.asarray(T.coords) T.array[:, 0, 0] = X[:, 0] + X[:, 1] if X.size else np.empty(0) - V = uw.discretisation.MeshVariable("V1236", mesh, 2, degree=1) # no flow + V = uw.discretisation.MeshVariable(f"V1236{tag}", mesh, 2, degree=1) # no flow adv = uw.systems.AdvDiffusionSLCN(mesh, u_Field=T, V_fn=V.sym, order=1) adv.constitutive_model = uw.constitutive_models.DiffusionModel adv.constitutive_model.Parameters.diffusivity = 1.0 @@ -47,5 +45,26 @@ def test_an_slcn_step_with_an_empty_rank_completes(): adv.tolerance = 1.0e-10 for _ in range(2): adv.solve(timestep=0.01) - error = float(np.abs(np.asarray(T.array).ravel() - (X[:, 0] + X[:, 1])).max()) if X.size else 0.0 - assert uw.mpi.comm.allreduce(error, op=uw.MPI.MAX) < 1.0e-9 + return mesh, np.asarray(T.array).reshape(-1), X + + +def _gathered(values, X): + """Every rank's (x, y, value), sorted by position, on every rank.""" + rows = np.column_stack([X, values]) if X.size else np.empty((0, 3)) + table = np.vstack(uw.mpi.comm.allgather(rows)) + table = np.unique(np.round(table, 12), axis=0) # shared nodes appear once + return table[np.lexsort((table[:, 1], table[:, 0]))] + + +@pytest.mark.skipif(uw.mpi.size < 3, reason="at np 2 the fixture's one cell is redistributed " + "onto both ranks, so no rank is empty") +def test_an_slcn_step_with_an_empty_rank_matches_one_without(): + empty_mesh, empty_values, empty_X = _two_steps(uw.mpi.size - 1, "e") + assert 0 in cells_per_rank(empty_mesh) + full_mesh, full_values, full_X = _two_steps(uw.mpi.size, "f") + assert 0 not in cells_per_rank(full_mesh) + with_empty, without = _gathered(empty_values, empty_X), _gathered(full_values, full_X) + assert with_empty.shape == without.shape + np.testing.assert_allclose(with_empty[:, :2], without[:, :2], rtol=0, atol=1e-12) + np.testing.assert_allclose(with_empty[:, 2], without[:, 2], rtol=0, atol=1e-10) + assert np.abs(with_empty[:, 2] - with_empty[:, 0] - with_empty[:, 1]).max() < 1e-4 From 082c3f4af6fda6badffc898afaeb87dfb83fef63 Mon Sep 17 00:00:00 2001 From: lmoresi Date: Fri, 20 Nov 2026 10:05:49 +1100 Subject: [PATCH 3/3] The empty-rank SLCN test's bound between partitions follows the solve tolerance (review of #1237) At a tolerance of 1e-10 two partitions differ by 2.4e-9 (measured by the reviewer at np 3); the bound is now 100 times the tolerance. The docstring gives the reviewer's route numbers: neither route holds x + y exactly. Underworld development team with AI support from Claude Code --- ...st_1236_slcn_step_on_an_empty_rank_parallel.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/parallel/test_1236_slcn_step_on_an_empty_rank_parallel.py b/tests/parallel/test_1236_slcn_step_on_an_empty_rank_parallel.py index d1c4d1650..54567655e 100644 --- a/tests/parallel/test_1236_slcn_step_on_an_empty_rank_parallel.py +++ b/tests/parallel/test_1236_slcn_step_on_an_empty_rank_parallel.py @@ -10,9 +10,12 @@ Two SLCN steps of the field x + y, with that value on the boundary, no flow and unit diffusivity, on the triangle with one rank empty, are compared with the same steps on the same triangle shared among every rank. Both take the -parallel route, so they differ by the partition's round-off only. A loose -check against x + y holds the answer, whatever the record's centroid nudge -does (serially it moved this field by 7.5e-5 in two steps). +parallel route, so they differ by what the solve tolerance leaves on two +partitions: 2.4e-9 at a tolerance of 1e-10 (measured by the reviewer at np 3). +The bound is 100 times the tolerance, far below the 5e-5 that separates the +serial route from the parallel one. A loose check against x + y holds the +answer; neither route keeps it exactly (in two steps the field moves 7.5e-5 +from x + y serially and 2.5e-5 at np 3, measured by the reviewer). """ import numpy as np @@ -24,6 +27,8 @@ pytestmark = [pytest.mark.level_1, pytest.mark.tier_b, pytest.mark.timeout(300)] +TOLERANCE = 1.0e-10 + def _two_steps(ranks_with_cells, tag): """The field after two steps, and its node coordinates, on this rank.""" @@ -42,7 +47,7 @@ def _two_steps(ranks_with_cells, tag): adv.constitutive_model = uw.constitutive_models.DiffusionModel adv.constitutive_model.Parameters.diffusivity = 1.0 adv.add_dirichlet_bc([x + y], "All_Boundaries") - adv.tolerance = 1.0e-10 + adv.tolerance = TOLERANCE for _ in range(2): adv.solve(timestep=0.01) return mesh, np.asarray(T.array).reshape(-1), X @@ -66,5 +71,5 @@ def test_an_slcn_step_with_an_empty_rank_matches_one_without(): with_empty, without = _gathered(empty_values, empty_X), _gathered(full_values, full_X) assert with_empty.shape == without.shape np.testing.assert_allclose(with_empty[:, :2], without[:, :2], rtol=0, atol=1e-12) - np.testing.assert_allclose(with_empty[:, 2], without[:, 2], rtol=0, atol=1e-10) + np.testing.assert_allclose(with_empty[:, 2], without[:, 2], rtol=0, atol=100 * TOLERANCE) assert np.abs(with_empty[:, 2] - with_empty[:, 0] - with_empty[:, 1]).max() < 1e-4