Fortran 2018 coarray distributed processing quick start

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.

Install

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

Gfortran

Install Fortran coarray library for Gfortran by:

  • Ubuntu ≥ 17.04: apt install libcoarrays-dev libopenmpi-dev
  • Ubuntu ≤ 16.10: using Linuxbrew: brew install gcc opencoarrays
  • Debian Stretch 9 (e.g. Raspberry Pi): apt install libcoarrays-dev libopenmpi-dev
  • Mac OS homebrew: brew install opencoarrays

Intel Fortran

Parts of Fortran 2008 and Fortran 2018 standards are supported by ifort. co_sum, co_broadcast and similar from TS18508 are missing from Intel Parallel Studio 2018.

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
enddo

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

end program

Examples

Fortran coarray examples are coarray_*.f90.

Notes

Leave a Comment