Blame examples/f90/pi3f90.f90

Packit 0848f5
!**********************************************************************
Packit 0848f5
!   pi3f90.f - compute pi by integrating f(x) = 4/(1 + x**2)     
Packit 0848f5
!
Packit 0848f5
!  (C) 2001 by Argonne National Laboratory.
Packit 0848f5
!      See COPYRIGHT in top-level directory.
Packit 0848f5
!     
Packit 0848f5
!   Each node: 
Packit 0848f5
!    1) receives the number of rectangles used in the approximation.
Packit 0848f5
!    2) calculates the areas of it's rectangles.
Packit 0848f5
!    3) Synchronizes for a global summation.
Packit 0848f5
!   Node 0 prints the result.
Packit 0848f5
!
Packit 0848f5
!  Variables:
Packit 0848f5
!
Packit 0848f5
!    pi  the calculated result
Packit 0848f5
!    n   number of points of integration.  
Packit 0848f5
!    x           midpoint of each rectangle's interval
Packit 0848f5
!    f           function to integrate
Packit 0848f5
!    sum,pi      area of rectangles
Packit 0848f5
!    tmp         temporary scratch space for global summation
Packit 0848f5
!    i           do loop index
Packit 0848f5
!****************************************************************************
Packit 0848f5
program main
Packit 0848f5
Packit 0848f5
 use mpi
Packit 0848f5
Packit 0848f5
 double precision  PI25DT
Packit 0848f5
 parameter        (PI25DT = 3.141592653589793238462643d0)
Packit 0848f5
Packit 0848f5
 double precision  mypi, pi, h, sum, x, f, a
Packit 0848f5
 integer n, myid, numprocs, i, rc
Packit 0848f5
!                                 function to integrate
Packit 0848f5
 f(a) = 4.d0 / (1.d0 + a*a)
Packit 0848f5
 
Packit 0848f5
 call MPI_INIT( ierr )
Packit 0848f5
 call MPI_COMM_RANK( MPI_COMM_WORLD, myid, ierr )
Packit 0848f5
 call MPI_COMM_SIZE( MPI_COMM_WORLD, numprocs, ierr )
Packit 0848f5
 print *, 'Process ', myid, ' of ', numprocs, ' is alive'
Packit 0848f5
 
Packit 0848f5
 sizetype   = 1
Packit 0848f5
 sumtype    = 2
Packit 0848f5
 
Packit 0848f5
 do 
Packit 0848f5
    if ( myid .eq. 0 ) then
Packit 0848f5
       write(6,98)
Packit 0848f5
 98    format('Enter the number of intervals: (0 quits)')
Packit 0848f5
       read(5,99) n
Packit 0848f5
 99    format(i10)
Packit 0848f5
    endif
Packit 0848f5
      
Packit 0848f5
    call MPI_BCAST(n,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr)
Packit 0848f5
Packit 0848f5
!                                 check for quit signal
Packit 0848f5
    if ( n .le. 0 ) exit
Packit 0848f5
Packit 0848f5
!                                 calculate the interval size
Packit 0848f5
    h = 1.0d0/n
Packit 0848f5
 
Packit 0848f5
    sum  = 0.0d0
Packit 0848f5
    do i = myid+1, n, numprocs
Packit 0848f5
       x = h * (dble(i) - 0.5d0)
Packit 0848f5
       sum = sum + f(x)
Packit 0848f5
    enddo
Packit 0848f5
    mypi = h * sum
Packit 0848f5
Packit 0848f5
!                                 collect all the partial sums
Packit 0848f5
    call MPI_REDUCE(mypi,pi,1,MPI_DOUBLE_PRECISION,MPI_SUM,0, &
Packit 0848f5
                    MPI_COMM_WORLD,ierr)
Packit 0848f5
Packit 0848f5
!                                 node 0 prints the answer.
Packit 0848f5
    if (myid .eq. 0) then
Packit 0848f5
        write(6, 97) pi, abs(pi - PI25DT)
Packit 0848f5
 97     format('  pi is approximately: ', F18.16, &
Packit 0848f5
               '  Error is: ', F18.16)
Packit 0848f5
    endif
Packit 0848f5
Packit 0848f5
 enddo
Packit 0848f5
Packit 0848f5
 call MPI_FINALIZE(rc)
Packit 0848f5
 stop
Packit 0848f5
end
Packit 0848f5
Packit 0848f5
Packit 0848f5
Packit 0848f5