Zuhal Karadavut
Theme

Simulation, numerics and systems

mixed-precision-stencils

In a climate-model-style stencil, where is float32 safe, where does it break, and which mixed-precision fix restores the float64 answer, measured rather than assumed?

Answer

It breaks where a small increment is added to a large stored value. Diffusing 0.01 K on 288 K, 84.5 % of the float32 updates were rounded away entirely, and after 1,000 steps the error was 386 % of the perturbation. Storing the deviation from 288 K in float32 brings the error down to 0.325 % of the scheme's own discretisation error. Doing the step in float64 and storing the result in float32 does not help: it still lost 84.7 % of the updates.

Rounding error after the last step, as a multiple of the discretisation error (times the discretisation error)
as large as the discretisation errorf326,248results/diffusion.jsonf32-state-f64-step6,248results/diffusion.jsonf32-kahan35.6results/diffusion.jsonf64-state-f32-tend18.6results/diffusion.jsonf32-perturbation0.00325results/diffusion.json

How it was measured

  • Three cases with an exact answer: tracer advection, diffusion of a small perturbation on a large background, and linear shallow-water gravity waves. Errors are measured against the exact solution of the discrete scheme, computed per Fourier mode, so rounding error is never measured against another run.
  • Six precision variants share one time loop written in increment form, q + dq, so the one place where a small change meets a large stored value is a single addition.
  • Each variant runs on 200 random initial conditions for diffusion. Medians come with bootstrap intervals, and variants are compared with paired tests.
  • The schemes converge at the orders theory predicts: 2.99, 2.01 and 2.00 for advection, diffusion and shallow water. One kernel is also written in gt4py and checked against NumPy after every step.

Proof

Every file is pinned to the commit the numbers come from, so the links cannot drift.

Reproduce it

pip install -r requirements-dev.txt
python -m mpstencils.run --config configs/full.json
pytest -q

CPU only. The run rewrites the result files and the README tables. CI recounts every number in the README against the result files.

The repository on GitHub

Limits

  • Linear cases with constant coefficients on a doubly periodic grid, not a full dynamical core. A nonlinear flux needs the reference state handled inside the flux; that is not tested.
  • NumPy evaluates each expression as written. A compiled model with fused multiply-add may round differently. On Linux, CI checks the compiled gt4py backend against NumPy.

Built with

  • Python
  • NumPy
  • SciPy
  • gt4py
  • numerical analysis