Skip to content

Count each node once in the gradient density metric's ranking (#1132) - #1158

Merged
lmoresi merged 2 commits into
developmentfrom
bugfix/metric-density-shared-nodes-1132
Nov 13, 2026
Merged

lmoresi merged 2 commits into
developmentfrom
bugfix/metric-density-shared-nodes-1132

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Fixes #1132.

The defect

metric_density_from_gradient evaluates |grad field| at every local DOF, gathers the values from every rank, and ranks them. The ranking sets the percentile window and, on the front-following path, each node's rank. A node on a partition seam is a local DOF on every rank that holds it, so it entered the gathered list once per copy. Each copy is evaluated separately, so the copies can differ in the last bit. Where |grad field| has ties (a plateau, a uniform region), that one bit decided whether a seam copy sat at the bottom or the top of a tie block, and the density there moved by O(1) from launch to launch and with the rank count.

The fix

Following the Charter rule, ownership is taken from topology, never from coordinates:

  1. The owner's |grad| is synced to every copy before the ranking.
  2. Only owned DOFs enter the gathered list. "Owned" means a non-negative offset in the field's global section, which is the point star forest's ownership. Every node counts once, at any rank count.
  3. Every density write is synced owner to ghost.

Serial is unchanged. I checked the other builder in metrics.py: mesh_metric_mismatch reduces over cells, which are not shared, so it does not need this change.

Test

tests/parallel/test_1132_metric_density_shared_nodes_parallel.py (min_size 2). The gradient is replaced by a staircase of exact ties, a function of the coordinates alone. That way the test pins the ranking, not the projection solve. It covers four cases: front-following, arc-length, percentile and raw.

  • test_seam_copy_last_bit: one rank's ghost copies (the rank with the most) are nudged by one ulp. The density at every DOF must be unchanged bit for bit, and every copy must equal its owner.
  • test_ties_match_serial: the density at every node must equal the np=1 value bit for bit. The np=1 value comes from serial_reference, which runs the module at np=1 on the same mesh.

The tests match nodes across rank counts by coordinate, and only to line up two partitions. The code never identifies points by coordinate.

Results on a 96-thread Linux workstation (single system image; Intel Xeon Gold 6240R, AVX-512; MPICH):

Four metric cases (front-following, arc-length, percentile, raw), two tests each, 8 in all:

np 2 np 3 np 4 np 5
development's metrics.py 5 of 8 fail 4 of 8 fail 6 of 8 fail
this branch 8 pass 8 pass 8 pass 8 pass

On development the tie test fails in front-following, arc-length and percentile at every rank count above. The seam nudge fails in front-following at np 2, 3 and 4, in percentile at np 2 and 4, and in arc-length at np 4. Raw passes on development: its density is the synced gradient itself.

Also run on this branch:

  • test_0750_meshing_follow_metric.py and test_0765_mmpde_descent_does_not_stall.py, serially: 15 pass.
  • test_1173_addon_record_after_a_move_parallel.py and test_0855_mesh_smoothing_parallel.py at np 2 and 4: 11 pass.
  • scripts/check_test_coverage.py: OK.

Underworld development team with AI support from Claude Code

metric_density_from_gradient evaluated |grad field| at every local DOF and
gathered every rank's values, so a node on a partition seam entered the
percentile window and the front-following ranks once per copy, and its
copies could differ in the last bit. With ties in |grad field|, one bit
decided where a seam copy sat in a tie block, and the density moved by
O(1) from launch to launch.

The owner's |grad| is now synced to every copy first, only owned DOFs
(the field's global section, i.e. the point star forest's ownership) are
gathered, and each density write is synced owner to ghost. Serial is
unchanged. mesh_metric_mismatch reduces over cells, which are not shared,
and needs no change.

Test: tests/parallel/test_1132_metric_density_shared_nodes_parallel.py,
with a staircase gradient of exact ties. A one-ulp nudge to one rank's
seam copies leaves the density bit-identical, and the density matches
np=1 bit for bit at np 2 to 5.

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

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

Verdict: READY with conditions at 560c65b. The fix is correct and the collectives are safe. The new test fails on development and has power against the two mutants that matter. One condition: the "development" row of the PR body's results table does not match the committed test and should be corrected (finding 2). Suggestion 3 and the follow-up in finding 1 do not block the merge.

