Skip to content

Size a multigrid generation by one percentile over the whole mesh (#865) - #1121

Merged
lmoresi merged 5 commits into
developmentfrom
bugfix/mg-level-selection-rank-independent-865
Nov 13, 2026
Merged

lmoresi merged 5 commits into
developmentfrom
bugfix/mg-level-selection-rank-independent-865

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

What this does

Mesh.adapt keeps one refinement generation as a multigrid level per halving of the cell size the mesh resolves with. That size was the smallest of the ranks' own 5th-percentile cell diameters, so it depended on how the cells were dealt out: on a box of 968 cells it was 0.04858 on one rank, 0.04592 on 5 and 0.04356 on 8. A generation near the threshold was then kept at one rank count and dropped at another, and the hierarchy, and so the iteration count, changed with the number of ranks (#865).

The size is now one 5th percentile over every rank's cells together, so it is a property of the mesh.

  • edge_split.percentile_over_ranks(values, pct) gives the percentile of the values held on all ranks. It finds the two order statistics by bisection on a value, summing each rank's count below it, and then interpolates as numpy.percentile does. Nothing is gathered to one rank. On one rank it equals numpy.percentile of the same values.
  • edge_split.resolved_cell_size(dm) is that percentile of the cell diameters, and Mesh._subsample_mg_levels uses it for every generation.
  • The edge_split engine's marking pass has just measured each generation, so the size is taken there, once, and handed to the selection as before.

The rule that picks the levels is not changed.

What changes for a user

On one rank, nothing: the levels of 11 hierarchies are the same before and after (below).

On several ranks, an adapted mesh now keeps the levels the serial run keeps, wherever the refinement gives the serial generations. An iteration count recorded in parallel on an adapted mesh can therefore move. On the fixtures measured on development (in #865 and in the record on #1119):

Fixture Ranks Level cells on development Level cells on one rank
h_near=0.00625, h_far=0.1, width=0.05, max_levels=4 2 242, 968, 1040, 1436 242, 968, 1144, 1436
the same 4 242, 968, 1040, 1230, 1436 242, 968, 1144, 1436
h_near=0.0125, h_far=0.05, width=0.08, max_levels=2 7, 8 one add-on level fewer 242, 968, 1208, 1360

What this does not fix

What has not been run

Nothing here has run on more than one rank. The defect cannot be shown on one rank: there the old rule and the new one give the same levels (415 selection calls compared, all identical), and a mutant that restores the old rule passes every serial test. The evidence for rank independence is:

  • the percentile, run as P simulated ranks in lockstep (P = 2, 3, 5, 8; 10976 cases): equal to numpy.percentile of the pooled values in every case, with the same number of collective calls on every rank;
  • tests/parallel/test_1177_mg_levels_same_at_any_rank_count_parallel.py, which deals a fixed set of values out over the ranks and asserts the pooled percentile, and asserts the serial levels of three fixtures at whatever rank count it is run on.

The test needs to be run at 1 to 8 ranks on development, where the level fixtures fail, and on this branch. At CI's 2 ranks only the h_far_above_the_base fixture can show the old rule; two_levels shows it at 7 and 8.

The cost in parallel has not been measured. The bisection makes about 111 collective calls per generation. On one rank that is 5.6 ms on 189 000 cells, beside 286 ms for the cell diameters themselves.

Parallel tests whose fixtures get a different hierarchy at 7 and 8 ranks, none run here: the users of fault_fixtures.adapted_2d(2) (test_1096, test_1089, test_1086, test_0078's parallel companions) get one more add-on level there; test_1097 and test_1134 sit near a threshold. No file under tests/parallel pins a level count by rank count. On #1119 the faulted fixture's welded branch at 7 and 8 ranks is no longer reached once this lands.

Measurements

Serial, on macOS (12-core Apple M4, serial runs only).

Level cells of 11 hierarchies, identical before and after this change:

Mesh Engine max_levels Level cells
2-D nvb 2 242, 968, 1208, 1360
2-D nvb 4 242, 968, 1144, 1436
2-D nvb 5 242, 968, 1114, 1328, 1712, 2206
2-D edge_split 2 242, 968, 2168, 2860
2-D edge_split 4 242, 968, 1088, 1242, 1494
2-D edge_split 5 242, 968, 2124, 2536, 2890, 3572
2-D sbr 2 242, 968, 1374, 3123
2-D sbr 4 242, 968, 1062, 1458, 2662
2-D sbr 5 242, 968, 1236, 2417, 6123, 18853
3-D nvb 3 370, 2960, 36525
3-D edge_split 3 370, 2960, 23290, 127932

The three 2-D nvb hierarchies with a fault added (add_fault) are also unchanged.

Tests

  • tests/parallel/test_1177_mg_levels_same_at_any_rank_count_parallel.py (tier B, also in the serial batch): 110 passed on one rank. Mutants, in memory: the lower-side and the upper-side interpolation alone each fail the percentile tests; every hint replaced by 1.0 fails the edge_split fixture.
  • Serial suite of 63 adapt, multigrid and fault files: 528 passed, 6 skipped.
  • scripts/check_test_coverage.py: all 446 files reachable. Docs build clean.

Issues

Underworld development team with AI support from Claude Code

Mesh.adapt keeps one refinement generation per halving of the cell size as a
multigrid level. The size of a generation was the smallest of the ranks' own
5th-percentile cell diameters, which moves with how the cells are dealt out,
so the same adapted mesh kept different levels at different rank counts.

The size is now the 5th percentile of the cell diameters over every rank's
cells together (edge_split.resolved_cell_size). The pooled values are not
gathered: edge_split.percentile_over_ranks finds each order statistic by
bisecting on a summed count. The edge_split marking pass takes the size of
each pass's mesh as it measures it, collectively.

Serial hierarchies are unchanged. The per-rank node_budget truncation and
repair=True still make the generations themselves depend on the partition.

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 13, 2026 01:46

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 13, 2026

Copy link
Copy Markdown
Member Author

Adversarial review record (before the PR opened)

Reviewed by a separate agent on the branch before the last commit; the findings below were answered in 8f86f02. Runs are serial, on macOS (12-core Apple M4, serial runs only).

Findings, and what was done

  1. No parallel run, and a serial run cannot tell the old rule from the new. A mutant restoring the smallest-of-the-ranks rule passes every test on one rank. Not answerable here: the runs at 1 to 8 ranks on development and on the branch are the evidence, and the PR body says so.
  2. An owned-cell mask and its test could not be verified. The first version masked overlap cells. No mesh's own dm carries overlap cells, the mask's test could raise on more than one rank, and no caller needed it. Both removed; the docstring states the assumption.
  3. The docs claimed more than is true. "The same levels at any rank count" does not hold with repair=True or a node_budget, where the generations themselves depend on the partition. The sentences are now scoped. The wiki's fault-multigrid page still says an adapt keeps one level fewer at 8 ranks and needs a line when this merges.
  4. At CI's rank count only one fixture is a regression test. h_far_above_the_base fails on development at 2 and 4 ranks; two_levels only at 7 and 8. The test's header now says which fixture guards which rank counts.
  5. The edge_split engine's hand-off of the measured size was pinned by no test. A third fixture uses that engine; a mutant replacing every handed size by 1.0 now fails.
  6. The node_budget partition dependence in adapt: the intermediate multigrid levels depend on the partition when h_far sits above the base's finest cell #865 is not fixed. Stated in the PR body. adapt: the intermediate multigrid levels depend on the partition when h_far sits above the base's finest cell #865 should not be labelled as fixed until the table at 2 to 8 ranks is in, and its node_budget part stays live.
  7. The side of the interpolation was not pinned. Percentile 12.3 and sizes 4 and 6 added; the lower-side and upper-side formulas alone each fail.
  8. Cost and memory. About 111 collective calls per generation (5.6 ms against 0.56 ms on one rank on 189 000 cells; the cell diameters themselves take 286 ms). The first version held every pass's diameter array; it now holds one number per generation. The parallel wall cost is unmeasured.
  9. Style. History narration removed from a docstring, the test's timeout lowered to 300 s, a direct mpi4py import removed from the test.

Found sound: the percentile as P simulated ranks in lockstep (P = 2, 3, 5, 8; 10976 cases, none differing, the same collective calls on every rank); the handed sizes are keyed on dms that are alive when read; every test file is reachable from scripts/test.sh.

Issue verdicts

Underworld development team with AI support from Claude Code

@lmoresi

lmoresi commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

Review of #1121: the multigrid levels an adapt keeps no longer depend on the rank count (#865)

From H3, the second review desk, on a 96-thread Linux workstation (single system image; Intel Xeon Gold 6240R, AVX-512; MPICH). Head 8f86f02 against its base, development 8a3b8d6, with a build of each. Runs used up to 8 ranks under the repository's MPI supervisor; the machine's load average was 27–35.

Verdict: READY at 8f86f02, on green CI (4 of 6 jobs green, 2 running when checked). The branch keeps the serial levels at every rank count from 1 to 8, and the percentile is exactly numpy's on the pooled values. It changes which levels some hierarchies keep at higher rank counts, so it needs a register row and the maintainer's sign-off.

For the maintainer: the levels kept, from coarsest to the child, in cells

Base: a 0.1 simplex box, refined once. The three fixtures of test_1177:

fixture at 1 rank, and on this branch at every count from 1 to 8 development, where it differs
two_levels (nvb, 2 levels) 242, 968, 1208, 1360 np 7, 8: 242, 968, 1360 (one level lost)
h_far_above_the_base (nvb, 4) 242, 968, 1144, 1436 np 4: 242, 968, 1040, 1436 · np 5, 6: 242, 968, 1040, 1230, 1436 · np 7: 242, 968, 1144, 1230, 1436
edge_split (edge_split, 4) 242, 968, 1088, 1242, 1494 np 3: …1088, 1154… · np 4–6: …1038, 1154… · np 7, 8: 242, 968, 1154, 1494
  • Development matches the serial levels at np 1 and 2 for all three, and differs in at least one fixture at every count from 3 to 8.
  • W1's expectations for np 2 (h_far_above_the_base failing) do not reproduce on this machine. At np 4 we see 1040 without 1230.

percentile_over_ranks, reviewed

  • Exact. All 110 tests of test_1177 pass on every rank at np 1 to 8. That includes 105 percentile cases (spread, tied and packed values; 0, 5, 12.3, 37.5 and 100%; 1 to 1000 values, so some ranks hold none), each asserting equality with numpy.percentile of the pooled values as floats. So the number is bit-identical at every rank count.
  • Why it is exact (from reading). The bisection keeps "count ≤ below ≤ k < count ≤ reached", and stops when no float lies strictly between the two. At that point a pooled value lies in (below, reached], so reached is that value. It returns a data value, not a midpoint. The interpolation is numpy's two-sided linear rule, from the same (pct/100)(n−1) position.
  • Edge cases, at np 1, 3 and 8, each equal to numpy:
    • all values equal (it returns at once: the first midpoint fails the strict test);
    • one value;
    • two values;
    • 999 values spanning 1e-300 to 1e300 (it terminates, 15–90 ms).
  • Empty ranks contribute +inf to the MIN, −inf to the MAX and 0 to the counts. Every branch of the loop is decided by reduced values, so all ranks make the same number of collective calls.
  • No values at all gives nan, and resolved_cell_size turns that into inf.

Fixtures whose hierarchy changes, at np 7 and 8

Tests: test_1096, test_1089, test_1086 (both files), test_0078 (both files), test_1097 and test_1134.

  • This branch: 28 passed on every rank at both counts.
  • Development: 3 failed at both counts. They are test_1089::test_a_failure_on_one_rank_stops_the_replay_on_every_rank [IndexError and RuntimeError], and test_1134::test_a_3d_split_level_keeps_the_075_gate. So the rank-dependent levels on development break these at high rank counts, and the branch passes them.
  • Velocity iterations of the last solve, as baselines:
    • test_0078_rebuild_memory_parallel is 5 on this branch at np 2, 7 and 8, and 2 on development at np 7 and 8. The branch keeps the rank-2 value; development's differs where its hierarchy changes.
    • test_0078_rebuild_memory_rotated_fmg is 6 on both builds.
    • test_1089's degraded-provenance case takes 111 at np 7 and 108 at np 8 on both builds.
    • test_1096 passes its vel_its <= 9 on both builds at np 7 and 8. Its counts were not captured.

Cost

The edge_split fixture's adapt at np 8, best of 3 under load: 0.73 s on this branch against 0.62 s on development.

Not touched

Neither node_budget nor repair=True is used by test_1177's fixtures or by fault_fixtures. So our runs do not exercise the rank-by-rank generations the PR body leaves open.

Underworld development team with AI support from Claude Code

@lmoresi

lmoresi commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

Addendum to the review record: the merged head 348d48d (8f86f02 merged with development 01dec8d, which brings in #1111). From the second review desk (session H3), on a 96-thread Linux workstation (single system image; Intel Xeon Gold 6240R, AVX-512; MPICH), with a build of 348d48d. Every run went through the repository's MPI supervisor, one mpirun per file. Load average was about 37.

The question was whether #1111's repair of dependent coarse columns behaves differently on the extra level this PR now gives np 7 and 8.

All pass.

file np 7 np 8
test_1177_mg_levels_same_at_any_rank_count_parallel 110 passed 110 passed
test_1096_addon_partition_parallel 3 passed 3 passed
test_1089_fault_split_multigrid_parallel 6 passed 6 passed
test_1086_boundary_slip_classification_parallel 6 passed 6 passed
test_1086_rotated_fault_transfer_rows 2 passed 2 passed
test_1097_place_sheet_partitioned_addon_parallel 4 passed 4 passed
test_1134_fac_cover_gate_split_parallel 4 passed 4 passed

The repair's merges. The custom_mg merge warning (#1101) appears in three tests. Its count is global, gathered from every rank, and it is the same at np 2, 7 and 8: 1 coarse unknown merged, on level 2.

test np 2 np 7 np 8
test_1086_rotated_fault_transfer_rows::test_rotated_fine_transfer_zeroes_opening_rows_by_global_row[True] 1 on level 2 1 on level 2 1 on level 2
test_1089::test_split_levels_converge_like_serial 1 on level 2 1 on level 2 1 on level 2
test_1096::test_add_fault_moves_nothing_after_the_partition_and_converges 1 on level 2 1 on level 2 1 on level 2
  • These three files skip at np 1 (they are parallel-only), so np 2 is the lowest rank count with a value to compare.
  • No count differs across rank counts. No other warnings appear.
  • None of these files prints iteration counts, so I have none to report beyond the earlier record.

The READY at 8f86f02 holds at 348d48d.

Underworld development team with AI support from Claude Code

One conflict, in the changelog: both entries are kept. Nothing in adapt or
_subsample_mg_levels conflicted.

Underworld development team with AI support from Claude Code
… level selection

Underworld development team with AI support from Claude Code
@lmoresi
lmoresi merged commit 36aa150 into development Nov 13, 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.

2 participants