Skip to content

test_0052: a guard that reads the rank is always rank-local (#874) - #1236

Merged
lmoresi merged 2 commits into
developmentfrom
bugfix/scan-rank-guards-874
Nov 20, 2026
Merged

lmoresi merged 2 commits into
developmentfrom
bugfix/scan-rank-guards-874

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Addresses #874: the rank-guard gap in test_0052's collective guard scan, from the reviews of #1225 by C1 (the development hub) and H1 (review and parallel test runs on a 96-thread Linux workstation). Review: H1. Test-only.

#1225 is merged and development is merged in: the diff is tests/test_0052_collective_guard_scan.py alone.

The gap

mpi.rank and comm.rank were in UNIFORM_TOKENS, so the scan treated the rank as the same on every rank. It therefore never reported a collective, or an agreement point, under if comm.rank == 0:, which is the classic hang the scan exists to catch. H1's raw scan showed:

  • none of these was flagged under an agree(), even in _build_body: comm.rank == 1, comm.rank, uw.mpi.rank == last, mpi.rank < 2, comm.rank == comm.size - 1;
  • comm.getRank() == 0 also passed when comm was a parameter, through the argument rule.

The fix

  • The rank tokens leave UNIFORM_TOKENS. The size tokens stay.
  • A guard that reads the rank is classed "rank" and reported, as the local-data guards are. It reads the rank if it contains mpi.rank, comm.rank, .rank, getRank() or Get_rank(), or a bare name rank.
  • That classification comes first: before the uniform tokens (hasattr, mpi.size) and before the argument rule. So uw.mpi.size > 1 and comm.rank == 0, hasattr(comm, "rank") and comm.rank == 0, and a parameter comm are all reported.

Self-tests

Tree triage

The scan: with the change, it reports no collective or agreement point under a rank guard anywhere in src/underworld3: 0 findings. No whitelist entry is needed, because the scan reports guarded collectives only, never a rank-0 print or write.

A wider skim: the scan can't see a collective inside a called helper. So I also skimmed the 144 rank-guarded blocks in the tree for calls that are collective inside: a save or write, a view, a barrier, a global-to-local scatter, an evaluate, a solve or an agreement. That found 11 candidates: 7 safe, and the 4 visualisation helpers a real hang (corrected after H1's review; see the first row):

site guard why it is safe
visualisation/parallel.py, 4 helpers uw.mpi.rank == viz_rank NOT safe: a real hang. My triage read the else branch as making the same global_evaluate call with empty points; I did not run it. H1 did. When the field is a sympy expression, the non-plotting ranks raise AttributeError on field.mesh building the empty points, so they never reach the collective global_evaluate and the plotting rank waits in it: parallel_line_plot hangs at np 2 and 3. The scan does not see it, because the collective is reached through a helper. Fixed separately by H6.
utilities/place_surface.py, 4 sites comm.rank == target the raise sits inside a try whose failure is caught into failure and agreed after the block
swarm.py:5177 uw.mpi.rank == 0 an XDMF text write, rank-local
utilities/boundary_flux.py:423, postprocessing/geoid.py:936 comm.rank == 0 the match was a variable named gathered; the body reads data already gathered

Apart from the visualisation helpers, which are a real hang through a helper and are fixed separately by H6, there is nothing to fix here.

Results

  • tests/test_0052_collective_guard_scan.py: 20 passed (the whole-tree scan included).

Underworld development team with AI support from Claude Code

C1 and H1 on #1225: mpi.rank and comm.rank were in UNIFORM_TOKENS, so the
scan took the rank for uniform and never reported a collective (or an
agreement point) under `if comm.rank == 0:`, the classic hang it exists to
catch. None of `comm.rank == 1`, `comm.rank`, `uw.mpi.rank == last`,
`mpi.rank < 2`, `comm.rank == comm.size - 1` was flagged, and
`comm.getRank() == 0` passed when comm was a parameter.

- The rank tokens leave UNIFORM_TOKENS (the size stays).
- RANK_TOKENS (mpi.rank, comm.rank, .rank, getRank(), Get_rank()) and a bare
  `rank` name make a guard "rank", checked before the uniform tokens
  (hasattr, mpi.size) and before the argument rule, and reported as the
  local-data guards are.
- Self-tests: each of H1's plants (and the getRank / Get_rank / bare rank /
  hasattr / size-and-rank forms) under an agree() is reported; in a
  _build_body snippet a planted rank guard is flagged beside the accepted
  guard keys.

Tree triage: with the change the scan reports no collective or agreement
point under a rank guard anywhere in src (0 findings), so no whitelist entry
is needed (the scan reports guarded collectives only, never prints or
writes). A skim of the 144 rank-guarded blocks for helper calls that are
collective inside found 11 candidates, all safe (listed in the PR).

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

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 19, 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.)

Verdict at 40603cd: ready to merge on green from this review. The scan change is right and its tests pass.

One of the eleven blocks in the triage table that the PR body marks safe is a real hang: the visualisation helpers (finding 1). The bug was already in the source before this PR, so it can be fixed separately, but the table's row should be corrected.

The diff

The scan

  • test_0052: 20 passed, including the 10 rank plants, the _build_body plant, and the whole-tree scan.

  • Rank guards are all reported. I put each guard in a function where comm is a parameter, with an agree() or a comm.allreduce under it:

    • comm.rank == 1, comm.rank, uw.mpi.rank == last, mpi.rank < 2 and comm.rank == comm.size - 1;
    • comm.getRank() == 0, comm.Get_rank() == 0, PETSc.COMM_WORLD.getRank() == 0 and MPI.COMM_WORLD.Get_rank() == 0.

    Each is classed rank and reported, under both kinds of body.

  • No false positive on uniform guards. uw.mpi.size > 1, uw.mpi.size == 1, comm.size > 1, mesh.dim == 2, mesh.cdim == 3 and var.degree > 1 are classed uniform and not reported.

Findings

  1. High (the bug was already in the source; the triage calls it safe): the visualisation/parallel.py helpers hang at np ≥ 2 for every field that global_evaluate accepts.

    • The cause. In all four helpers, the else branch builds np.array([]).reshape(0, field.mesh.dim) before its global_evaluate call. A sympy expression has no .mesh: neither T.sym, nor T.sym[0], nor 2*T.sym[0] does. So the non-plotting ranks raise AttributeError and never enter global_evaluate, while the plotting rank waits inside it.
    • Measured with parallel_line_plot:
      • It hangs at np 2 with viz_rank 0 and with viz_rank last, and at np 3 with viz_rank 0. The supervisor kills the job with the plotting rank in _refuse_points_far_outside and the others back at module level.
      • Passing the MeshVariable itself does not hang, because every rank enters. But global_evaluate then rejects it on every rank (AttributeError, no .args), so the helpers cannot work in parallel with any argument.
      • parallel_scatter_plot, parallel_profile_comparison and parallel_custom_plot use the same field.mesh.dim construction. I read them but did not run them.
    • The pattern itself is sound. Called directly, global_evaluate with 20 points on rank 0 and none on the others, and the reverse, returns correctly on every rank at np 2 and 3.
    • The fix, checked. Taking the dimension from the points instead, np.zeros((0, np.asarray(sample_points).shape[1])), makes parallel_line_plot run at np 2 and 3 with every rank entering global_evaluate. The same one-line change applies to the other three helpers.
  2. Low: the rank classing is token-based.

    • Missed: a rank held under another name. A parameter my_rank or r guarded by my_rank == 0 is classed argument and not reported.
    • False positive: .rank matches self.ranks, so self.ranks is not None and len(self.ranks) > 1 are classed rank.
    • Neither shows up in the tree today, where the scan reports 0 findings. Worth a comment next to RANK_TOKENS.

Spot-checks of the triage

  • place_surface's caught-and-agreed raises: these are the _place_one_parallel sites in place_along_lines. I ran a plain placement, and two failures forced on the surgery rank inside the target-rank try: one in _resample, and one that makes _inside_mesh report false. At np 2 and 3, every rank raises the same "place_along_lines failed on the surgery rank: ..." RuntimeError, with no hang. The PR body's reason holds.
  • swarm.py:5177 (by reading): the rank-0 block writes an XDMF file and reads an HDF5 file serially with h5py. There is no collective under it. The PR body's reason holds.

Not run

  • The other three visualisation helpers, which I only read.
  • boundary_flux.py:423 and geoid.py:936, which I neither read nor ran.
  • np 4 and above.

Logs kept by the reviewer.

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