Fortran

Guide To Learn

Controlling the order of image execution

When executing the program on multiple images, there’s no imposed order in which the images execute–they all run at their own pace and independently from each other.

Any program that requires an exchange of data between processors will also require synchronization at one or more times during the calculation. This is true of the tsunami simulator as well! If image 2 needs data from image 1, it’s important that image 1 send that data before it updates it with the solution in the next iteration. This is where Fortran’s sync statement comes in. sync is used to request synchronization between any or all parallel images. The most basic form of the sync statement is sync all–synchronize all images. sync all basically states that no image will go past this point until all other images have arrived (figure 7.8).

Figure 7.8 Using the sync all statement to make images wait for each other

For those of you who have experience with parallel programming with MPI, the sync all statement is equivalent to call mpi_barrier().

To illustrate using synchronization to enforce order, the following listing demonstrates the coarray “Hello, World!” program from before, but with a slight twist.

Listing 7.5 Synchronizing the images to enforce order

program hello_images_ordered
  implicit none
  integer :: n
  do n = 1, num_images()                                             ❶
    if (this_image() == n) &                                         ❷
      print *, 'Hello from image', this_image(), 'of', num_images()
    sync all                                                         ❸
  end do
end program hello_images_ordered

❶ Loops over the images in order

❷ If it’s my turn, print to the screen.

❸ Synchronizes with everybody before moving on

The output of this program will be the same as what you saw in subsection 7.3.2, except that here the images are guaranteed to report in ascending order.

Controlling the order of image execution

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top