+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
bc9a89f
master
1#include "mpi.h" 2#include <math.h> 3#include <stdio.h> 4 5int main (argc ,argv ) 6int argc ; 7char * argv []; 8{ 9int done = 0 ,n ,myid ,numprocs ,i ,rc ; 10double PI25DT = 3.141592653589793238462643 ; 11double mypi ,pi ,h ,sum ,x ,a ; 12 13 14MPI_Init (& argc ,& argv ); 15MPI_Comm_size (MPI_COMM_WORLD ,& numprocs ); 16MPI_Comm_rank (MPI_COMM_WORLD ,& myid ); 17//printf("My ID is %d", myid); 18 19n = 64 ;// number of intervals; 20/* int MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int source, MPI_Comm comm) */ 21MPI_Bcast (& n ,1 ,MPI_INT ,0 ,MPI_COMM_WORLD ); 22 23 24h = 1.0 / (double )n ; 25sum = 0.0 ; 26for (i = myid + 1 ;i <=n ;i += numprocs ) { 27x = h * ((double )i - 0.5 ); 28sum += 4.0 / (1.0 + x * x ); 29 } 30mypi = h * sum ; 31 32 33 34/* int MPI_Reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int target, MPI_Comm comm) */ 35MPI_Reduce (& mypi ,& pi ,1 ,MPI_DOUBLE ,MPI_SUM ,0 , 36MPI_COMM_WORLD ); 37 38 39 40if (myid == 0 ) 41printf ("pi is approximately %.16f, Error is %.16f\n" , 42pi ,fabs (pi - PI25DT )); 43MPI_Finalize (); 44}