This repository has been archived by the owner on Feb 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpiavg.cpp
187 lines (164 loc) · 4.17 KB
/
mpiavg.cpp
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <fstream>
#include <vector>
#include <sys/time.h>
#include <cstdlib>
#include <sstream>
#include <string>
#include <cmath>
#include <mpi.h>
using namespace std;
int main(int argc, char * argv[])
{
// arg check
if (argc != 4) {
fprintf(stderr, "Wrong parameter\n");
return EXIT_FAILURE;
}
int myid, gid, np, ierr;
int irank, drank;
// create group ranks
irank = atoi(argv[2]);
drank = atoi(argv[3]);
int* iranks = new int[irank];
int* dranks = new int[drank];
for (int i = 0; i < irank; i++)
iranks[i] = i;
for (int i = 0; i < drank; i++)
dranks[i] = i+irank;
// init MPI
MPI_Group orig_group, new_group;
MPI_Comm new_comm;
ierr = MPI_Init (&argc, &argv);
if (ierr != MPI_SUCCESS) {
cout << "MPI_Init failed: " << ierr << endl;
}
MPI_Comm_size(MPI_COMM_WORLD, &np);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
// arg check
if (np != irank + drank) {
fprintf(stderr, "Wrong parameter\n");
return EXIT_FAILURE;
}
// comm root measures runtime
timeval start, end;
if (myid == 0) {
gettimeofday(&start, 0);
}
vector<double> numbers;
int size, range;
// group roots read numbers
if (myid == 0) {
string value;
ifstream file_numbers(argv[1]);
if (file_numbers.is_open()) {
while (getline(file_numbers, value)) {
istringstream i(value);
double x;
i >> x;
numbers.push_back(floor(x));
}
file_numbers.close();
}
// range calculation to match data size with "irank" size
size = numbers.size();
range = 1;
if (size < irank) {
for (int i = 0; i < irank - size; i++) {
numbers.push_back(0);
}
} else if (size > irank) {
range = ceil((double) size / irank);
for (int i = 0; i < irank * range - size; i++) {
numbers.push_back(0);
}
}
} else if (myid == irank) {
string value;
ifstream file_numbers(argv[1]);
if (file_numbers.is_open()) {
while (getline(file_numbers, value)) {
istringstream i(value);
double x;
i >> x;
numbers.push_back(x);
}
file_numbers.close();
}
// range calculation to match data size with "drank" size
size = numbers.size();
range = 1;
if (size < drank) {
for (int i = 0; i < drank - size; i++) {
numbers.push_back(0);
}
} else if (size > drank) {
range = ceil((double) size / drank);
for (int i = 0; i < drank * range - size; i++) {
numbers.push_back(0);
}
}
}
// extract the original group
MPI_Comm_group(MPI_COMM_WORLD, &orig_group);
// divide tasks into two distinct groups based upon rank
if (myid < irank)
MPI_Group_incl(orig_group, irank, iranks, &new_group);
else
MPI_Group_incl(orig_group, drank, dranks, &new_group);
// create new communicator
MPI_Comm_create(MPI_COMM_WORLD, new_group, &new_comm);
// spread values
MPI_Bcast(&range, 1, MPI_INT,
0, new_comm);
double* values = new double[range];
MPI_Scatter(&numbers[0], range, MPI_DOUBLE,
values, range, MPI_DOUBLE,
0, new_comm);
// sum up values
double local_sum = 0;
for (int i = 0; i < range; i++)
local_sum += values[i];
// reduce sum
double global_sum = 0;
MPI_Reduce(&local_sum, &global_sum, 1,
MPI_DOUBLE, MPI_SUM,
0, new_comm);
// roots measure average from sums
double avg = global_sum / size;
// roots create output file
if (myid == 0) {
#ifdef DEBUG
cout << "Integer-precision Average: " << avg << endl;
#endif
ofstream output_file("output.txt");
if (output_file.is_open()) {
output_file << avg << endl;
output_file.close();
}
else
cout << "Unable to open file" << endl;
}
// barrier to sync write access and the correct order of the output data
MPI_Barrier(MPI_COMM_WORLD);
if (myid == irank) {
#ifdef DEBUG
cout << "Double-precision Average: " << avg << endl;
#endif
ofstream output_file("output.txt", ios_base::app);
if (output_file.is_open()) {
output_file << avg << endl;
output_file.close();
}
else
cout << "Unable to open file" << endl;
}
// comm root measures runtime, barrier to sync runtime of all nodes
MPI_Barrier(MPI_COMM_WORLD);
if (myid == 0) {
gettimeofday(&end, 0);
double elapsed = (end.tv_sec - start.tv_sec) + ((end.tv_usec - start.tv_usec)/1000000.0);
cout << "Runtime: " << elapsed << " s" << endl;
}
MPI_Finalize();
}