Skip to content

Rotated workspace: reuse the coefficient enumeration only for the same kernels (#546) - #1056

Merged
lmoresi merged 2 commits into
developmentfrom
bugfix/rotated-cache-enumeration-546
Nov 11, 2026
Merged

lmoresi merged 2 commits into
developmentfrom
bugfix/rotated-cache-enumeration-546

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 11, 2026

Copy link
Copy Markdown
Member

The rotated free-slip workspace keeps the operator between solves and reassembles it when a variable the operator depends on changes. It reads which variables those are from the constitutive parameters, and reused that list whenever the geometry matched. A rewire that adds a variable (a viscosity that now depends on a temperature) was safe only because every rewire also tears the workspace down (#546, m3). The list is now reused only when the cached constants signature carries the same JIT key as the current kernels, so a new variable is watched by construction.

Tests

tests/test_1158_rotated_cache_watches_new_coefficients.py (new, tier B). It sets up a workspace that claims older kernels with no coefficients, as a rewire that kept the workspace would leave it, then changes the newly added variable.

  • With the old reuse, the first increment rode the viscosity-1 operator (residual 0.19 -> 0.072), and the linear solve took 2 increments.
  • With this change it takes 1 increment and matches a fresh solver to 1e-6.
  • H1 ran it at np 1 and 2, failing on development and passing here.
  • The JIT key is agreed across ranks by construction (an allgathered source sha), and the enumeration is symbolic, with no collectives. A forced divergence at np 2 neither hung nor gave a wrong answer. The enumeration costs 0.12 ms.

Serial: test_1158 and test_1018 give 28 passed.

Issues

Underworld development team with AI support from Claude Code

…e kernels (#546, m3)

The workspace reassembles the operator when a variable it depends on changes.
It reads which variables those are from the constitutive parameters, and used
to reuse that list whenever the geometry matched. A rewire that adds a variable
was safe only because every rewire also tore the workspace down. The list is
now reused only when the cached constants signature carries the same JIT key,
so the new variable is watched by construction.

test_1158 (new, tier B) leaves a workspace that claims older kernels with no
coefficients, then changes the newly added variable. With the old reuse the
first increment rode the viscosity-1 operator (residual 0.19 -> 0.072) and the
linear solve took 2 increments; with this change it takes 1 and matches a
fresh solver to 1e-6.

#546 m1, probed on development: a new set_custom_fmg registration rebuilds the
workspace (velocity MG 3 -> 2 levels, prolongations 2 -> 1), and a new tolerance
drops the KSP context (tol_ok). Nothing to fix there.

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

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

Copy link
Copy Markdown
Member Author

Review record: #546 m3 (3bc1e6b)

Verdict: ready. This is the m3 part of a pre-PR review of bundle C4, run on hyperion against the branch head 005a34b. 3bc1e6b changes src/ in exactly the same lines as 005a34b's m3 commit. The measurements ran on a build that also carried the #910 floor (caa31c1). The m3 behaviour below does not depend on it: the m3-reverted variant (caa31c1 alone) fails test_1158, and the floor mutants pass it.

Check 5: #546 m3 (and m1)

test_1158

development branch caa31c1 (m3 reverted) nofloor / noscale / x1e6
np 1 fail (the white-box [] == ['T1158']) pass fail pass
np 2 fail pass fail pass
behaviour only, np 1 2 increments, rel 4.0e-10 1 increment, rel 4.9e-10 — —
behaviour only, np 2 2 increments, rel 8.4e-10 1 increment, rel 8.4e-10 — —

W1's behavioural claim holds: 2 increments against 1, the same answer.

Can the JIT-key comparison be rank-divergent?

  • [read] The key is _GextResult.cache_key, a sha256 of the canonical C source. _agree_source_across_ranks allgathers it and adopts rank 0's source on any disagreement, so the key is identical on all ranks by construction.
  • [read] The stored operator_sig is None only when did_assemble or reuse_operator is false. Both follow collective decisions; op_ok is allgathered before it gates anything.
  • [read] The enumeration (_operator_coefficient_variables) is symbolic: unwrap, mesh_vars_in_expression and a sort by _uw_id. It makes no PETSc, DM or MPI calls, so a rank that enumerates while the others reuse cannot desynchronise a collective.
  • Measured, at np 2: I forced the divergence. Rank 0 got a forged key, so it re-enumerates. Rank 1 got a stale empty coefficient list, so it reuses. Then T changed twice.
    • Result: no hang, 1 Newton increment each time, and a velocity relative difference of 1.8e-10 against a fresh solver, on both development and the branch.
    • The per-rank lists then stay divergent ([Tc4] on rank 0, [] on rank 1) until the next rebuild. The allgathered op_ok absorbs this: any watching rank forces the refresh everywhere.
  • Not tested: a mesh with an empty rank. The repository's empty-rank fixture needs a one-cell mesh at np 2, which cannot carry a rotated annulus problem, or np 3. The key itself does not depend on the cells.

Cost. The extra enumeration when the key is None costs 0.11 to 0.13 ms per call; the constants signature costs 0.012 ms. Both were measured at np 2 on a 0.2 annulus. That is negligible next to a solve.

Is the stale state reachable through the public API today? It is not. Setting Parameters.shear_viscosity_0 = 1 + T takes the function-rewire path (flags (False, False, True)), and the next solve rebuilt the rotation: new Q handle, rotation_reused False, enumeration ['Tc4']. This was measured at np 1 and np 2, on development and the branch. W1's "hardening" description is accurate.

m1, measured at np 1 and np 2, the same on development and the branch:

  • A tolerance change (1e-8 to 1e-9) drops the KSP context (ctx None) and keeps Q. The next solve builds a context at the new tolerance: outer rtol 1e-9, atol 4.95e-12.
  • set_custom_fmg re-registration sets is_setup = False. That triggers a full teardown (cache None, Q None), and the next solve builds a new workspace with velocity_pc = custom-FMG.

W1's m1 verdict is confirmed.

Mutants against test_1158 (np 1 and np 2)

variant test_1158
m3 reverted fails
no floor, floor with ref = 1, 1e6 multiplier (#910 variants) pass

Findings for this PR

  • F5 (low). A forced rank divergence leaves per-rank coefficient lists that differ until the next rebuild. The allgathered op_ok makes this harmless, and the JIT key is agreed across ranks by construction [read], so the divergence is not reachable. If a guard is wanted, allgather same_kernels (one bool).
  • test.sh. The test_1158 line is correct: it appears once, in batch 4, and no other glob matches it.

Issue status. #546 m3 is fixed; it is hardening, since the public API doesn't reach the stale state. m1 is already fixed on development, as the probe above confirms.

Not run: np 4, and an empty-rank partition.

The full C4 record is on hyperion at runs/2026-11-11-910/review/c4-review.md. Its #910 part goes with the #910 rework.

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit 5234423 into development Nov 11, 2026
10 checks passed
lmoresi added a commit that referenced this pull request Nov 11, 2026
… via #1056)

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