Fortran 2018 coarray distributed processing quick start

1 minute read

Fortran coarrays are an abstract, higher-level construct than using OpenMPI directly. Coarrays support multiple parallel processing libraries including OpenMPI. Much simpler Coarray vs. OpenMPI syntax makes debugging easier in general. Gfortran has the best/widest support for Fortran 2018, particularly the Coarray features so useful in high-performance computing.


Coarray Fortran as enabled in Fortran 2008 / Fortran 2018 standards is available from multiple compilers.


Install Fortran coarray library for Gfortran by:

  • Ubuntu 18.04 / Debian Stretch 9 / Raspberry Pi: apt install libcoarrays-dev libopenmpi-dev open-coarrays-bin
  • Ubuntu 16.04: using Linuxbrew: brew install gcc opencoarrays
  • Mac OS homebrew: brew install opencoarrays

When compiling, add -fcoarray=lib and -lcaf_mpi. For example:

gfortran -fcoarray=lib myfun.f90 -lcaf_mpi

See examples

Intel Fortran

ifort Fortran standard
support includes:

  • Fortran 2008
  • parts of Fortran 2018

co_sum, co_broadcast and similar from TS18508 are missing from Intel Parallel Studio 18.

ifort -coarray

enables the coarray features.

Replace missing TS18508 features

Minimal working coarray example has a co_sum() replacement.

program cosubreplace
use, intrinsic:: iso_fortran_env, only: dp=>real64
implicit none

real(dp) :: psum[*]  ! [*] declares a coarray, here a scalar
integer :: i
integer, parameter :: N=100 ! arbitrary for your code

! --- example do loop

do i=this_image(), N, num_images()
    psum = psum + i**2  ! trivial example

! ----- alternative workaround for fortran 2018 co_sum()
sync all
if (this_image()==1) then
  do i = 2, num_images()
    psum = psum + psum[i]
!------ end co_sum() workaround

end program


Fortran coarray examples are under coarray/. This includes a CMakeLists.txt file for coarrays.


Leave a Comment