Fortran

Guide To Learn

Collective subroutines syntax

Fortran 2018 defines a total of five collective subroutines:

  • co_broadcast–Sends the value of a variable from the current image to all others
  • co_max–Computes the maximum value of a variable over all images
  • co_min–Computes the minimum value of a variable over all images
  • co_sum–Computes the sum of all values of a variable across all images
  • co_reduce–Applies a reduction function across all images

These cover most collective operations that you’ll likely encounter in your work. However, the language won’t stop you from implementing your own custom collectives using coarrays and synchronization, should you ever need them. The rest of this section describes co_sum and co_broadcast in more detail. To learn more about co_reduce, the most complex collective subroutine, see section 12.7 for reference.

Note If you’re familiar with parallel programming using MPI, Fortran 2018 collective subroutines will look familiar, as they’re analogs to their MPI counterparts.

Figure 12.4 illustrates how co_sum works when invoked on four images.

Figure 12.4 A collective sum invoked over four parallel images, with arrows indicating possible data flow directions

In this example, we invoke co_sum(a) on each image, which triggers a summation of values of a across all images. The exact data exchange pattern may vary depending on compilers and underlying libraries, but the point is that you can use this built-in subroutine and not worry about explicitly copying data via coarrays and synchronizing images to avoid race conditions. By default, the result of the collective sum is made available on all images, and the value of a is updated on each image to the global sum value. However, if you need this value on only one image, you can specify it as an argument; for example, call co_sum(a, 3) would compute a sum over all images but update the value of a only on image 3.

The full syntax for invoking co_sum is

call co_sum(a[, result_image, stat, errmsg])

where

  • a is a variable that has the same type across all images. It doesn’t need to be declared as a coarray. This is an intent(in out) argument, so its value may be modified in-place.
  • result_image is an optional integer scalar indicating on which image to store the result. If omitted, the result is stored on all images.
  • stat and errmsg, both optional, are scalar integer and character variables, respectively. They have the same meaning as in allocate and deallocate statements, and allow for explicit error handling.

As you might guess, co_sumco_min, and co_max are implemented for numeric types only (integerreal, and complex).

Exercise 3: Calculating the global average of water height

In almost all applications of computational fluid dynamics, it’s an important property of the simulation code to conserve fundamental physical properties, such as mass and energy. In this exercise, do the following:

  1. Use the collective subroutine co_sum to calculate the global average of the water height.
  2. Print the mean water height value to the screen, like we did for the minimum and maximum value.
  3. Confirm that the tsunami simulator conserves mass by making sure that the average water height (and thus the total water mass) stays constant throughout the simulation.

The solution to this exercise is given in the “Answer key” section near the end of the chapter.

Collective subroutines syntax

Leave a Reply

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

Scroll to top