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, Cray and Intel are among the compilers supporting Fortran 2018 coarray features so useful in high-performance computing.


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

  • Intel ifort
  • Cray ftn

The OpenCoarrays library is popular for enabling coarray features in compilers including:

  • GNU 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 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

ifort Fortran standard
support includes:

  • Fortran 2008
  • growing Fortran 2018 (including coarray) support
ifort -coarray

enables the coarray features.


OpenCoarrays includes CMake scripts. CMake itself can recognize generic coarray support. For those with older versions of CMake, use 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.




Written by Michael Hirsch, Ph.D. //