Skip to content

PerturbedEquilibrium & KineticForces: parallelize serial workflow loops (coupling, NTV torque) + reduce allocations#325

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

PerturbedEquilibrium & KineticForces: parallelize serial workflow loops (coupling, NTV torque) + reduce allocations#325
logan-nc wants to merge 3 commits into
developfrom
perf/perturbed-equilibrium-threading

Conversation

@logan-nc

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

Copy link
Copy Markdown
Collaborator

PerturbedEquilibrium & KineticForces: parallelize serial workflow loops (+ allocation cleanup)

Three performance improvements to the perturbed-equilibrium / kinetic-diagnostic workflow on
the DIII-D-like reference case. Each is verified to leave results unchanged (bit-identical or
within regression tolerance) and independent of thread count.

1. Parallelize the NTV-torque bounce-harmonic loop (KineticForces)

The NTV-torque outer ψ-integral (QuadGK BatchIntegrand) stays serial — its small per-batch
node counts make threading the ψ loop fork-join-bound — but the inner bounce-harmonic loop
(2·nl+1 harmonics per ψ node) carries the ~40 ms of per-node work and is embarrassingly
parallel. Threaded it: each harmonic's tpsi! writes per-intr scratch buffers/hints, so
each thread gets its own deepcopy(intr) (the pattern the self-consistent matrix build uses),
and per-harmonic results are reduced in fixed harmonic order.

  • fgar NTV torque: 35.3 s → 8.2 s at 8 threads (4.3×).
  • Bit-identical: 82.1808181112855 − 14.6098675986818i to every digit across the serial
    baseline, threaded t=1, and threaded t=8.

2. Parallelize the singular-coupling loop over rational surfaces (PerturbedEquilibrium)

Each rational surface builds its own vacuum Green's functions (allocation-local
compute_vacuum_response) and writes disjoint coupling rows / sing[s], so the loop is now
@threads :static over resonant pairs (BLAS pinned to 1 across it).

  • Singular coupling: ~0.54 s → ~0.18 s at 8 threads (~3×); results identical across thread
    counts and equal to serial to ~13–15 significant figures (BLAS-pin reordering).

3. Reduce allocations in field reconstruction (PerturbedEquilibrium)

reconstruct_physical_fields: in-place mode DFTs, @view eigenmode slices, cholesky!, BLAS
pin. ~30% fewer allocations (594 → 420 MB), bit-identical. (A memory cleanup — reconstruction
is bandwidth-bound, not GC-bound.)

Verification

Regression fingerprints of every affected output (b_n/xi_n, C_resonant_*, resonant_*,
and the NTV torque) match the serial baseline and are thread-count-independent; the repo's
coordinate-invariance, coils, full-run, and HDF5-replay suites pass.

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
The NTV-torque outer psi-integral (QuadGK BatchIntegrand) stays serial -- its small
per-batch node counts make threading the psi loop fork-join-bound -- but the inner
bounce-harmonic loop (2*nl+1 harmonics per psi node) carries the ~40 ms of per-node
work and is embarrassingly parallel. Thread it: each harmonic's tpsi! writes per-`intr`
scratch buffers and spline hints, so each thread gets its own deepcopy(intr) (the same
per-thread-scratch pattern the self-consistent kinetic matrix build already uses), and
per-harmonic results are stored by index and reduced in fixed harmonic order.

Effect on the DIII-D reference case (fgar NTV torque): 35.3 s -> 8.2 s at 8 threads
(4.3x). The torque is bit-identical -- 82.1808181112855 - 14.6098675986818i to every
digit across the serial baseline, threaded t=1, and threaded t=8 -- because the fixed-
order reduction reproduces the serial sum and the per-thread scratch removes the data
race, so the result is independent of thread count.
@logan-nc logan-nc changed the title PerturbedEquilibrium: reduce allocations and parallelize the singular-coupling loop PerturbedEquilibrium & KineticForces: parallelize serial workflow loops (coupling, NTV torque) + reduce allocations 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