Fortran 2018 coarray quick start

Fortran coarrays are an abstract, higher-level construct than using MPI directly. Coarrays support multiple parallel processing libraries including MPI. The simpler Coarray syntax can make debugging easier in general. Gfortran, NAG, Intel oneAPI and Cray are among the compilers supporting Fortran 2018 coarray features so useful in high-performance computing.

Coarray Fortran as enabled in Fortran standard is available from multiple compilers. Compilers with built-in coarray support (not needing external libraries) include:

  • Intel ifx
  • NAG nagfor
  • Cray ftn

The OpenCoarrays library is popular for enabling coarray features in GFortran. Install Fortran OpenCoarrays library for Gfortran by:

Ubuntu / Debian / Raspberry Pi (packages names may vary by Linux distro version)

apt install gfortran libcoarrays-dev libcoarrays-openmpi-dev

macOS: using Homebrew

brew install gcc opencoarrays

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

gfortran -fcoarray=lib myfun.f90 -lcaf_mpi

When manually running use cafrun like:

cafrun -np 4 ./myprog

Intel Fortran standard support includes coarrays.

ifx -coarray

enables the coarray features.

OpenCoarrays includes CMake scripts. CMake itself can recognize generic coarray support with FindCoarray.cmake

An example top-level CMakeLists.txt using Fortran coarray contains:

find_package(Coarray REQUIRED)

add_executable(coarray_pi pi.f90)
target_compile_options(coarray_pi PRIVATE ${Coarray_COMPILE_OPTIONS})
target_link_libraries(coarray_pi ${Coarray_LIBRARIES})

add_test(NAME CoarrayPi
COMMAND ${Coarray_EXECUTABLE} ${Coarray_NUMPROC_FLAG} ${Coarray_MAX_NUMPROCS} ./coarray_pi)

Fortran coarray examples includes CMakeLists.txt for Fortran coarrays.