/* MPI example program that computes the value of Pi using a Monte Carlo simulation. The program samples points inside the rectangle delimited by (0,0) and (1,1) and counts how many of these are within a circle with a radius of 1. The ratio between the number of points inside the circle and the total number of samples is Pi/4. Compile with mpicc -O3 cpi_mc.c -o cpi_mc */ #include "mpi.h" #include #include #include #include int main(int argc,char *argv[]) { const double PI24 = 3.141592653589793238462643; int n, myid, numprocs, i; double pi, maxval; double sum_n, in_circle, sum_c; double starttime, endtime; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); maxval = (double)RAND_MAX; /* The largest value returned by random() */ srandom(myid); /* Seed the random number generator */ /* Read the number of random samples in each process */ if (myid == 0) { printf("Give number of samples in each process: \n"); fflush(stdout); scanf("%d", &n); } /* Send n to all proceses */ MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); if (myid == 0) printf("Number of samples is %d\n", n); fflush(stdout); /* Start measuring time */ if (myid == 0) starttime = MPI_Wtime(); in_circle = 0.0; /* Draw n random points and count how many are inside the circle */ for (i=0; i