Skip to content
LumenaryAcademy
Foundations 60 min

Arrays, Fortran's greatest strength

Arrays as first-class values, with custom bounds, sections, whole-array arithmetic, masks, reductions and allocatable storage.

Code tested · 4AI-draftedAwaiting review by a computer scientist
MotivationH

Arrays are values, not pointers

In C, an array is a block of memory and a pointer to its first element; the language knows almost nothing else about it. In Python, the fast arrays live in a separate library, NumPy. In Fortran, since the 1990 standard, an array is a first-class value of the language. It knows its own shape and bounds, you can add two arrays with a + b, take the sine of a whole array, select every second element of a column, and assign the result to another array, all without writing a loop, and the compiler turns it into tight machine code.

This is the feature that most separates modern Fortran from FORTRAN 77, and it is why numerical code in Fortran often reads like the mathematics it implements. It also gives the compiler more freedom to optimise, because a whole-array expression says what to compute without fixing the order in which elements are visited.

DefinitionsH

Declaring, constructing and slicing

Definition— Array, rank, shape, bounds

An array is a collection of elements of one type, indexed by one or more integer subscripts. Its rank is the number of dimensions (up to 15), its shape is the extent of each dimension, and each dimension has a lower and upper bound, which default to 1 and the extent but may be any integers.

fortran
! Declaring, constructing and slicing arrays
program array_basics
  implicit none
  integer :: i
  real :: rain(12)                          ! indices 1 to 12
  integer :: offsets(-3:3)                  ! indices -3 to 3
  integer :: grid(2, 3)
  integer, parameter :: squares(5) = [(i**2, i = 1, 5)]   ! an implied-do constructor

  rain = [78., 64., 92., 120., 140., 31., 12., 18., 45., 88., 130., 95.]
  offsets = [(10 * i, i = -3, 3)]
  grid = reshape([1, 2, 3, 4, 5, 6], [2, 3])

  print '(a, 5i4)', 'squares:', squares
  print '(a, 7i5)', 'offsets:', offsets
  print '(a, i0, a, i0, a, i0)', 'offsets(-3) = ', offsets(-3), ', bounds ', lbound(offsets, 1), ':', ubound(offsets, 1)

  print '(a, 3f6.0)', 'first quarter:     ', rain(1:3)
  print '(a, 6f6.0)', 'every other month: ', rain(1:12:2)
  print '(a, 4f6.0)', 'last four:         ', rain(9:)
  print '(a, 3f6.0)', 'months 1, 6, 12:   ', rain([1, 6, 12])      ! a vector subscript
  print '(a, 12f6.0)', 'reversed: ', rain(12:1:-1)

  print '(a, 3i3)', 'grid row 1:   ', grid(1, :)
  print '(a, 2i3)', 'grid column 3:', grid(:, 3)
  print '(a, 6i3)', 'grid in memory order:', grid             ! column by column
  print '(a, i0, a, 2i2)', 'size ', size(grid), ', shape', shape(grid)
end program array_basics

It prints:

text
squares:   1   4   9  16  25
offsets:  -30  -20  -10    0   10   20   30
offsets(-3) = -30, bounds -3:3
first quarter:        78.   64.   92.
every other month:    78.   92.  140.   12.   45.  130.
last four:            45.   88.  130.   95.
months 1, 6, 12:      78.   31.   95.
reversed:    95.  130.   88.   45.   18.   12.   31.  140.  120.   92.   64.   78.
grid row 1:     1  3  5
grid column 3:  5  6
grid in memory order:  1  2  3  4  5  6
size 6, shape 2 3

What the program shows:

  • Declaration. real :: rain(12) declares 12 elements numbered 1 to 12. integer :: offsets(-3:3) chooses the bounds, which is natural for data centred on zero, such as a stencil or a grid with ghost cells. lbound and ubound report them. (You may also see the older real, dimension(12) :: rain, which means the same.)
  • Constructors. [78., 64., ...] builds an array from a list, and [(i**2, i = 1, 5)] is an implied-do constructor that generates the elements in a loop. reshape pours a flat list into an array of a given shape.
  • Sections. rain(1:3) is the first three elements, rain(1:12:2) every second one, rain(9:) from 9 to the end, and rain(12:1:-1) the array reversed. A vector subscript, rain([1, 6, 12]), picks arbitrary elements. For a matrix, grid(1, :) is row 1 and grid(:, 3) is column 3.
  • Column-major order. Printing the whole of grid lists its elements in memory order, 1 2 3 4 5 6, which goes down the first column, then the second: Fortran stores the first subscript fastest. When you loop over a matrix, put the first subscript in the innermost loop, so that consecutive iterations touch neighbouring memory. Julia, MATLAB and R follow the same convention; C and NumPy (by default) do the opposite.
  • Formats repeat. 5i4 means five i4 items, and later you will see *(i0, 1x), which repeats a group as often as needed.
Worked examplesH

Whole-array operations

Arithmetic operators and most intrinsic functions apply to arrays element by element, provided the arrays have the same shape (are conformable); a scalar combines with every element.

fortran
! Whole-array operations: arithmetic, elemental intrinsics, masks and reductions
program whole_arrays
  implicit none
  character(len=9), parameter :: cities(3) = [character(len=9) :: 'Reykjavik', 'Cairo', 'Lima']
  real :: temps(3, 7)                       ! 3 cities x 7 days, degrees Celsius
  real :: fahrenheit(3, 7), anomaly(3, 7)
  integer :: c

  temps(1, :) = [-2.0, 0.5, 1.0, -4.5, -1.0, 2.0, 0.0]
  temps(2, :) = [24.0, 26.5, 28.0, 27.0, 25.5, 30.0, 31.5]
  temps(3, :) = [19.0, 18.5, 19.5, 20.0, 18.0, 17.5, 19.0]

  fahrenheit = temps * 9.0 / 5.0 + 32.0               ! every element at once
  print '(a, 7f6.1)', 'Cairo in Fahrenheit: ', fahrenheit(2, :)

  ! Reductions, along a dimension
  print '(a, 3f7.2)', 'weekly means:   ', sum(temps, dim=2) / size(temps, 2)
  print '(a, 3f7.2)', 'weekly maxima:  ', maxval(temps, dim=2)
  print '(a, 3i7)',   'hottest day:    ', maxloc(temps, dim=2)

  ! Masks: logical arrays from comparisons
  print '(a, i0)', 'days below freezing anywhere: ', count(temps < 0.0)
  print '(a, l1, 1x, l1)', 'any day above 30? every Lima day above 15? ', any(temps > 30.0), all(temps(3, :) > 15.0)
  print '(a, 7f6.1)', 'Reykjavik days above 0: ', pack(temps(1, :), temps(1, :) > 0.0)

  ! where: assign only where a condition holds
  do c = 1, 3
     anomaly(c, :) = temps(c, :) - sum(temps(c, :)) / 7.0
  end do
  where (abs(anomaly) < 1.0)
     anomaly = 0.0                                    ! ignore small deviations
  elsewhere
     anomaly = sign(1.0, anomaly)                     ! keep only the direction
  end where
  do c = 1, 3
     print '(a9, 1x, 7f5.0)', cities(c), anomaly(c, :)
  end do
end program whole_arrays

It prints:

text
Cairo in Fahrenheit:   75.2  79.7  82.4  80.6  77.9  86.0  88.7
weekly means:     -0.57  27.50  18.79
weekly maxima:     2.00  31.50  20.00
hottest day:          6      7      4
days below freezing anywhere: 3
any day above 30? every Lima day above 15? T T
Reykjavik days above 0:    0.5   1.0   2.0
Reykjavik   -1.   1.   1.  -1.   0.   1.   0.
Cairo       -1.  -1.   0.   0.  -1.   1.   1.
Lima         0.   0.   0.   1.   0.  -1.   0.
  • Elementwise arithmetic. temps * 9.0 / 5.0 + 32.0 converts all 21 temperatures at once.
  • Reductions. sum, product, maxval, minval, count, any and all reduce an array to a scalar, or, with dim=, reduce along one dimension: sum(temps, dim=2) adds along the days, giving one total per city. maxloc returns the position of the maximum, here the day of the week on which each city was hottest.
  • Masks. A comparison such as temps < 0.0 produces a logical array of the same shape. count counts the .true. elements, any and all test them, and pack(array, mask) gathers the elements where the mask holds into a one-dimensional array. Most reductions also accept a mask= argument, as in sum(temps, mask=temps > 0.0).
  • where assigns only to the elements where a mask is true, with an optional elsewhere for the rest: the array version of an if. Here it classifies each day as near the city's average (0), above it (1) or below it (−1); sign(1.0, x) returns 1.0 with the sign of x.

Note the character array: [character(len=9) :: 'Reykjavik', 'Cairo', 'Lima'] gives the constructor an explicit type and length, because the standard requires all the strings in a constructor to have the same length otherwise.

ComputationC

Allocatable arrays

Often you know an array's size only when the program runs: the number of particles comes from an input file, or a list grows as results arrive. For this Fortran has allocatable arrays, declared with a deferred shape (:) and given storage later.

fortran
! Allocatable arrays: sizes decided at run time, growth, and safe allocation
program allocatables
  implicit none
  real, allocatable :: samples(:), squares(:), bigger(:), huge_grid(:, :)
  integer, allocatable :: primes(:)
  integer :: n, k, d, status

  n = 6
  allocate(samples(n))                         ! size known only at run time
  samples = [(0.5 * k, k = 1, n)]
  print '(a, 6f5.1)', 'samples: ', samples

  squares = samples**2                         ! assignment allocates squares automatically
  print '(a, i0, a, 6f6.2)', 'squares (size ', size(squares), '): ', squares

  ! Growing an array by assigning a longer one (each step copies; fine for small arrays)
  allocate(primes(0))
  do k = 2, 30
     if (all(mod(k, [(d, d = 2, k - 1)]) /= 0)) primes = [primes, k]   ! no divisor from 2 to k-1
  end do
  print '(a, *(i0, 1x))', 'primes: ', primes

  ! move_alloc transfers storage without copying
  allocate(bigger(2 * size(samples)))
  bigger(1:size(samples)) = samples
  bigger(size(samples) + 1:) = 0.0
  call move_alloc(bigger, samples)
  print '(a, i0, a, l1)', 'samples now has ', size(samples), ' elements; bigger allocated? ', allocated(bigger)

  ! Asking for too much memory: stat= turns a crash into an error you can handle
  allocate(huge_grid(huge(1), 2**20), stat=status)     ! about 8 pebibytes: far too much
  if (status /= 0) then
     print '(a, i0)', 'allocation failed, stat = ', status
  else
     print '(a)', 'allocation succeeded (unexpectedly)'
  end if

  ! Array assignment evaluates the right-hand side completely first
  squares = [1., 2., 3., 4., 5., 6.]
  squares(2:6) = squares(1:5)                  ! shifts right, no smearing
  print '(a, 6f4.0)', 'shifted: ', squares
end program allocatables

It prints:

text
samples:   0.5  1.0  1.5  2.0  2.5  3.0
squares (size 6):   0.25  1.00  2.25  4.00  6.25  9.00
primes: 2 3 5 7 11 13 17 19 23 29
samples now has 12 elements; bigger allocated? F
allocation failed, stat = 5014
shifted:   1.  1.  2.  3.  4.  5.
  • allocate(samples(n)) gives the array storage for n elements. allocated(x) tests whether it has storage, and deallocate(x) releases it.
  • Allocation on assignment. Since Fortran 2003, assigning to an allocatable array automatically (re)allocates it to the shape of the right-hand side. squares = samples**2 needed no allocate, and primes = [primes, k] grows the array by one element. Each such growth copies the whole array, so this is fine for a few dozen elements but slow for millions; for those, allocate generously and keep a count, or double the capacity when full.
  • move_alloc(from, to) hands the storage of one allocatable array to another without copying, leaving from unallocated. It is the standard way to resize: allocate a bigger array, copy the old contents in, then move it into place.
  • stat= turns an allocation failure into a status code you can test, instead of a crash. Asking for about 8 pebibytes failed, as it should, with a nonzero status; the particular number is compiler-specific. An errmsg= argument can also return a message, though GFortran 11's message for this case, which we tried, was not helpful.
  • Automatic deallocation. An allocatable array local to a procedure is deallocated automatically when the procedure returns, so memory leaks, the plague of C, are rare in Fortran code that uses allocatable rather than pointer.
  • Whole-array assignment is safe against overlap. squares(2:6) = squares(1:5) behaves as if the right-hand side were evaluated completely before anything is stored, so the elements shift correctly. A naive element-by-element loop in that order would have copied the first element all the way along.
Worked examplesH

A stencil with array sections

Array sections let you write numerical methods almost exactly as they appear on paper. Consider the equation −u′′(x)=1-u''(x) = 1 on [0,1][0, 1] with u(0)=u(1)=0u(0) = u(1) = 0, whose exact solution is u(x)=12x(1−x)u(x) = \tfrac12 x(1 - x). On a grid with spacing hh, the second derivative is approximated by (ui−1−2ui+ui+1)/h2(u_{i-1} - 2u_i + u_{i+1})/h^2, so the discrete equations are ui=12(ui−1+ui+1+h2)u_i = \tfrac12(u_{i-1} + u_{i+1} + h^2). Jacobi iteration solves them by applying that formula to every interior point repeatedly until nothing changes:

fortran
! Array sections as stencils: solving -u'' = 1 on [0, 1] with u(0) = u(1) = 0 by Jacobi iteration
program jacobi
  implicit none
  integer, parameter :: n = 11                 ! grid points, including the two boundaries
  real :: u(n), unew(n), x(n), exact(n), h
  integer :: k, sweep

  h = 1.0 / (n - 1)
  x = [((k - 1) * h, k = 1, n)]
  exact = 0.5 * x * (1.0 - x)                  ! the known solution, for comparison
  u = 0.0                                      ! initial guess; u(1) and u(n) stay 0

  do sweep = 1, 2000
     ! each interior point becomes the average of its neighbours, plus the source term
     unew(2:n-1) = 0.5 * (u(1:n-2) + u(3:n) + h**2 * 1.0)
     unew(1) = 0.0
     unew(n) = 0.0
     if (maxval(abs(unew - u)) < 1.0e-7) exit
     u = unew
  end do

  print '(a, i0, a)', 'converged after ', sweep, ' sweeps'
  print '(a)', '    x      u(x)     exact'
  do k = 1, n, 2
     print '(f5.2, 2f10.5)', x(k), u(k), exact(k)
  end do
  print '(a, es10.2)', 'largest error: ', maxval(abs(u - exact))

  ! The same matrix as a dense matrix, to check the residual with matmul
  block
     real :: a(n-2, n-2)
     integer :: i
     a = 0.0
     do i = 1, n - 2
        a(i, i) = 2.0 / h**2
        if (i > 1) a(i, i-1) = -1.0 / h**2
        if (i < n - 2) a(i, i+1) = -1.0 / h**2
     end do
     print '(a, es10.2)', 'largest residual |A u - 1|: ', maxval(abs(matmul(a, u(2:n-1)) - 1.0))
  end block
end program jacobi

It prints:

text
converged after 224 sweeps
    x      u(x)     exact
 0.00   0.00000   0.00000
 0.20   0.08000   0.08000
 0.40   0.12000   0.12000
 0.60   0.12000   0.12000
 0.80   0.08000   0.08000
 1.00   0.00000   0.00000
largest error:   1.71E-06
largest residual |A u - 1|:   1.84E-05

The whole update is one line: unew(2:n-1) = 0.5 * (u(1:n-2) + u(3:n) + h**2 * 1.0). The section u(1:n-2) is "the left neighbour of every interior point" and u(3:n) is "the right neighbour", so the line is the formula itself, applied to all points at once. The result matches the exact solution to within the convergence tolerance (for this quadratic solution, the finite-difference formula itself is exact). The program then checks the answer a second way, by building the same system as a dense matrix inside a block and computing the residual with the intrinsic matmul. Jacobi iteration is slow to converge on fine grids, and real codes use faster methods such as multigrid or conjugate gradients, but they are built from exactly these array operations.

ExplanationH

Arrays in professional code

Know the intrinsic library. Before writing a loop, check whether an intrinsic already does the job. Besides the reductions above, these come up constantly:

IntrinsicWhat it does
matmul(a, b), dot_product(u, v), transpose(a)matrix product, dot product, transpose
norm2(v)Euclidean norm, which compilers compute so as to avoid needless overflow (Fortran 2008)
findloc(a, value)position of the first element equal to value (Fortran 2008)
merge(t, f, mask)elementwise choice: t where mask is true, otherwise f
spread(v, dim, n)copies a vector n times along a new dimension
cshift(a, k), eoshift(a, k)circular and end-off shifts, useful for periodic and fixed boundaries
minval, minloc, product, count, any, allreductions, all accepting dim= and mask=

For serious linear algebra, though, professionals call BLAS and LAPACK (for example dgemm for matrix products and dgesv for linear systems) through optimised implementations such as OpenBLAS or Intel MKL, which are usually much faster than matmul for large matrices. GFortran can even replace large matmul calls with BLAS calls automatically when given -fexternal-blas.

Prefer allocatable to pointer. Fortran also has pointer arrays, which can point into other arrays or be allocated, but they can alias, leak and dangle, and they prevent some optimisations. Use allocatable for anything that owns data, and keep pointers for the rare cases that genuinely need them, such as linked data structures.

Mind contiguity. A section with a stride, such as t(1, :) (one row of a column-major matrix), is not contiguous in memory. Passing it to a procedure that expects contiguous data, especially an old FORTRAN 77 routine or a C function, may make the compiler copy it into a temporary and back, silently costing time and memory. Organise data so that the slices you pass around most are columns, and use the contiguous attribute (next lessons) when a procedure needs contiguous arrays.

Let the shape travel with the array. Old code passed arrays together with their dimensions, as in subroutine solve(a, n). Modern Fortran passes assumed-shape arrays, real, intent(in) :: a(:, :), whose shape is known inside the procedure through size and shape. The next lesson shows how, and why it needs a module or an explicit interface.

Common misconceptionsH

Misconceptions

Misconception

"Array syntax is slower than loops" (or faster). Both compile to similar machine code in simple cases. Array syntax can create hidden temporary arrays when the compiler cannot prove that the two sides do not overlap, and explicit loops can be clearer for complicated index patterns. Write whichever is clearer, and measure the hot spots.

Misconception

"Out-of-bounds indices cause an error." Not by default. Without -fcheck=bounds (or -fcheck=all), writing rain(13) silently reads or corrupts whatever is next in memory. Always develop and test with bounds checking on.

Misconception

"Arrays start at 1, so a(0) is a typo." They start at 1 by default. An array declared a(0:n) has a perfectly valid a(0). When you pass an array to a procedure, its lower bound inside the procedure becomes 1 again unless you declare otherwise, which the next lesson discusses.

Exercises

Exercises

1. Arrays, Fortran's greatest strength

CoreComputational

With a = [(i, i = 1, 10)], what does print '(*(i0, 1x))', a(2:9:3) print? (Give the numbers separated by single spaces.)

2. Arrays, Fortran's greatest strength

CoreComputational

With m = reshape([1, 2, 3, 4, 5, 6], [2, 3]), what is m(2, 3)?

3. Arrays, Fortran's greatest strength

CoreComputational

With a = [(i, i = 1, 10)], what is sum(a, mask = mod(a, 2) == 0)?

4. Arrays, Fortran's greatest strength

Warm-upComputational

With a = [(i, i = 1, 10)], what does print '(*(i0, 1x))', pack(a, a > 7) print?

5. Arrays, Fortran's greatest strength

StretchComputational

What does maxloc([3, 9, 2, 9]) return? (It returns a one-element array; give that element.)

6. Arrays, Fortran's greatest strength

CoreComputational

For integer :: b(0:4), what does print '(i0, 1x, i0)', size(b), lbound(b, 1) print?

7. Arrays, Fortran's greatest strength

Warm-upComputational

With m = reshape([1, 2, 3, 4, 5, 6], [2, 3]), what does print '(*(i0, 1x))', m(:, 2) print?

8. Arrays, Fortran's greatest strength

CoreHuman

You must visit every element of a large matrix t(1000, 1000). Which loop nest walks through memory in order?

Choose one

Further reading and resources