Channel Synchronization in Co-array Fortran
Our first example demonstrates channel synchronization. We can use channels to synchronize execution across co-images. Here’s an example of using a blocking receive to wait for a co-image to finish. When waiting for multiple co-images to finish, you may prefer to use a sync statement.
To run the program:
In this Co-array Fortran version:
We use a co-array variable
done
to synchronize between co-images, similar to the channel in the original example.The
worker
subroutine is executed on the second co-image (image 2).The main program (image 1) waits for the
done
variable to be set, which is analogous to receiving from the channel.We use
sync all
andsync memory
statements for synchronization between co-images.The program is executed with two co-images using the
cafrun
command.
If you removed the waiting loop from this program, the program might exit before the worker even started or finished its work.
Co-array Fortran doesn’t have built-in sleep functionality, so you might need to implement a sleep routine or use a vendor-specific one. The sleep
call in this example is just for illustration.
This example demonstrates basic co-image synchronization in Co-array Fortran, which is conceptually similar to goroutine synchronization using channels in the original example.