Fortran

Guide To Learn

Computing the minimum and maximum of distributed arrays

Let’s try this out in the tsunami simulator. In our working version of the simulator so far, for every time step, we were reporting the time step count to the screen, while the program was writing raw data into files in the background:

program tsunami
  ...                                      ❶
  time_loop: do n = 1, num_time_steps      ❷
    if (this_image() == 1) &               ❸
      print *, 'Computing time step', &    ❸
               n, '/', num_time_steps      ❸
    ...                                    ❹
  end do time_loop                         ❺
 
end program tsunami

❶ Initialization part of the program

❷ Iterates through simulation time steps

❸ If image 1, reports time step count to the screen

❹ The simulation calculation goes here.

❺ End of the simulation time loop

At the beginning of each time step, we print the current time step count and the total number of time steps to the screen. We do this only from one image to avoid printing the same message from all images. Let’s augment this short report by adding the minimum, maximum, and average water height value to each print statement. Like in the thought experiment of a parallel climate model, the water height values here are also distributed across parallel images. The following listing shows how we’d calculate global minimum and maximum values using standard collectives co_min and co_max, respectively.

Listing 12.7 Calculating global minimum and maximum values of the water height array

...
real(ik) :: hmin, hmax                                   ❶
...
time_loop: do n = 1, num_time_steps
  ...
  hmin = minval(h % data)                                ❷
  call co_min(hmin, 1)                                   ❸
 
  hmax = maxval(h % data)                                ❹
  call co_max(hmax, 1)                                   ❺
 
  if (this_image() == 1) print '(a, i5, 2(f10.6))', &    ❻
    'step, min(h), max(h):', n, hmin, hmax               ❻
 
end do time_loop

❶ Declares temporary variables

❷ Calculates the local minimum on each image

❸ Calculates the collective minimum from hmin on each image and stores it into hmin on image 1

❹ Calculates the local maximum on each image

❺ Calculates the collective maximum from hmax on each image and stores it into hmax on image 1

❻ Prints the current time step and global minimum and maximum to the screen

To compute the global minimum of water height, we first calculate the local minimum on each image using the minval function and store it into the temporary variable hmin. Recall that h is a type(Field) instance, so we access the raw values through its component h % data. Second, we use the collective subroutine co_min to calculate the minimum value of hmin across all images. The first argument to co_min is an intent(in out) scalar, and the second argument (optional) is the number of the image on which to store the result. In this case, all images invoke co_min, and only the value of hmin on image 1 is modified in-place. If the image number were not specified (call co_min(hmin)), the value of hmin would be updated in-place on all images. This implies that invoking the collective subroutine will inevitably overwrite the value of the input on at least one image.

We repeat the same procedure to compute the global maximum using co_max. Finally, we report the current time step and minimum and maximum values to the screen using a modified print statement. Here’s the sample output:

step, min(h), max(h):    1  0.000000  1.000000
step, min(h), max(h):    2  0.000000  0.996691
step, min(h), max(h):    3  0.000000  0.990097
...
step, min(h), max(h):  998 -0.072596  0.186842
step, min(h), max(h):  999 -0.072279  0.188818
step, min(h), max(h): 1000 -0.071815  0.190565

This was an introduction to the co_min and co_max subroutines by example. In the next section, I’ll describe the rest of the collectives and provide their general syntax.

Note Collective subroutines are built into the language and are available out of the box, just like the regular functions minmax, and sum.

Computing the minimum and maximum of distributed arrays

Leave a Reply

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

Scroll to top