Nested Complex Do Loops (without information exchange between arrays)
! mpif90 scatter_gather_2d.f90
! mpirun -np 2 ./a.out
PROGRAM main
implicit none
INCLUDE 'mpif.h'
integer ( kind = 4 ), parameter :: nx = 3
integer ( kind = 4 ), parameter :: ny = 4
integer ( kind = 4 ) i, j
integer ( kind = 4 ) root, ny_loc, dim_array
INTEGER ( kind = 4 ) NPROCS, MYRANK, IERR !MPI
complex, dimension(nx,ny) :: in, out
complex, allocatable :: loc(:,:)
! INITIALIZE MPI
CALL MPI_INIT(IERR)
CALL MPI_COMM_SIZE(MPI_COMM_WORLD, NPROCS, IERR)
CALL MPI_COMM_RANK(MPI_COMM_WORLD, MYRANK, IERR)
root = 0
! ny has to be an integer multiple of ny_loc.
ny_loc = ny / NPROCS
allocate(loc(nx,ny_loc))
dim_array = nx*ny_loc
! input data
if (MYRANK == root) then
do j = 1, ny
do i = 1, nx
in(i,j) = (1.0d0,1.0d0)
print*, i,j,in(i,j),"IN"
enddo
enddo
endif
! DISTRIBUTE AMONG MEMORY
CALL MPI_SCATTER( in, dim_array, MPI_complex, loc, dim_array, MPI_complex, root, &
MPI_COMM_WORLD, IERR )
! evaluate task
print*,
do j = 1,ny_loc
do i = 1,nx
loc(i,j) = loc(i,j) + (1.0d0,1.0d0)
print*, i,j+ny_loc*MYRANK,loc(i,j),MYRANK
enddo
enddo
print*,
! COLLECT FROM MEMORY
CALL MPI_GATHER( loc, dim_array, MPI_complex, out, dim_array, MPI_complex, root, &
MPI_COMM_WORLD, IERR )
! output data
if (MYRANK == root) then
do j = 1, ny
do i = 1, nx
print*, i,j,out(i,j),"OUT"
enddo
enddo
endif
! FINALIZE MPI
CALL MPI_FINALIZE(IERR)
END