program int_pi1
c
c For API
c
c This simple program approximates pi by computing pi = integral
c from 0 to 1 of 4/(1+x*x)dx which is approximated by sum from
c k=1 to N of 4 / ((1 + (k-1/2)**2 ). The only input data required is N.
c
c Parallel version #1: All instances are started at load time, but no
c messages or division of work
c RLF 4/4/93 16:02
c revised: 6/4/93 riordan
c Converted to MPI: 11/12/94 Xianneng Shen
c
include "mpif.h"
integer ierr, status(MPI_STATUS_SIZE)
parameter (maxproc=100)
real err, f, pi, sum, w
integer i, N, nprocs, mynum
f(x) = 4.0/(1.0+x*x)
pi = 4.0*atan(1.0)
N = 1000
print *,'Approximation intervals, N, is set to 1000'
c
c All instances call the startup routine to get their instance number (mynum)
call MPI_INIT(ierr)
call MPI_COMM_RANK(MPI_COMM_WORLD, mynum, ierr)
call MPI_COMM_SIZE(MPI_COMM_WORLD, nprocs, ierr)
c ------- Each new approximation to pi begins here. -------------------
c Do the computation in N steps
c (Ultimately, this work should be divided up among the processes)
w = 1.0/N
sum = 0.0
do i = 1,N
sum = sum + f((i-0.5)*w)
enddo
sum = sum * w
c Print the results
c (Ultimately, partial results will have to be sent to the master,
c who will then print the answer)
err = sum - pi
print *, 'sum, err =', sum, err
call MPI_FINALIZE(ierr)
end