Skip to content

Solver DM clones create their coordinate field on every rank - #1271

Merged
lmoresi merged 3 commits into
developmentfrom
bugfix/empty-rank-labels-load
Nov 20, 2026
Merged

lmoresi merged 3 commits into
developmentfrom
bugfix/empty-rank-labels-load

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 20, 2026

Copy link
Copy Markdown
Member

A fix for two empty-rank failures found in the review of #1232. There is no issue; both have the same cause. Review: H1, the session that reviews pull requests and runs the parallel tests on a 96-thread Linux workstation (single system image; Intel Xeon Gold 6240R, AVX-512; MPICH).

The cause

Every PETSc object created on a communicator takes a tag from it. PETSc's two-sided exchanges, such as an SF set-up or a matrix stash, take theirs from the same counter, so the counter must advance in step on every rank.

A solver works on clones of the mesh DM (Mesh.clone_dm_hierarchy). DMClone copies the coordinates but not the coordinate DMField over them, so PETSc creates that field lazily, the first time an assembly needs a cell's geometry. A rank holding no cells does not need it in the residual-only evaluations, so after a solve it has created fewer objects on the mesh's communicator, and its tag counter is behind the others'. The next two-sided exchange on that communicator then pairs messages under different tags.

Measured at np 3 with one rank empty, after one viscoelastic solve: the next tag on COMM_WORLD was 536863619, 536863619 and 536863621 on the three ranks. A gdb trace of every tag-taking object creation, compared rank by rank, has exactly one difference: two DMFieldCreate calls under DMCreateCoordinateField_Plex, from DMPlexComputeResidualByKey on three solver clones, made on the ranks with cells and not on the empty rank.

The two symptoms

  1. np 3, one rank empty. After a viscoelastic solve, the next gmsh mesh load hangs in its label read (ReadLabelStratumHDF5). Two ranks are in an SF reduce and the empty rank in an SF broadcast, all in PetscCommBuildTwoSided. This is why test_1107's parallel file hangs at np 3 under a test-only shell partition that leaves the last rank empty.
  2. np 2, one rank empty. A semi-Lagrangian history on an add_fault child, after a solve on the unfaulted parent, fails in UW_DMCreateInterpolationWithinPlex with an MPI error ("Invalid buffer pointer" in PetscCommBuildTwoSided_Allreduce, under MatAssemblyBegin_Preallocator) on one rank, while the other waits.

The change

clone_dm_hierarchy creates the coordinate field on each clone as it makes it, where every rank clones. It uses the existing dm_force_coordinate_field, which the mesh already calls on its own DM, so the field is never created lazily inside an assembly. Merged with #1244: the coarse levels are moved with the finest first, as before.

Test

tests/parallel/test_1107_mesh_load_after_a_solve_on_an_empty_rank_parallel.py runs a viscoelastic Stokes solve on the empty-rank right triangle (empty_rank_fixtures). It skips at np 2, where that fixture leaves no rank empty. It then makes two checks:

  • that the communicator's next tag is the same on every rank (read through PetscCommDuplicate);
  • that a gmsh mesh then loads.
run development this branch
next tag the same on every rank, np 3 / 4 / 5 fails (2 distinct tags) passes
gmsh load after the solve, np 3 hangs (124) passes, np 3 / 4 / 5
test_1107 parallel file, np 3, last rank empty (shell partition) hangs (124) 3 passed
symptom 2's driver on #1232's base, np 2, last rank empty MPI error, then hang completes on that base with this change applied

Regression

