Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contiguity of arrays in modern Fortran

I am reading "Modern Fortran Explained" (2018 edition). It says on p24:

Most implementations actually store arrays in contiguous storage in array element order, but we emphasize that the standard does not require this.

I believe that this already dates back to old fortran 77 (and earlier). Given a declaration

real, dimension(5,5) :: a

my understanding is that the standard does not require that this array is contiguous. This agrees with what this book says in Sec. 7.18.1 on p140:

... the Fortran standard has shied away from specifying whether arrays are contiguous in the sense of occupying sequential memory locations with no intervening unoccupied spaces.

But then the book goes on (still p140):

Any of the following arrays are considered to be contiguous by the standard:

  • an array with the contiguous attribute;
  • a whole array (named array or array component without further qualification) that is not a pointer or assumed-shape;
  • ...
  • I understand the first criterion; but I am confused about the second criterion. Doesn't this disagree with the earlier statement that the fortran standard does not require to store arrays in contiguous storage in array element order? Has modern fortran moved away from this paradigm? Or are the above quotes not contradicting each other due to reasons that go beyond my understanding of this topic? In the above example, is array a required to be contiguous or not?

    like image 925
    Twirl Avatar asked Nov 16 '25 23:11

    Twirl


    1 Answers

    The language standard specifies what has to happen, in terms of the concepts described by the standard (statements being executed in a certain order, variables being defined with values, content being written to things called "files"), but it says nothing about how those things happen/how those things are implemented.

    There may be an obvious method of implementation, perhaps even only one practical method, but still, the standard does not specify implementation.

    The standard does not even require a "Fortran processor" to be an electronic device.

    Practically, it is a pretty safe assumption that a "contiguous array" (Fortran standard language term) will be implemented by the bytes that represent the values of the array elements being stored next to each other in RAM, but that's implementation detail, not a language standard requirement.

    It is handy for programmers to be aware of likely implementation methods, particularly for debugging or understanding performance aspects, but implementation and specification shouldn't be conflated.

    like image 119
    IanH Avatar answered Nov 18 '25 20:11

    IanH