-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputePi.cpp
More file actions
173 lines (140 loc) · 3.85 KB
/
computePi.cpp
File metadata and controls
173 lines (140 loc) · 3.85 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <stdlib.h>
#include <mpi.h>
#include <climits>
#include <math.h>
// This example is adapted from "Using MPI, second edition"
// by Gropp, Lusk, and Skellum
void initialize( int argc, char* argv[] )
{
MPI_Init( &argc, &argv );
}
void identifyRankAndSize( int& rank, int& size, MPI_Comm comm )
{
MPI_Comm_rank( comm, &rank );
MPI_Comm_size( comm, &size);
}
void constructWorkerCommGroup( MPI_Comm& workerComm, int size )
{
// construct the world group
MPI_Group worldGroup;
MPI_Comm_group( MPI_COMM_WORLD, &worldGroup );
// construct the worker group
MPI_Group workerGroup;
int ranks[1];
ranks[0] = size - 1; // exclude the server
MPI_Group_excl( worldGroup, 1, ranks, &workerGroup );
MPI_Comm_create( MPI_COMM_WORLD, workerGroup, &workerComm );
MPI_Group_free( &worldGroup );
MPI_Group_free( &workerGroup );
}
int computeRandom()
{
return random();
}
void runServer( )
{
const int numRands = 1000;
int* rands = new int[ numRands ];
int request;
const int c_fin = 0;
const int c_req = 1;
const int c_rep = 2;
do
{
MPI_Status status;
MPI_Recv( &request, 1, MPI_INT, MPI_ANY_SOURCE, c_req,
MPI_COMM_WORLD, &status );
if ( request )
{
for ( int i = 0; i < numRands; )
{
rands[i] = computeRandom();
if ( rands[i] <= INT_MAX ) ++i;
}
MPI_Send( rands, numRands, MPI_INT, status.MPI_SOURCE, c_rep,
MPI_COMM_WORLD );
}
} while ( request > 0);
delete[] rands;
}
double runWorker( MPI_Comm& workerComm, int rank, int size)
{
const int numRands = 1000;
int* rands = new int[ numRands ];
// request constants
const int c_fin = 0;
const int c_req = 1;
const int c_rep = 2;
double x;
double y;
bool notDone = true;
int in = 0;
int out = 0;
int totalin = 0;
int totalout = 0;
double epsilon = 0.00000001;
double error;
double Pi;
int request = c_req;
int server = size - 1;
MPI_Status status;
MPI_Send( &request, 1, MPI_INT, server, c_req, MPI_COMM_WORLD );
while ( notDone )
{
MPI_Recv( rands, numRands, MPI_INT, MPI_ANY_SOURCE, c_rep,
MPI_COMM_WORLD, &status );
for ( int i = 0; i < numRands; )
{
x = (((double)(rands[i++]))/(double)(INT_MAX)) * 2 - 1;
y = (((double)(rands[i++]))/(double)(INT_MAX)) * 2 - 1;
if (( x*x + y*y ) < 1.0 ) ++in;
else ++out;
}
MPI_Allreduce(&in, &totalin, 1, MPI_INT, MPI_SUM, workerComm );
MPI_Allreduce(&out, &totalout, 1, MPI_INT, MPI_SUM, workerComm );
Pi = (4.0 * totalin) / ( totalin + totalout );
error = fabs( Pi - 3.141592653589793238462643 );
notDone = ( (error > epsilon) && (totalin + totalout) < 10000000 );
if ( rank == 0 && !notDone )
{
request = c_fin;
MPI_Send( &request, 1, MPI_INT, server, c_req, MPI_COMM_WORLD );
}
else
{
request = c_req;
if ( notDone )
{
MPI_Send( &request, 1, MPI_INT, server, c_req,
MPI_COMM_WORLD );
}
}
}
MPI_Comm_free( &workerComm );
delete[] rands;
return Pi;
}
int main( int argc, char* argv[] )
{
initialize( argc, argv );
int myRank;
int worldSize;
identifyRankAndSize( myRank, worldSize, MPI_COMM_WORLD );
MPI_Comm workerComm;
constructWorkerCommGroup( workerComm, worldSize );
double pi;
if ( myRank == worldSize - 1 )
{
runServer();
}
else
{
pi = runWorker( workerComm, myRank, worldSize );
}
if (myRank == 0 )
{
std::cout << "Pi is " << pi << std::endl;
}
MPI_Finalize();
}