Blame examples/f77/fpi.f

Packit 0848f5
c**********************************************************************
Packit 0848f5
c   pi.f - compute pi by integrating f(x) = 4/(1 + x**2)     
Packit 0848f5
c
Packit 0848f5
c  (C) 2001 by Argonne National Laboratory.
Packit 0848f5
c      See COPYRIGHT in top-level directory.
Packit 0848f5
c     
Packit 0848f5
c   Each node: 
Packit 0848f5
c    1) receives the number of rectangles used in the approximation.
Packit 0848f5
c    2) calculates the areas of it's rectangles.
Packit 0848f5
c    3) Synchronizes for a global summation.
Packit 0848f5
c   Node 0 prints the result.
Packit 0848f5
c
Packit 0848f5
c  Variables:
Packit 0848f5
c
Packit 0848f5
c    pi  the calculated result
Packit 0848f5
c    n   number of points of integration.  
Packit 0848f5
c    x           midpoint of each rectangle's interval
Packit 0848f5
c    f           function to integrate
Packit 0848f5
c    sum,pi      area of rectangles
Packit 0848f5
c    tmp         temporary scratch space for global summation
Packit 0848f5
c    i           do loop index
Packit 0848f5
c****************************************************************************
Packit 0848f5
      program main
Packit 0848f5
Packit 0848f5
      include 'mpif.h'
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
c                                 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
 10   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
c                                 check for quit signal
Packit 0848f5
      if ( n .le. 0 ) goto 30
Packit 0848f5
Packit 0848f5
c                                 calculate the interval size
Packit 0848f5
      h = 1.0d0/n
Packit 0848f5
Packit 0848f5
      sum  = 0.0d0
Packit 0848f5
      do 20 i = myid+1, n, numprocs
Packit 0848f5
         x = h * (dble(i) - 0.5d0)
Packit 0848f5
         sum = sum + f(x)
Packit 0848f5
 20   continue
Packit 0848f5
      mypi = h * sum
Packit 0848f5
Packit 0848f5
c                                 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
c                                 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
      goto 10
Packit 0848f5
Packit 0848f5
 30   call MPI_FINALIZE(rc)
Packit 0848f5
      stop
Packit 0848f5
      end