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) 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. ------------------- 5 continue c Step (1): get a value for N, the number of intervals in the approximation c (Parallel versions: the initial instance, or master, reads this in.) c This would be a good place to add message-passing, so that the master c can send N to the other nodes, and the other nodes can get N from the c master. if (mynum .eq. 0) then print *,'Enter number of approximation intervals:(0 to exit)' read *, N else N=0 endif c Step (2): check for exit condition. if (N .le. 0) then print *,'node (',mynum, ') left' call exit endif c Step (3): 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 Step (4): 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 go to 5 call MPI_FINALIZE(ierr) end