PALEOtoolkit / PALEOboxes.jl

Model coupler for the PALEO model framework
https://PALEOtoolkit.github.io/PALEOboxes.jl
MIT License
2 stars 1 forks source link

Document how to benchmark and optimize models #84

Open sjdaines opened 1 year ago

sjdaines commented 1 year ago

With Julia (and unlike Fortran) there can be a trade-off between Python-like code that is simple and clear, and optimised code where care is taken to help type inference succeed and avoid allocating memory for temporary arrays etc.

PALEO Reactions that are intended to be widely used should usually be written so that the initialize_ and do_ methods (called repeatedly in the model main loop) avoid any allocations. This usually also means that these methods are fully typed and approach Fortran or C speed.

NB: this only applies to the initialize_ and do_ methods ! All other code (eg in register_methods!) that only runs at model startup should be written for simplicity and clarity and can make full use of temporary arrays, type unstable variables, etc.

In most cases it is easier to require zero allocations and make small modifications to the code in the do_ methods than to try and work out how many allocations might matter. The design of PALEOboxes.jl usually makes this easy to achieve.

Generic Julia optimisation techniques and gotchas are described in https://docs.julialang.org/en/v1/manual/performance-tips/, see also https://m3g.github.io/JuliaNotes.jl/stable/memory/ and PALEO-specific information is at the end of https://paleotoolkit.github.io/PALEOtutorials.jl/dev/HOWTOJuliaUsage/

PALEO utilities to help test and achieve this: