Improving Fortran standardization process (lessons from C++23 getting multidimensional arrays)

Sort of, potentially also more powerful/customizable once you combine it with the whole C++ template machinery (depending how you see it). The general idea is there are two new containers mdarray (similar to allocatable arrays) and mdspan (closer to Fortran pointer arrays or assumed-shape array dummy arguments; can also be strided/non-contiguous). The new containers build upon the new multi-dimensional operator[] syntax (P2128R6 - Multidimensional subscript operator).

Writing a 3D seven-point stencil code may look as follows (input and output here are pointers to some kind of floating point buffer;N, M, and O are the “array” extents; v is a C++ range - a light-weight iterator-like abstraction):

image

This examples also makes use of the C++ standard parallelism features to state the iteration space can be updated in parallel and unsequentially. An analogous example of arrays and loop nests in Fortran could be:

real, pointer, contiguous :: b(:,:,:), a(:,:,:)
integer :: i, j, k

do concurrent(i = 2,size(a,1)-1, &
              j = 2,size(a,2)-1, &
              k = 2,size(a,3)-1)

  b(i,j,k) = ( a(i,j,k-1) + &
               a(i-1,j,k) + &
               ... )
end do

The goal for mdspan/mdarray is also they become the standard C++ multi-dimensional array solution. The lack of proper multi-dimensional arrays in C++ has led to the appearance of several competing solutions in practice, which can be hindering at times:

My guess is that libraries which already use Eigen, Blaze or other array libraries will continue to do so (the damage has already been done…). Upcoming versions however may add an operator which converts their own array container to a mdspan (essentially something like the Fortran array descriptor). C++ allows implicit conversions when passing dummy arguments, so going forward, if your C++ procedures/API accept a mdspan dummy argument, it might be usable with all existing (and future) array libraries.

As I’ve shown in Resistance to modernization - #13 by ivanpribec, you could also cast a Fortran array descriptor to a mdspan (or vice-versa), to call a C++ library from Fortran (or vice-versa).

*The snippets above are taken from the talk C++ Standard Parallelism - Bryce Adelstein Lelbach - CppNow 2022.

7 Likes