HDF5 Fortran dimension order
The Fortran array dimension order is column-major, meaning the first dimension changes fastest in memory. This is consistent with how Fortran and Matlab store arrays in memory. When indexing arrays, particularly in a loop, significant performance differences can arise depending on the dimension order. Accessing elements in the fastest-changing dimension first is generally more efficient as it’s memory contiguous, reducing cache misses and improving performance.
The HDF5 document on C versus Fortran interfaces explains how HDF5 handles arrays in different programming languages. Most languages, including C, C++, Java, and Python use row-major order, where the last dimension changes fastest in memory, which is also how the HDF5 interface to those languages works. For Fortran and Matlab, the HDF5 interface expects arrays in column-major order, consistent with Fortran’s memory layout. This column-major array ordering is also true for the HDF5 APIs of the h5fortran object-oriented Fortran HDF5 interface and the stdlib for Matlab libraries.
The HDF5 Library stores arrays along the fastest-changing dimension. This approach is often referred to as being “in C order.” C, C++, and Java work with arrays in row-major order. In other words, the row, or the last dimension, is the fastest-changing dimension. Fortran, on the other hand, handles arrays in column-major order making the column, or the first dimension, the fastest-changing dimension.