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 well supports 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. The OpenCoarrays library is popular for enabling coarray features.

Gfortran

Install Fortran OpenCoarrays 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
  • growing Fortran 2018 (including coarray) support

However, co_sum, co_broadcast and similar from TS18508 are missing from Intel Parallel Studio ≤ 2019.

ifort -coarray

enables the coarray features.

CMake

OpenCoarrays includes CMake scripts. An example top-level CMakeLists.txt:

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