Example of the message passing code
From EGEE-see WIki
The page is part of SEE-GRID Gridification Guide
This topic is contributed by Center for Scientific Research of SASA and University of Kragujevac,Serbia
Description and solution
Some fundamental MPI function calls will be introduced through the computation of a simple integral by the mid-point rule. This simple numerical task can be parallelized easily since its parts are independent and the interprocess communication is not intensive. The following figure illustrates the problem and the approach:

All the processes (inlcuding master process) compute their own sum storing it in my_int variable. Then the master process gathers the numbers being sent from all the others, computing the sum and printing it out. The example coded in C looks like this:
#include "mpi.h"
#include <stdio.h>
#include <math.h>
float integral(float a, int i, float h, int n)
{
int j;
float h2, aij, integ;
integ = 0.0; /* initialize integral */
h2 = h/2.;
/* sum over integrals in partition i*/
for (j=0; j<n; j++)
{
aij = a + (i*n + j)*h; /* lower limit of integration of j*/
integ += cos(aij+h2)*h; /* contribution due to j */
}
return integ;
}
int main(int argc,char *argv[])
{
int n, p, i, myid, source, master, tag;
float h, result, a, b, pi, my_int, integral_sum;
MPI_Status status; /* MPI data type */
MPI_Init(&argc,&argv); /* start MPI processess */
MPI_Comm_size(MPI_COMM_WORLD,&p); /* # of processes */
MPI_Comm_rank(MPI_COMM_WORLD,&myid); /* current proc. id */
pi = acos(-1.0); /* = 3.14159... */
a = 0.; /* lower limit of integration */
b = pi/2.; /* upper limit of integration */
n = 500; /* number of increment within each process */
master = 0; /* define the process that computes the final result */
tag = 123; /* set the tag to identify this particular job */
h = (b-a)/n/p; /* length of increment */
my_int = integral(a,myid,h,n); /* local sum due process myid */
printf("Process %d has the partial integral of %f\n", myid,my_int);
if(myid == master)
{
integral_sum = my_int;
for (source=1;source<p;source++)
{
MPI_Recv(&my_int, 1, MPI_FLOAT, source, tag,
MPI_COMM_WORLD, &status); /* safe */
integral_sum += my_int;
}
fprintf(stdout,"The Integral =%f\n", integral_sum);
}
else
{
MPI_Send(&my_int, 1, MPI_FLOAT, master, tag,
MPI_COMM_WORLD); /* send my_int to “master” */
}
MPI_Finalize(); /* let MPI finish up ... */
return 0;
}
This sample code shows only point to point communication with blocking send/receive among the processes. The collective communication routines like function MPI_Bcast are very useful when one process has the communication with the user and then wants to spread the user supplied value to all other processes and in many other use cases.
Compilation and execution
A way to compile and execute the above code on MPICH 1.2.6 and SL3 (and SL4 as well) in absence of the batching system is:
mpicc example1.c -o example1 -lm mpirun -np <numprocs> example1
In case of Fortran77/90, similar compilation scripts are used – mpif77 and mpif90. The parameter denoting the number of processes does not necessarily mean the number of processors. The code can be tested even on single CPU machine and then deployed onto the larger system. Of course, the performance improvements can only be seen in multi CPU environment.
