By: Carlie Coats (carlie.delete@this.jyarborough.com), August 25, 2022 7:39 am
Room: Moderated Discussions
ananon (whhhyy.delete@this.trashcan8765.com) on August 25, 2022 12:56 am wrote:
[snip...]
> How you write code is extremely important, because it has to be read and understood by both
> humans and compilers. Aiming for clean and straightforward code whenever possible should
> be your priority, not to try doing micro-optimizations while writing functional code.
I've documented one extreme example here, at :
The early-1990's vintage MM5v3 weather model (and likewise, the early-2000's WRF) frequently had routines written for the limitations of early-1980's vector machines and their compilers (where the innermost loop should be "long" and there should be at most one conditional and at most one AX+B in each innermost loop). One example is the EXMOISR MM5 moist-thermodynamics routine (and the WRF WSM* family of routines), where there is a long sequence of loops of
the form:
among which data is transferred by a large set of cache-busting (X,Z)-subscripted scratch variables (over 1 MB-worth, for a typical case), and most of which contain an
conditional, resulting in LOTS of small basic blocks and lots of memory traffic, just to fit the limitations of then-obsolete hardware.
Rewriting the code for simplicity:
LOOP OVER X-SUBSCRIPT
LOOP OVER Z-SUBSCRIPT
....
IF FREEZING
...
ELSE
...
ENDIF
ENDLOOP
ENDLOOP
has a much simpler huge-basic-block structure, and allows one to replace nearly all the scratch variables by scalars. The resulting source-code has less than half the size of the original, is much less cluttered by now-unnecessary subscripting, loop-nests, and logic, and is eight times faster for a typical case (on 2000-vintage processors).
And the same people mirrored that original slow and complex code-structure a decade later for the WRF meteorology model. Note that met-modeling is a classic "real-time Blue-Book" problem for which performance is critical...
FWIW.
[Sorry about the indentation; the idiot who specified the "code" markup did not properly support it.]
[snip...]
> How you write code is extremely important, because it has to be read and understood by both
> humans and compilers. Aiming for clean and straightforward code whenever possible should
> be your priority, not to try doing micro-optimizations while writing functional code.
I've documented one extreme example here, at :
The early-1990's vintage MM5v3 weather model (and likewise, the early-2000's WRF) frequently had routines written for the limitations of early-1980's vector machines and their compilers (where the innermost loop should be "long" and there should be at most one conditional and at most one AX+B in each innermost loop). One example is the EXMOISR MM5 moist-thermodynamics routine (and the WRF WSM* family of routines), where there is a long sequence of loops of
the form:
LOOP OVER Z-SUBSCRIPT
LOOP OVER X-SUBSCRIPT
....
ENDLOOP
ENDLOOP
,among which data is transferred by a large set of cache-busting (X,Z)-subscripted scratch variables (over 1 MB-worth, for a typical case), and most of which contain an
IF FREEZING
...
ELSE
...
ENDIF
conditional, resulting in LOTS of small basic blocks and lots of memory traffic, just to fit the limitations of then-obsolete hardware.
Rewriting the code for simplicity:
LOOP OVER X-SUBSCRIPT
LOOP OVER Z-SUBSCRIPT
....
IF FREEZING
...
ELSE
...
ENDIF
ENDLOOP
ENDLOOP
has a much simpler huge-basic-block structure, and allows one to replace nearly all the scratch variables by scalars. The resulting source-code has less than half the size of the original, is much less cluttered by now-unnecessary subscripting, loop-nests, and logic, and is eight times faster for a typical case (on 2000-vintage processors).
And the same people mirrored that original slow and complex code-structure a decade later for the WRF meteorology model. Note that met-modeling is a classic "real-time Blue-Book" problem for which performance is critical...
FWIW.
[Sorry about the indentation; the idiot who specified the "code" markup did not properly support it.]