Select in Co-array Fortran
Our example demonstrates the use of multiple image synchronization in Co-array Fortran. This is similar to selecting across multiple channels in other languages.
In this example, we use Co-array Fortran’s parallel execution model to simulate concurrent operations. Each image (similar to a goroutine) performs a different operation.
We define a co-array
msg
that can hold a string message.Depending on the image number, we simulate different operation times using the
sleep
subroutine and set different messages.We use
sync all
to ensure all images have completed their operations before proceeding.Finally, the first image checks and prints the messages from all images.
To run the program:
Note that the total execution time will be approximately 2 seconds, as both the 1 and 2 second sleep operations execute concurrently on different images.
This example demonstrates how Co-array Fortran can be used to perform concurrent operations and synchronize between different images, similar to selecting across multiple channels in other languages.