Research Article

Using Coarrays to Parallelize Legacy Fortran Applications: Strategy and Case Study

Listing 5

Using MPI_ALLGATHER to collect local arrays into a global array.
integer  :: my_rank, num_procs
integer, allocatable, dimension(:)  :: &
   my_first, my_last, counts, displs
call mpi_comm_size(MPI_COMM_WORLD, num_procs, ierr)
call mpi_comm_rank(MPI_COMM_WORLD, my_rank, ierr)
allocate(my_first(num_procs), my_last(num_procs), &
   counts(num_procs), displs(num_procs))
my_first(my_rank + 1) = lbound(sn, 2)
my_last(my_rank + 1) = ubound(sn, 2)
call mpi_allgather(MPI_IN_PLACE, 1, MPI_INTEGER, &
 my_first, 1, MPI_INTEGER, MPI_COMM_WORLD, ierr)
call mpi_allgather(MPI_IN_PLACE, 1, MPI_INTEGER, &
 my_last, 1, MPI_INTEGER, MPI_COMM_WORLD, ierr)
do i = 1, num_procs
 displs(i) = my_first(i) − 1
 counts(i) = my_last(i) − my_first(i) + 1
end do
call mpi_allgatherv(sn, 5 * counts(my_rank + 1), &
 MPI_DOUBLE_PRECISION, sn_global, 5 * counts, &
 5 * displs, MPI_DOUBLE_PRECISION, MPI_COMM_WORLD, ierr)
call mpi_allgatherv(cr, 5 * counts(my_rank + 1), &
 MPI_DOUBLE_PRECISION, cr_global, 5 * counts, &
 5 * displs, MPI_DOUBLE_PRECISION, MPI_COMM_WORLD, ierr)