What is MPI ?
MPI stands for Message Passing Interface. It is a set of subroutines used to communicate between
different number of processes. It was designed to handle distributed memory systems. However, it
can be utilized on shared memory/Hybrid memory architectures.
In MPI, there are six (6) indispensable functions, you can not work without.
(Note: Max. functions in MPI ~125 or more).
============================================================
These are:
1. MPI_Init => initialize mpi
routine used: MPI_INIT (ierr)
2. MPI_Comm_size => give total number of processes
routine used: MPI_COMM_SIZE (comm,size,ierr)
3. MPI_Comm_rank => which process i am
routine used: MPI_COMM_RANK (comm,rank,ierr)
4. MPI_Send => send a message call
routine used: MPI_SEND (buf,count,datatype,dest,tag,comm,ierr)
5. MPI_Recv => receive a message call
routine used: MPI_RECV (buf,count,datatype,source,tag,comm,status,ierr)
6. MPI_Finalize => terminate mpi
routine used: MPI_FINALIZE (ierr)
NOTE: above comm <=> MPI_COMM_WORLD
============================================================
Fortran program to calculate the integral of function: f(x)=4/(1+x^2)
Note: if we take integral of this function from 0 to 1, we get pi. So, to get the value of pi numerically, we
need to divide the interval from 0 to 1 into n number of intervals, where value of n can be assigned at run time.
----------------------------------------------------------------------------------------------------------------------------------------------------------
program pii
! fortran77 require header file: mpif.h
include "mpif.h"
! OR you can use "use mpi" module, if supported by mpi
! double precision PI25DT
! parameter (PI25DT = 3.141592653589793238462643d0)
double precision, parameter :: PI25DT = 3.141592653589793238462643d0
double precision :: mypi, pi, h, sum, x, f, a
double precision :: starttime, endtime
integer :: n, rank, size, i, ierr
! rank => which process starting from 0.
! size => number of processes.
!c function to integrate
f(a) = 4.d0 / (1.d0 + a*a)
! Call to MPI_INIT is always required in every MPI program. Also
! it must be the first MPI call in the program.
call MPI_INIT(ierr)
! By calling MPI_COMM_RANK, each process will find out its rank in the
! group associated with a communicator.
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
! call MPI_COMM_SIZE returns the number of processes user has started.
call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr)
10 if ( rank .eq. 0 ) then
print *, 'Hi, I am Master Process.'
print *, 'Enter the number of intervals for (0,1) &
& [ OR enter 0 to quit ] '
Read(*,*) n
endif
starttime = MPI_WTIME( )
!c broadcast n
! MPI_BCAST broadcasts to every process in communication world with a copy of n.
! The data to be communicated is described by the address (n), the number of items (1),
! datatype (MPI_INTEGER), process (master) with the original copy specified by the
! fourth argument (0)in
call MPI_BCAST(n,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr)
!c check for quit signal
if ( n .le. 0 ) goto 30
!c calculate the interval size
h = 1.0d0/n
sum = 0.0d0
do 20 i = rank+1, n, size
x = h * (dble(i) - 0.5d0)
sum = sum + f(x)
20 continue
mypi = h * sum
!c collect all the partial sums
! First two arguments gives the source and result addresses respectively.
! third argment gives the data being collected. and type MPI_DOUBLE_PRECISION
! by fourth argument. Fifth argument shows that operation is addition (MPI_SUM)
! and the result of the operation is to be placed (in pi) on the process with rank 0
! i.e. master process (fifth argument).
! The first two arguments of MPI_REDUCE must be different variables.
call MPI_REDUCE(mypi,pi,1,MPI_DOUBLE_PRECISION,MPI_SUM,0,MPI_COMM_WORLD,ierr)
endtime = MPI_WTIME( )
!c node 0 prints the answer.
if (rank .eq. 0) then
print *, 'pi is : ', pi, '; Error is :', abs(pi - PI25DT)
print *, 'Time taken is : ', endtime-starttime, ' seconds'
endif
goto 10
30 call MPI_FINALIZE(ierr)
! stop
! end
end program pii
----------------------------------------------------------------------------------------------------------------------------------------------------------
OUTPUT:
Hi, I am Master Process.
Enter the number of intervals for (0,1) [ OR enter 0 to quit ]
20000
pi is : 3.14159265379813 ; Error is : 2.083324623924909E-010
Time taken is : 4.858970642089844E-004 seconds
Hi, I am Master Process.
Enter the number of intervals for (0,1) [ OR enter 0 to quit ]
0
----------------------------------------------------------------------------------------------------------------------------------------------------------
more to be added..