cuFFT
cuFFT.jl offers NVIDIA's fast Fourier transforms at two levels:
- The functions of AbstractFFTs.jl, which cuFFT.jl re-exports, work on
CuArrays:fft,ifft,bfft,rfft,irfft,brfft, the in-placefft!,ifft!andbfft!, and the planning functionsplan_fftetc. that return a reusableCuFFTPlan, or anAbstractFFTs.ScaledPlanwrapping one. cuFFT transformsFloat16,Float32andFloat64data and their complex versions; integer and rational inputs are converted to floating point first. Strided views are supported, and are copied when cuFFT cannot address them directly. - The generated bindings in
libcufft.jlfollow NVIDIA's C API, and change with the CUDA toolkit. ACuFFTPlanconverts to thecufftHandlethey take.
using CUDA, cuFFTx = CUDA.rand(ComplexF32, 1024, 64)p = plan_fft(x, 1) # transform the first dimension, batched over the secondy = p * xp \ y ≈ x # trueFusing broadcasts into transforms
Element-wise work around a transform, such as windowing, converting or scaling, normally runs as separate kernels that each pass over the data. With the @~ macro of LazyArrays.jl, cuFFT executes it as part of the transform instead:
using CUDA, cuFFT, LazyArraysp = plan_fft(x, 1)y = abs2.(@~ p * (w .* x)) # window, transform and take the power in one goy .= (@~ p * (w .* x)) ./ n # store into an existing arrayacc .+= abs2.(@~ p * x) # accumulate; the destination can be part of the broadcast@~ p * arg is a lazy p * arg, where arg is an array or a broadcast. cuFFT fuses that broadcast, and the broadcast the result is used in, into the transform. The result is the same as without @~, up to rounding. The input can have any element type that converts to the plan's, e.g. integer samples, and a plan that normalizes (plan_ifft, inv(p)) does so as part of the transform.
@~ makes every call in its expression lazy, so give it variables: @~ p * x rather than @~ plan_fft(x) * x.
Fusion needs CUDA 13.5 or newer and a GPU with compute capability 10.0 (Blackwell) or newer, and applies to out-of-place Float32 and Float64 transforms. In all other cases the same code still works: cuFFT then executes p * arg and the broadcast around it separately. That also happens when:
- the transform has a size with a prime factor of 127 or more, a layout that cuFFT has to permute, or is one of the few for which cuFFT doesn't support fusion;
- the broadcast may throw an exception, e.g. because it converts to an integer;
- the broadcast contains several transforms, or changes the shape of the output;
- the destination is not a dense
CuArray, or shares memory with an input; - the code is captured into a graph.
Complex-to-real transforms only fuse the broadcast around them. To find out why a broadcast is not fused, enable debug logging with JULIA_DEBUG=cuFFT.
Fused broadcasts are compiled and linked into cuFFT's kernels the first time they are used, which takes longer than compiling an ordinary broadcast.
Public
cuFFT.CUFFTError — Type
CUFFTError(code::cufftResult)The exception thrown when a cuFFT call fails. code is the status that cuFFT returned.
cuFFT.CuFFTPlan — Type
CuFFTPlan{T,S,K,inplace,N,R} <: AbstractFFTs.Plan{S}A cuFFT plan, as returned by the AbstractFFTs planning functions (plan_fft, plan_rfft, plan_bfft!, ...) for CuArrays. Apply it with p * x or mul!(y, p, x), and invert it with inv(p) or p \ x; plan_ifft and inv(p) return an AbstractFFTs.ScaledPlan that wraps a CuFFTPlan. Plans are not constructed directly.
The type parameters describe the transform, so that methods can dispatch on them:
T: element type of the output;S: element type of the input. Real-to-complex transforms have a realSand a complexT, complex-to-real ones the reverse;K: the direction,cuFFT.CUFFT_FORWARD(-1) orcuFFT.CUFFT_INVERSE(+1). Real-to-complex transforms are forward, complex-to-real ones inverse;inplace: whether the plan overwrites its input (true) or writes a separate output (false);N: the number of dimensions of the arrays;R: the number of dimensions that are transformed.
A plan can be applied to arrays of its input size and element type, including strided views, from several tasks at once, each using its own stream. It has to be used in the CUDA context it was created in. Its fields are not part of the API. The underlying cuFFT handle is returned to a cache when the plan is garbage collected, or earlier with CUDA.unsafe_free!(p), after which the plan cannot be used any more.
cuFFT.functional — Method
cuFFT.functional() -> BoolWhether cuFFT was loaded and initialized successfully.
cuFFT.version — Method
cuFFT.version() -> VersionNumberThe version of the cuFFT library in use. This is the library's own version, which differs from that of the CUDA toolkit it ships with.
Private
cuFFT.callback_issue — Method
callback_issue(p::CuFFTPlan) -> Union{Nothing,String}Why the transform p cannot be executed with callbacks on the current device and stream, or nothing if it can.
cuFFT.compiled_callback — Method
compiled_callback(W) -> Union{Nothing,CallbackResults}Compile the callback wrapper W in the current world, or return earlier results that are still valid. The results object identifies the compiled code: a different one is returned after redefining a method the callback uses. Returns nothing for callbacks that cannot be compiled, e.g. because they may throw.
cuFFT.execute_fused! — Method
execute_fused!(p::CuFFTPlan, input, output; load=nothing, store=nothing)Enqueue the out-of-place transform p with callbacks: load(x, i) provides input element i instead of input, and store(y, i, v) consumes output element v at index i. input can be nothing when there is a load callback, which cuFFT then calls instead of reading its input. output always has to be an array of the plan's output type and size, even with a store callback that writes elsewhere: cuFFT may use it as scratch space, so its contents are undefined afterwards unless the store callback writes them.
Returns nothing, or the reason why nothing was executed: a callback that does not compile (e.g., as it may throw) or is not a bits value after cudaconvert, or a transform for which cuFFT doesn't call callbacks. The caller has to check callback_issue first.