Skip to content

GPU ​

With KernelAbstractions.jl and a GPU package loaded, the same functions accept GPU arrays. The example needs a GPU, so it is shown but not run during the documentation build. Some GPU packages, Metal.jl among them, export AbstractFFTs's fft, plan_fft, …, which are different functions from FFTL's. With such a package loaded, an unqualified fft is ambiguous, so the example writes FFTL.fft.

julia
using FFTL, Metal, KernelAbstractions, LinearAlgebra  # or CUDA, AMDGPU, oneAPI

xd = MtlArray(randn(ComplexF32, 1024, 1024))
yd = FFTL.fft(xd)                      # a new GPU array
p = FFTL.plan_fft(xd, 2)               # plan along dimension 2
yd2 = similar(xd)
mul!(yd2, p, xd)                       # out of place: yd2 !== xd

Rd = FFTL.rfft(MtlArray(randn(Float32, 1024, 1024)))

What to expect ​

  • Out of place. GPU transforms write into a new array. mul!(y, p, x) needs y !== x.

  • Asynchronous. Kernels are queued on the device without waiting. Copying a result to the host waits for them. For timing, call KernelAbstractions.synchronize(backend) before reading the clock.

  • Threads are ignored. The device runs the parallelism.

  • Plans cost memory. A plan holds one or two buffers of the array's size on the device (two where the intermediate results use the padded layout, on Apple GPUs from half the system level cache on). Plans made with buffers = false hold none, see below.

  • Plans compile. Making a plan for a new size generates and compiles its kernels. Make plans once and keep them, as on the CPU.

  • Element types. Float32 and Float64 where the device supports Float64. Complex transforms are the tuned path.

What is tuned ​

Complex transforms use multi-stage kernels. Each kernel computes one pass of a large radix (up to 4096 points) in workgroup memory, and a transform is one to five passes per dimension. Pass and workgroup sizes are tuned on an Apple M1 Max: there, Float32 transforms were faster than Metal.jl's FFT in nearly all measured cases. The CUDA path is tuned and measured on an NVIDIA RTX A6000 against cuFFT and VkFFT (Benchmarks).

Real transforms (rfft, irfft, brfft) compute the half spectrum through a complex transform of half the length, with the pre- and post-processing inside its first and last pass. Odd lengths put two real lines into one complex line, a complex DFT of length per pair.

The real-to-real kinds are reduced to complex DFTs: of half the length for DCT-II to DCT-IV and DST-II to DST-IV of even length, of length   for DCT-I and   for DST-I, mostly with the extra steps inside the passes. On CUDA, a DCT-I whose length   has a prime factor above 31 takes a four-step DFT instead of Bluestein's algorithm (the pass radices stop at 31). DCT-II and DST-II of odd length take one complex DFT of length per two lines; odd lengths of the other kinds take a complex DFT of length , the Hartley and halfcomplex kinds one of length .

Lengths with a prime factor above 31: a prime whose   has prime factors up to 13 runs Rader's algorithm in one kernel per dimension, other lengths Bluestein's algorithm (one kernel when the convolution fits one pass). Odd real transforms of such lengths do the packing of the real lines inside that kernel. DCT-II and DST-II are the most tuned of them; for the other kinds the speed relative to VkFFT depends on the kind and the size, from faster to several times slower.

Other GPUs run through the same code. They are not tuned, and AMD and Intel GPUs have not been tested.

Sharing one scratch array between plans ​

With buffers = false a plan allocates no device memory for intermediate results. Each call passes a scratch array instead, and plans that never run at the same time can share it:

julia
pf = FFTL.plan_fft(xd; buffers = false)
pb = FFTL.plan_bfft(xd; buffers = false)
scratch = similar(xd, max(FFTL.scratch_size(pf), FFTL.scratch_size(pb)))
mul!(yd, pf, xd, scratch)
mul!(xd, pb, yd, scratch)

The scratch array is any dense device array of element type Complex{T} with at least FFTL.scratch_size(p) elements, also a larger one that holds other data between the calls. r2c and c2r plans (plan_rfft, plan_brfft, plan_ka_rfft, plan_ka_brfft) take the same keyword and a complex scratch array.

Transforming selected lines ​

Experimental

The lines keyword may still change, and the CPU plans do not take it yet.

lines restricts a plan along one dimension to the lines whose indices in the other dimensions lie in given ranges. Each other dimension takes Colon(), a range, or a pair of ranges for a periodic wrap. A pruned three-dimensional transform, for example of an oversampled grid whose nonzero values lie in a box of half the size around the corners, transforms one quarter of the lines along the first dimension, half along the second and all along the third:

julia
n = 256; k = 128; w = (1:k ÷ 2, n - k ÷ 2 + 1:n)
p1 = FFTL.plan_fft(xd, 1; lines = (w, w))   # xd of size (n, n, n)
p2 = FFTL.plan_fft(xd, 2; lines = (:, w))
p3 = FFTL.plan_fft(xd, 3)
mul!(t1, p1, xd); mul!(t2, p2, t1); mul!(yd, p3, t2)

The values of the output outside the selected lines stay as they were. The passes run on the selected lines only, at the speed of a contiguous array of their size. lines works with complex plans along one dimension.

Making plans without a GPU array ​

FFTL.plan_ka(backend, T, dims, σ; region) makes a plan for any KernelAbstractions backend, including the CPU backend, which is useful for testing GPU code without a GPU. plan_ka_rfft, plan_ka_brfft and plan_ka_r2r do the same for the real transforms.