/* From "COMPUTATIONAL PHYSICS", 3rd Ed, Enlarged Python eTextBook by RH Landau, MJ Paez, and CC Bordeianu Copyright Wiley-VCH Verlag GmbH & Co. KGaA, Berlin; Copyright R Landau, Oregon State Unv, MJ Paez, Univ Antioquia, C Bordeianu, Univ Bucharest, 2015. Support by National Science Foundation */ /* Calculation of pi via Stone Throwing Using MPI */ /* Obtained from http://www.dartmouth.edu/~rc/classes/soft_dev/mpi.html */ /* Note: if the sprng library is not available, you may use rnd */ #include #include #include #include #include #include #include #define USE_MPI #define SEED 35791246 main(int argc, char *argv[]) { int niter=0; double x,y; int i,j,count=0,mycount; /* # of points in the 1st quadrant of unit circle */ double z; double pi; int myid,numprocs,proc; MPI_Status status; int master =0; int tag = 123; int *stream_id; /* stream id generated by SPRNGS */ MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); if (argc <=1) { fprintf(stderr,"Usage: monte_pi_mpi number_of_iterations\n"); MPI_Finalize(); exit(-1); } sscanf(argv[1],"%d",&niter); /* 1st argument is the number of iterations*/ /* initialize random numbers */ stream_id = init_sprng(myid,numprocs,SEED,SPRNG_DEFAULT); mycount=0; for ( i=0; i