Skip to content

Rotated free slip: a workspace rebuild frees what the old workspace dropped (#1007) - #1013

Merged
lmoresi merged 4 commits into
developmentfrom
bugfix/rotated-prolongation-leak-1007
Nov 8, 2026
Merged

lmoresi merged 4 commits into
developmentfrom
bugfix/rotated-prolongation-leak-1007

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 8, 2026

Copy link
Copy Markdown
Member

A rotated free-slip solve whose expression is reassigned each step no longer grows memory in parallel (#1007).

The bug

A reassigned expression re-JITs the solver and rebuilds its DM. The solver's own _reset_rotated_solver_cache then drops the rotated workspace before solve_rotated_freeslip runs. The next rotated solve finds no cache, so it builds a new rotation, operator and custom-FMG prolongation.

The dropped prolongation list and the builders' intermediate matrices were never destroyed. In parallel petsc4py defers each destroy to a collective PETSc.garbage_cleanup, and only a new mesh ran one (test_0078). So every rebuild kept them. On development at np 2, #1007's table has peak RSS rising about 7 MB per rank per solve.

The change

  • A solve that built a new workspace (not rotation_reused) ends with mpi._free_petsc_garbage(solver.dm.comm), the cleanup a new mesh runs.
    • Every rank makes the same choice: the cache is dropped by a collective reset or by the allgathered geometry verdict, and the persist gate reads the global KSP reason.
    • The cleanup runs at the end of the solve, once the new workspace has replaced what the old one shared.
    • A first attempt ran it at the geometry verdict instead. Hyperion showed it was never reached on this path, because the solver's reset empties the cache first.
  • Both prolongation builders destroy the velocity submatrix they only need for the product.
  • Cost: a gc.collect() plus the cleanup on each solve that rebuilds. That includes every solve that keeps no workspace, such as the direct-LU diagnostic and datum solves.

Evidence

From hyperion, Linux, np 2, against development 06c574e. The record is posted below as the review.

  • leak.py, rebuild every solve (peak RSS over both ranks):
    • this branch: 784, 790, 797, 801, 801, 801 MB, flat from the third solve;
    • development: 786 to 856 MB, still rising.
  • leak.py, reuse path: flat on both.
  • Development with the same cleanup called by hand after each solve is flat after three solves too, so those three are heap warm-up and not leak.
  • The 23 parallel files that use rotated free slip: np 4, 91 passed, 6 skipped and 1 xfailed; np 2, no regressions. On wombat, serial: test_1131, test_1142 and test_0022, 46 passed and 1 skipped.

tests/parallel/test_1144, tier B, at np 2:

  • What it does: reassigns the body force each solve, asserts the workspace was rebuilt each time, and counts the entries in petsc4py's deferred-destroy list (PETSc.garbage_view) after each solve.
  • Why not RSS: a resident-set version passed on development whenever an earlier test in the same process had freed enough heap to absorb the leak, so it depended on where CI's batches of six files fell (hyperion).
  • Result: on development the list grows at every rebuild, and the test fails alone, in its CI batch, after test_1139 and after test_1097. On this branch it passes in all four.

Issues

Underworld development team with AI support from Claude Code

…ropped (#1007)

A reassigned expression re-JITs the solver and rebuilds its DM, and dm.handle is in
the rotated workspace's geometry signature, so the workspace rebuilds: rotation,
operator and custom-FMG prolongation. The old prolongation list and the builders'
intermediate matrices were dropped, not destroyed, and in parallel petsc4py defers
each destroy to a collective garbage cleanup, which only a new mesh ran: about 7 MB
per rank per solve at np 2 (#1007's table). The rebuild now runs
mpi._free_petsc_garbage on the solver's communicator; the verdict that triggers it is
allgathered, so every rank takes it together. Both builders destroy the velocity
submatrix they only need for the product.

tests/parallel/test_1144: a body force reassigned each solve at np 2, the workspace
rebuilt every time (asserted), resident-set growth under 2 MB per solve and rank
(Linux only, as test_0078).

Underworld development team with AI support from Claude Code
…ew workspace

The cleanup at the cache verdict was never reached on the case it was for: a
reassigned expression rebuilds the solver, whose own _reset_rotated_solver_cache
empties the rotated cache before solve_rotated_freeslip runs, so the verdict sees no
cache (hyperion, np 2: no change against development, 6.6 MB per solve). It now runs
at the end of any solve that built a new workspace (not rotation_reused, the same on
every rank: the reset is collective and the verdict allgathered), once the new
workspace has replaced what the old one shared. Hyperion's control, the same cleanup
called by hand after each solve, flattens the growth after three solves.

Underworld development team with AI support from Claude Code
A test that runs earlier in the same process can free enough heap to absorb the leak
before the resident set moves: on development test_1144 passed after test_1139 or
test_1097 (about 33 MB absorbed, hyperion 2026-11-08), so whether it caught the leak
depended on where CI's batches of six files fell. The test now reads petsc4py's
deferred-destroy list through PETSc.garbage_view (file descriptor 1 captured around
the call), which belongs to this process and does not depend on what ran before:
fixed, every rebuild's drops are freed at the end of its solve and the summary does
not grow; unfixed, it grows at every rebuild. No longer Linux-only.

Underworld development team with AI support from Claude Code
#1007)

Hyperion: names and object ids that print wider could grow the summary by a byte or
two with no growth in the list, a false failure. The test sums the "Total entries: N"
lines garbage_view prints (rank 0 prints every rank's). At np 2 the byte-length form
failed on development in all four orders tried (alone, in its CI batch, after
test_1139 and after test_1097: rank 0 grew 5412 bytes a rebuild) and passed on the fix
in all four.

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

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

Copy link
Copy Markdown
Member Author

Review record for this PR: #1007 at np 2 and 4, hyperion

Verdict: ready, no open merge condition. The leak is gone at np 2. test_1144 fails on development and passes on this branch whatever runs before it in the same process, and no rotated parallel file regresses. Head 5d431d3; the fix is unchanged since ee3f4d4 and the later commits only touch the test. Base development 06c574e. Linux, PETSc 3.25.3, MPICH, through scripts/mpi_supervisor.py.

The first fix did not work, and why

On 8b39ba6 the cleanup ran at the geometry verdict. At np 2 peak RSS still went 785, 798, 813, 826, 840, 854 MB over six rebuild solves, against 786 to 856 on development. The RSS-based test_1144 failed on both, at 6.61 MB per solve per rank. Controls in test_1144's loop, current RSS on the worst rank, change per solve:

Run MB per solve
8b39ba6 7.0, then 6.5 every solve
8b39ba6, uw.mpi._free_petsc_garbage() after each solve in user code 2.9, 3.0, 1.9, then 0
development, the same cleanup after each solve 4.6, 3.0, 1.9, then 0
8b39ba6 serial 6.9, 3.1, then about 0
8b39ba6, body force not reassigned (workspace reused) 0

A collective cleanup after the solve was enough on development too, so the cleanup at the verdict was never reached on this path. W1 traced it: the solver's own reset empties the cache first. This PR moves it to the end of the solve.

This PR, np 2

leak.py, peak RSS summed over both ranks, MB:

Mode 5d431d3 (fix as ee3f4d4) 06c574e
rebuild every solve 784, 790, 797, 801, 801, 801 786, 801, 815, 828, 843, 856
reuse 781, 788 ×5 783, 791 ×5

test_1144's loop, current RSS on the worst rank, change per solve: 3.4, 3.2, 1.9, then 0 for five solves. That matches development with the hand cleanup, so the first three solves are heap warm-up, not leak.

The test can fail

The RSS version of test_1144 depended on run order. On development it failed alone (6.61) and after test_1142 (6.56), but passed after test_1139 or test_1097 in the same mpirun: the heap those tests free absorbed the leak. CI's batches are six files in glob order, so the outcome depended on where the boundaries fell. The test now counts petsc4py's deferred-destroy entries from PETSc.garbage_view:

Order, np 2 06c574e + this test 5d431d3
alone fails: [190, 256, 322, 388, 454] entries passes
after test_1139 fails: the same list passes
CI batch (1140, 1142, 1144), byte-length version at 9ca1c19 fails passes
after test_1097, byte-length version fails passes

Development keeps 66 more entries at every rebuild. The list is the same whatever ran before. With nothing deferred, garbage_view prints one line per rank, Rank r:: Total entries: 0, with no timestamp. Rank 0 prints every rank's table, so rank 1's capture is empty; the sum over the captured lines covers both ranks.

Regression: the 23 tests/parallel files that use rotated free slip, at ee3f4d4

  • np 4: 91 passed, 6 skipped, 1 xfailed.
  • np 2: my first pass hit my own 3600 s cap at 73% while hyperion was loaded. The supervisor saw both ranks still moving, and nothing had failed. The 7 remaining files then gave 33 passed in 5:00, against 5:02 for the same set on development.

Found on the way

#1012: Stokes with bodyforce reassigned each solve keeps 23 MB per solve serially and 28 MB per rank at np 2 on development. It is not deferred garbage, and not this PR's path. A uw.expression changed in place does not leak, and neither does Poisson with f reassigned.

Issues

#1007: fixed by this PR at np 2. #1012: separate, still live.

Could not test

np above 4; long time loops beyond 9 solves; the spherical-shell rotated files, which skip on this host's gmsh; the adjoint path's reuse of the forward prolongation, which was checked only by reading.

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit ba3fc23 into development Nov 8, 2026
10 checks passed
lmoresi added a commit that referenced this pull request Nov 8, 2026
…rence-policy

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