GPU Acceleration
SHTnsKit uses the array type to select NVIDIA CUDA or AMD ROCm execution. Transforms return arrays on the same device, so a pipeline can stay resident without repeated host transfers.
Install a backend
using Pkg
# NVIDIA
Pkg.add(["CUDA", "GPUArrays", "GPUArraysCore", "KernelAbstractions"])
# AMD
Pkg.add(["AMDGPU", "GPUArrays", "GPUArraysCore", "KernelAbstractions"])Load SHTnsKit, the vendor package, and the three shared GPU packages in the same Julia session.
Device-resident roundtrip
For NVIDIA:
using SHTnsKit, CUDA, GPUArrays, GPUArraysCore, KernelAbstractions
CUDA.functional() || error("CUDA is not functional")
cfg = create_gauss_config(64, 66)
field = CUDA.rand(Float64, cfg.nlat, cfg.nlon)
coefficients = analysis(cfg, field)
recovered = synthesis(cfg, coefficients)
@assert coefficients isa CUDA.AnyCuArray
@assert recovered isa CUDA.AnyCuArrayFor AMD, load AMDGPU instead and create the input with AMDGPU.ROCArray(rand(cfg.nlat, cfg.nlon)). Scalar, vector, QST, packed, and batch transform families follow the same storage-based dispatch.
Reuse a plan
Repeated transforms should reuse workspace and device outputs:
plan = SHTPlan(cfg; use_rfft=true)
coefficients = CUDA.zeros(ComplexF64, cfg.lmax + 1, cfg.mmax + 1)
recovered = CUDA.zeros(Float64, cfg.nlat, cfg.nlon)
analysis!(plan, coefficients, field)
synthesis!(plan, recovered, coefficients)Output arguments come before inputs. Plans own mutable workspace, so use one plan per concurrent task.
Explicit placement
Ordinary calls infer the backend from their arrays. Use analysis(CPU(), ...) or analysis(GPU(), ...) only when execution intent must be strict. GPU() raises BackendUnavailableError rather than silently moving the whole operation to the CPU.
to_device performs an intentional transfer and on_device reports placement. If both CUDA and AMDGPU are loaded, pass an existing device array as the prototype when a transfer needs to select one vendor.
Troubleshooting
- Check
CUDA.functional()orAMDGPU.functional()before allocating. - Keep inputs, outputs, scratch, and prototypes on one vendor.
- Load every package listed for the selected backend in Installation.
- Avoid host/device copies inside a time-stepping loop; transfer once, compute, then copy back only the final result.
For multi-rank arrays, first establish the CPU workflow in Distributed Computing, then use device-backed PencilArray storage with a GPU-aware MPI stack.