Threads
Plans take a threads keyword, which defaults to Threads.nthreads(). Start Julia with the threads you want (julia -t 8 or julia -t auto); FFTL does not start threads itself.
using FFTL, LinearAlgebra, Random
Random.seed!(6)
x = randn(ComplexF64, 512, 512)
p8 = plan_fft(x; threads = 8)
y8 = p8 * x
y8 ≈ fft(x)trueThe threaded result agrees with the single-threaded one up to rounding. Long one-dimensional transforms use a different algorithm on several threads (four-step), so their last bits can differ. With 64-byte vectors (AVX-512), very long lines (16 times the last-level cache of a core and more) take the four-step on one thread as well.
When threads are used
Threads only help when there is enough work to split. FFTL decides per dimension, from the number of values the transform touches:
A transform with fewer than
values in total runs on one thread, whatever threadssays. Splitting it would cost more than it saves.A batch of lines along a dimension is split over the threads as blocks of lines, each block sized to fit the cache of one core.
A single long one-dimensional transform of smooth length uses the four-step algorithm when it is large enough for the threads it has: the line is written as an
matrix, transformed in both directions with threads, and the twiddle factors in between. On aarch64 this holds from 128 KiB of data (values in Float32,in Float64) with two threads or more; on x86 fromvalues with six threads or more. Below that the line runs on one thread, which is faster there (see Design decisions). A long line whose length has a large prime factor (at least
) runs on threads as well. When the smooth part of the length is split off ( , see Algorithms) andis at least the number of threads, the resampled transforms of length are spread over the threads in chunks of lanes; the result is bitwise the same as on one thread. Otherwise one resampled transform of the whole length runs with its grid transform as a four-step transform on the threads, and its interpolation split over them. With the preference float64_large_primes = "bluestein"(see Accuracy),Float64takes Bluestein's algorithm in both cases, its two convolution transforms as four-step transforms on the threads.
A 1D transform on a machine where some cores are busy is held up by the slowest piece, so fewer threads than cores can be the faster choice there.
Nested threading
Code that already threads over its own work should not also ask FFTL for threads. The oversubscription is worse than the parallelism it adds:
chunks = Iterators.partition(eachindex(images), cld(length(images), Threads.nthreads()))
@sync for c in chunks
Threads.@spawn begin
p = plan_fft(images[first(c)]; threads = 1)
for i in c
mul!(out[i], p, images[i])
end
end
endEach task makes its own plan with threads = 1; see Plans and reuse.
Plans are not shared between tasks
A plan owns its work buffers and its thread buffers. Two tasks applying one plan at the same time corrupt each other's results. Make one plan per task (see Plans and reuse).
Apple silicon
With -t auto, Julia starts one thread per core, efficiency cores included. A thread on an efficiency core can hold up a threaded pass, so on Apple silicon start Julia with the number of performance cores (julia -t 8 on an M1 Max). Measured on the M1 Max, the 64³ transform with 8 threads was slower when the efficiency cores took part. The libfftl build uses the performance cores for the same reason (see libfftl).