PerturbedEquilibrium: reduce allocations and parallelize the singular-coupling loop#325
Open
logan-nc wants to merge 2 commits into
Open
PerturbedEquilibrium: reduce allocations and parallelize the singular-coupling loop#325logan-nc wants to merge 2 commits into
logan-nc wants to merge 2 commits into
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.
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: 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 thelargest PE phase: each rational surface builds its own vacuum Green's functions (an
allocation-local
compute_vacuum_response). The surfaces are independent -- each writesdisjoint coupling-matrix rows and its own
sing[s]entry -- so the loop is now threaded(
@threads :staticover resonant pairs), with BLAS pinned to one thread across it (eachsurface's solve is small; multi-threaded BLAS would oversubscribe the cores against the
Julia threads).
~0.94 s to ~0.59 s. (Field reconstruction, which is memory-bandwidth-bound, is now the
dominant PE phase.)
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_fieldsallocated heavily per flux surface and ran multi-threaded BLASinside its
@threadsloops. Trimmed with bit-identical results:inverse_transform!/transform!into per-thread buffers)instead of the allocating
inverse()/ft()functors.@viewthempert x mperteigenmode-matrix slices insum_eigenmode_contributions.cholesky!in place on the per-thread scratch matrix in the clebsch solve.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
b_n/xi_nspectra,C_resonant_*coupling matrices,
resonant_*vectors): identical across thread counts and matching theserial baseline to all-but-the-last-digit.
transforms).