Page MenuHomec4science

pi_reduction.f90
No OneTemporary

File Metadata

Created
Sun, Oct 13, 11:21

pi_reduction.f90

! ==========================================================================
! This exercise is taken from the class Parallel Programming Workshop (MPI,
! OpenMP and Advanced Topics) at HLRS given by Rolf Rabenseifner
! ==========================================================================
program pi_reduction
implicit none
integer :: myrank, num_threads
integer, parameter :: n = 10000000
double precision :: omp_t1, omp_t2
double precision :: dx, x, sm, lsum, pif
double precision, external :: second,f
!$ double precision, external :: omp_get_wtime
integer :: i
! calculate pi = integral [0..1] 4 / (1 + x**2) dx */
omp_t1 = second()
!$ omp_t1 = omp_get_wtime()
dx = 1. / n
sm = 0.0;
!$OMP parallel do private(i) reduction(+:sm)
do i = 1, n
x = (1. * i - 0.5) * dx
sm = sm + f(x)
enddo
!$OMP end parallel do
pif = dx * sm
omp_t2 = second()
!$ omp_t2 = omp_get_wtime()
print *, 'computed pi =',pif
print *, 'Running time = ', (omp_t2-omp_t1)
end program pi_reduction
double precision function f(a)
implicit none
double precision, intent(in) :: a
f = 4. / (1. + (a**2))
end function f

Event Timeline