Coarrays

Contents
Images and this_image
Coarray syntax
SYNC ALL
Run on images

Images and this_image

A coarray program runs as several copies called images, and each image executes the same code with its own number.

The this_image function returns the number of the current image, while num_images returns the total count.

The program below greets the user once per image with its own number attached.

program demo implicit none print *, "hello from image", this_image() print *, "of", num_images() end program demo

Coarray syntax

A coarray declaration adds square brackets to a normal declaration, giving every image its own copy of the variable.

An undecorated name means the local copy, while a name with brackets in square form reads the copy on another image.

Here each image stores its own number and image one collects the value from image two.

program demo implicit none integer :: me[*] me = this_image() sync all if (this_image() == 1) print *, me[2] end program demo

SYNC ALL

Images run at their own pace, so a sync all statement is the meeting point where every image waits for all the others.

Place it after remote writes and before remote reads, so no image reads data that a slower image has not stored yet.

The sketch writes a value for the neighbor, synchronizes, and only then reads the neighbor value.

program demo implicit none integer :: slot[*] slot = this_image() * 10 sync all if (this_image() == 1) print *, slot[num_images()] sync all end program demo

Run on images

A normal run starts a single image, while a coarray launcher starts several images of the same program at once.

The launcher name and flags depend on the compiler setup, so check local docs before running multi-image jobs on a cluster.

The text below shows the idea with four images and the output it would produce.

cafrun -n 4 ./a.out

hello from image 1 of 4

Next: OpenMP basics

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: