+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

vijaygarg1initbc9a89f

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