Skip to content

Batch-threaded accumulation for the PCG reconstructor

Status, 2026-11-29: planned, decisions 1 to 15 ruled as recommended by the maintainer; for an autonomous run pcg_slabs on dell2; not launched (the launch waits for the base commit of decision 12 and his go). Base: the PCG code was checked at 211c6a24d. The run's base is a later commit that must hold the diff of batch_threaded_plane_insertion.md (run rec_slabs, in the working tree on 2026-11-29) and, by decision 12, the diff of float32_kernels_and_pcg_precond.md (run f32_pcg). The launch copies the checkout and Phase 0 records the base it got and re-checks the facts below there. Supersedes: none. Depends on: the batch insertion of the gridding reconstructor (validated: yes, dell2 and the M5 Ultra, batch_threaded_plane_insertion_report.md); the slab ownership of simple_pcg_lattice.f90 (validated: yes, in production). Integration owner: reconstructor_pcg (src/main/volume), inside accumulate_batch. No layer changes and no caller changes: the strategy that hands over the batches is not edited. Queue: f32_pcg (the Dell, in its last phase on 2026-11-29) edits simple_reconstructor_pcg.f90 in the operator's apply path, the fold and the solve, and adds a PCG benchmark to the test entry files; its diff at phase 7 has no hunk in the three routines this plan works in. rec_slabs' diff adds its benchmark to the same test entry files. Decision 12 sequences the three.

Some terms. The PCG reconstructor accumulates every particle into two accumulators on a twofold padded Fourier lattice: the right-hand side B (complex) and the density D (real). A particle's plane value is deposited through a window of 3 x 3 x 3 lattice points. A team is the threads of a parallel region; a meeting is a point where the whole team waits for its slowest thread (a barrier, the end of a work-sharing loop, a single). rec3D accumulation is the timer of the reconstruction stage in SIMPLE's bench file.

1. Why

On the M5 Ultra the PCG accumulation does not scale. reconstruct3D rec_backend=pcg on 3,000 simulated particles (box 192, c3, Release build of 8c4276822, 2026-11-29, two rounds) gave for rec3D accumulation:

Threads PCG, round 1 / 2 (s) Gridding before rec_slabs (s)
1 35.8 / 35.0 8.39
2 30.8 / 28.1 4.93
4 18.8 / 22.7 3.14
10 24.9 / 22.6 4.95
20 26.2 / 25.1 8.79

The gridding insertion had the same shape, and for it the cause was the team meeting several times per plane. Its batch form, whose threads each own a slab of the lattice for a whole batch and never meet inside it, takes 0.18 of the per-plane loop's time at ten threads on the M5 and 0.49 on dell2's sixteen cores, with identical bytes (batch_threaded_plane_insertion_report.md). The PCG accumulation already runs one parallel region per batch, but inside it the team meets about ten times per particle (code check 2). This plan removes those meetings the same way. No gain is estimated: the share of the stage's time that lies in the scatter is not measured (the timer also covers the operator's set-up and the write of the raw accumulators), dell2 has no PCG timing at all, and Phase 0 measures both before any edit (decision 14).

