-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompute_pi.c
More file actions
74 lines (62 loc) · 2.27 KB
/
Copy pathcompute_pi.c
File metadata and controls
74 lines (62 loc) · 2.27 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <mpi.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void Get_input(int argc, char* argv[], int myRank, long* totalNumTosses_p);
long Toss (long numProcessTosses, int myRank);
int main(int argc, char** argv) {
int myRank, numProcs;
long totalNumTosses, numProcessTosses, processNumberInCircle, totalNumberInCircle;
double start, finish, loc_elapsed, elapsed, piEstimate;
double PI25DT = 3.141592653589793238462643; /* 25-digit-PI*/
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
Get_input(argc, argv, myRank, &totalNumTosses); // Read total number of tosses from command line
numProcessTosses = totalNumTosses/numProcs;
MPI_Barrier(MPI_COMM_WORLD);
start = MPI_Wtime();
processNumberInCircle = Toss(numProcessTosses, myRank);
finish = MPI_Wtime();
loc_elapsed = finish-start;
MPI_Reduce(&loc_elapsed, &elapsed, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
MPI_Reduce(&processNumberInCircle, &totalNumberInCircle, 1, MPI_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
if (myRank == 0) {
piEstimate = (4*totalNumberInCircle)/((double) totalNumTosses);
printf("Elapsed time = %f seconds \n", elapsed);
printf("Pi is approximately %.16f, Error is %.16f\n", piEstimate, fabs(piEstimate - PI25DT));
}
MPI_Finalize();
return 0;
}
void Get_input(int argc, char* argv[], int myRank, long* totalNumTosses_p){
if (myRank == 0) {
if (argc!= 2){
printf("number of tosses missing\n");
*totalNumTosses_p = 0;
} else {
*totalNumTosses_p = atoi(argv[1]);
}
}
// Broadcasts value of totalNumTosses to each process
MPI_Bcast(totalNumTosses_p, 1, MPI_LONG, 0, MPI_COMM_WORLD);
// 0 totalNumTosses ends the program
if (*totalNumTosses_p == 0) {
MPI_Finalize();
exit(0);
}
}
/* Function implements Monte Carlo version of tossing darts at a board */
long Toss (long processTosses, int myRank){
long toss, numberInCircle = 0;
double x,y;
unsigned int seed = (unsigned) time(NULL);
srand(seed + myRank);
for (toss = 0; toss < processTosses; toss++) {
x = rand_r(&seed)/(double)RAND_MAX;
y = rand_r(&seed)/(double)RAND_MAX;
if((x*x+y*y) <= 1.0 ) numberInCircle++;
}
return numberInCircle;
}