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?
CodePudding user response:
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.
CodePudding user response:
There are two different concepts of contiguity here.
In the Fortran sense, contiguous means (Fortran 2018, 3.41):
having array elements in order that are not separated by other data objects
This concept of contiguity allows for "padding" between array elements if there are stored in memory which has that possibility. What contiguity means is that A(1)
and A(2)
have no object between them.
That is, a scalar may take up some space, but as an element of an array the same object may take up more space. As given by (F2018 16.9.184, Note 1):
An array element might take more bits to store than an isolated scalar, since any hardware-imposed alignment requirements for array elements might not apply to a simple scalar variable.
The description of what arrays are considered to be contiguous refer to this sense of contiguity. It is the different concept of contiguity in the second quote of the question which is not mandated.
From your example
real, dimension(5,5) :: a
we do have a contiguous array a
: it is a whole array which is not a pointer and is not assumed-shape. It may still have some degree of padding between elements.