Workshop on Software Strategies for Sustainable Physical Modelling
ECMWF, Reading
2026-11-25
To access links or follow on your own device these slides can be found at:
jackatkinson.net/slides
Except where otherwise noted, these presentation materials are licensed under the Creative Commons Attribution-NonCommercial 4.0 International (CC BY-NC 4.0) License.
Vectors and icons by SVG Repo under CC0(1.0) or FontAwesome under SIL OFL 1.1
GOTO language for weather and climate (and other) scientific applications

Fortran logo from fortran-lang/assets, MIT licence. ICCS logo © ICCS, University of Cambridge.

Neural Net by 3Blue1Brown under fair dealing.
Many large scientific models are written in Fortran (or C, or C++).
Much machine learning is conducted in Python.




![]()



NCAR logo © UCAR.
PyTorch, the PyTorch logo and any related marks are trademarks of The Linux Foundation
Mathematical Bridge by cmglee used under CC BY-SA 3.0
iso_c_binding.We therefore:
libtorch C++libtorch C++ API,use ftorch
implicit none
real, dimension(5), target :: in_data, out_data ! Fortran data structures
type(torch_tensor), dimension(1) :: input_tensors, output_tensors
type(torch_model) :: torch_net
in_data = ... ! Prepare data in Fortran
! Create Torch input/output tensors from the Fortran arrays
call torch_tensor_from_array(input_tensors(1), in_data, torch_kCPU)
call torch_tensor_from_array(output_tensors(1), out_data, torch_kCPU)
call torch_model_load(torch_net, 'path/to/saved/model.pt', torch_kCPU) ! Load ML model
call torch_model_forward(torch_net, input_tensors, output_tensors) ! Infer
call further_code(out_data) ! Use output data in Fortran immediatelyCUDA, HIP, XPU, MPS with multiple devicesI could talk for an hour in detail, see jackatkinson.net/slides or come chat.
JOSS logo — Journal of Open Source Software.
GRIST-MPS - ML physics suite, stable six-year AMIP runs (Wang et al. 2026)MiMA - gravity-wave parameterisations “identical” offline behave very differently online (Mansfield and Sheshadri 2024)GloSea6 - ML replacing a BiCGStab bottleneck (Park and Chung 2025)WaveWatch III - online NLML emulator of nonlinear interactions (Ikuyajolu et al. 2026)E3SM - emulation of cloud-resolving models (Hu et al. 2025)LS-DYNA - RNN constitutive model for bone in an explicit FEM solver (Saenz-Betancourt et al. 2027)More at cambridge-iccs.github.io/FTorch/community/case_studies




FTorch originally focussed on deploying models trained offline.
However, it is clear (Mansfield and Sheshadri 2024) that attention to online performance is important.
Utilise Torch’s autograd machinery on torch_tensors:
autograd, optimzers, loss functions=,+,-,*,/,**)Making an entire model differentiable with FTorch would be invasive and we do not advocate for this…
Work led by Joe Wallwork.

Enzyme logo from enzyme.mit.edu, used with permission. LLVM logo from llvm.org.
enzyme module and add a hook for calculating the derivative.iso_c_bindingsubroutine/void
Generate LLVM Intermediate Representation (IR) with flang:
flang square.f90 -S -emit-llvm \
-I /.../Enzyme/build/modules -o input.llThe call to Enzyme is an unresolved declaration at this stage:
call void @f__enzyme_autodiff_(...)
...
define void @_QQmain() #0 {
%1 = alloca float, i64 1, align 4
%2 = alloca float, i64 1, align 4
store float 3.000000e+00, ptr %1, align 4
%3 = call ptr @_FortranAioBeginExternalListOutput(i32 6, ptr @_QQclX2F776F726B2F7371756172652E66393000, i32 7)
%4 = call contract float @_QFPsquare(ptr %1)
%5 = call i1 @_FortranAioOutputReal32(ptr %3, float %4)
%6 = call i32 @_FortranAioEndIoStatement(ptr %3)
store float 0.000000e+00, ptr %2, align 4
call void @f__enzyme_autodiff_(ptr @_QFPsquare, ptr %1, ptr %2)
%7 = call ptr @_FortranAioBeginExternalListOutput(i32 6, ptr @_QQclX2F776F726B2F7371756172652E66393000, i32 10)
%8 = load float, ptr %2, align 4
%9 = call i1 @_FortranAioOutputReal32(ptr %7, float %8)
%10 = call i32 @_FortranAioEndIoStatement(ptr %7)
ret void
}
...Run the Enzyme pass over the IR with opt:
opt input.ll -S \
-load-pass-plugin=/.../FlangEnzyme.so \
-passes=enzyme \
-o output.llThe pass:
@f__enzyme_autodiff_ becomes @diffe_QFPsquare...
define void @_QQmain() #0 {
%1 = alloca float, i64 1, align 4
%2 = alloca float, i64 1, align 4
store float 3.000000e+00, ptr %1, align 4
%3 = call ptr @_FortranAioBeginExternalListOutput(i32 6, ptr @_QQclX2F776F726B2F7371756172652E66393000, i32 7)
%4 = call contract float @_QFPsquare(ptr %1)
%5 = call i1 @_FortranAioOutputReal32(ptr %3, float %4)
%6 = call i32 @_FortranAioEndIoStatement(ptr %3)
store float 0.000000e+00, ptr %2, align 4
call void @diffe_QFPsquare(ptr %1, ptr %2, float 1.000000e+00)
%7 = call ptr @_FortranAioBeginExternalListOutput(i32 6, ptr @_QQclX2F776F726B2F7371756172652E66393000, i32 10)
%8 = load float, ptr %2, align 4
%9 = call i1 @_FortranAioOutputReal32(ptr %7, float %8)
%10 = call i32 @_FortranAioEndIoStatement(ptr %7)
ret void
}
...
define internal void @diffe_QFPsquare(ptr noalias readonly captures(none) %0, ptr captures(none) %"'", float %differeturn) #5 {
%"'de" = alloca float, align 4
store float 0.000000e+00, ptr %"'de", align 4
%"'de1" = alloca float, align 4
store float 0.000000e+00, ptr %"'de1", align 4
%2 = load float, ptr %0, align 4, !alias.scope !4, !noalias !7
br label %invert
...Apply optimisations to the original and differentiated code with opt:
opt output.ll -S -O2 -o output_opt.llOptimisations to Existing Fortran code are preserved.
...
define void @_QQmain() local_unnamed_addr #0 {
%1 = tail call ptr @_FortranAioBeginExternalListOutput(i32 6, ptr nonnull @_QQclX2F776F726B2F7371756172652E66393000, i32 7)
%2 = tail call i1 @_FortranAioOutputReal32(ptr %1, float 9.000000e+00)
%3 = tail call i32 @_FortranAioEndIoStatement(ptr %1)
%4 = tail call ptr @_FortranAioBeginExternalListOutput(i32 6, ptr nonnull @_QQclX2F776F726B2F7371756172652E66393000, i32 10)
%5 = tail call i1 @_FortranAioOutputReal32(ptr %4, float 6.000000e+00)
%6 = tail call i32 @_FortranAioEndIoStatement(ptr %4)
ret void
}
- Note the values have become hardcoded here!
...Compile the optimised LLVM IR to an executable:
flang output_opt.ll -o squareRun:
./squareOutput:
9.
6.
d square/dx = 2 * x = 6.0 as expected!
Multiple-file compilation is possible with LTO:
flang -flto -c \
-I /.../Enzyme/build/modules square_mod.f90 \
-o square_mod.o
flang -flto -c \
-I /.../Enzyme/build/modules main.f90 \
-o main.o
flang -flto -fuse-ld=lld -Wl, \
--load-pass-plugin=/.../FlangEnzyme.so \
square_mod.o main.o -o square
Enzyme logo from enzyme.mit.edu, used with permission. LLVM logo from llvm.org.
__enzyme_autodiff provides reverse mode,__enzyme_fwddiff provides forwards modeenzyme_const - constants with no derivativeenzyme_dup - duplicated argumentsMITGCM results figure courtesy of Joe Wallwork, ICCS.
We publish and maintain SKILLs for our (and others’) software.
Screenshot: Cambridge-ICCS/ICCS-SKILLS (MIT licensed).
EGU27 logo © European Geosciences Union.
Get in touch:
Thanks to Mikolaj Kowalski, Valentin Churavy,
Tom Meltzer, Isaac Akanho, and all of the ICCS team.
The ICCS received support from 
FTorch has been supported by 

Logos: Schmidt Futures, C2D3, Cambridge-ICCS/FTorch, enzyme.mit.edu, and ICCS — University of Cambridge.
CUDA, HIP, MPS, XPU currently exposedtorch_kCUDAcall torch_tensor_from_array(input_tensors(1), in_data, torch_kCUDA, torch_kCUDA device_index=2) ! Input on CUDA:2
call torch_tensor_from_array(output_tensors(1), out_data, torch_kCPU) ! Output on CPU
call torch_model_load(torch_net, 'path/to/saved/model.pt', torch_kCUDA device_index=2) ! Load net on CUDA:2
call torch_model_forward(torch_net, input_tensors, output_tensors) ! Inferautograd: requires_grad, torch_tensor_backward, gradient accessors=,+,-,*,/,**) on torch_tensorSGD, Adam, AdamW), and loss functions MSE, L1, CrossEntropy with sum and mean.! Construct input tensors with requires_grad=.true.
! Compute some mathematical expression
call torch_tensor_from_array(Q, Q_arr, torch_kCPU)
Q = 3.0 * (a**3 - b**2)
! Reverse mode
call torch_tensor_backward(Q)
call torch_tensor_from_array(dQda, dQda_arr, torch_kCPU)
call torch_tensor_get_gradient(a, dQda)Allows computation of loss downstream from the neural net.
alloca/load/store, basic blocks