Sharing Data Across Fortran Images with Coarrays
Coarray Fortran lets program images share memory directly through square-bracket syntax, with explicit synchronization required between parallel reads and writes.
29 Dec 2025, 01:52 UTC

The problem: parallel Fortran without message-passing boilerplate
You have a numerical Fortran code that would run faster on multiple cores, but adding MPI means rewriting data movement as explicit sends and receives. Coarray Fortran offers a different model: images—separate program instances—can read and write each other's variables directly through coarray syntax, while the runtime handles the underlying communication.
What a coarray is
A coarray is an array with an extra trailing dimension written in square brackets, such as integer, dimension[*] :: value. Each image gets its own storage for every coarray element, and image N can address image M's copy as value[M]. The intrinsic this_image() returns the current image number, and num_images() returns the total count.
Because images run concurrently, writes by one image are not visible to others until you issue a synchronization. The most common is sync all, a barrier that waits until every image reaches it.
Worked example: summing image indices
This program allocates one coarray slot per image, writes the local image number into it, synchronizes, then lets image 1 add every slot together.
program coarray_sum
implicit none
integer, dimension[*], allocatable :: slot
integer :: me, images, i, total
me = this_image()
images = num_images()
allocate(slot[*])
slot[me] = me
sync all
if (me == 1) then
total = 0
do i = 1, images
total = total + slot[i]
end do
print *, 'Sum of image indices:', total
end if
deallocate(slot)
end program coarray_sum
Save this as coarray_sum.f90. Compile with a compiler that supports coarrays, for example GNU Fortran:
gfortran -fcoarray=lib -o coarray_sum coarray_sum.f90
Run from a normal user shell; no administrative permissions are required. Depending on your OpenCoarrays setup, launch multiple images with a runtime wrapper such as cafrun -n 4 ./coarray_sum, or consult your compiler's documentation if it launches images directly from ./coarray_sum.
Expected result: image 1 prints Sum of image indices: 10 when run with four images. If image 1 prints zero or an inconsistent value, a missing sync all or a runtime that started only one image is the likely cause.
Compilation checks and portability
gfortran -fcoarray=single -c coarray_sum.f90verifies that the compiler accepts coarray syntax in single-image mode.- If compilation fails with an unrecognized flag or coarray error, your compiler version is too old or lacks coarray support.
- Run on a multi-core machine and confirm output comes from more than one image to validate actual parallel execution.
Trade-offs and limitations
Coarrays remove explicit message passing, but they do not remove concurrency bugs. Forgetting sync all creates race conditions that are hard to reproduce. Compiler support is uneven: older gfortran releases and some proprietary toolchains implement coarrays incompletely or require separate runtime libraries. Code that depends heavily on coarrays is also less portable to environments where the Fortran compiler cannot build the coarray runtime.
What to do next
Start with a small, compute-independent problem where each image works on distinct data and only combines results at the end. Add sync all before any image reads another image's coarray, test with two images first, then scale up. If your target environment cannot guarantee a coarray-capable compiler, keep an MPI fallback or isolate the coarray code behind a feature check.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.