program main
use mpi
integer maxn
parameter (maxn = 1024+2)
double precision a(maxn,maxn), b(maxn,maxn), f(maxn,maxn)
integer nx, ny
integer myid, numprocs, ierr
integer comm1d, nbrbottom, nbrtop, s, e, it
double precision diff, diffnorm, dwork
double precision t1, t2
external diff
call MPI_INIT( ierr )
call MPI_COMM_RANK( MPI_COMM_WORLD, myid, ierr )
call MPI_COMM_SIZE( MPI_COMM_WORLD, numprocs, ierr )
if (myid .eq. 0) then
nx = 1024
endif
call MPI_BCAST(nx,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr)
ny = nx
call MPI_CART_CREATE( MPI_COMM_WORLD, 1, numprocs, .false., &
.true., comm1d, ierr )
call MPI_COMM_RANK( comm1d, myid, ierr )
call MPI_Cart_shift( comm1d, 0, 1, nbrbottom, nbrtop, ierr )
call MPE_DECOMP1D( ny, numprocs, myid, s, e )
call onedinit( a, b, f, nx, s, e )
call MPI_BARRIER( MPI_COMM_WORLD, ierr )
t1 = MPI_WTIME()
do it=1, 100
call exchng1( a, nx, s, e, comm1d, nbrbottom, nbrtop )
call sweep1d( a, f, nx, s, e, b )
call exchng1( b, nx, s, e, comm1d, nbrbottom, nbrtop )
call sweep1d( b, f, nx, s, e, a )
dwork = diff( a, b, nx, s, e )
call MPI_Allreduce( dwork, diffnorm, 1, MPI_DOUBLE_PRECISION, &
MPI_SUM, comm1d, ierr )
if (diffnorm .lt. 1.0e-5) exit
enddo
if (myid .eq. 0 .and. it .gt. 100) print *, 'Failed to converge'
t2 = MPI_WTIME()
if (myid .eq. 0) then
print *, 'Converged after ', 2*it, ' Iterations in ', t2 - t1, &
' secs '
endif
call MPI_FINALIZE(ierr)
end