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.

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 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.

CMake

OpenCoarrays includes CMake scripts. They are a little dated intheir syntax; here’s what I currently use in my CMakeLists.txt using OpenCoarrays:

find_package(OpenCoarrays)
  
if(OpenCoarrays_FOUND)
  list(APPEND OpenCoarrays_LIBRARIES caf_mpi)
  add_compile_options(-fcoarray=lib)
else()
  add_compile_options(-fcoarray=single)
endif()

...

target_link_libraries(myexe PRIVATE ${OpenCoarrays_LIBRARIES})

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 under coarray/. This includes a CMakeLists.txt file for coarrays.

Notes

Leave a Comment