First page Back Continue Last page Summary Graphics
C example
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
int n=1000000, myid, np, i;
double sum, pi, h;
char host[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&np);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
MPI_Get_processor_name(host,&n);
fprintf(stderr,"Process %d on %s\n", myid, host);
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
h = 1.0 / n;
sum = 0;
for (i=myid+1; i<=n; i+=np)
{
double x = h*(i-0.5);
sum += 4/(1+x*x);
}
sum *= h;
MPI_Reduce(&sum , &pi , 1 , MPI_DOUBLE , MPI_SUM , 0 , MPI_COMM_WORLD);
if (myid == 0) printf("pi =%.16f, error=%.16f\n",
pi, pi -3.141592653589793238462643);
MPI_Finalize();
return 0;
}
See Appendix C for a more complete example
Notes: