Coarrays & do concurrent

A program runs as multiple images (processes); coarray variables are accessible across images with [ ].

30.1 Coarrays — parallelism built into the language (F2008/2018)

A program runs as multiple images (processes); coarray variables are accessible across images with [ ].

Features: sync all, sync images(...), critical, lock/unlock, event post/wait, co_sum, co_min, co_max, co_broadcast, co_reduce, form team/change team (teams), failed-image handling, atomic_* subroutines.

program coarray_demo
  implicit none
  integer :: me, n
  real :: x[*]                        ! a coarray: one x per image
  real :: total

  me = this_image()
  n = num_images()
  x = real(me)

  sync all                            ! barrier
  if (me == 1) then
    total = 0.0
    block
      integer :: i
      do i = 1, n
        total = total + x[i]          ! remote read from image i
      end do
    end block
    print *, "Sum over", n, "images =", total
  end if

  ! Collective subroutines (F2018)
  call co_sum(x)                      ! every image now holds the sum
  if (me == 1) print *, "co_sum:", x
end program coarray_demo
# gfortran + OpenCoarrays
caf coarray_demo.f90 -o coarray_demo && cafrun -n 4 ./coarray_demo
# Intel: ifx -coarray=shared coarray_demo.f90 ; export FOR_COARRAY_NUM_IMAGES=4

30.2 do concurrent

Asserts that iterations are independent. Compilers can vectorize, multithread (-ftree-parallelize-loops=8 in gfortran, -qopenmp with ifx), or offload to GPUs (nvfortran -stdpar=gpu).

do concurrent (i = 1:n) local(tmp) shared(a, b)        ! F2018 locality specifiers
  tmp = b(i) * 2.0
  a(i) = sqrt(tmp)
end do

do concurrent (i = 1:n) reduce(+:s)                     ! F2023 reduce clause
  s = s + a(i)
end do