Page MenuHomec4science

pi_p2p_sendrecv_ring.f90
No OneTemporary

File Metadata

Created
Sun, Nov 17, 07:33

pi_p2p_sendrecv_ring.f90

! ==========================================================================
! This exercise is taken from the class Parallel Programming Workshop (MPI,
! OpenMP and Advanced Topics) at HLRS given by Rolf Rabenseifner
! ==========================================================================
program pi_p2p_sendrecv_ring
use mpi
implicit none
integer, parameter :: n = 10000000
double precision :: omp_t1, omp_t2
double precision :: dx, x, sm, pif
double precision, external :: f
integer :: i
integer :: ierror, prank, psize
integer :: nlocal, istart, iend
integer :: next, prev
double precision :: send, recv
call MPI_init(ierror)
call MPI_Comm_size(MPI_COMM_WORLD, psize, ierror)
call MPI_Comm_rank(MPI_COMM_WORLD, prank, ierror)
! calculate pi = integral [0..1] 4 / (1 + x**2) dx
omp_t1 = MPI_Wtime()
nlocal = n / psize
istart = 1 + nlocal * prank
iend = nlocal * (prank + 1);
dx = 1. / n
sm = 0.0;
do i = istart, iend
x = (1. * i - 0.5) * dx
sm = sm + f(x)
enddo
next = mod((prank + 1),psize)
prev = mod((prank - 1 + psize), psize)
send = sm
do i = 2, psize
call MPI_Sendrecv(send, 1, MPI_DOUBLE, next, 13, recv, 1, MPI_DOUBLE, prev, 13, MPI_COMM_WORLD, MPI_STATUS_IGNORE, ierror);
sm = sm + recv
send = recv
enddo
pif = dx * sm
omp_t2 = MPI_Wtime()
if (prank.eq.0) then
print *, 'computed pi =',pif
print *, 'Running time = ', (omp_t2-omp_t1)
endif
call MPI_Finalize(ierror)
end program pi_p2p_sendrecv_ring
double precision function f(a)
implicit none
double precision, intent(in) :: a
f = 4. / (1. + (a**2))
end function f

Event Timeline