Skip to content

PerturbedEquilibrium: reduce allocations and parallelize the singular-coupling loop#325

Open
logan-nc wants to merge 2 commits into
developfrom
perf/perturbed-equilibrium-threading
Open

PerturbedEquilibrium: reduce allocations and parallelize the singular-coupling loop#325
logan-nc wants to merge 2 commits into
developfrom
perf/perturbed-equilibrium-threading

Conversation

@logan-nc

@logan-nc logan-nc commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

PerturbedEquilibrium: reduce allocations and parallelize the singular-coupling loop

Two independent PerturbedEquilibrium performance improvements on the DIII-D-like reference
case, both verified to leave results unchanged (bit-identical / within regression tolerance,
and thread-count-independent).

1. Parallelize the singular-coupling loop over rational surfaces (the real speedup)

The singular-coupling metrics loop (SingularCoupling.jl) was serial, and it was the
largest PE phase: each rational surface builds its own vacuum Green's functions (an
allocation-local compute_vacuum_response). The surfaces are independent -- each writes
disjoint coupling-matrix rows and its own sing[s] entry -- so the loop is now threaded
(@threads :static over resonant pairs), with BLAS pinned to one thread across it (each
surface's solve is small; multi-threaded BLAS would oversubscribe the cores against the
Julia threads).

  • Singular coupling: ~0.54 s -> ~0.18 s at 8 threads (~3x), cutting PE wall time from
    ~0.94 s to ~0.59 s. (Field reconstruction, which is memory-bandwidth-bound, is now the
    dominant PE phase.)
  • Results identical across thread counts (deterministic, no data race) and equal to the
    serial result to ~13-15 significant figures (floating-point reordering from the BLAS pin).

2. Reduce allocations in field reconstruction (a memory cleanup)

reconstruct_physical_fields allocated heavily per flux surface and ran multi-threaded BLAS
inside its @threads loops. Trimmed with bit-identical results:

  • R/Z/phi mode DFTs run in place (inverse_transform!/transform! into per-thread buffers)
    instead of the allocating inverse()/ft() functors.
  • @view the mpert x mpert eigenmode-matrix slices in sum_eigenmode_contributions.
  • cholesky! in place on the per-thread scratch matrix in the clebsch solve.
  • Pin BLAS to one thread across reconstruct_physical_fields.

Effect: PE allocations drop ~30% (594 -> 420 MB), GC time falls, outputs bit-identical.
This is a memory cleanup, not a wall-time win on this case -- reconstruction is
bandwidth-bound, not GC-bound.

Verification

  • Regression fingerprint of every PE physics output (b_n/xi_n spectra, C_resonant_*
    coupling matrices, resonant_* vectors): identical across thread counts and matching the
    serial baseline to all-but-the-last-digit.
  • Repo suites pass: coordinate-invariance, coils, full-run, HDF5-replay (and Fourier
    transforms).

logan-nc added 2 commits July 9, 2026 19:41
The per-surface field reconstruction allocated heavily on every surface and ran
multi-threaded BLAS inside its Julia @threads loops. Cut the allocation and remove
the oversubscription, with bit-identical results:

- Run the R,Z,phi mode DFTs in place (inverse_transform!/transform! into per-thread
  buffers) instead of the allocating inverse()/ft() functors.
- @view the mpert x mpert eigenmode-matrix slices in sum_eigenmode_contributions
  (mul! takes the view; no per-surface copy).
- cholesky! in place on the per-thread scratch matrix in the clebsch solve.
- Pin BLAS to one thread across reconstruct_physical_fields (save/restore): the
  per-surface BLAS kernels are small, so multi-threaded BLAS oversubscribes the
  cores against the Julia @threads over psi.

Effect on the DIII-D reference case: PerturbedEquilibrium allocations drop ~30%
(594 -> 420 MB), GC time falls, and the b_n / xi_n spectra, resonant coupling
matrices, and resonant vectors are bit-identical before and after and independent
of thread count. This is a memory/allocation cleanup: it does NOT materially change
PE wall time, which on this case is dominated by the (serial) singular-coupling
metrics and the response-matrix linear algebra rather than the threaded
reconstruction.
…ional surfaces

The singular-coupling metrics loop over rational surfaces was serial, and it is the
largest PerturbedEquilibrium phase because each surface builds its own vacuum Green's
functions (an allocation-local `compute_vacuum_response`). The surfaces are independent --
each writes disjoint coupling-matrix rows and its own `sing[s]` entry -- so thread the loop
(`@threads :static` over resonant pairs), with BLAS pinned to one thread across it (each
surface's solve is small; multi-threaded BLAS would oversubscribe against the Julia threads).

Effect on the DIII-D reference case: the singular-coupling phase drops from ~0.54 s to
~0.18 s at 8 threads (~3x), cutting the PerturbedEquilibrium wall time from ~0.94 s to
~0.59 s. Results are unchanged: the resonant coupling matrices and applied resonant vectors
are identical across thread counts (deterministic, no data race) and agree with the serial
result to ~13-15 significant figures (floating-point reordering from the BLAS pinning);
the coordinate-invariance, coils, full-run, and HDF5-replay regression suites pass.
@logan-nc logan-nc changed the title PerturbedEquilibrium: reduce allocations in field reconstruction PerturbedEquilibrium: reduce allocations and parallelize the singular-coupling loop Jul 11, 2026
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.

1 participant