These runs used a Python overlay, since the change is Python only.

  • Serial suite: all batches pass. Its last part was rerun on its own after a time limit: 600 passed, 4 skipped.
  • np 2 parallel phase of scripts/test.sh: no failure or hang.
  • After merging development (The coarse levels of a refined hierarchy move with the finest level #1244):
    • test_1240, test_1176, test_1173 and test_0764 serially: 58 passed;
    • test_1103, test_1176 and test_0874's custom_mg build at np 2 and 3: 16 passed, 3 skipped each.
  • scripts/check_test_coverage.py: OK.

#975 (metricCreateIsotropic on an empty rank)

This is the same mechanism, but this PR does not fix it. With the reproducer on the issue (cells 8 / 8 / 0 at np 3, the indicator not field 0), the next tag after metricCreateIsotropic is 536870582, 536870582 and 536870583, and the next distribute hangs, on development and on this branch alike. With the indicator as field 0, the tags agree and it completes.

The gdb comparison again has exactly one difference: a coordinate DMField created by DMCreateCoordinateField_Plex on ranks 0 and 1, and not on the empty rank. That DM is made inside the metric call, out of clone_dm_hierarchy's reach. So #975's work-around (option 1 on the issue: build the metric on a DM where the indicator is field 0, or with the field forced first) still stands.

Underworld development team with AI support from Claude Code

A clone of the mesh DM does not carry the coordinate field (DMClone copies
the coordinates, not the DMField over them), so PETSc created it lazily, the
first time an assembly needed a cell's geometry. A rank holding no cells did
not need it in the residual-only evaluations, so after a solve it had created
fewer objects on the mesh's communicator. Each object takes a tag from that
communicator, so the ranks' tag counters drifted apart, and the next two-sided
exchange on it waited on messages under a tag the other ranks never use:

- np 3, one rank empty: after a viscoelastic solve, the next gmsh mesh load
  hung in its label read (ReadLabelStratumHDF5);
- np 2, one rank empty: a semi-Lagrangian history on an add_fault child
  failed in UW_DMCreateInterpolationWithinPlex's matrix assembly with an MPI
  error on one rank while the other waited.

clone_dm_hierarchy now creates the field on each clone, where every rank
clones. The new parallel test compares the communicator's next tag over the
ranks after a solve with one rank empty, then loads a gmsh mesh.

Underworld development team with AI support from Claude Code
…ank-labels-load

# Conflicts:
#	src/underworld3/discretisation/discretisation_mesh.py
Copilot AI balanced review requested due to automatic review settings November 20, 2026 06:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@lmoresi

lmoresi commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Review from the Underworld development team with AI support from Claude Code (H1: review and parallel test runs on a 96-thread Linux workstation (Intel Xeon Gold 6240R, AVX-512; MPICH))
(H1 is the session that reviews pull requests and runs the parallel tests.)

Review of #1271 at c756d45

Verdict at c756d45: ready to merge on green from this review.

The fix does what it says. After a viscoelastic solve with one rank empty, development's ranks disagree on the next tag on COMM_WORLD by 1, and the next gmsh load hangs in its label read. On the branch the tags agree and the load finishes. Every solver answer we compared is bit-identical to development, a restart round trip gives the same answers, and the cost cannot be seen in a solve. #975 is still live on the branch, as the author says. Three low findings, none blocking.

Method

  • The PR is Python only (discretisation_mesh.py and one new test). It calls dm_force_coordinate_field, which development already has. So we laid the head and merge-base (9564201) .py sources over one installed build whose compiled sources match the merge base exactly. Every run prints underworld3.__file__, and every run used the intended overlay.
  • MPICH, cores 0-3 only. Every parallel run went through the repository's MPI supervisor (silence 60 s, or 120-300 s for the slower solves and restarts) inside an outer timeout. A hang shows as exit 124 with the stacks of every rank.
  • Empty rank: either the test fixture's triangle mesh, or a partition hook that bands a gmsh mesh over np - 1 ranks and leaves the last rank with no cells ("emptyband": cells [680, 678, 0] at np 3).
  • Tag probe: the same method as the new test. PetscCommDuplicate on COMM_WORLD returns the next tag on each rank, and the values are gathered and compared.

1. The new test

np head development (merge base)
3 2 passed tag test fails; the load hangs in _from_plexh5 (every rank in PetscSFSetUp under PetscCommBuildTwoSided), exit 124
4 2 passed same: fails, then hangs, exit 124
5 (5 ranks on 4 cores, oversubscribed; MPICH allows it) 2 passed same: hangs in ReadLabelStratumHDF5_Static, exit 124

2. Tag probe after one solve of each kind (np 3, gmsh box, cells [680, 678, 0])

Each solve is followed by a gmsh load. Spread = the largest minus the smallest next tag over the ranks.

solve head: spread after the solve head: load development: spread development: load
Stokes, viscous 0 done 0 done
VE Stokes (VEP model, order 1), 3 steps 0 / 0 / 0 done 1 / 1 / 1 (the empty rank is 1 behind) hangs in ReadLabelStratumHDF5_Static, exit 124
SLCN adv-diff, 3 steps 0 done 0 done
Stokes on a refined mesh (2 levels, default solver) 0 done 0 done

The same probes with every rank holding cells (np 3 and np 2): spread 0 everywhere, on both. On the refined mesh, head uses one more tag than development, on every rank. That is the coarse level's field, now made at clone time.

3. Solver tests and answers, head vs development (check 1)

Files chosen from tests/parallel by grep (VE/stress, adv-diff/SLCN/SUPG, Stokes/MG/fault):
test_0780_ve_stokes_first_solve_mpi (needs np 4, run at np 4), test_1062_forward_stress_history_mpi, test_1107_stress_history_fault_guard_parallel, test_1077_advdiff_supg_parallel, test_1078_navier_stokes_supg_parallel, test_1236_slcn_step_on_an_empty_rank_parallel, test_0781_empty_rank_mesh_sequence, test_1105_level_set_mass_shift_is_one_constant_parallel, test_0786_stokes_rank_without_cells_parallel, test_1179_velocity_nullspace_rank_without_cells_parallel, test_1074_free_surface_on_an_empty_rank_parallel, test_1017_custom_mg_parallel_mpi, test_1089_fault_split_multigrid_parallel, test_1114_mg_coarse_gamg_parallel, test_1064_rotated_freeslip_parallel, test_0078_rebuild_memory_parallel, and test_0768_unowned_boundary_natural_bc_mpi for natural boundary conditions.

np 2 np 3
pairs (file, np) with the same outcome on head and development 17 of 17 17 of 17
failures none test_1114 [rotated] fails on both: iterations (4, 10) against a bound of (4, 9). It fails on development too, so it is not this PR

Skips are the same on both, and they come from the tests' own rank-count limits. test_0780 passes at np 4 on both.

The probe's solution norms, head vs development:

solve np 2 np 3 np 3, empty rank
Stokes: ‖v‖, ‖p‖ identical identical identical
VE Stokes, 3 steps identical identical identical
SLCN, 3 steps: ‖T‖, ∫T identical identical identical
refined-mesh Stokes identical identical identical

Identical means all 13 printed digits match.

4. Restart round trip (check 2)

The model is Stokes driven by a temperature field, plus SLCN advection-diffusion of that field, for 4 steps. It is checkpointed after step 2 with write_timestep (mesh, v, p, T, plus the native PETSc payload). A fresh process reloads it and runs steps 3-4. It is written at np 2, at np 3, and at np 3 with an empty rank. It is read back at np 1, 2 and 3 with read_timestep, and at the writer's np with read_checkpoint. The answer is T at 49 fixed points.

The SLCN history is not in the checkpoint, so a restart cannot match an unbroken run exactly (max difference 2.4e-2 at the points, the same on head and development). The control is therefore an unbroken run that rebuilds its solvers after step 2.

writer readers max |restart - control| head vs development (same file)
np 2 ck np 2; ts np 1, 2, 3 1.46e-5 to 1.59e-5 0 (one case 1.5e-14)
np 3 ck np 3; ts np 1, 2, 3 1.46e-5 to 1.58e-5 ≤ 1.4e-14
np 3, cells [315, 299, 0] ck np 3; ts np 1, 2 (reload [614, 0]), 3 (reload [315, 299, 0]) 1.46e-5 to 1.59e-5 0

The restart-vs-control gap is the same on both. The native reader gives the same gap as the remap reader, so the gap comes from what the model does not checkpoint, not from the reload. All runs exit 0, and the tags agree at the end of every phase. A swarm was not included.

5. #975 (check 3)

The issue's narrowing probe, at np 3 with cells [8, 8, 0]:

case head development
indicator as field 0 tags agree; next distribute done same
indicator as field 1 spread 1 after metricCreateIsotropic; next distribute hangs (rank 0 in PetscCommBuildTwoSided under DMPlexPartitionLabelCreateSF), exit 124 same

So #975 is not fixed, as the author says. The issue's option 1 stands.

6. Cost

head development
clone_dm_hierarchy, 1 level, 151,702 cells, serial (median of 10) 0.14 ms 0.13 ms
clone_dm_hierarchy, 4 levels, 153,600 fine cells 1.68 ms 0.33 ms
RSS per retained hierarchy clone, 151,702 cells < 0.01 MB < 0.01 MB
VE Stokes, 3 steps, np 3 (3 runs) 4.80 / 4.81 / 4.95 s 5.23 / 4.78 / 5.01 s
refined-mesh Stokes solve, np 3 (3 runs) 0.56 / 0.54 / 0.73 s 0.54 / 0.52 / 0.60 s

The extra 1.3 ms on a 4-level hierarchy is paid once per solver setup. The solve times differ by no more than the run-to-run spread.

Reading the diff

  • The field is forced on every rank: clone_dm_hierarchy is collective, the only call before the loop (_move_coarse_levels_with_the_finest) is collective, and nothing in the loop depends on the rank. The probes confirm a rank with no cells takes the same tag as the others.
  • It is forced on every level of the hierarchy, coarse levels included: the loop covers all of dm_hierarchy.
  • We checked serially that a clone does not inherit the field: forcing it on a fresh clone takes one tag, on every level.
  • UW_DMForceCoordinateField also strips every label except depth and celltype from the clone's coordinate DM. The clone has its own coordinate DM, and the original's labels were checked unchanged. See finding 2.

Findings

  1. Low: other DM clones still make the field lazily. The library has other clones and sub-DMs that are not made by clone_dm_hierarchy. If geometry is ever assembled on one of them on the ranks with cells only, it would drift the same way. Candidates to probe as follow-ups (not run here):

    • the scratch DM in function/gradient_evaluation.py (two sites)
    • the coordinate-DM clones in utilities/boundary_flux.py and in MeshVariable's coordinate write
    • the custom multigrid clones in utilities/custom_mg.py (the level wrap's self.dm = dm.clone(), the coarse reduced map, the adapt clone)
    • the frozen DM in meshing/smoothing/mmpde.py
    • the createSubDM sites in adjoint.py and discretisation_mesh_variables.py
    • the clones in fault_split.py, edge_split.py and line_cut.py

    metricCreateIsotropic on an empty rank leaves a later distribute hanging #975 is the known case of this kind. The probe from the new test, a tag gather after each of these paths with an empty rank, is a cheap way to sort them.

  2. Low: the helper's second effect is not mentioned. dm_force_coordinate_field also removes the boundary labels from the clone's coordinate DM. On a refined mesh the finest level's coordinate DM carries the boundary labels (Bottom, Top, Left, Right, All_Boundaries, UW_Boundaries), and its solver clone now loses them. That is the Parallel bug - Calculating a boundary integral causes the next Stokes solve to hang #96 work-around and no test changed. A sentence in the new comment would stop the next reader wondering.

  3. Low: a docstring is now inaccurate. _rebuild_level_coordinate_field says "a solver's clone of the level inherits it". It does not, which is the cause of this bug. Worth correcting in the same PR.

Run and not run

Run: the new test at np 3, 4 and 5 (np 5 oversubscribed on 4 cores) on head and development. 17 solver test files at np 2 and 3 on both (test_0780 at np 4). A tag and answer probe for Stokes, VE, SLCN and refined-mesh Stokes at np 2, np 3, and np 3 with an empty rank. The restart round trip above. #975's probe on both. The cost and memory measurements.

Not run:

Logs kept by the reviewer.

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit 9a38b36 into development Nov 20, 2026
10 checks passed
lmoresi added a commit that referenced this pull request Nov 24, 2026
…ield-sites

Lazy coordinate fields: an np 2 empty-rank case and the notes from #1271's review
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants