DO CONCURRENT

do concurrent is a loop with a promise attached: the iterations do not depend on each other and the order they run in does not matter. The compiler may then run them in any order, vectorise them, or hand them to threads, which a plain do does not allow. This lesson states the promise, shows a body that keeps it, and shows the specifiers that give an iteration its own copy of a variable.

What the construct promises

The header looks like a counted loop: do concurrent (i = 1:n). The difference is a requirement on the body, and it is the program's job to keep it:

Break the promise and the program is wrong in a way the compiler usually cannot see: the answer depends on the order the iterations happened to run in, so it changes with the optimiser level.

A first DO CONCURRENT

The body below writes one element per iteration and reads only its own x(i). That is the shape almost every correct use has: an elementwise kernel over arrays.

The file concurrent.f90:

program concurrent implicit none integer, parameter :: n = 5 real :: x(n), y(n) integer :: i x = [(real(i), i = 1, n)] do concurrent (i = 1:n) y(i) = x(i) * x(i) end do print *, "x:", x print *, "y:", y end program concurrent

gfortran -Wall -Wextra -std=f2018 concurrent.f90 -o concurrent
./concurrent

x: 1.00000000 2.00000000 3.00000000 4.00000000 5.00000000
y: 1.00000000 4.00000000 9.00000000 16.0000000 25.0000000

The same body with a plain do gives the same output, and that is the point: the construct is about permission, not about syntax. A reader who writes it as a plain do loses the permission, and a compiler that vectorises the plain loop does so only after proving the same independence itself.

The rules a body must keep

The two mistakes that look harmless both break the promise. A running total written from inside the loop has every iteration fighting for the same variable, and the result depends on the order:

total = 0.0 do concurrent (i = 1:n) total = total + x(i) end do

A sum is a reduction, and a reduction needs the intrinsic: total = sum(x) is correct and the construct is not. The second mistake is a shift that reads a neighbour:

do concurrent (i = 2:n) y(i) = x(i) - x(i-1) end do

This one is legal: iteration i reads x(i-1) and x(i), and no iteration writes x. The line would be wrong only if the body also wrote x, which would make one iteration read what another wrote. That distinction, reads of the input against writes of it, is the whole rule.

Local and shared

A temporary that each iteration needs of its own is declared with a locality specifier after the loop control. local(t) gives every iteration an uninitialised copy of t, local_init(t) a copy with the value the variable had at entry, and shared(t) says that the variable is one for all iterations, which is the default for a variable declared outside.

The file locality.f90:

program locality implicit none integer, parameter :: n = 4 real :: a(n), b(n), t integer :: i a = [(real(i), i = 1, n)] do concurrent (i = 1:n) local(t) t = a(i) * 2.0 b(i) = t + 1.0 end do print *, "b:", b end program locality

gfortran -Wall -Wextra -std=f2018 locality.f90 -o locality
./locality

b: 3.00000000 5.00000000 7.00000000 9.00000000

Without local(t) the program would still print the same answer, because each iteration writes before it reads and the compiler ran the iterations one at a time. It would stop being correct the moment two iterations ran at once, and the declaration is what tells the compiler that it may.

What the compiler does with it

gfortran 15 accepts the construct and compiles it as a loop it is allowed to reorder and vectorise. It does not turn it into threads by itself: the option that would map the construct to OpenMP, present in some builds of GCC, is not recognised here:

gfortran -Wall -Wextra -std=f2018 -fdo-concurrent-to-openmp=host concurrent.f90 -o conc_omp

gfortran: error: unrecognized command-line option '-fdo-concurrent-to-openmp=host'

So the construct buys a promise and not a thread pool on this compiler. What it does buy is the guarantee that the loop may be vectorised without the compiler proving anything, and portability: a compiler that does parallelise it, or a future build with OpenMP behind it, needs no change to the source.

For the parallel version today the tool is OpenMP, which the OpenMP part covers: the same promise, written as a directive in front of a plain do, with the thread count under the program's control.

Exercises

  1. Replace do concurrent with a plain do in the first program. Does the output change?
  2. Write the sum of the array with a running total inside a do concurrent and run it twice. Why is the answer not something a program may rely on?
  3. Add local_init instead of local and give t a value before the loop. What does each iteration start from?
  4. Write the neighbour difference above as a do concurrent and explain why it is legal.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does DO CONCURRENT promise the compiler?

    Answer

    That the iterations are independent and may run in any order

    The construct is a permission, and the program must keep the independence it claims.

  2. Which procedure may a DO CONCURRENT body call?

    Answer

    A pure one

    A side effect would make the order of the iterations visible.

  3. Why is a running total inside DO CONCURRENT wrong?

    Answer

    Every iteration writes the same variable, so the result depends on the order

    A reduction needs the intrinsic sum, or a plain loop.

  4. What does local(t) give each iteration?

    Answer

    Its own uninitialised copy of t

    local is an uninitialised private copy; local_init copies the entry value.

  5. What does this program print? program p implicit none integer, parameter :: n = 3 integer :: i, y(n) do concurrent (i = 1:n) y(i) = 2 * i end do print *, y end program p

    Answer

    2 4 6

    Each iteration writes its own element, so the array is filled deterministically.

  6. Which statement is not allowed inside DO CONCURRENT?

    Answer

    EXIT

    Leaving the construct from one iteration would make the order meaningful.

  7. What does -fdo-concurrent-to-openmp=host do on this compiler?

    Answer

    It is not a recognised option, and gfortran reports that

    The option belongs to other GCC builds; here the construct stays sequential.

  8. Why is a neighbour read y(i) = x(i) - x(i-1) legal in DO CONCURRENT?

    Answer

    x is only read, and no iteration writes it

    Reads of input written by nobody are independent of the order.

  9. What does a plain DO give up compared with DO CONCURRENT?

    Answer

    The permission for the compiler to reorder and vectorise on the promise alone

    The compiler may still vectorise a plain loop, but it must prove independence first.

  10. Which tool parallelises a loop on this compiler today?

    Answer

    OpenMP, with a directive in front of a plain DO

    OpenMP is the parallel mechanism available in this toolchain.

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