Skip to content

Stokes refuses, on every rank, a mesh that leaves a rank without cells (#962, interim) - #1137

Merged
lmoresi merged 1 commit into
developmentfrom
bugfix/stokes-refuses-a-rank-without-cells-962
Nov 13, 2026
Merged

lmoresi merged 1 commit into
developmentfrom
bugfix/stokes-refuses-a-rank-without-cells-962

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 13, 2026

Copy link
Copy Markdown
Member

What this does

A Stokes solve on a mesh where some rank holds no cells now stops at set-up with the same error on every rank. Before, it stopped inside MPI on the ranks without cells, or hung (#962).

This is an interim refusal. It does not make the solve work. The fix is a change to how the two matrices are assembled, and it is a separate piece of work that needs its own measurements.

Cause

The Stokes solver registers a Jacobian preconditioner, so SNES assembles two matrices. PETSc's DMPlexComputeJacobianByKey leaves early on a rank with no cells, before it has looked at whether there is a second matrix, and so skips the MatAssemblyBegin/End of the Jacobian that every other rank makes. The ranks are then out of step in the next collective call. This is in PETSc 3.25.3 (the conda-forge build we ship against) and in PETSc main.

A solver whose Jacobian and preconditioner are one matrix is not affected: PETSc assembles once on every rank. That is every scalar and vector solver here. Only SNES_Stokes_SaddlePt (and its multiplier fields) registers a Jacobian preconditioner.

Three cases reach it, and the cell count against the rank count does not decide any of them:

  • a coarse mesh: a 14-cell box on 8 ranks is partitioned [0, 3, 2, 2, 0, 3, 2, 2];
  • a refined mesh with a coarse base. The base is partitioned before it is refined and every level keeps that partition, so a rank the base leaves empty holds no cells at any level: Annulus(cellSize=0.8, refinement=2) on 8 ranks has [48, 48, 0, 64, 0, 64, 64, 64];
  • a region taken with extract_region from a mesh whose other ranks hold none of that region. An extracted region keeps its parent's partition, so this is the ordinary case for a small region on many ranks.

Every such run stops in MPI or hangs today, so the refusal refuses nothing that runs.

The check

SNES_Stokes_SaddlePt._refuse_a_rank_without_cells, called from _setup_solver:

  • It gathers each rank's dm.getHeightStratum(0) size once on the DM's communicator, and raises the same RuntimeError on every rank if some count is zero and some is not. The message names the number of ranks without cells, the rank count, the total cell count, the PETSc routine and Rotated free-slip solve aborts when a rank has no cells (mismatched Allreduce on cell-less ranks; test_1140 at np 8) #962, and says that the base of a refined mesh, or the region, must reach every rank.
  • This is the test PETSc makes on a DM with one discrete system, which is what every default set-up builds. Its cell range comes from DMPlexGetAllCells_Internal, which is the rank's own top stratum of the depth label, ghost cells included; getHeightStratum(0) is that range. A rank that owns no cells but holds ghost cells is not refused, and PETSc does not leave early there either.
  • It returns at once on one rank.
  • It runs after the solver's DM is built, which is after the pointwise functions are compiled. The count is the same on the mesh's DM, but the test is made on the DM PETSc assembles on.
  • A solver with a second discrete system on part of the mesh (set_active_region, used by one example script and by no default path) can meet the same early exit on a rank whose first cell is in the inactive part. That is not tested for here.
  • It is not called on a rewire, which keeps the DM that a full set-up has already checked.
  • It reads the solver's own DM only. Coarse multigrid levels with a rank without cells are not refused: their operators are Galerkin products, not assembled by this routine.
  • Scalar and vector solvers do not call it and gain no reduction.

There is no tolerance and no coordinate comparison in the change: the decision is an integer count.

Tests

tests/test_0786_stokes_refuses_a_rank_without_cells.py (level 1, tier B, one rank): the method is run against a stand-in DM whose communicator reports several ranks. It asserts no refusal and no gather on one rank, no refusal where every rank holds cells or none does, the same text on every rank for three sets of counts, and that a full set-up of a real solver calls it. With the refusal removed from the set-up, or with the decision or the text changed, this file fails by assertion.

tests/parallel/test_0786_stokes_refuses_a_rank_without_cells_parallel.py (level 2, tier B, two ranks or more). Here a regression shows as a stopped or hung run, ended by the supervisor's silence limit:

  • a Stokes solve on a triangle of cells with one rank left empty by a shell partition, and on a region held entirely by rank 0, is refused with the same text on every rank, and the text carries the counts the test gathered itself;
  • a Poisson solve on the same region is not refused and returns the linear field its walls prescribe to 1e-8 (the only tolerance in the tests).

Both fixtures leave a rank without cells at every rank count from 2 up, and each test asserts that premise. At 2 ranks the triangle fixture is a single cell; the region fixture has the shape of the reported cases at every count. CI runs this file at 2 ranks.

Two existing Stokes tests now skip, with the cell counts in the reason, when a rank holds no cells: tests/parallel/test_1140_rotated_analytic_normal_wall_nodes_parallel.py (meshes of 14 and 18 cells) and test_stokes_on_submesh_parallel in tests/parallel/test_0770_submesh_extract_mpi.py (a region of an annulus, on its parent's partition).

docs/developer/design/submesh-solver-architecture.md says that a Stokes solve needs cells of the region on every rank, and docs/developer/guides/fault-multigrid.md that the base of a hierarchy must give every rank a cell.

What was run

Serial only, on macOS (12-core Apple M4). Nothing in this PR has been run on more than one rank by us: the refusal itself, the parallel test file and the two skips need the parallel desk (a 96-thread Linux workstation), which is asked to show the hang or stop on development first and then run tests/parallel/test_0786_*, test_1140_* and test_0770_submesh_extract_mpi.py at 2 to 8 ranks.

What Result
tests/test_0786_stokes_refuses_a_rank_without_cells.py 9 passed
The same file under five in-memory changes to the method (refuse when no rank holds cells; decide on this rank's count alone; gather on one rank too; never refuse; count the ranks that hold cells) each fails by assertion: 1, 4, 2, 3 and 2 tests
The parallel file's two Stokes problems and its Poisson problem, built and solved on one rank converge (SNES reason 2; Poisson error 2.7e-12 against the 1e-8 bound, and 0.01 with a wall value changed)
tests/test_0052_collective_guard_scan.py and the serial Stokes files (test_1010*, test_1012*, test_1013*, test_1014*, test_1017*, test_1018*, test_0771*, test_1058*) 122 passed, before the rebase onto development at 70b240c. After it, on a fresh build: test_0786, the guard scan, test_1010_stokesCart.py and test_0771_extract_region.py, 23 passed
scripts/check_test_coverage.py all test files reachable
Docs build no warning from the two pages edited, beyond one that development has already (submesh-solver-architecture.md is in no toctree)

Not shown by a run: that the test "a full set-up reaches the refusal" fails when the call is taken out of _setup_solver. The call site is compiled, so it cannot be changed in memory; the test replaces the method on the instance and expects its error from solve().

test_stokes_on_submesh_parallel (test_0770) passes in CI today at 2 ranks, so the region reaches both ranks there, the skip does not fire, and CI keeps running it. The skip is conditional on the premise (some rank holds none of the region). Where it fires, it is one of the tests the assembly fix will turn back on, with test_1140's.

An adversarial review was made before this PR opened; its record is in the first comment.

Issue state

#962 stays open and is not labelled as fixed: the refusal replaces a hang with an error, and the solve still cannot be made. The refusal is removed when the assembly is fixed.

Solver register: row 16, "a Stokes or constrained solve on a mesh where some rank has no local cells: refuse at setup, collectively, naming the rank count, the cell count and the PETSc defect; no numerics; removed when the shipped PETSc is fixed".

Underworld development team with AI support from Claude Code

#962)

A Stokes solve registers a Jacobian preconditioner, so SNES assembles two
matrices. PETSc's DMPlexComputeJacobianByKey (3.25.3 and main) leaves early on
a rank with no cells, before it has looked for the second matrix, and skips the
assembly of the Jacobian that every other rank makes. The ranks are then out of
step and the run stops in MPI or hangs.

Until the assembly is fixed, the set-up gathers the local cell counts of the
solver's DM once and raises the same error on every rank when some rank holds
none. The count is the one PETSc tests: the rank's own top stratum, ghost cells
included. Scalar and vector solvers have one matrix, are not affected and do
not make the check.

Three meshes reach this, none decided by cells against ranks: a coarse mesh
(14 cells on 8 ranks), a refined mesh whose base left a rank empty (every level
keeps the base's partition), and a region extracted from a mesh whose other
ranks hold none of it.

Tests: the decision and the text on one rank against a stand-in communicator
(tests/test_0786); the refusal on two fixtures that leave a rank empty at every
rank count from 2, and a scalar solve on the same region that is not refused
(tests/parallel/test_0786). Two existing Stokes tests skip when a rank holds no
cells (test_1140, test_0770's region solve).

This does not fix #962: the solve still cannot be made on such a mesh.

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

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)

Made by a separate reviewing agent on the branch before its first commit, then answered in the commit that is here. Everything was run on macOS, 12-core Apple M4 (serial runs only). No finding was against the decision to refuse. The reviewer re-read DMPlexComputeJacobianByKey in PETSc 3.25.0, 3.25.3 and main and confirmed the early exit before the test for a second matrix in all three.

# Finding Evidence What we did
1 test_stokes_on_submesh_parallel (test_0770) would fail, not skip, at a rank count where a rank holds none of the region. By reading: the test solves Stokes on an extracted region with no premise on the partition. The test now gathers the region's cells per rank and skips, with the counts in the reason, when one is zero. CI runs it at 2 ranks, where the region reaches both and the skip does not fire.
2 The message gave a remedy a user of an extracted region cannot follow ("use fewer ranks or a finer mesh"), disagreed in number ("1 ranks"), and a DM with no cells on any rank was refused although every rank leaves early alike. Read from the text; the all-empty case by a run of the method against stand-in counts (0, 0). Message rewritten: counts of ranks without cells, ranks and cells, the PETSc routine, the issue, and "the base, or the region, must reach every rank". A DM empty on every rank is not refused.
3 The docs and docstrings said the condition is "fewer cells than ranks". It is not: a 14-cell box on 8 ranks has two empty ranks, and a refined annulus of 352 cells has two. Cells per rank [0, 3, 2, 2, 0, 3, 2, 2] and [48, 48, 0, 64, 0, 64, 64, 64], from the parallel desk's runs (the second is in a comment on #888). Wording corrected in the design note, the guide and the test docstrings.
4 CI runs the parallel file at 2 ranks only, where the triangle fixture is one cell. scripts/test.sh --p 2. Stated in the PR body. The region fixture has the reported shape at every count; runs at 2 to 8 ranks are asked of the parallel desk.
5 In the parallel file a regression shows as a hang ended by the supervisor, and nothing tested the decision itself. By reading. Added tests/test_0786_stokes_refuses_a_rank_without_cells.py: the decision and the text on one rank against a stand-in communicator. Five in-memory changes to the method each fail it by assertion (1, 4, 2, 3, 2 tests). The first run of "never refuse" failed by a TypeError, not an assertion; an explicit assertion was added and the run repeated.
6 With a second discrete system on part of the mesh (set_active_region) PETSc's early exit is on the region's cells, not the rank's, so the check and PETSc's test differ there. By reading DMPlexComputeJacobianByKey; one example script uses the call, no default path does. Not tested for. The docstring says the test is PETSc's for a DM with one discrete system, and the PR body says the other case is not covered.
7 The refusal comes after the pointwise functions are compiled, so a run that will be refused pays for the compile first. By reading _setup_solver. Left. The test is made on the DM PETSc assembles on, which exists only then. Stated in the PR body.
8 Only the submesh design note recorded the limit, and it said "vector solves" run on such a region with no test of one. By reading the docs; the parallel file tests a scalar solve only. The fault multigrid guide now says the base must give every rank a cell, with the annulus counts. The note says "a scalar solve". The adaptive-meshing and adapt-on-top-faults guides were read and not changed: neither describes a partition.
9 Tier markers were on the module, and a helper's name did not say what it built. Charter §8. Tier is on each test; the helper is _region_held_by_rank_0.

Not closed by this review

  • Nothing here has run on more than one rank. The refusal, the parallel file and both skips wait on the parallel desk, starting with the stop or hang on development.
  • That taking the call out of _setup_solver fails a test is not shown by a run (the call site is compiled).
  • A side finding outside this change: a mesh wrapped around a bare DM lists All_Boundaries among its boundaries while the DM carries no such label, and a Stokes solve with an essential condition on it stopped with a segmentation fault in one of our serial probes (it solves once the label is made and marked first). It has gone to the development hub as a note and is not filed.

Issue state. #962: still live. This branch turns a stop in MPI or a hang into an error; the solve on such a mesh is still not possible. #888 is closed and its work is on the roadmap; its item 3 (a faulted annulus stopping at 8 ranks) was shown there to be #962, so that item stays open with #962. Until then the way round is a base fine enough to give every rank a cell.

Underworld development team with AI support from Claude Code

@lmoresi

lmoresi commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

READY at 1fd2f1d. On development the new file's Stokes cases stop in MPI at every rank count. On the branch they are refused on every rank with one message, the process returns, and the scalar solve on the same region passes. The refusal is what stands between the user and the stop: with it taken out, the file stops again. Nothing hangs, and no skip fires where it should not.

Parallel review by H1 (review and parallel test runs on a 96-thread Linux workstation (Intel Xeon Gold 6240R, AVX-512; MPICH)).

  • Branch: built at 1fd2f1d from its own worktree, because the change is in petsc_generic_snes_solvers.pyx.
  • Development: 70b240c as Python files laid over a 01dec8d build. No compiled file differs between the two.
  • Method: tests ran from git archive copies of the branch's tree. Every MPI job ran under scripts/mpi_supervisor.py on 4 logical CPUs, with np above 4 oversubscribed. Logs are kept by the reviewer.

a. Development 70b240c: the stop the refusal replaces

test_0786_stokes_refuses_a_rank_without_cells_parallel.py::test_stokes_is_refused_on_every_rank was copied onto development, where the refusal does not exist, and run one fixture at a time.

fixture np 2 np 3 np 4 np 8
triangle with an empty rank MPI abort, "Message truncated; 4 bytes received but buffer size is 8" MPI abort MPI abort MPI abort, "4 bytes … buffer size is 16"
region held by rank 0 MPI abort, "4 bytes … 8" SIGABRT inside stokes.solve (solvers.py:2230) MPI abort MPI abort, "4 bytes … 8"

None of the eight passes, so the premise holds at every rank count tried. Under -q pytest prints nothing before the abort kills it; run with -s to see the abort text.

b. The branch: the new parallel file at np 2 to 8

np 2 3 4 5 6 7 8
result, every rank 3 passed 3 passed 3 passed 3 passed 3 passed 3 passed 3 passed

The test asserts that the outcome string is identical on all ranks, that it carries the counts and "#962", and that the ranks are in step for the gather afterwards. Each run returns in 4 to 10 s, and the scalar solve on the region converges to the prescribed linear field at every np.

Beyond the file, the same refusal at three other places this defect was reproduced:

case np message on every rank
plain Stokes, Dirichlet walls, the 14-cell box 8 "ranks without cells of this mesh: 2 of 8 (14 cells in all)"
faulted annulus (cell 0.8, refinement 2, add_fault, rotated free slip, fault condition) 8 "… 2 of 8 (356 cells in all)"
extract_region("Inner") of a BoxInternalBoundary with z_int = 0.25 4 "… 2 of 4 (68 cells in all)"

Each returns instead of stopping in MPI. The rotated and fault paths are covered because the refusal sits in _setup_solver, ahead of everything else.

c. test_1140 and test_0770 on the branch, np 2 to 8

test_1140. Nothing hangs at any np.

np result #962 skip, with the counts in the reason
2 12 passed none
3 10 passed, 2 skipped none (both are "not measured to hold the case at np 3")
4 11 passed, 1 skipped none (the HOLDS_AT skip)
5 8 passed, 4 skipped "cells per rank [4, 3, 4, 3, 0]"
6 10 passed, 2 skipped none
7 6 passed, 6 skipped "[2, 3, 2, 3, 2, 2, 0]", "[3, 3, 3, 0, 3, 3, 3]"
8 6 passed, 6 skipped "[0, 3, 2, 2, 0, 3, 2, 2]", "[3, 0, 3, 3, 3, 0, 3, 3]"

The skip fires exactly where a fixture leaves a rank empty, and never at np 2.

test_0770. 6 passed at every np from 2 to 8, with no skip: this region reaches every rank at these counts, so test_stokes_on_submesh_parallel still runs and passes at np 2, as in CI.

d. The mutant: the refusal made a no-op

SNES_Stokes_SaddlePt._refuse_a_rank_without_cells was replaced by a no-op through a sitecustomize patch; no file was edited. test_stokes_is_refused_on_every_rank then stops in MPI:

np result
2 abort, "4 bytes received but buffer size is 8"
3 abort, "1 bytes … 8"
8 abort, "4 bytes … 16"

So the file cannot pass without the refusal.

Reading

  • [read] Scope. PetscDSSetJacobianPreconditioner is called only in SNES_Stokes_SaddlePt, the one solver class that registers a second matrix. SNES_NavierStokes subclasses it and does not override _setup_solver, so it gets the refusal. SNES_Scalar, SNES_Vector and SNES_MultiComponent have one matrix and are rightly not refused; the region's scalar solve confirms this.
  • [read] Collective. The refusal is one allgather on the DM's communicator, reached by every rank before any rank-dependent branch. Its decision uses only the gathered counts. A rewire (_rewire_only) skips it, which is right, since the DM has already been through a full set-up.
  • [read] The message. It names the counts, the PETSc function and version, Rotated free-slip solve aborts when a rank has no cells (mismatched Allreduce on cell-less ranks; test_1140 at np 8) #962, and the two ways users reach it: a refined mesh keeps its base's partition, and an extracted region keeps its parent's. Fault-split integration tests: cross-partition transfer and empty-rank cases not exercised in CI #888 item 3 confirms the first: a refined annulus kept two of eight ranks empty.

Checks

  • scripts/check_test_coverage.py on the branch: "OK: all 447 test files are reachable from scripts/test.sh".
  • Attribution: the commit and the PR body end with "Underworld development team with AI support from Claude Code". There is no Co-Authored-By trailer, no model name and no emoji.

Not done

  • No 3-D case.
  • The serial file tests/test_0786 was not rerun here; the author reports it passing.
  • The refusal stops a solve that would otherwise stop in MPI. It does not make these meshes solvable: that needs the PETSc fix upstream, or a communicator without the empty ranks for submeshes.

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit d66c7da into development Nov 13, 2026
10 checks passed
lmoresi added a commit that referenced this pull request Nov 13, 2026
…ction (#962)

_constrain_interior_multipliers_in_section removes the multipliers'
interior unknowns and rebuilds the DM's global section, which is
collective. It returned early on a rank with nothing of its own to
remove. With a rank that holds no cells that rank went on to the field
decomposition while the others were inside the rebuild, and the solve
hung at set-up (found by the parallel review at np 8; #1137's refusal
had hidden it).

The ranks now decide with one allreduce, so every rank rebuilds or none
does. On one rank nothing changes.

Test: a constrained solve on the region that rank 0 holds entirely
(multipliers on its side walls, no slip on its base) converges to its
one-rank speed, and asserts that one rank removes multiplier unknowns
beside a rank that has none. Run on one rank only here; its first
parallel runs are CI's, at np 2 and np 3.

Underworld development team with AI support from Claude Code
jcgraciosa pushed a commit to jcgraciosa/underworld3 that referenced this pull request Nov 17, 2026
…all (underworldcode#962)

A solver with kernels for both the Jacobian and the matrix its preconditioner
is built from is handed two matrices, and PETSc assembles them in one pass. On
a rank without cells that pass leaves before it has read that there are two
and skips an assembly of the Jacobian that the other ranks make
(DMPlexComputeJacobianByKey, PETSc 3.25.3 and main): the run stops in MPI or
hangs.

The local Jacobian routine registered by UW_DMPlexSetSNESLocalFEM now hands
PETSc one matrix and one set of kernels at a time: the Jacobian with the
preconditioner's kernel tables set aside, then the preconditioner's matrix
with its tables in the Jacobian's place. Both are decided by the discrete
systems, which every rank has alike. A solver with one matrix, or with no
preconditioner kernels, makes the one call it made before.

The preconditioner's tables are set aside for the first call because PETSc's
boundary assembly adds both boundary sets to one matrix when it is handed the
same matrix twice, and the Nitsche and multiplier terms are in both sets.

The Stokes refusal on a mesh with a rank without cells (underworldcode#1137) is removed.

On one rank both matrices equal PETSc's one-pass matrices in structure and bit
for bit in value: plain, Nitsche and constrained solves
(tests/test_0786_stokes_jacobian_and_preconditioner_one_call_each.py).

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.

2 participants