Plan-owned per-thread workers, direct strided pencil reads, threading across pencils - #137
Open
pankgeorg wants to merge 9 commits into
Open
Plan-owned per-thread workers, direct strided pencil reads, threading across pencils#137pankgeorg wants to merge 9 commits into
pankgeorg wants to merge 9 commits into
Conversation
With FFTW.jl loaded alongside FFTA, plan_rfft(::Vector{Float64}, ::Int) was
ambiguous between FFTW's StridedArray method and FFTA's method annotated
with region::RegionTypes, turning rfft(x) into a MethodError. Leave region
unannotated on the AbstractFFTs entry points (as plan_fft already does) and
normalise it in an internal function, so FFTW's methods are strictly more
specific and take over as AbstractFFTs intends.
A coexistence test runs in a subprocess (loading FFTW in the test process
would make every other test exercise FFTW).
Real-input/real-output plans only implemented *, so every rfft/irfft allocated, mul!(y, p, x) with a preallocated output was a MethodError, rfft along one dimension of an N-d array went through mapslices, and 2D real plans ran a full complex transform and discarded half of it. FFTAPlan_re now carries a scratch buffer and two pencil kernels (_rfft_pencil!/_brfft_pencil!) implement the even-length half-size trick and the odd-length transform on AbstractVectors, so views work as input and output. mul! is defined for 1D plans on 1D and N-d arrays (looping over pencils along the region) and for 2D plans on N-d arrays (real transform along the first region dimension, complex along the second), and * allocates the output and calls mul!. The 2D plan's first call graph is built for the half length like the 1D plan's.
inv(p) threw a TypeError for every FFTA plan: no plan_inv method existed
and the dummy pinv::FFTAInvPlan field made AbstractFFTs' pinv_type
resolve to Union{}. The plan structs are now mutable with an initially
undefined pinv field, as in FFTW.jl, so AbstractFFTs.inv caches the
result of the new plan_inv methods for complex and real plans; p \ x,
ldiv!(y, p, x), plan_ifft and plan_irfft work through them.
plan_fft!/plan_bfft! return an FFTAPlan_inplace wrapping an ordinary plan
plus a buffer: when input and output alias, the input is copied to the
buffer and transformed out of place (FFTA's kernels are out of place, and
the 1D pencil path is not alias-safe); otherwise the wrapped plan is used
directly. fft!, bfft! and ifft! from AbstractFFTs now work — the internal
kernel that shadowed AbstractFFTs.fft! is renamed fft_kernel!.
…llow AbstractFFTs backend hooks in the ExplicitImports check
…e the other AbstractFFTs entry points
… across pencils Multidimensional mul! allocated two pencil buffers per call and copied every pencil in and out of them; a 1D plan applied along one dimension of an array transformed its pencils one after the other; and the single call-graph workspace of a plan made it impossible to use threads. A plan now owns one Worker per thread it may use (num_threads keyword of the plan_* functions, default Threads.nthreads(), kept by inv): call graphs sharing the plan's nodes but with their own workspace, plus the pencil buffers. One driver, _foreach_pencil, runs every pencil loop (1D plans on N-d arrays, N-d plans, real pencil kernels): serially and allocation-free below THREAD_THRESHOLD or with one worker, otherwise as one Threads.@Spawn task per worker over a contiguous chunk of pencils. Each chunk uses its own worker, so results are bit-identical whatever the thread count. Kernels read the strided pencil views directly; only the copy-out from the worker's output buffer remains.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #137 +/- ##
==========================================
- Coverage 98.80% 97.85% -0.96%
==========================================
Files 5 4 -1
Lines 585 652 +67
==========================================
+ Hits 578 638 +60
- Misses 7 14 +7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…nels Replacing mapslices with strided views made real transforms along dims=2 of wide matrices (64 x N) 1.2-1.3x slower on x86-64: the mapslices copy had been an unlabelled copy-in that turned a stride-of-a-cache-line gather into one contiguous pass before the kernel. Pencils whose parent arrays are unit-stride along the transform dimension still go to the kernels directly (the dims=1 gain stays); any other pencil is copied to two plan-owned contiguous buffers first and copied back after, so execution stays allocation-free and the dims=2 result is now identical to the mapslices one.
…d-buffers-threads The copy buffers live in each Worker so that pencils transformed on different tasks never share them.
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.
Item D of the plan in #130, stacked on #133 (the diff includes #132 and #133 until they land).
Before, every multidimensional
mul!allocated two pencil buffers per call, copied each pencil into a contiguous buffer, transformed, and copied it back; a 1D plan applied along one dimension of a matrix transformed the pencils one after the other; and a plan's single call-graph workspace made it impossible to spread the work over threads.Now
Workerper thread it may use: call graphs that share the plan's nodes and twiddle tables but have their own workspace and Bluestein work arrays, plus the pencil buffers. The count is the newnum_threadskeyword ofplan_fft/plan_rfft/… (same name as FFTW.jl's; defaultThreads.nthreads();inv(p)keeps it)._foreach_pencil, runs every pencil loop: 1D plans on N-d arrays, N-d plans, and the real-input pencil kernels. BelowTHREAD_THRESHOLD(2^15 elements) or with one worker it runs serially and allocation-free; above it, pencils are split into one contiguous chunk per worker and the chunks run asThreads.@spawntasks. Each chunk always uses its own worker, so results are bit-identical to the single-worker result and do not depend on the number of threads (tested with 1, 2, 3 and 5 workers on a 1-thread runtime and on 4 threads).rfft/brfftalongdimspath use the same machinery, so batched real transforms — DSP.jl's Welch/periodogram/fftfiltshape — are threaded too.Numbers (aarch64 Neoverse-N1, Julia 1.12.6,
ComplexF64unless noted, planned execution; 1 vs 4 workers onjulia -t 4):Single worker vs main (nd/batched sections, FFTA 1 worker; the change here is the buffer/copy handling only):
Threading (8 threads,
julia -t 8; FFTA planned withnum_threads=8, FFTW with 8 threads):With 8 workers FFTA's speedup over its single-worker time (5.6–7.7×) matches FFTW's own 8-thread speedup (5.9–6.8×), so the FFTA/FFTW ratio in the threaded case is back to the single-threaded 1.7–3.3× instead of the 7–20× measured before this PR. The single-worker rows are the reference: no change is expected for complex transforms (the buffer copies were a small fraction), and none is seen beyond noise; the real-transform rows carry #132's gains. Bluestein sizes still allocate in this branch (that is #134's job).
x86-64 note. An independent run of the fully merged branch on an AVX2 machine showed wide
64×Nrfftalongdims=2slower thanmain; a five-branch probe there attributed it to #132's strided real pencil path, not to this PR (this PR'sdims=1control was neutral, 1.03–1.04×, and D/B was 0.95–1.04× on the affected rows), and #132 now copies strided pencils to contiguous buffers. With that fix in place the merged stack was re-verified on x86-64: the strided rows are 1.34–1.66× faster thanmain, the contiguous rows 3.8–3.9× faster, and the results at 8 threads are bit-for-bit identical to the single-threaded ones (no shared buffers) at zero allocation.Not in this PR: threading within a single 1D transform (FFTW does this above ~2^16 elements); that needs a breadth-first pass structure and is the remaining item where FFTW's threading is ahead.