Fortran 2018 defines a total of five collective subroutines:
co_broadcast–Sends the value of a variable from the current image to all othersco_max–Computes the maximum value of a variable over all imagesco_min–Computes the minimum value of a variable over all imagesco_sum–Computes the sum of all values of a variable across all imagesco_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])
ais a variable that has the same type across all images. It doesn’t need to be declared as a coarray. This is anintent(inout)argument, so its value may be modified in-place.result_imageis an optional integer scalar indicating on which image to store the result. If omitted, the result is stored on all images.statanderrmsg, both optional, are scalar integer and character variables, respectively. They have the same meaning as inallocateanddeallocatestatements, and allow for explicit error handling.
As you might guess, co_sum, co_min, and co_max are implemented for numeric types only (integer, real, 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:
- Use the collective subroutine
co_sumto calculate the global average of the water height. - Print the mean water height value to the screen, like we did for the minimum and maximum value.
- 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.