PerturbedEquilibrium & KineticForces: parallelize serial workflow loops (coupling, NTV torque) + reduce allocations#325
Open
logan-nc wants to merge 3 commits into
Open
Conversation
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-batchnode counts make threading the ψ loop fork-join-bound — but the inner bounce-harmonic loop
(
2·nl+1harmonics per ψ node) carries the ~40 ms of per-node work and is embarrassinglyparallel. Threaded it: each harmonic's
tpsi!writes per-intrscratch buffers/hints, soeach thread gets its own
deepcopy(intr)(the pattern the self-consistent matrix build uses),and per-harmonic results are reduced in fixed harmonic order.
82.1808181112855 − 14.6098675986818ito every digit across the serialbaseline, 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 :staticover resonant pairs (BLAS pinned to 1 across it).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,@vieweigenmode slices,cholesky!, BLASpin. ~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.