diff --git a/scripts/test.sh b/scripts/test.sh index b6caaedda..6786f467c 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -268,6 +268,10 @@ if [ $PARALLEL_RANKS -gt 0 ]; then "${PARALLEL_FILES[@]:i:PARALLEL_BATCH}" || status=1 done + # Three ranks exercise a process owning no prescribed-wall groups. + OMPI_MCA_rmaps_base_oversubscribe=1 $SUPERVISE mpirun -n 3 python -m pytest --with-mpi \ + tests/parallel/test_1084_rotated_datum_collectives.py || status=1 + # A SECOND pass at four ranks, because two is a special case: the failure # this suite exists to catch is a collective entered by some ranks and not # others, and with two ranks the mismatched pair often still meets. Both diff --git a/src/underworld3/utilities/rotated_bc.py b/src/underworld3/utilities/rotated_bc.py index 33b5bdad6..8de4ec487 100644 --- a/src/underworld3/utilities/rotated_bc.py +++ b/src/underworld3/utilities/rotated_bc.py @@ -368,6 +368,26 @@ def _eval_datum(dspec, coords, solver): return np.asarray(uw.function.evaluate(dspec, np.ascontiguousarray(coords))).flatten() +def _datum_batches(boundaries, datum_specs, datum_nodes, coordinates, dim): + """Schedule datum evaluation by registered boundary, including empty ranks. + + FE datum evaluation updates a mesh local vector collectively. Scheduling + by locally owned nodes (or local expression identities) can give ranks + different call counts/orders and deadlock before the first Stokes solve. + Registration order is common to all ranks; coordinate arrays may be empty. + """ + if datum_specs is None: + return + for spec in boundaries: + name, _ = _boundary_spec(spec) + dspec = datum_specs.get(name) + if dspec is None: + continue + items = [(row, q, sign) for row, q, boundary, sign in datum_nodes if boundary == name] + coords = np.asarray([coordinates[q] for _, q, _ in items], dtype=float).reshape(-1, dim) + yield dspec, coords, items + + def build_rotation(solver, boundaries, datum_specs=None): """Global sparse rotation Q on the composite saddle vector: identity except a per-node (normal,tangential) block at each velocity node of `boundaries`. @@ -394,7 +414,7 @@ def build_rotation(solver, boundaries, datum_specs=None): for q, nrm in _boundary_velocity_nodes(solver, name, normal=normal): node_normals.setdefault(q, []).append(nrm) if dspec is not None: - node_dspec.setdefault(q, dspec) + node_dspec.setdefault(q, name) # Split-fault contact pairs (fault_contact.add_frictionless_fault_bc): # their 2·dim mean/jump blocks widen the widest row of Q. @@ -421,7 +441,7 @@ def build_rotation(solver, boundaries, datum_specs=None): Q.setValue(i, i, 1.0) # identity default (owned rows) normal_rows = [] - datum_nodes = [] # (grows[0], q, dspec) for simple nodes + datum_nodes = [] # (global row, point, boundary name, normal sign) for q, nrms in node_normals.items(): lo = lsec.getFieldOffset(q, _VELOCITY_FIELD) grows = [int(l2g.apply([lo + c])[0]) for c in range(dim)] @@ -559,19 +579,17 @@ def build_rotation(solver, boundaries, datum_specs=None): Q.assemble() Qt = Q.transpose(PETSc.Mat()) - # Evaluate the prescribed normal datum at each simple constrained node, batched per - # distinct datum spec (usually one — the surface). Rotated row grows[0] == v_n. + # Every rank evaluates every registered datum, even with no owned nodes. + # FE expressions contain collectives; local-only batching can deadlock. datum_map = {} - if datum_nodes: + if datum_specs is not None: csec = dm.getCoordinateSection() cvec = np.asarray(dm.getCoordinatesLocal().array).reshape(-1, dim) v0, v1 = dm.getDepthStratum(0) - by_spec = {} - for grow0, q, dspec, sgn in datum_nodes: - by_spec.setdefault(id(dspec), (dspec, []))[1].append((grow0, q, sgn)) - for dspec, items in by_spec.values(): - coords = np.array([_point_coord(dm, dim, cvec, csec, v0, v1, q) - for _, q, _ in items]) + coordinates = {q: _point_coord(dm, dim, cvec, csec, v0, v1, q) + for _, q, _, _ in datum_nodes} + for dspec, coords, items in _datum_batches( + boundaries, datum_specs, datum_nodes, coordinates, dim): vals = _eval_datum(dspec, coords, solver) for (grow0, _, sgn), val in zip(items, vals): # A zero datum IS free-slip; leave those rows out so an all-zero datum diff --git a/tests/parallel/test_1084_rotated_datum_collectives.py b/tests/parallel/test_1084_rotated_datum_collectives.py new file mode 100644 index 000000000..d79ab9af3 --- /dev/null +++ b/tests/parallel/test_1084_rotated_datum_collectives.py @@ -0,0 +1,47 @@ +"""Prescribed FE wall data must be evaluated in the same order on empty ranks. + +Run serial or with mpiexec. This exercises the datum schedule/evaluator only; +no Stokes model or spherical mesh is run. +""" + +import numpy as np +import pytest +import underworld3 as uw +from mpi4py import MPI + +from underworld3.utilities.rotated_bc import _datum_batches, _eval_datum + + +pytestmark = [pytest.mark.level_1, pytest.mark.tier_b] + + +@pytest.mark.parametrize("empty_rank", [1, 2]) +def test_registered_boundaries_determine_collective_schedule_not_local_nodes(empty_rank): + rank = uw.mpi.rank + boundaries = ["Upper", "Lower"] + # All ranks have the same registration, but zero, one or two local groups. + owned = {0: [(10, 1, "Upper", 1.0), (11, 2, "Lower", -1.0)], + 1: [(20, 2, "Lower", 1.0)]}.get(rank, []) + if rank == empty_rank: + owned = [] + coordinates = {1: [2.0, 0.0], 2: [1.0, 0.0]} + batches = list(_datum_batches(boundaries, {"Upper": 2.0, "Lower": 3.0}, owned, coordinates, 2)) + assert len(batches) == 2 + for expected_datum, (datum, coords, items) in zip([2.0, 3.0], batches): + assert uw.mpi.comm.allgather(datum) == [expected_datum] * uw.mpi.size + assert coords.shape == (len(items), 2) + if rank == empty_rank or rank > 1: + assert coords.shape == (0, 2) + + +def test_fe_datum_evaluation_accepts_empty_coordinate_batches_on_other_ranks(): + mesh = uw.meshing.Annulus(radiusInner=1.22, radiusOuter=2.22, cellSize=1/32, qdegree=2) + field = uw.discretisation.MeshVariable("DatumCollectiveP2", mesh, 1, degree=2, continuous=True) + field.array[:, 0, 0] = 0.13*field.coords[:, 0]-0.2*field.coords[:, 1] + # Only one rank queries nodes; every rank must still enter FE evaluation. + coords = np.asarray(field.coords[:3]) if uw.mpi.rank == 0 else np.empty((0, 2)) + for _ in range(2): + actual = _eval_datum(field.sym[0], coords, None) + expected = 0.13*coords[:, 0]-0.2*coords[:, 1] + np.testing.assert_allclose(actual, expected, atol=1e-12) + assert uw.mpi.comm.allreduce(1, op=MPI.SUM) == uw.mpi.size