DO CONCURRENT

Contents
Loop without order
Locality specs
Reductions
Offload note (conceptual)

Loop without order

A do concurrent loop states that iterations have no required order, so the compiler may run them in any sequence or in parallel.

Use it only when iterations are truly independent: each step must write its own element and never depend on a neighbor step.

The loop below doubles every element, and any iteration order gives the same array.

program demo implicit none integer :: i real :: a(100) a = 1.0 do concurrent (i = 1:100) a(i) = 2.0 * a(i) end do print *, a(1), a(100) end program demo

Locality specs

Locality specs declare which variables are private to each iteration and which are shared across all of them.

A temporary scratch variable belongs in local, while the array being computed stays shared by default.

The example gives every iteration its own copy of t, so parallel steps cannot overwrite each other scratch value.

program demo implicit none integer :: i real :: a(100), t a = 0.0 do concurrent (i = 1:100) local(t) t = 0.5 * i a(i) = t * t end do print *, a(100) end program demo

Reductions

A reduction combines one value from every iteration with an operator such as plus, and the reduce clause makes that intent explicit.

Each iteration works on a private partial result, and the compiler merges the parts safely at the end of the loop.

The loop below sums one hundred elements without any manual synchronization.

program demo implicit none integer :: i real :: a(100), s a = 1.0 s = 0.0 do concurrent (i = 1:100) reduce(+:s) s = s + a(i) end do print *, s end program demo

Offload note (conceptual)

Some compilers can offload do concurrent loops to a graphics processor, but that ability depends on the compiler and its flags.

Treat offload as a bonus rather than a promise: write a correct order-free loop first, then study the compiler manual for the offload option.

The conceptual build below shows where such a flag would appear, with no claim about any specific machine in this lesson.

gfortran -O2 demo.f90 -o demo

build ok, run on the host CPU

Next: Coarrays

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: