FORALL
forall is the construct that writes one assignment for a set of index values instead of a loop. It looks like a do loop, but it is not one: there is no order between the iterations, and every right-hand side sees the array as it was before the statement. The construct is also obsolescent in Fortran 2018, and the compiler says so. This lesson writes it anyway, because old code is full of it and because the warning is a lesson in itself.
The FORALL statement
The header holds the index and its range, like the header of a do loop; the body holds one assignment, and it may be a whole-array assignment. The block form ends with end forall; the single-line form fits on one line, as the second statement of the program does.
The file forall.f90:
program forall_demo
implicit none
integer, parameter :: n = 5
integer :: a(n), i
a = 0
forall (i = 1:n)
a(i) = i * i
end forall
print *, "squares :", a
forall (i = 1:n, mod(i, 2) == 1) a(i) = -a(i)
print *, "signed :", a
end program forall_demo
gfortran -Wall -Wextra -std=f2018 forall.f90 -o forall
./forall
forall.f90:7:18:
7 | forall (i = 1:n)
| 1
Warning: Fortran 2018 obsolescent feature: FORALL construct at (1)
forall.f90:12:47:
12 | forall (i = 1:n, mod(i, 2) == 1) a(i) = -a(i)
| 1
Warning: Fortran 2018 obsolescent feature: FORALL construct at (1)
squares : 1 4 9 16 25
signed : -1 4 -9 16 -25
Both warnings come from the compiler, not from a mistake on the page: Fortran 2018 lists FORALL as obsolescent, and -Wall is enough to hear about it. The output follows the warning because the two streams of the build are shown together here; the warning itself appears with or without -Wall, because gfortran reports an obsolescent feature by default.
A mask in FORALL
The header takes a mask after the index range, separated by a comma: forall (i = 1:n, mod(i, 2) == 1) assigns only for the odd indices. The mask is a scalar logical for each value of the index, which is the difference from the array mask of the WHERE lesson: there the mask was an array of the same shape, here it is a condition on the index itself.
That is why a FORALL can fill positions no array of the same shape could name: the mask tests the index, so forall (i = 2:n, mod(i, 2) == 0) a(i) = 0 selects every even position, and the same header with two indices selects cells of a grid.
A matrix by index control
Two indices with their own ranges give a rectangular set of cells, and the mask can be a condition on both. The program below fills a matrix with zeros, puts ones on the diagonal, and reads the diagonal back with an implied DO, which is the array constructor of the array lesson.
The file diagonal.f90:
program diagonal_demo
implicit none
integer, parameter :: n = 4
integer :: m(n, n), i, j
forall (i = 1:n, j = 1:n)
m(i, j) = 0
end forall
forall (i = 1:n) m(i, i) = 1
print *, "m :", m
print *, "trace :", sum([(m(i, i), i = 1, n)])
end program diagonal_demo
gfortran -Wall -Wextra -std=f2018 diagonal.f90 -o diagonal
./diagonal
m : 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
trace : 4
A matrix of zeros needs no mask at all: m = 0 would do it, and the FORALL is there to show the two-index header. The sixteen values print in memory order, column by column, so the ones sit every fifth value: the diagonal of this fill is visible in the flat output.
Assignments from copies
The standard defines a FORALL as if all its assignments were made at once, each from the array as it stood before the statement. A do loop over the same assignment reads what it has already written, and the two differ as soon as an iteration reads an element an earlier one changed.
The file copy.f90 shifts an array right in both ways:
program copy_demo
implicit none
integer :: a(5), b(5), i
a = [1, 2, 3, 4, 5]
b = a
forall (i = 2:5) a(i) = a(i - 1)
do i = 2, 5
b(i) = b(i - 1)
end do
print *, "forall :", a
print *, "do :", b
end program copy_demo
gfortran -Wall -Wextra -std=f2018 copy.f90 -o copy
./copy
forall : 1 1 2 3 4
do : 1 1 1 1 1
The FORALL shifted the array by one place: element two got the old first element, element three the old second, and so on, which is the result a section assignment a(2:5) = a(1:4) gives. The loop fills the rest with the new first element, because every iteration reads the value its predecessor wrote.
Why it is rarely needed
Every FORALL over an array can be written as an array assignment, a WHERE, a section or a do loop, and the standard deprecates it: it was added in Fortran 95 to give parallel machines a loop-like construct with no order, and do concurrent does the same job with explicit locality and without the warning. The rule for new code is to write the whole-array form, and do concurrent when a loop is genuinely needed; the rule for old code is to read FORALL as "a loop that may run in any order, from copies".
The construct is still worth knowing for three reasons: it appears in numerical libraries written between 1995 and 2010, its mask over indices is hard to write any other way in one line, and the warning teaches that a compiler can mark a whole feature obsolescent without removing it, so the code still builds and still runs.
Exercises
- Replace the FORALL of forall.f90 with a = [(i * i, i = 1, n)]. Does the warning go away, and is the output the same?
- Fill only the upper triangle of the matrix with a FORALL whose mask is j >= i. Count the cells it wrote.
- Write the shift of copy.f90 as a(2:5) = a(1:4) and compare the output with the FORALL line.
- Compile copy.f90 with -std=f2018 and then with -std=legacy. Which one is quiet, and why?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
forall (i = 1:n) a(i) = i * ido?Answer
Assigns the squares to every element, with no order between the assignments
FORALL writes one assignment for each value of the index set.
-
What does gfortran say about a FORALL with
-Wall -std=f2018?Answer
Fortran 2018 obsolescent feature: FORALL construct
The construct is obsolescent, so it warns and still compiles.
-
What is the mask in
forall (i = 1:n, mod(i, 2) == 1) a(i) = 0?Answer
A condition on the index, tested for each value of i
The mask of a FORALL is a scalar logical evaluated for each index value.
-
What does this program print?
program p implicit none integer :: a(4), i a = 1 forall (i = 2:4) a(i) = a(i - 1) + 1 print *, a end program pAnswer
1 2 2 2
Every right side reads the original array, where all four elements are 1, so each written element becomes 2.
-
How does a DO loop of the same assignment differ from the FORALL?
Answer
The loop reads what it has already written, the FORALL reads the original array
FORALL is defined on copies; a loop walks the array as it stands.
-
Which construct does Fortran 2018 offer for a loop with no order between its iterations?
Answer
DO CONCURRENT
DO CONCURRENT is the current form; FORALL is obsolescent.
-
How many assignments may the body of a FORALL block hold?
Answer
One assignment statement
The body of a FORALL is a single assignment, possibly a whole-array one.
-
What does
forall (i = 1:n, j = 1:n) m(i, j) = 0select?Answer
Every cell of the matrix
Two index ranges with no mask cover all their combinations.
-
Why does the course not use FORALL for new code?
Answer
It is obsolescent, and an array assignment or DO CONCURRENT says the same thing
The standard deprecates it in favour of array syntax and DO CONCURRENT.
-
What is
sum([(m(i, i), i = 1, n)])for a four by four matrix?Answer
The trace of the matrix
The implied DO builds the diagonal as a vector, and sum adds it.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev