What have you tried so far?
For this specific problem (parallel sum), you don’t need coarrays. You can work with regular arrays and use Fortran 2018 collective subroutines. Here are a few hints:
- Declare array
xsuch that each image has its portion of the domain. To do this you’ll need to make the arrayallocatable, and then allocate it on each image with start and end indices that depend on the values ofthis_image()andnum_images(). Edit: start and end indices on each image don’t need to be different across images, the local arrays only need to have the same shape. And if you plan to work with coarrays, they will need to have the same extent (start and end indices) across images. - Assign meaningful values to
xon each image. - Use the
co_sumintrinsic subroutine to do the parallel reduction (sum). Careful withco_sumand other collective subroutines as they update the first argument in place on all images by default, or on a specified image if you use theresult_imageoptional argument. See co_sum docs by GFortran. - Now you have a section of your array summed across images, and you can use the local
sumover that section to get the ultimate sum of the distributed array.