GPU support
What runs on a GPU, on which backend, at what precision — the fGN and sheet pipelines, the Euler engine, and the configurations that stay on the host.
Every process carries its sampling backend as a type parameter and is re-typed with .on::<B>() (see backends). This page is the inventory: which processes run on which device, at what precision, and which configurations stay on the host.
The support matrix
| Family | Processes | Capability bound | Cpu | Accelerate | Metal | Cuda |
|---|---|---|---|---|---|---|
| fGN noise | Fgn (1) | FgnBackend<T> | SIMD + ndrustfft | vDSP FFT | metal-oxide FFT pipeline, f32 only | cuFFT + Philox, f32 / f64 |
| Sheet | Fbs (1) | SheetBackend<T> | SIMD + ndrustfft | same as Cpu | metal-oxide sheet pipeline, f32 only | cuFFT + Philox, f32 / f64 |
| Euler engine, one component | 89 processes | EulerBackend<T> | the process's own SIMD sampler | same as Cpu | Euler–Maruyama kernel compiled by metal-oxide, f32 only | NVRTC kernel, f32 / f64 |
| Euler engine, systems | 39 processes | EulerBackend<T> | the process's own SIMD sampler | same as Cpu | the same kernel, up to four components | the same kernel |
| Euler engine, fractional | Fbm, Fou, Fcir, Fgbm, Fjacobi, Fvasicek, Cfgns, Cfbms, Cfou, JumpFou, JumpFouCustom (11, counted above) | EulerBackend<T> | the process's own SIMD sampler | same as Cpu | fGN pipeline feeds the same Metal kernel | fGN pipeline feeds the same NVRTC kernel |
Metal needs Apple Silicon with macOS 15 or newer, since its kernels ship as a metallib built for macOS 15; on an earlier macOS the device answers DeviceError::Unavailable, and Cpu runs every process there.
Every process reaches a device: Fgn and Fbs through pipelines of their own, the other 128 through the Euler engine. The engine covers the diffusions; the stochastic-volatility models under both their Euler and quadratic-exponential schemes; the short-rate models, including the curve-driven ones; the fractional processes; the jump models (a process generic over its jump-size law when that law is SimdNormal or SimdExp, and Bates under its LogNormalJump too); the Lévy processes and subordinators; the order-(1,1) conditional-variance and first-order lag models; the noise generators; the Riemann–Liouville and Volterra models under the Markov lift of their kernel; the rough-volatility models with a memory integral, Lfsm, Arima and Sarima on the host's exact scheme for grids up to 512 points; the point processes in count mode; and the matrix and basket models within the limits below.
What stays on the host
The engine steps a bounded forward recursion over four scalar state slots and four shocks. These configurations exceed it and sample on the host, bit-identically to the Cpu build, whatever the backend; device_fallback() names the reason:
- History models on a long grid.
Fheston,FbatesSvj,RoughBergomi,Lfsm,Arima,SarimaandVolterra's reference-convolution branch read their whole past each step, which the kernels carry for grids up to 512 points. - Coefficients of the state written as a Rust closure, a Python callable or a
Grid2D(Cheyette'ssigma(t, x),VolterraSde's drift and diffusion,HestonSlv's leverage). Written as anExpr, the same coefficient runs on the device — see below. A coefficient of time alone, such as Hull–White'stheta(t), is tabulated on the grid and runs on the device, whether it is a Rust function or a Python callable. - Too many coupled dimensions.
Wishartaboved = 2or below degree 3;Lmm,MultiGbmandMcgnsabove four components;MultifactorHestonwith three or more factors;RegimeSwitchingDiffusionwith five or more regimes;MultivariateHawkesabove two components or with per-pair decays. - Point processes on their own clock.
Hawkes,MultivariateHawkes,Poisson,CompoundPoisson,CustomJtandCompoundCustomin horizon mode, where the output length is the number of events;CustomJt,CompoundCustomandJumpFouCustomwith an inter-arrival law other than the exponential;Ctrwwith gamma, inverse-Gaussian or stable waits. - Jump-size laws other than
SimdNormal/SimdExpfor the processes generic over one (Merton,Kou,LevyDiffusion,Bates,JumpFou,JumpFouCustom);Batesalso draws itsLogNormalJumpon a device. - Dense jump counts. A process whose kernel reads the step's Poisson count, past
λ·dt = 25expected events a step, where the kernels' 64-event count would cut the tail. - Lag order above one for
Ar,Maand the GARCH family. Svcgmywith4κη/ζ² < 1, where the exact variance step needs a Poisson mixture.InverseAlphaStableSubordinatorwith more than 512u_steps.Fbswhenm − 1orn − 1is not a power of two.
Coefficients as expressions
Cheyette, VolterraSde and HestonSlv take their coefficients as Fn2D callables of time and state. A Rust closure or a Python callable of the state runs on the host only (a closure of time alone is tabulated on the grid and reaches the device). Written as an Expr, the same coefficient reaches the device:
use stochastic_rs::traits::Expr;
// σ(t, x) = 0.01 + 0.4 |x|, a displaced local volatility
let sigma = (Expr::lit(0.01) + Expr::x().abs() * 0.4).compile()?;
let cheyette = Cheyette::<f32, _>::new(f0, 0.5, sigma, 252, Some(1.0), seed).on::<Cuda>();Expr::t() and Expr::x() are the time and the state, Expr::lit a constant; the arithmetic operators, sqrt, exp, ln, abs, tanh, powf, max and min build the tree. Expr::compile() (or Fn2D::try_from) turns it into a postfix Program of at most 62 operations on a stack of at most 8, and returns a ProgramError for a tree no kernel could run; HestonSlv::new(.., leverage.compile()?, ..) is the shape. The host evaluates the same program, so host and device compute the same coefficient, not a tabulation of it.
What the device path does
- fGN noise. The circulant-embedding FFT runs on the device, one launch per
sample_parbatch. - Sheets.
Fbsruns its two-dimensional circulant embedding as a pipeline beside the fGN one; the embedding's eigenvalue roots are computed once on the host when the process is built. - Fractional diffusions.
Fbm,Fou,Fcir,Fgbm,Fjacobi,Fvasicek,Cfgns,CfbmsandCfoutake their increments from the fGN pipeline in device memory and run the whole recursion in the Euler kernel, so a batch crosses the bus once. On an M-series Metal device against the host: fOU with 2000 paths of 512 steps took 5.8 ms against 101 ms, fBM with 4000 paths of 1024 steps 19.2 ms against 252 ms. - Euler engine. The whole Euler–Maruyama recursion of a path runs in one kernel thread;
sample_par(m)is one launch for allmpaths. A system writes one plane per component, so a Heston draw comes back as two arrays. A square-root state steps under the process'sVarianceBoundary(Lord, Koekkoek & van Dijk 2010), as on the host. The CUDA and Metal kernels run one generator and counter layout from two sources (C for CUDA, Rust for Metal), soMetalandCudaagree seed for seed up tof32libm rounding. - Host backends.
Cpuis the process's own sampler;Acceleratediffers from it only for the fGN family (vDSP FFT).
Large batches
A device handle holds at most batch_budget bytes of path data per launch (1 GiB by default; Cuda::from_env()? reads STOCHASTIC_RS_DEVICE_BATCH_BYTES, or set Cuda::default().with_batch_budget(n)). A larger sample_par(m) runs as several launches whose union is bit-identical to one launch, because every draw's Philox counter carries the global path index; sample_map(m, f) maps each chunk on the host before the next is launched, so the batch never has to fit in memory at once. The fGN and sheet pipelines chunk the same way, each normal's counter carrying its batch-global cell.
A handle keeps what it compiled and allocated between calls — kernels, FFT plans, device buffers, the CUDA pinned staging — and frees nothing on its own. Cuda::default().release() frees what the crate caches for that ordinal, Metal::default().release() what the calling thread caches for it (Metal's caches are per thread), and device::release_host_scratch() the host FFT workspace of the calling thread and of the rayon pool. The next launch rebuilds what it needs and draws the same paths from the same seed.
Precision and reproducibility
| Backend | Precision | Same seed, same device | Bit-identical to Cpu |
|---|---|---|---|
Cpu | f32 / f64 | yes, thread-count independent | — |
Accelerate | f32 / f64 | reproducible-effort only (vDSP reduction order can differ across core types) | no |
Metal | f32 (an f64 process is a compile error) | yes | no |
Cuda | f32 / f64 (a failing f64 launch is reported, never replaced by the f32 kernel) | yes | no |
The device seed is drawn from the process's own seed source, so the device follows the host's rule: two processes built from the same Deterministic seed value give the same paths, consecutive calls on one process advance the stream, and an Unseeded process draws fresh entropy every call. Host and device paths agree in distribution, not bit for bit, because the device draws its own stream.
Probing a device, handling its failures and choosing an ordinal are on Sampling backends.
From Python
89 process classes take device= (PyGbm(..., dtype="f32", device="metal")); the ones without it, listed on Python bindings, sample on the host. The name is one of "cpu", "accelerate", "cuda", "metal", optionally with an ordinal ("cuda:1"); one the build does not carry raises ValueError with a rebuild hint, and the device is probed when the object is built. Arrays come back in the class's dtype, and the f32-only Metal backend needs dtype="f32". A class also answers device_ready() and device_fallback() for the configuration it holds. The published wheels are CPU-only; the device backends need a source build — maturin develop --features metal on a Mac, --features cuda on an NVIDIA machine. See Python bindings.
Trying the CUDA back-end without a CUDA machine
notebooks/colab_cuda_check.ipynb opens in Google Colab: on a T4 runtime it installs Rust, clones the repository, runs the native CUDA tests (the fGN pipeline and the Euler engine), builds the Python wheel with device="cuda" and plots what the GPU produced. The Rust tests build in 10–20 minutes on Colab's two cores, the wheel in another ten.
A run on a T4
notebooks/runs/colab_cuda_check_T4_2026-09-03.ipynb is the executed copy of a run on 2026-09-03 (Tesla T4, driver 580.82 / CUDA 13.0, toolkit 12.8; GitHub's viewer may not render Colab outputs, nbviewer does). The native CUDA tests passed, including a chunked batch equal to one launch path for path in f32 and f64, and so did the wheel's device tests.
The variance of fractional Brownian motion sampled on the GPU sits on the t^{2H} lines for H = 0.3, 0.5, 0.8 where the CPU samples sit, and the Euler engine's GBM terminal mean over 100k paths was 105.15 against the 105.13 forward. fGN wall time per call, warm, against Colab's two CPU cores:
| Case | CPU | Cuda (T4) |
|---|---|---|
fGN, n = 4096, m = 2000, f64 | ≈ 0.55 s | ≈ 0.22 s (2.5×) |
fGN, n = 16384, m = 1000, f64 | ≈ 0.65 s | ≈ 0.36 s (1.8×) |
The fGN pipeline is FFT-bound and already wins at these sizes; the gap widens with m. For the Euler engine on a T4, dtype="f32" is the setting to use: the card runs double precision at 1/32 of its single-precision rate. Current Euler-engine numbers on the T4 are on Benchmarks.
Stability
What is public and stable is the capability a bound names (EulerBackend<T> and the others), .on::<B>(), with_backend(handle), device_ready(), device_fallback(), probe(), release(), release_host_scratch() and the try_* calls. The engine's launch hooks, family specifications and slot limits are hidden from the rendered documentation and outside the stability promise.
Last updated on
Sampling backends
Compile-time device selection through `.on::<B>()` and `with_backend(handle)` — the `Backend` marker, its capability subtraits, one fallback rule.
Design philosophy
Why the library is shaped the way it is — generic simulation, paper-anchored implementations, one error convention, what is stable, and the non-goals.