#include "mpi.h" #include #include #include const long A = 1000000000; int main(int argc, char *argv[]) { int i, j; int id; //Process rank int np; //Num of processes double x, y, PI; //coordinates long in, globalIn; // number of points inside the circle double elapsed_time; double PI25DT = 3.141592653589793238462643; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &id); MPI_Comm_size(MPI_COMM_WORLD, &np); MPI_Barrier(MPI_COMM_WORLD); elapsed_time = - MPI_Wtime(); srand ( time(NULL)*id ); in = 0; for(i = 0; i < A/np; i++) { x = ((double)rand() / RAND_MAX); y = ((double)rand() / RAND_MAX); if (x*x + y*y <= 1) in++; } MPI_Reduce(&in, &globalIn, 1, MPI_LONG, MPI_SUM, 0, MPI_COMM_WORLD); elapsed_time += MPI_Wtime(); if(id == 0) { PI = (double)4*globalIn/A; printf("PI = %26.25f, and elapsed time is %f\n",PI, elapsed_time); printf("PI25 - PIour = %26.25f\n",PI25DT - PI); } MPI_Finalize(); return 0; }