1

All references to arrays in this post are multi-dimensional.

I came to know that when an array is passed a subroutine, it can be declared with different dimensions/sizes as the caller.

As a specific example, BMAIN is declared with DIMENSION(6,5) in the main program. BMAIN is passed to a subroutine as BSUB, which is declared as:

INTEGER, INTENT(IN) :: BSUB(3,2,0:4)

Questions:

Are the entries in BSUB simply filled one by one from SBMAIN until it is filled (in the order explained here Linear Indexing of Multi-Dimension Arrays in Fortran)?

Are there any dimension matching performed by the compiler? For instance, had BSUB been declared as BSUB(0:4,3,2), would it still hold the correct entries in the correct places?

Community
  • 1
  • 1
boxofchalk1
  • 493
  • 1
  • 6
  • 13
  • How would you define correct? Both definitions of `BSUB` will have all the elements of `BMAIN` because all have 30 elements. You will simply find them at different indices. – sigma Jun 29 '14 at 21:47

2 Answers2

3

Multi-dimensional arrays in Fortran are stored in column-major order. In memory, they the elements are linear order and the memory offset is calculated from the multi-dimensional indices. The equation is given at http://en.wikipedia.org/wiki/Row-major_order. The Fortran compiler will use that equation and calculate an position in memory based on the dimensions that you provide. To figure out the correspondence between elements when you declare a multi-dimensional array with different dimensions, apply the equation twice using the different dimensions. The compiler doesn't move values in memory. It calculates position in memory from the index values, based on the dimensions.

There are cases where the code generated by the Fortran compiler will copy values, creating a temporary array. e.g., if the actual array argument in the call involves a stride, the compiler might need to create a contiguous temporary array to be compatible with the argument of the subroutine.

M. S. B.
  • 28,968
  • 2
  • 46
  • 73
3

From the Fortran 2008 standard (12.5.2.11.4):

An actual argument that represents an element sequence and corresponds to a dummy argument that is an array is sequence associated with the dummy argument if the dummy argument is an explicit-shape or assumed-size array. The rank and shape of the actual argument need not agree with the rank and shape of the dummy argument, but the number of elements in the dummy argument shall not exceed the number of elements in the element sequence of the actual argument. If the dummy argument is assumed-size, the number of elements in the dummy argument is exactly the number of elements in the element sequence.

It is completely legal to use a different shape and rank of the array in the subroutine, just do not reference more elements in the subroutine than the array actually has. Array temporary may be necessary, but often it is not.

  • Just for reference, the same goes for Fortran 90 and is stated in Section 12.4.1.4 of the Fortran 90 standard. Thanks Vladimir F. – boxofchalk1 Nov 26 '14 at 20:48