What the code check found (2026-11-29, at 211c6a24d)

  1. accumulate_batch (public) detaches the accumulators and calls the private accumulate_rhs_density, which opens one parallel region for the batch of nb particles.
  2. Inside the region every thread runs the loop over particles and symmetry copies. Per particle the team meets at the end of the work-sharing loop of prepare_fused_planes (the CTF, the shift phase and the weights of one plane into two shared arrays). Per symmetry copy it meets three more times: after the even-numbered slabs, after the odd-numbered slabs, and at a single that deposits the windows wrapping around the lattice's edge. That is 4 meetings per particle in c1, 10 in c3 and 13 in d2.
  3. Slabs are cut along the third lattice index, each as thick as a window (3 planes: KBWINSZ 1.5, OSMPL_PAD_FAC 2); box 192 has 128 of them. A window belongs to the slab its first plane lies in and reaches into the next one, hence the two phases. Thread t owns the slabs with mod(islab/2, nthr) = t.
  4. Every lattice element receives its windows in a fixed order whatever the team: by particle, by symmetry copy, then the even slab's windows, the odd slab's windows and the wrapping windows, each in the order of the plane's rows and columns. Within one phase the windows that reach an element all start in one slab. The accumulators' bytes therefore do not depend on the team (stated in the lattice's comment; Phase 0 measures it).
  5. The production caller accumulate_pcg_state_half (simple_rec3D_pcg_strategy.f90) hands over batches of at most MAXIMGBATCHSZ = 500 particles and, under the weighted insertion (topk > 1), each batch's extra poses as a second batch from copies of the same planes. The batch already exists; no pool is needed.
  6. rec3D accumulation of the PCG worker is the time of a whole state half (operator construction, prep_particles, the batches, the write of the raw accumulators) minus reading and preparation.
  7. The accumulators of box 256 take about 1.6 GB (512 cubed elements of 12 bytes); a plane's two prepared arrays take 0.79 MB at box 256, so 500 of them take 396 MB beside the 264 MB of the batch's planes.
  8. There is no tester submodule of simple_reconstructor_pcg; simple_pcg_lattice_tester.f90 exists. The private loop can be compared with a new one only from a submodule (test policy, section 4.3). The public get_raw_accum hands out the open accumulators, so a benchmark can compare their words without one.
  9. rec_backend defaults to gridding: no default workflow runs the PCG.
  10. The gridding batch entry (insert_planes_batch) takes optional slab count and axis for its tester and benchmark, runs its slabs under schedule(dynamic,1) and keeps the per-plane loop for a team of one and for batches whose heaviest slab holds more than 1.5 times a thread's share.
  11. dell2's machine facts, build recipe, environment script and frozen real inputs are recorded in batch_threaded_plane_insertion.md, Progress, Phase 0, and lie under ~/agent_runs/rec_slabs/scratch/keep there.

2. Contract

  1. B and D after accumulate_batch are today's word for word, at every team and for every batch, weight, symmetry and CTF setting. Nothing downstream of them changes by a bit.
  2. Today's loop stays in the file with no changed line (accumulate_rhs_density, prepare_fused_planes apart from decision 3's shared kernel, and the lattice's existing routines). It is the reference of every byte check and the fallback where the rules of decisions 6 and 7 build one.
  3. Signatures of the public procedures of reconstructor_pcg keep their required arguments; optional arguments may be added (decision 10).
  4. Off state: where a rule sends a batch to today's loop, no line of the new walk runs for it.
  5. No parameter, no command-line key, no change of a file format.

3. Decisions and rulings

Proposed 2026-11-29 and ruled the same day by the maintainer, all fifteen as recommended ("Rule as recommended"). The numbers marked guessed stay guesses: the run reports what they did, it does not tune them.

  1. Today's loop is kept, unchanged. Its retirement is reported, not done: the report says in how many cells the batch walk would have lost against it.
  2. Ownership by element, today's order replayed. Each thread owns a contiguous range of the accumulators along one lattice index for a whole chunk of the batch and adds only the window taps that fall inside its range. It walks the chunk's particles in order and, per symmetry copy, the windows of even slabs, then of odd slabs, then the wrapping windows: code check 4's order restricted to its elements. A window across a boundary is deposited in part by each neighbour. No meeting inside a chunk.
  3. The plane values are prepared per chunk. Before a chunk's walk the two prepared arrays of all its particles are filled, in parallel over particles, by one kernel that prepare_fused_planes also calls. A chunk ends where its prepared arrays would exceed 256 MB (guessed) and holds at least one particle; a cut changes no byte. Not built: each thread evaluating the values itself (no memory, but the arithmetic twice).
  4. The layout is chosen by a frozen protocol, equal work first. Candidates: W1, boundaries placed for equal estimated work with one range per thread; W2, the same with two ranges per thread under a dynamic schedule; E2, equal thickness with two per thread under a dynamic schedule (the gridding entry's layout). The estimate is ruling R6's of rec_slabs: a histogram of the chunk's samples per layer from every third row and column, a range's cost its samples plus one third of the window layers they put inside it, the contiguous partition whose heaviest range is lightest (the two numbers are guessed and shape balance only). Six alternating passes over teams 4, 8 and 16, boxes 96, 160 and 256 and the fixtures that take ranges; geometric mean of the batch times. W1 is adopted unless its mean is more than 3 % (guessed) above the lowest; else W2 by the same rule; else E2. The maintainer's reason for the order (2026-11-29): "even if the equal work approach is just matching it is nicer in a way because it is free of parameters".
  5. The index is chosen per chunk: the third or the second lattice index, whichever partition has the lighter heaviest range; the third on a tie. The first index is not used (it is the fold's).
  6. Fallback by a test on the partition itself, built only if a judged cell misses its bound without it: a chunk whose heaviest range holds more than L times a thread's share takes today's loop, L the value of 1.5, 2 and 3 with the lowest geometric mean that meets every bound.
  7. A team of one runs the walk with one range. If a one-thread cell misses its bound, teams below PCG_ACC_MIN_THR take today's loop, the constant being the smallest team that meets its bounds.
  8. Bytes are proven in a new tester submodule and in every benchmark row. Cases: both indices; one range to one-layer ranges; strongly unequal boundaries; teams of 1 and 4; c1 and d2; CTF on and off; weights; a noise spectrum; a chunk cut at several places; a batch given in two calls; a box small enough to have wrapping windows. Planted control: the chunk walked in reverse must differ. Debug build with bounds checks.
  9. Acceptance. (a) No loss: in every benchmark cell the batch time is at most (1 + max(0.03, 2 x spread)) times today's loop's, the spread being today's loop's between two sweeps, frozen in Phase 1 (0.03 guessed); a cell within 5 % of its bound is judged on the median of three invocations. (b) Gain verdict, reported: on dell2's sixteen cores, box 160, uniform planes, c1, at most 0.80 of today's loop's time (derived from the gridding entry's 0.43 there). (c) Production bytes: every output of reconstruct3D rec_backend=pcg that the base reproduces is identical between base and new, at 1 and at 16 threads. (d) Production time, reported: rec3D accumulation new over base, three runs each way, interleaved.
  10. The benchmark is a second measure-only entry in the gridding benchmark's file, simple_test_exec test=pcg_accum_bench, with that entry's fixtures (uniform, top, side, tilt, cube), its row format and its keys; boxes 96, 160 and 256; c1, c4 and d2; 500 planes; arms plane (today's loop) and batch. From Phase 2 on it reaches today's loop through an optional range count on accumulate_batch (zero: today's loop), as the gridding entry does, and compares the words through get_raw_accum. Timing protocol and core sets as rec_slabs decision 10: teams of 1, 2 and 4 on the P cores, 8 on the E cores, 12 and 16 mixed, pinned with taskset, two sweeps.
  11. Where: dell2 under arun, on the precedent of rec_slabs, whose kept builds, scripts and inputs it reads. Master is not frozen and the run does not sync with it.
  12. Sequencing with rec_slabs and f32_pcg. The run launches on a commit that holds both diffs. Recommended, because all three add entries to the same test files and two edit the PCG reconstructor. Alternative if f32_pcg's commit is more than a day away: launch on the commit holding rec_slabs alone and merge by hand afterwards (the shared files are the test entry tables, the test policy, the PCG policy and simple_reconstructor_pcg.f90, where the two diffs work in different routines).
  13. A Mac read precedes any commit, shorter than rec_slabs': the unit suite, the benchmark at 1, 2, 4, 10 and 20 threads at boxes 160 and 256 in c1 and d2, and reconstruct3D rec_backend=pcg on the simulated set, base against new. Kept if the bytes are identical in every row and the batch takes at most 0.50 (derived from the gridding entry's 0.18) of today's loop's time at ten threads, box 160, uniform, c1. The no-loss bound is reported there, not gated: the machine is shared with other runs.
  14. Premise check in Phase 0. A scratch build with timers splits rec3D accumulation into set-up, preparation of the plane values, scatter and write. If the time inside accumulate_batch is under 30 % (guessed) of the stage at sixteen threads on all three sets, the run stops with the profile. If today's loop scales well on dell2, the run goes on: the case is then the Mac's, as it was for the gridding entry.
  15. Real data, reported and not gated: the two real sets of rec_slabs decision 14 (streptavidin, d2, box 128; beta-galactosidase, d2, box 256) with rec_backend=pcg, and one refine3D refine=prob iteration with extra poses.

Defaults taken without a question: the note's and the run's names; the entry's name; the constants' names (PCG_ACC_*, private to the module); the tester's file name simple_reconstructor_pcg_tester.f90 and its suite under unit_reconstruction.

4. Not in this plan

A change of any byte. The solve, the preconditioner, the fold and the kernel path (f32_pcg's). The matrix-free apply and its colouring. Flex's PCG operator and the wave operators, which extend the same lattice: its existing routines are not edited. The strategy and its batch size. The gridding reconstructor. A retirement of today's loop. OMP_WAIT_POLICY or any runtime setting. The Dell while f32_pcg runs there.

5. Change types and gates

Phase Kind Gate
0 none builds, fast gate, the base's production cases twice, decision 14
1 tests-only fast gate and registry check; today's loop's table and spreads, frozen
2 identical, new code beside the old decision 8; Debug run; decision 9 (a), (b)
3 none (measurement) decision 9 (c), (d); decision 15; the suites of section 6
4 documents fast gate; the report

6. Acceptance

  • Bytes (decision 8): zero differing words of B and D in every tester case and benchmark row; the planted control differs; today's loop's bytes do not depend on the team; 20 repeats of the largest cell at 16 threads, bit for bit.
  • git diff shows no changed line inside accumulate_rhs_density and none in an existing routine of simple_pcg_lattice.f90 (contract 2).
  • Decision 9 (a) in every cell with the frozen bounds; 9 (c) on every case Phase 0 found reproducible.
  • build/bin/simple_test_exec test=unit_reconstruction (Release and Debug), test=pcg_recon, test=pcg_frac_update, test=lib_reconstruction, test=unit_heterogeneity (flex's operator on the same lattice); the fast gate. Base failures recorded in Phase 0 are left and named.
  • Reported, not gated: decisions 9 (b) and (d), 15, the retirement count of decision 1, the Mac table.

7. Common rules and stop conditions

The run builds in its own trees, commits nothing, edits only what the file table gives its phase and adds a row before editing a file the table lacks. No new parameter. No floor or tolerance moved. Experimental variants (decision 4's candidates, decision 6's constants, the timers of decision 14) are patched copies of a source tree under the run's scratch and never enter the diff. One heavy job at a time; the load is recorded with every timing and a timing under foreign load is retaken. A missed verdict of decision 9 (b), a fixture that takes the fallback and a failed real-data base run are results, not stops.

Stops, each with a report and a recommendation:

  • A word differs and two attempts find no cause in the walk's logic. If the cause is code generation, the allowed remedy is to write the walk's deposit and weight statements as today's routines write them; if identity then needs an edit of today's loop or a compile flag, stop.
  • Decision 14's premise fails.
  • Decision 9 (a) cannot be met at the largest team even with decisions 6 and 7 applied.
  • Decision 9 (c) fails, or the base reproduces itself in no production case.
  • The base lacks rec_slabs' benchmark file, or a fact of the code check no longer holds at the base.
  • A file outside the table, or a required argument of a public procedure, would have to change.

No ruling may waive: decision 8's byte identity; contract 2; decision 9 (c); the fast gate; the rule that no parameter is added.

8. Phases

Phase 0: the base

Record the base and re-check code checks 1 to 10 there. Build Release and Debug, run the fast gate on both, keep the base build. Run the entries of section 6 and record failures. reconstruct3D rec_backend=pcg on the simulated 6VXX project of rec_slabs and on the two real sets, at 1 and at 16 threads, each twice: which outputs reproduce, rec3D accumulation with its spread. Decision 14's profile.

Exit: the gate passes; Progress holds the base, the reproducible outputs, the stage's times and their split; decision 14's premise holds or the run has stopped.

Phase 1: the benchmark and today's loop's table

Add pcg_accum_bench with the plane arm and the byte comparison of today's loop across teams. Measure decision 10's table twice.

Exit: the entry runs from the installed build; the fast gate and its registry check pass; Progress holds today's loop's seconds and scaling per core set and the frozen bounds of decision 9 (a).

Phase 2: the batch walk

Write the walk (decisions 2, 3, 5), the tester submodule (decision 8) and the batch arm. Choose the layout by decision 4 in scratch builds. Judge decision 9 (a); build decisions 6 and 7 only by their rules. Judge 9 (b).

Exit: section 6's bytes in full; decision 9 (a) met in every cell or a stop; Progress holds the table per core set, the layout's protocol table, whether decisions 6 and 7 were built, and the verdict of 9 (b).

Phase 3: production evidence

No edit is expected. Decision 9 (c) and (d) on the cases of Phase 0, decision 15, a distributed reconstruct3D rec_backend=pcg over two parts, the suites of section 6, the chunk memory at box 256.

Exit: decision 9 (c) holds; Progress holds the production times base against new and the real sets' table.

Phase 4: documents and report

The accumulation's section of the PCG policy, a dated entry in the PCG decision log, the benchmark's row in the test policy, the README row, this note's status line. The report pcg_batch_accumulation_report.md by section 8 of doc/how2s/how2write_agent_plans.md: dell2's tables beside empty Apple-silicon columns, decision 13's commands, the losers with their mechanisms, proposed skill lines (not applied) and a commit message.

Exit: the fast gate passes on the final tree; no job of the run is left; the report exists.

File table

File What Phases
doc/implementation_notes/planned/pcg_batch_accumulation.md this note: Progress, rulings applied, the status line each
src/main/commanders/test/simple_commanders_test_highlevel_recbench.f90 the second benchmark entry and its arms 1, 2
src/main/commanders/test/simple_commanders_test_highlevel.f90 the entry's commander type and interface 1
src/main/exec/simple_test_exec_highlevel.f90 the entry's route 1
src/main/ui/simple_test/simple_test_ui_highlevel.f90 the entry's UI and help 1, 2
doc/policies/test_environment_policy.md section 1.3: the entry 1, 4
src/main/volume/simple_reconstructor_pcg.f90 the walk, its ranges and constants, the optional range count; a new tester submodule beside it 2, 3
src/main/volume/simple_pcg_lattice.f90 new routines only: deposits clipped to a range, the row interval for the second index 2, 3
src/main/commanders/test/simple_commanders_test_class.f90 the tester's suite row 2
src/main/ui/simple_test/simple_test_ui_class.f90 the suite's help 2
doc/policies/3D/reconstruct3D_pcg_policy.md the accumulation's threading 4
doc/implementation_notes/completed/pcg_decision_log.md the dated entry 4
doc/implementation_notes/README.md this note's row 4
doc/implementation_notes/planned/pcg_batch_accumulation_report.md new: the run's report 4

A listed x.f90 also covers x_tester.f90 (the fence's companion rule).

Progress

(one entry per phase, written by the run)