Review by H1 (review and parallel test runs on a 96-thread Linux workstation (Intel Xeon Gold 6240R, AVX-512; MPICH)). All runs were pinned to 8 cores.

Method.

  • The PR's base is exactly development bc3d536 (git merge-base).
  • The "development" arm is the installed bc3d536 package. The "branch" arm is the same package with meshing/smoothing/metrics.py replaced by the PR's version, put first on PYTHONPATH. uw.__file__ was checked in both arms.
  • Every parallel job ran under scripts/mpi_supervisor.py --silence 60.

Findings, by severity

1. (Medium, follow-up, not this PR) With a real gradient, the metric still depends on the rank count, because the gradient projection differs by about 1e-3 between partitions

I used a smooth front on the PR's test mesh: UnstructuredSimplexBox, cellSize 0.07, 303 nodes, with T = 0.5 (1 - tanh((y - 0.5 - 0.1 cos 6x)/0.05)). The real projected gradient was used, not the test's staircase. Each density is compared with np 1, node by node, using the owner's value:

case (branch, default projection tolerance) np 2 np 4 np 4, random partition np 8 np 8, empty rank
raw (that is, the gradient itself) 303 nodes, max 5.4e-3 303, 5.2e-3 303, 5.1e-3 303, 4.9e-3 303, 4.8e-3
front-following, refinement 4 206 nodes, max 0.41 200, 0.30 206, 0.36 201, 0.32 203, 0.42
percentile 113, 6.4e-4 112, 5.1e-4 114, 4.7e-4 113, 5.3e-4 112, 5.9e-4
arc-length, refinement 4 293, 0.010 293, 8.1e-3 293, 7.2e-3 293, 6.9e-3 293, 6.9e-3
tight projection tolerance front-following percentile arc-length raw
branch, np 4 0 nodes differ 99 nodes, max 5.6e-16 54, 7.1e-15 269, 5.3e-15
branch, np 8 random partition 0 nodes differ 101, 1.2e-15 62, 2.0e-14 281, 1.0e-14
development, np 4 301 nodes, max 0.72 112, 1.1e-2 294, 0.17 269, 5.3e-15

So the PR removes the ranking defect completely. With the same tight-tolerance input, development is still wrong by up to 0.72. What remains on the branch at the default tolerance is the projection.

I suggest a follow-up issue on the metric's gradient projection tolerance, or on how the front-following ranking treats the flat region (for example, ranking above a noise floor). It does not belong in this PR.

Reproduce: run the PR's mesh with the T above, and call metric_density_from_gradient with each case at np 1, 2, 4 and 8. Gather the owned values sorted by coordinate and diff them against np 1. Repeat after setting the cached projection's tolerance as above.

2. (Low, PR body; this is the condition) The "development" row of the results table does not match the committed test

The committed test nudges the rank with the most ghost rows. Development then fails as follows:

np 2 3 4 5 8
development, measured 4 of 6 fail 3 of 6 4 of 6 3 of 6 4 of 6
PR body "2 of 6 (tie tests)" "front-following nudge fails" "3 of 6" (blank)
  • At np 2 the failures are both tie tests and the front-following and percentile nudge cases: 54 DOFs, up to 0.0145, and 103 DOFs, up to 4.65e-16. These match the PR body's own follow-up paragraph exactly, so the table seems to come from an earlier version of the test.
  • At np 3 the nudge numbers match the body: 74 DOFs, up to 0.117. The two tie tests also fail.
  • Please make the row match the committed test.

3. (Low, suggestion) The test does not pin arc-length, which this PR also fixes

On development, metric_choice="arc-length" depends on the partition through g_hi. With the staircase gradient it differs from np 1 at 293 of 303 nodes: up to 0.114 at np 4, and up to 0.349 at np 8 on a random partition. On the branch it is bit-identical at every layout I tried.

The test's CASES covers front-following, percentile and raw only. Adding "arc-length": dict(refinement=4.0, metric_choice="arc-length") would pin it. On development, raw passes every test at every rank count, because only max enters it. It still earns its place: it catches mutant E below.

Verified claims

  • Collectives.
    • Every collective is reached on every rank: the owner-to-ghost sync of |grad|, createSubDM in _owned_dof_rows, gather_data, and every _write.
    • All branch conditions come from arguments or from the broadcast g_lo/g_hi, so every rank takes the same path. There is no rank-local early return before a collective.
    • A rank with no cells and no owned nodes completes and contributes zero rows. Shell partitions leaving the last rank empty were run at np 3, 4 and 8, with no hang and an exact match with np 1 (below).
  • Owner-to-ghost semantics. pack_raw_data_to_petsc(sync=True) does localToGlobal with INSERT, then globalToLocal. The test's _ghosts_agree confirms that every copy equals its owner, at np 2 to 8.
  • mesh_metric_mismatch needs no change: confirmed.
    • mesh.dm is distributed without cell overlap. Overlap is used only on a separate navigation DM.
    • The local cell counts sum to the serial 544 at every rank count and layout I ran (np 2: 260 + 284; np 8: 70 + 66 + 67 + 71 + 68 + 68 + 68 + 66).
    • Its reductions are per-cell sums and a gathered median, so no cell is counted twice.
  • Serial unchanged: confirmed bit for bit. The np 1 densities are identical between the two arms for all six cases, with the staircase gradient, the real gradient and the tight-tolerance gradient.

What passed

The PR's test (test_1132_metric_density_shared_nodes_parallel.py, pytest --with-mpi -s -rA):

np 2 np 3 np 4 np 5 np 8
branch 6 pass 6 pass 6 pass 6 pass 6 pass
development 4 fail 3 fail 4 fail 3 fail 4 fail

On development, front-following and percentile fail at every np, in both the nudge and tie tests (front-following nudge at np 8: 78 DOFs, up to 0.356). The exception is np 3 and 5, where the percentile nudge happens to pass.

Partition independence with the staircase gradient. This used the test's own staircase, plus gradient-uniform, arc-length and refinement=1 cases. The branch is bit-identical to np 1 at all 303 nodes, in all 6 cases, at every layout:

  • np 2, 3, 4 and 8 with the default partitioner;
  • np 4 with horizontal bands;
  • np 4 and np 8 with random cell assignment (np 8: owned counts 0, 1, 1, 8, 21, 45, 78, 149, so one rank owns no nodes while holding cells);
  • np 3, 4 and 8 with the last rank empty.

On development, front-following differs at 302 of 303 nodes, by up to 1.87 at np 8 with a random partition.

Mutants (the PR's test at np 2 and np 4):

mutant result
B: ghosts counted again (gather every local DOF, not owned rows only) caught: both tie tests fail at np 2 and 4
E: no pre-ranking sync, and the density write unsynced (sync=False) caught: 2 fail at np 2, 3 fail at np 4 (nudge cases; for example raw, 22 DOFs, up to 4.4e-16)
A: no owner-to-ghost sync of grad
D: no pre-sync, density written with rho0.data[:, 0] = ... passes

A and D behave the same as the branch, not missed defects. Once only owned rows are gathered, a ghost's own |grad| never enters the ranking, and the final write overwrites the ghost with the owner's value (the .data setter syncs too, as D shows). The pre-ranking sync is a second layer of defence. It is cheap and harmless.

Serial regressions. tests/test_0750_meshing_follow_metric.py and tests/test_0765_mmpde_descent_does_not_stall.py: 15 pass on both arms.

CI wiring. No change to scripts/test.sh is needed:

  • The file is a tests/parallel/test_*.py with min_size=2, so the PARALLEL_FILES=(tests/parallel/test_*.py) batch picks it up in the --p 2 phase that CI runs.
  • scripts/check_test_coverage.py reports OK (458 files reachable).
  • Development fails the test at np 2, so CI would have caught the defect.

Not run

  • The two-launch mover check at np 4 (another session is doing it).
  • 3-D meshes, degree-2 metric fields, and the smoothing_length / gradient_smoothing_length paths.
  • Launch-to-launch reproducibility at a fixed rank count with the real gradient.
  • Open MPI.

Logs, drivers, partition hooks and saved arrays are kept by the reviewer.

Underworld development team with AI support from Claude Code

The fix covers arc-length (its window comes from the same gathered list),
and no case pinned it. Development fails its tie case at np 2, 3 and 4 and
its seam nudge at np 4; the branch passes 8 of 8 at np 2 to 5.

Underworld development team with AI support from Claude Code
@lmoresi

lmoresi commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

Re-check of #1158 at bd74390: READY. From H1 (review and parallel test runs on a 96-thread Linux workstation (Intel Xeon Gold 6240R, AVX-512; MPICH)), following its review at 560c65b. The PR is Python only, so this was run with its files laid over development bc3d536's build.

Underworld development team with AI support from Claude Code

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