Skip to content

A disk snapshot restore writes swarm-variable values through to PETSc (#971) - #972

Merged
lmoresi merged 6 commits into
developmentfrom
bugfix/disk-snapshot-swarm-values
Nov 7, 2026
Merged

lmoresi merged 6 commits into
developmentfrom
bugfix/disk-snapshot-swarm-values

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

A disk snapshot restore now writes swarm-variable values through to the PETSc field (#971).

The bug

checkpoint.disk_snapshot._read_swarm_from_sidecar restored each swarm variable into a detached view: current = np.asarray(var.data); current[...] = saved. The DMSwarm field therefore kept the values the re-added particles were given. These readers saw those values instead of the saved ones:

  • the cells proxy of the particle Lagrangian stress history, which reads the field directly;
  • any read after a cache invalidation, as migrate() does after advection.

So a run restarted from disk with stress_transport="lagrangian" lost its stress history. On the viscoelastic cylinder, a Lagrangian-stress restart dropped cd by about 1.2, the polymer contribution. With the fix, a resolution-20 restart matches the uninterrupted run to five figures.

The change

The restore writes through var.data[...] = saved. That fires the pack callback to PETSc, as the in-memory restore (Swarm.apply_snapshot_payload) has done since #313. This is the raw variable-to-variable copy at the persistence boundary that .data is kept for.

Tests

tests/test_0020_snapshot_disk_swarm_values.py (tier B, in shard 4's test_00[0-4]* batch):

  • after load_state(file) the PETSc field holds the saved values;
  • they survive _invalidate_canonical_data().

Against development eddab70's disk_snapshot.py, both tests fail; on this branch both pass. The test sets its values up through .array.

The fix was written on 2026-10-06. This branch merges development (eddab70) into it rather than rebasing.

Issues

A parallel case, tests/parallel/test_0020_snapshot_disk_swarm_values_parallel.py, checks the restore on every rank (hyperion measured it at np 2 and 4: no mismatches, and a Lagrangian restart matching an uninterrupted run to about 1e-16).

Underworld development team with AI support from Claude Code

lmoresi and others added 4 commits October 6, 2026 16:49
_read_swarm_from_sidecar wrote the saved values into a detached numpy
view (np.asarray(var.data)), so the DMSwarm field kept the values the
re-added particles were given. Anything reading the field directly, or
re-reading it after a cache invalidation, saw those instead: a run
restarted with stress_transport="lagrangian" lost its stress history
(the cells proxy reads PETSc; the first migrate() drops the cache).
Write through var.data, as Swarm.apply_snapshot_payload already does
for the in-memory path (#313).

Found on the viscoelastic cylinder: a Lagrangian-stress restart dropped
cd by ~1.2 (the polymer contribution); with the fix a res-20 restart
matches the uninterrupted run to five figures. test_0012 checks the
PETSc field and a post-invalidation read after load_state(file).

Underworld development team with AI support from Claude Code

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Underworld development team with AI support from Claude Code

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Underworld development team with AI support from Claude Code
A proven regression test starts at tier B. The values the test sets up and
compares go through .array (three-index shapes) rather than the flat .data;
the PETSc field itself is still read through unpack_raw_data_from_petsc,
which is what the test is about. On development's disk_snapshot.py both
tests fail; with the write-through both pass.

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 6, 2026 09:43

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

Copy link
Copy Markdown
Member Author

Adversarial review of #972

Verdict: ready. Reviewed head 338e899. The merge of development into the branch is clean, and the fix works at np 1, 2 and 4, including a rank with no particles. Nothing we tried breaks it. The findings are low severity; only the missing parallel test is worth acting on in this PR.

Findings

  1. Low, test reach. Nothing in tests/parallel/ exercises the per-rank sidecar restore. test_0020 is serial, tier B, in the test_00[0-4]* glob. Our np 2 and 4 probes are the only parallel evidence. We suggest one parallel case for the value restore.
  2. Low, pre-existing. A shape mismatch in _read_swarm_from_sidecar raises on one rank only, and load_state has no agreement step afterwards. With the rank-1 sidecar variable cut to 265 rows at np 2, rank 1 raised shape mismatch (265,1) vs (270,1), rank 0 returned normally and went on to migrate, and the job hung if the exception was caught on rank 1. This needs a corrupt sidecar plus a caught exception; in normal use all ranks raise together. Rank 1 is also left half restored.
  3. Low, pre-existing, read not run. The reader rejects a sidecar variable the live swarm lacks, but not a live variable absent from the sidecar, which Swarm.apply_snapshot_payload does reject. The docstring's "mirrors ... exactly" is not true.
  4. Info, pre-existing. A fresh process cannot load a lagrangian snapshot straight away: snapshot variable '_eval_work_scalar' not registered on mesh. It loads once one solve and one evaluate have run first. eddab70 behaves the same.

Callbacks: writing through var.data fires canonical_data_callback, which runs no collective, so the order across variables does not matter. The proxy refresh is lazy. The sidecar writer skips DMSwarm* variables, so the coordinate variable is never written back this way.

Attacks that failed

  • Disk save and restore with scalar proxied variables, a vector and a 2x2 tensor, compared with the saved values on the PETSc field: np 2, 270+270 particles, 0 mismatches; np 4, 138/138/132/132, 0 mismatches. Still correct after _invalidate_canonical_data() and migrate(remove_sent_points=True), with 540 particles kept.
  • Empty ranks, 300 particles in x<0.25: np 2 n=300/0; np 4 n=0/0/111/189. Values, invalidation and migrate pass on every rank, no hang.
  • Different rank count: written at np 2, read at np 4: refused on all four ranks before any swarm change.
  • Lagrangian restart end to end, Maxwell shear box, 6 steps, save, 6 more, against an uninterrupted 12-step run: np 1 matches to the last printed digit (0.681369181748757); np 2 and 4 agree to about 1e-16. eddab70's restart gives 0.43553 at np 2 against 0.68137.
  • eddab70 fails the same probes at np 2 and 4 on every rank, with a proxy range of 0..0.
  • Both test_0020 tests fail on eddab70 for the stated reason: PETSc field all 0 against saved values near 2.1. Both pass here.
  • Merge: git diff eddab707 HEAD --stat is 2 files, +71 -1, the patch only.
  • CI reach: test_0020 is in shard 4's glob, and the pytest line is -m "not tier_c".

Issues

Merge conditions

None required. Suggested: one tests/parallel/ case for the sidecar value restore, and an issue or link for findings 2 and 3.

Could not test

Restore inside migration_disabled; IndexSwarmVariable and integer dtype; np 8 and a written-4 read-2 refusal; test_0020 under mpirun, which hung at np 2 because tmp_path differs per rank, a test-design effect; the viscoelastic-cylinder figure in the PR body.

Underworld development team with AI support from Claude Code

The sidecar is read per rank, so the restore is checked on every rank: the
PETSc field after load_state(file), and a re-read after the canonical cache
is dropped. Hyperion's review measured the same at np 2 and 4. The snapshot
file is rank 0's temporary path, broadcast to all.

Underworld development team with AI support from Claude Code
…m-values

Underworld development team with AI support from Claude Code
@lmoresi
lmoresi merged commit 1733fd6 into development Nov 7, 2026
8 checks passed
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