-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiNumberCalculator.c
More file actions
60 lines (41 loc) · 1.12 KB
/
PiNumberCalculator.c
File metadata and controls
60 lines (41 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "stdio.h"
#include "mpi.h"
#include <math.h>
double getTaylorElement(int n){
double value = 1.0 / ( (2.0 * n) + 1.0);
return (n % 2 == 0)? value : -value;
}
int main(argc,argv)
int argc;
char *argv[];
{
int numOfTerms;
int nodeId;
int numberOfNodes;
double PI25DT = 3.141592653589793238462643;
double nodeSum = 0;
double totalSum;
double predictedPi;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numberOfNodes);
MPI_Comm_rank(MPI_COMM_WORLD,&nodeId);
if (nodeId == 0) {
printf("Enter the number of intervals: (0 quits) ");
scanf("%d",&numOfTerms);
}
MPI_Bcast(&numOfTerms, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (numOfTerms == 0){
return 0;
}
for(int i = nodeId; i<numOfTerms; i += numberOfNodes){
nodeSum += getTaylorElement(i);
}
printf("NodeId=[%d] Summation=[%.4f]\n", nodeId, nodeSum);
MPI_Reduce(&nodeSum, &totalSum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (nodeId == 0){
predictedPi = 4.0 * totalSum;
printf("pi is approximately %.16f, Error is %.16f\n", predictedPi, fabs(predictedPi - PI25DT));
}
MPI_Finalize();
return 0;
}