Skip to content

Make the isotropic metric on every rank in step (#975) - #1287

Merged
lmoresi merged 2 commits into
developmentfrom
bugfix/metric-on-an-empty-rank-975
Nov 20, 2026
Merged

lmoresi merged 2 commits into
developmentfrom
bugfix/metric-on-an-empty-rank-975

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 20, 2026

Copy link
Copy Markdown
Member

Fixes #975. 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

This is the mechanism of #1271, at another site:

  • The projection: when the indicator is not the mesh's first field, DMPlexMetricCreateIsotropic projects it onto the metric's P1 space with DMProjectFieldLocal on the indicator vector's own DM. That DM is the variable's one-field DM, not mesh.dm.
  • The lazy field: the projection needs that DM's coordinate field, which PETSc creates lazily, the first time a cell's geometry is needed, so only on ranks that hold cells.
  • The tag drift: each object created on the communicator takes a tag from it, so a rank with no cells falls one tag behind. The next two-sided exchange on the communicator, such as a later distribute, then pairs messages under different tags and hangs.

Measured with the reproducer on the issue (cells 8 / 8 / 0 at np 3, the indicator as field 1), the next tag on COMM_WORLD after the call was:

  • development: 536870582, 536870582 and 536870583, and the next distribute hangs;
  • this branch: the same on all three ranks, and the distribute completes.

A gdb comparison of every tag-taking object creation on the ranks has exactly one difference: a coordinate DMField (DMCreateCoordinateField_Plex) made on the ranks with cells and not on the empty rank. With the indicator as field 0, PETSc copies instead of projecting, so the tags agree. That is why H1's probe needed a second field.

The change

adaptivity._isotropic_metric(dm, indicator_vec, field_id) creates the coordinate field on the indicator vector's DM, on every rank, with the existing dm_force_coordinate_field, and then calls metricCreateIsotropic.

  • Mesh.remesh and adaptivity.mesh_adapt_meshVar both use it.
  • The TODO(BUG) in remesh goes.

This was the issue's suggested direction. Option 1 there, building the metric on a DM where the indicator is field 0, is not needed once the field exists on every rank.

Test

tests/parallel/test_0975_metric_on_an_empty_rank_parallel.py uses the empty-rank right triangle (empty_rank_fixtures), with the indicator as the second field.

  • The routes: it runs remesh and mesh_adapt_meshVar. This PETSc has no metric remesher, so both stop at adaptMetric with their plain error, after the metric is made. The test skips if a remesher is present.
  • The checks: that the next tag on COMM_WORLD is the same on every rank, then that a new mesh distributes.
  • np 2: it skips, since that fixture leaves no rank empty.
run development this branch
np 3 2 failed (2 distinct tags) 2 passed
np 4 2 failed 2 passed
np 5 2 failed 2 passed

Regression

  • Serial files that use the metric routes (test_0810, test_0830, test_0834): 4 passed, 6 skipped. The skips need a remesher.
  • Parallel files that use them (test_0783's empty-rank remesh transfer, test_0785 and this file):
    • np 2: 21 passed, 5 skipped;
    • np 3: 24 passed, 2 skipped.
  • scripts/check_test_coverage.py: OK.

Follow-ups from #1271's review (other clone and sub-DM sites, probed one by one) come in a separate PR.

Underworld development team with AI support from Claude Code

When the indicator is not the mesh's first field, PETSc's
DMPlexMetricCreateIsotropic projects it onto the metric's P1 space on the
indicator vector's own DM. The projection needs that DM's coordinate field,
which PETSc creates lazily, only on ranks holding cells. Each object created
on the communicator takes a tag from it, so a rank with no cells fell one
tag behind, and the next two-sided exchange (a later distribute) hung.

adaptivity._isotropic_metric creates the coordinate field on the indicator's
DM first, on every rank, then makes the metric. Mesh.remesh and
mesh_adapt_meshVar both use it; the TODO(BUG) in remesh goes.

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 20, 2026 11:16

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 #1287 at 6f42ac3

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

The fix does what it says. On development, with one rank holding no cells and the indicator as field 1 or 2, the empty rank's next tag on COMM_WORLD is one behind the others after the isotropic metric is made, and the next distribute hangs. This is true at np 3, 4 and 5, through both library routes (Mesh.remesh and adaptivity.mesh_adapt_meshVar). On the branch the tags agree and the distribute completes. With the indicator as field 0 both are clean, as the issue says. The metric values are bit-identical to development, and the cost is too small to measure. No other library code makes a PETSc metric. Three low findings, none blocking.

Method

  • The PR is Python only (adaptivity.py, discretisation_mesh.py and one new test). It calls dm_force_coordinate_field, which development already has. No existing build had compiled sources matching the merge base (233d3c4) exactly, so we built the merge base in a new worktree. Then we laid the head and merge-base .py sources over copies of that installed build. Every run prints underworld3.__file__, and every run used the intended overlay.
  • PETSc 3.25.3 (conda-forge), MPICH, 4 cores. Every parallel run went through the repository's MPI supervisor (silence 60 s) inside an outer timeout. A hang shows as exit 124, with the stacks of every rank. np 5 ran 5 ranks on 4 cores (oversubscribed; MPICH allows it).
  • Empty rank: either the test fixture's right triangle (16 cells on np - 1 ranks: [8, 8, 0] at np 3, [6, 5, 5, 0] at np 4, [4, 4, 4, 4, 0] at np 5), or a partition hook that bands a gmsh box over np - 1 ranks and leaves the last rank with no cells.
  • Tag probe: the same method as the new test. PetscCommDuplicate on COMM_WORLD returns each rank's next tag, and the values are gathered. Spread = the largest minus the smallest. After the metric, the probe builds and distributes a new mesh (the triangle fixture at 36 cells).
  • "lib" route: on the branch, adaptivity._isotropic_metric; on development, the plain mesh.dm.metricCreateIsotropic, which is what development's library code calls.

1. Tag probe, triangle fixture with an empty rank ("lib" route)

np indicator field branch: spread / next distribute development: spread / next distribute
3 0 0 / done 0 / done
3 1 0 / done 1 (empty rank behind) / hangs, exit 124
3 2 0 / done 1 / hangs, exit 124
4 0 0 / done 0 / done
4 1 0 / done 1 / hangs, exit 124
4 2 0 / done 1 / hangs, exit 124
5 (oversubscribed) 0 0 / done 0 / done
5 (oversubscribed) 1 0 / done 1 / hangs, exit 124
5 (oversubscribed) 2 0 / done 1 / hangs, exit 124

In every hang, the supervisor's stacks match the issue's. Every rank is in the next right_triangle_of_cells(). Rank 0 is in PetscCommBuildTwoSided_Allreduce under DMPlexPartitionLabelCreateSF, and the other ranks are in an MPI_Allreduce.

On the branch at field 1, every rank ends on the tag that development's ranks with cells reach (536870582 at np 3). So the forced field is the object the empty rank was missing, and no other tag is taken. At field 0, the branch takes one tag more than development on every rank. That is the forced field, which the copy path does not otherwise make. It is the same on every rank, so it does no harm.

Control: the plain mesh.dm.metricCreateIsotropic call on the branch, at np 3 field 1, still drifts and hangs. The PETSc behaviour is unchanged. Only Underworld's call sites are guarded (finding 3).

2. The two library routes

This PETSc has no metric remesher (hasExternalPackage is False for mmg, parmmg and pragmatic), so both routes make the metric and then raise their plain "needs a PETSc built with a metric remesher" error at adaptMetric. The probe catches the error, then checks the tags and distributes.

route case branch development
Mesh.remesh triangle, np 3 / 4 / 5, field 1 spread 0, done (all three) spread 1, hangs (all three)
mesh_adapt_meshVar triangle, np 3 / 4 / 5, field 1 spread 0, done (all three) spread 1, hangs (all three)
both triangle, np 3, field 2 spread 0, done spread 1, hangs
"lib" and Mesh.remesh gmsh box, empty band, np 3 ([20, 22, 0]) and np 4 ([14, 14, 14, 0]), field 1 spread 0, done spread 1, hangs
Mesh.remesh gmsh box, empty band, np 2 ([42, 0]), field 1 spread 0, done spread 1, hangs

A grep of src for metricCreate, MetricCreate, createMetric, isotropic, adaptMetric and the other metric* PETSc calls finds exactly two calls that make a PETSc metric. Both now go through _isotropic_metric. There is no anisotropic or other PETSc metric path. adaptivity.create_metric and its callers build the metric as a MeshVariable in Python and make no PETSc metric. The coarsening path (dm_hierarchy[i].coarsen()) goes through PETSc's own adapt and is outside this change.

3. Repeated call

The metric was made twice on the same indicator DM, and then dm_force_coordinate_field was called once more on that DM (np 3, fields 1 and 2, branch).

  • Spread 0 after each call, and the next distribute completes.
  • The second metric call takes the same number of tags on every rank. That number is one fewer than the first call took, so the field is made once.
  • The extra force takes no tag (the change seen is the probe's own tag).
  • The second metric's values equal the first's.

On development the drift of 1 stays after the second call and the distribute hangs.

4. Answers

The metric vector was gathered on each rank, branch against development, at fields 0, 1 and 2:

case entries compared (field 0 / 1 / 2) result
np 1, triangle (16 cells) 15 / 60 / 105 bit-identical
np 3, triangle on every rank ([6, 5, 5]) 25 / 87 / 149 bit-identical
np 3, gmsh box, band partition ([14, 14, 14]) 44 / 171 / 298 bit-identical

5. Cost

The time to make the metric (largest over the ranks) was 0.40-0.57 ms on both branch and development in every run, np 1 to 5. The differences are within run-to-run noise. The forced field is made once per indicator DM.

6. The new test

np branch development (merge base)
2 2 skipped (rank-count reason) 2 skipped
3 2 passed 2 failed: AssertionError: [536870583, 536870583, 536870584]
4 2 passed 2 failed
5 (oversubscribed) 2 passed 2 failed

Development fails at the tag assertion, before the distribute, so the test fails fast rather than hanging. The remesher skip is reached only when hasExternalPackage reports mmg, parmmg or pragmatic. This PETSc has none, so here the test always ran.

7. Neighbouring tests, branch vs development

Serial (4 xdist workers): test_0810, test_0830, test_0834, src/underworld3/tests/test_adaptivity_metrics.py, test_0750, test_0751, test_0762, test_0764, test_0835, test_0836, test_0843. Both: 85 passed, 6 skipped. The skips need a remesher.

Parallel, np 2 and np 3:

file np 2 (both) np 3 (both)
test_0783_remesh_empty_rank_transfer 5 passed, 2 skipped 6 passed, 1 skipped
test_0785_evaluated_values_reach_storage_parallel 16 passed, 1 skipped 16 passed, 1 skipped
ptest_0763_mesh_adapt_transfer 1 skipped 1 skipped
test_0873_adapt_collective_stop_mpi 4 passed 4 passed
test_1082_mmpde_mover_empty_rank_parallel 1 passed, 1 skipped 2 passed
test_1132_metric_density_shared_nodes_parallel 8 passed 8 passed
test_1189_adapt_nvb_rank_without_cells_parallel 1 skipped 1 passed
test_1122_adapt_node_budget_global_parallel 7 passed 7 passed

Every (file, np) pair has the same outcome on branch and development.

8. Reading the diff

  • _isotropic_metric has no rank-local branch. Every rank calls dm_force_coordinate_field(indicator_vec.getDM()) and then metricCreateIsotropic, including ranks with no cells.
  • Both call sites are reached by every rank. mesh_adapt_meshVar has no branch before the metric.
  • remesh has one rank-local branch above the metric, which this PR does not touch. When the mesh has regions, if cell_is: decides per rank whether _CellRegions_ is created and whether rgLabel_name is set. On a rank with no cells that may differ from the other ranks. A label lives on the rank's own communicator, so it should take no COMM_WORLD tag. But a differing rgLabel_name would reach adaptMetric. Our meshes have no regions, so this branch did not run. It matters only once a parallel remesher is in the build, and it is out of scope here.
  • The TODO(BUG) in remesh has been removed completely, and the new comment says why the field is forced. Nothing else is left of it.
  • from underworld3 import adaptivity was already imported in remesh before this change.

Findings

  1. Low: the test does not cover np 2, but an empty rank can occur there. The skip is right for the triangle fixture, which Mesh redistributes at np 2. But a gmsh mesh with a shell partition leaves a rank empty at np 2 ([42, 0]), and development hangs there through Mesh.remesh while the branch passes (table 2). A case built that way would cover np 2, which is the rank count CI is most likely to run. This is optional. Separately, the skip reason ("the fixture's one cell is redistributed onto both ranks") reads oddly, since the fixture has 16 cells, all on one rank. Something like "the fixture's cells, all on one rank, are redistributed onto both ranks" would be clearer.
  2. Low: dm_force_coordinate_field does more than create the field. It also removes every label except depth and celltype from the indicator DM's coordinate DM (the Parallel bug - Calculating a boundary integral causes the next Stokes solve to hang #96 fix). The new docstring mentions only the field. We found no effect in any run here, and the mesh constructor already does the same to mesh.dm. We did not trace whether the indicator's sub-DM shares its coordinate DM with mesh.dm. One clause in the docstring would save the next reader from looking it up.
  3. Low: the PETSc behaviour is unchanged, and only Underworld's two call sites are guarded. A user script that calls mesh.dm.metricCreateIsotropic directly on an indicator that is not field 0 still drifts and hangs on the branch (control in table 1). The issue's option 2, a PETSc report with the reproducer, still stands. Not for this PR.

What was run and not run

Run: the probes above at np 1, 2, 3, 4 and 5 (np 5 oversubscribed on 4 cores), branch and development; the new test at np 2-5; the serial and parallel neighbours listed above.

Not run:

  • A real adaptMetric: this PETSc has no mmg, parmmg or pragmatic, so nothing here adapts on any rank count.
  • Random partitions: only band and empty-band.
  • 3-D meshes.
  • The full test suite.

Other jobs on the machine were running at the same time on other cores. Timings are indicative only. Logs kept by the reviewer.

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit 07f450b into development Nov 20, 2026
10 checks passed
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