Manual 12 CPU or GPU

Performance

CUDAFloat32threads

CPU or GPU

With CUDA.jl loaded, CudaMode runs the simulation on the GPU, and the code is the same as on the CPU (see Monte Carlo configuration). Whether it pays depends on the number of paths: every time step launches a kernel, so below about ten thousand paths the launches cost more than the computation, and above it the GPU wins by an increasing margin. Single precision helps much more on a GPU than on a CPU, because consumer GPUs have few double precision units.

Time of a Black-Scholes European call against the number of paths, on CPU and GPU

Price of a European call under Black-Scholes with 50 time steps, minimum time in milliseconds:

PathsCPU, Float64GPU, Float64GPU, Float32
1 0000.724.544.69
10 0009.144.824.98
100 000122.3222.906.17
1 000 0001640.16201.7641.74

Measured on an Intel Core i7-8550U (one thread) and an NVIDIA GeForce MX150, with Julia 1.13 and BenchmarkTools, synchronizing the GPU with CUDA.@sync (kernels are asynchronous, so without it the time stops at the launch). The script is docs/performance/scaling.jl; it needs a GPU, so the figure is not regenerated when the documentation is built.

Beyond the diffusion, three steps of some models and payoffs could go through the host. In CudaMode they stay on the GPU or move only what they need:

  • the gamma and inverse Gaussian subordinators of variance gamma and normal inverse Gaussian are drawn on the GPU (other laws of a SubordinatedBrownianMotion are drawn on the host and copied);
  • the jumps of Kou and Merton are drawn on the host as a list of times and sizes, and one kernel adds them to the paths;
  • barrier and Asian payoffs are reductions of the paths (minimum, maximum, mean) on the GPU; American and Bermudan payoffs, whose Longstaff-Schwartz regressions run on the host, still copy the paths there.

100 000 paths × 50 steps, median time and host memory, before (0.6.0) and after these changes:

Case0.6.0nowhost memory, 0.6.0 → now
Black-Scholes, European27.9 ms24.8 ms59 KiB → 59 KiB
Kou, European75.3 ms39.4 ms39 MiB → 10 MiB
Merton, European74.4 ms36.3 ms39 MiB → 10 MiB
Variance gamma, European236 ms131 ms851 KiB → 86 KiB
Normal inverse Gaussian, European194 ms64 ms851 KiB → 86 KiB
Black-Scholes, barrier (down and out)73.8 ms26.3 ms79 MiB → 64 KiB
Black-Scholes, Asian (fixed strike)74.1 ms27.3 ms79 MiB → 65 KiB

Measured on the same NVIDIA GeForce MX150 with BenchmarkTools and CUDA.@sync; the Black-Scholes European case, whose code did not change, shows the noise between runs (about 10%).

Getting the most out of a run

  • Price several payoffs on the same paths: a vector of payoffs, a FinancialMonteCarlo.Position or a smile simulate once. A smile on the GPU computes all its strikes in one kernel and keeps the prices there, so further work on them, such as implied volatilities, needs no transfer.
  • European payoffs need a single time step: the number of steps only matters for path dependent payoffs, early exercise and models without an exact one step scheme.
  • Antithetic variates halve the normal draws and usually the variance too, at no extra cost.
  • Constant rates are cheaper than curves, whose drift is integrated at every step.
  • On the CPU, MultiThreading splits the paths into batches priced on different threads; the results do not depend on the number of threads. The paths of all the steps are stored, so the simulation is bound by memory traffic: 4 threads price 40 000 paths × 50 steps 2.8 times faster than one.
  • For gradients, ForwardDiff is the fastest backend up to a few dozen parameters, Enzyme in reverse mode (or Mooncake) above; ReverseDiff is about a hundred times slower, see Automatic differentiation.
  • Keep the numeric type uniform: Float32 inputs give Float32 paths and prices, and a single Float64 input promotes the whole computation.