Subcategory: None · Core · Maths

Compiler Optimisations (Release Builds)

Modern compilers support many automatic low-level optimisations which can improve the performance of the compiled code. The actual performance gain depends on your specific code, but it is not uncommon to see 5–10x speedups, and in some cases, 50x or more, especially for compute-intensive tasks.

Read More

Loop ordering

In Fortran, MATLAB, and R, arrays are stored in column-major order, meaning that the entries from the left-most index are stored contiguously in memory. This is often referred to as the left-most index varying quickest. This is different from many other popular scientific programming languages such as C, C++, and Python, which use a row-major ordering. The implication for writing loops is that it is always best to order loops by which indices vary quickest. In this way, the loop nest will traverse contiguous memory, which can be done efficiently.

Read More

Reshape

It’s often required to reshape arrays used in Fortran code. This can be achieved in several ways, the most naive of which is to use hand-coded do loops. This is not recommended as it is error-prone and verbose. A better approach is to use the intrinsic reshape function, which is concise and clear.

The most efficient way to reshape arrays is to use pointers. This is a more advanced approach and care must be taken to ensure that (a) the original array is not deallocated while the pointer is still in use and (b) you are aware that modifications to the “reshaped” array will modify the original array and vice versa because they share the same memory.

Read More

Use BLAS/LAPACK Instead of Reimplementing Maths Functions

BLAS (Basic Linear Algebra Subprograms) and LAPACK (Linear Algebra PACKage) are widely used open-source libraries for doing maths, especially matrix operations. They are highly optimised, often for specific CPU architectures by the vendors (Intel, AMD, etc.) themselves, and can be found on all modern HPC systems. Therefore, they are significantly faster than anything we can write ourselves, especially for larger matrices, and should be used in most circumstances.

Read More

Move invariant conditional out of the loop to facilitate vectorisation

Many Fortran compilers will attempt to automatically vectorise a loop if possible. There are several bad practices which can inhibit this automatic vectorisation. One such pattern is when a conditional evaluates to the same value for all loop iterations and can be moved outside the loop. In this scenario, not only are we redundantly evaluating the same conditional but we are also inhibiting automatic vectorisation.

Read More