CliMA / Thermodynamics.jl

A package containing a library of moist thermodynamic relations.
https://clima.github.io/Thermodynamics.jl/dev/
Apache License 2.0
61 stars 2 forks source link

`if`-statements may reduce performance on GPUs #177

Open glwagner opened 8 months ago

glwagner commented 8 months ago

if statements appear throughout this package. For example:

https://github.com/CliMA/Thermodynamics.jl/blob/d73a83404eccbf5351722777dfa2f59800e5d106/src/relations.jl#L1290-L1311

In all cases except those in which one branch cannot be executed in some situation, if statements should be replaced by ifelse. For example, CUDA.jl's "performance tips" states:

  • Avoid using control flow instructions such as if which cause branches, e.g. replace an if with an ifelse if possible;

Note that this performance tip is not limited to GPUs; it's just that CPUs are better at branch prediction (and are slower) so the slowdown incurred by the presence of an if statement within a hot inner loop may be more difficult to observe.

As a rule, we should always use ifelse, | and & in performance-critical code, and avoid the "short-circuiting" operators if, || and &&.

We could expend some effort to attempt to determine whether or not the if statements are incurring a slowdown. However, I think this is a waste of time unless we cannot use ifelse for some reason. This rule of thumb should also be followed for all future code.