Particle Cache Policy¶
Retired 2026-11-02. The downscaled particle disk cache (cache=yes,
simple_ptcl_cache) was removed with the dynamic centring (ruling 19 of
doc/implementation_notes/completed/polar_shift_search.md), which prepares each
particle under its own mask; the cropped-particle path in memory
(crop_particles2D, the pass's records) stays. cache_dir remains as the flex
PCA plane cache's location (simple_flex_pca_plane_cache). The code is in git
history before that date; this document is kept as its record.
1. Purpose and Scope¶
This document defines the policy for the downscaled particle disk cache
(cache=yes, cache_dir=<dir>), implemented in
src/main/strategies/search/simple_ptcl_cache.f90 and consumed by the 2D
matcher workflows only. The 3D workflows (refine3D family, solve3D,
probabilistic 3D preparation, matcher reconstruction) do not consume the cache
and reject cache=yes with a hard error; caching never worked reliably for 3D
and was removed from those paths.
The samplers draw a fresh ~nsample subset of the particles every iteration and
each selected particle is read at full box and Fourier-cropped to box_crop.
In probabilistic mode the particle is read once per iteration since the batch
pipeline (2026-11-04): the matcher's batch serves the probabilistic scoring,
the search and the class-average restoration (particle_read_path_policy.md). The cache writes
the iteration-independent prefix of that preprocessing to disk once and serves
all subsequent reads from it, trading disk space at cache_dir for a roughly
(box/box_crop)^2 reduction in read volume.
Because the samplers sweep near-disjoint subsets before revisiting anything, a partial cache would never hit; the cache always covers all active particles.
2. What Is Cached¶
An entry is the noise-normalized (against the full-box lmsk),
Fourier-cropped particle, stored as a real-space box_crop image. Fourier
crop -> inverse FFT -> forward FFT is a round trip that is exact in
mathematics, not in single-precision arithmetic, so reading an entry back
reproduces the coefficients prepimg4align would have computed to rounding,
not bit for bit. The real-space round trip also drops the part of the cropped
Nyquist lines that no real image can hold.
The shift is not cacheable (re-read from the project every iteration) and the
CTF is deliberately left out so the cache does not depend on CTF parameters.
Cache files (stack, index, key) live in cache_dir (else
SIMPLE_PTCL_CACHE_DIR, else the execution directory). The basename carries
the project name, box_crop, and a hash of the absolute execution directory,
so concurrent runs sharing a fast disk cannot collide and every distributed
rank recomputes the same name locally (qsys workers cd into the master's
directory).
3. Consumers¶
- 2D alignment (
refine2D_exec: the probabilistic fill and the search): substitution equal to rounding (Section 2). - 2D class-average restoration (
cavger_update_sumswithcropped_ptcls=.true.): deliberate numerics change — edge taper and gridding source grid live atbox_crop, no second noise normalization, shift and CTF pixel size scaled to the cropped grid. Without the cache the 2D matcher and the 2D probability table make the same entry in memory from the full-size read of each batch, align from it and restore from it the same way (solve2D_policy.md,crop_particles2D), wheneverbox_crop < boxoutside the stream's 2D pool, so an uncached run is byte-identical to a cached one; the cache then only saves the reads and the per-iteration preparation. Not consumers, by design: every 3D workflow (alignment, probabilistic preparation, and reconstruction all read the full-size originals; a croppedrefine3Dreconstruction takes the observations its own search made from them, in memory, seeparticle_read_path_policy.md; therefine3Dexecution strategies throw oncache=yes), the one-off starting-volumereconstruct3D,volassemble(no particle reads), streaming 2D variants (long-lived processes over changing particle sets would thrash the validity fingerprint), and the flex / offload reconstruction paths.flex_pcakeeps a cache of its own that shares this module's machinery (Section 9).
The runs that set cache=yes themselves are the stream's initial-analysis
solve2D job (simple_stream_stage_initial_analysis.f90) and the solve2D of
the solve3D state-assignment route (simple_state_assignment.f90). solve2D
exposes cache and cache_dir, refine2D only cache.
A cache hit is read by the cache's own reader (ptcl_cache_read_batch: one
handle, read serially along the sorted particle indices), not by the read
session of particle_read_path_policy.md; its read time is kept apart. The
build reads the originals through the session.
4. Validity Contract¶
The key file is the commit record; no reader accepts cache files without a matching key. It records:
- the geometry line:
box,box_crop,smpd,smpd_crop,msk,oritype, particle and stack counts, and the execution directory verbatim (so a name-hash collision from a different directory can never validate); - the source fingerprint: every stack in project order with its particle
range, physical size and mtime, plus every particle's
stkind/indstkmapping as the project holds it. A reordered or remapped project over unchanged stacks therefore cannot validate a stale cache.
The check is conservative: a touched file forces a rebuild. Only the rank that decides whether to rebuild pays the full fingerprint; consumers check the geometry line and inherit the verdict.
5. Lifecycle and Ownership¶
The process that builds — or adopts a valid leftover, e.g. after a killed
predecessor in the same execution directory — owns the cache files and
removes them on normal exit and on hard exception, via the
cache_cleanup_glob hook in simple_defs (called from simple_error
and the tails of simple_exec/single_exec). Workers never take ownership,
so a dying worker cannot delete the cache under the other ranks or a
resubmitted part. Deletion is key-file-first, so a partially completed
cleanup can never leave a cache that still validates.
solve2D holds box_crop fixed across its stages, so the stable key lets
the owner fast path reuse one cache for every stage; there is no per-stage
invalidation.
Per-iteration calls hit an ownership fast path in ptcl_cache_ensure (same
owned key name, key file exists) and skip the full revalidation.
6. Space Budget and Uniform Fallback¶
Before building, the exact predicted size (nsel * box_crop^2 * 4 bytes plus
headers) is checked against the free space at the destination (statvfs); the
cache may claim at most 25% of it. Over budget — or with no active
particles, or a non-particle oritype — the whole
run falls back to uncached execution uniformly: disable_cache flips
cache=no on both params and the command line before any worker command
line is generated.
Uniformity is mandatory, not best-effort: a rank that fell back would prepare
its particles by another route than its peers, so ranks must never mix modes.
ptcl_cache_assert_ready hard-stops a worker that expected a cache and
cannot find one (the classic case: node-local cache_dir not visible to
every rank).
7. Eligibility¶
The cache is refused, uniformly and at every decision level (in_use,
assert_ready, ensure), when:
box_crop >= box(nothing to gain);oritypeis notptcl2D/ptcl3D— cls3D "particles" are class averages inos_out, which the stack fingerprint cannot see.
8. Known Limitations¶
- Signals and Fortran runtime errors bypass the cleanup hook. SIGKILL,
scancel, node failure, or a runtime abort leaves the files behind. Correctness is preserved (a rerun validates or rebuilds), and a rerun in the same execution directory reclaims the orphan, but a run in a fresh directory strands the old files incache_diruntil manually removed. - Node-local
cache_diron multi-node jobs. The master builds on its own node; workers on other nodes then hard-stop with the uniformity error. Intentional, but there is no replicate-to-node-local mechanism. - Mid-run activation of previously inactive particles (after the ownership fast path has skipped revalidation) fails loudly at read time rather than being served silently; this does not occur in the current workflows.
- Cached and uncached runs outside the stream's 2D pool are byte-identical (Section 3). Against the full-size preparation that the pool keeps, the cached class-average restoration is accepted on statistical parity, not bit equality, and the alignment paths agree to rounding (Section 2).
9. Shared Machinery¶
simple_ptcl_cache exports three helpers for other disk caches:
ptcl_cache_dir: the location rule of Section 2;ptcl_cache_run_token: the hash of the execution directory used in the cache names;ptcl_cache_space_ok: the budget of Section 6, at most a quarter of the free space at the destination; an unknown free space does not block the build.
Their only other user is the flex_pca plane cache
(src/main/flex/fit/simple_flex_pca_plane_cache.f90). It holds each selected
particle's prepared, padded Fourier transform on the box_crop grid, and is
used with cache=yes when box_crop < box.
Both caches follow the same rules:
- they live where
ptcl_cache_dirsays, and the directory is created when missing; - a stale cache is deleted before the budget check, so the check measures the space the rebuild can use;
- over budget, the run reads its particles without the cache;
- the cache is built under a temporary name that contains the run token and
_part, and its commit record is written last. The particle cache renames its stack and index into place and then writes the key. The plane cache writes its header record into the finished file and then renames the file. A reader therefore finds either no cache or a complete one.
The plane cache differs in its lifetime and validation. Its published name
carries box_crop and a hash of the absolute project path, not the run token.
The run that built it deletes it once the master has its embedding, because
nothing after the embedding reads planes (and with mkdir=yes every run works
on its own project copy, so no later run could adopt it). The header records the project path and modification
time, the geometry and a hash of the master's particle selection. The master
accepts only the exact selection. A worker accepts the cache when its partition
lies within it, and otherwise reads its particles natively.
10. Future Extensions¶
- Orphan scavenger: sweep stale
ptcl_cache_*file sets incache_diratptcl_cache_ensuretime (age- or dead-key-based) to reclaim signal-killed leftovers automatically. - Benefit predicate:
ensureknowsnsel; with the planned iterations, sampling schedule, and crop ratio it can compute predicted bytes saved versus the one-time build, then decline uneconomical caching through the existing uniform fallback. - UI promotion:
cache/cache_dirareUI_VIS_DEVELOPER; promote after validation, and consider defaultingcache=yesforsolve2D.