-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpxgemr2d_miniapp.cpp
293 lines (259 loc) · 11 KB
/
pxgemr2d_miniapp.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// from std
#include "../utils/pxgemr2d_utils.hpp"
#include <cxxopts.hpp>
#include <unordered_set>
using namespace costa;
int main(int argc, char **argv) {
// **************************************
// setup command-line parser
// **************************************
cxxopts::Options options("COSTA PXGEMR2D MINIAPP",
"A miniapp redistributing the matrix between two different block-cyclic distributions and comparing the performance of COSTA VS SCALAPACK.");
// **************************************
// readout the command line arguments
// **************************************
// matrix dimensions
// dim(A) = n*m, dim(C) = m*n
options.add_options()
("m,m_dim",
"number of rows of A and C.",
cxxopts::value<int>()->default_value("1000"))
("n,n_dim",
"number of columns of B and C.",
cxxopts::value<int>()->default_value("1000"))
("block_a",
"block dimensions for matrix A.",
cxxopts::value<std::vector<int>>()->default_value("128,128"))
("block_c",
"block dimensions for matrix C.",
cxxopts::value<std::vector<int>>()->default_value("128,128"))
("p,p_grid_a",
"processor 2D-decomposition.",
cxxopts::value<std::vector<int>>()->default_value("1,1"))
("q,p_grid_c",
"processor 2D-decomposition.",
cxxopts::value<std::vector<int>>()->default_value("1,1"))
("r,n_rep",
"number of repetitions",
cxxopts::value<int>()->default_value("2"))
("t,type",
"data type of matrix entries.",
cxxopts::value<std::string>()->default_value("double"))
("test",
"test the result correctness.",
cxxopts::value<bool>()->default_value("false"))
("algorithm",
"defines which algorithm (costa, scalapack or both) to run",
cxxopts::value<std::string>()->default_value("both"))
("h,help", "Print usage.")
;
const char** const_argv = (const char**) argv;
auto result = options.parse(argc, const_argv);
if (result.count("help")) {
std::cout << options.help() << std::endl;
return 0;
}
auto m = result["m_dim"].as<int>();
auto n = result["n_dim"].as<int>();
auto block_a = result["block_a"].as<std::vector<int>>();
auto block_c = result["block_c"].as<std::vector<int>>();
auto p_grid_a = result["p_grid_a"].as<std::vector<int>>();
auto p_grid_c = result["p_grid_c"].as<std::vector<int>>();
bool test_correctness = result["test"].as<bool>();
auto n_rep = result["n_rep"].as<int>();
auto type = result["type"].as<std::string>();
// transform to lower-case
std::transform(type.begin(), type.end(), type.begin(),
[&](char c) {
return std::tolower(c);
}
);
// check if the type option takes a correct value
std::unordered_set<std::string> type_options = {
"float", "double", "zfloat", "zdouble"
};
if (type_options.find(type) == type_options.end()) {
std::cout << "COSTA (pxgemr2d_miniapp.cpp): ERROR: --type option: can only take the following values: " << std::endl;
for (const auto& el : type_options) {
std::cout << el << ", ";
}
std::cout << std::endl;
return 0;
}
// make lower-space
auto algorithm = result["algorithm"].as<std::string>();
std::transform(algorithm.begin(), algorithm.end(), algorithm.begin(),
[&](char c) {
return std::tolower(c);
}
);
// check if the algorithm option takes a correct value
std::unordered_set<std::string> algorithm_options = {
"costa", "scalapack", "both"
};
if (algorithm_options.find(algorithm) == algorithm_options.end()) {
std::cout << "COSTA (pxgemr2d_miniapp.cpp): ERROR: --algorithm option: can only take the following values: " << std::endl;
for (const auto& el : algorithm_options) {
std::cout << el << ", ";
}
std::cout << std::endl;
return 0;
}
// some basic checks
if (test_correctness) {
// if testing correctness, n_rep = 1;
n_rep = 1;
std::cout << "COSTA(pxgemr2d_miniapp.cpp): WARNING: correctness checking enabled, setting `n_rep` to 1." << std::endl;
if (algorithm != "both") {
std::cout << "COSTA(pxgemr2d_miniapp.cpp): WARNING: correctness checking enabled, setting `algorithm` to `both`." << std::endl;
algorithm = "both";
}
}
std::vector<long> costa_times;
std::vector<long> scalapack_times;
bool result_correct = true;
// initilize MPI
MPI_Init(&argc, &argv);
int rank, P;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &P);
int Pa = p_grid_a[0] * p_grid_a[1];
int Pc = p_grid_c[0] * p_grid_c[1];
int num_ranks = std::max(Pa, Pc);
// check if processor grid corresponds to P
if (num_ranks != P) {
p_grid_a[0] = 1;
p_grid_a[1] = P;
p_grid_c[0] = 1;
p_grid_c[1] = P;
if (rank == 0) {
std::cout << "COSTA(pxgemr2d_miniapp.cpp): warning: number of processors in the grid must be equal to P, setting grid to 1xP instead." << std::endl;
}
}
// *******************************
// perform the reshuffling
// ******************************
// no blacs functions will be invoked afterwards
bool exit_blacs = true;
try {
if (type == "double") {
pxgemr2d_params<double> params(m, n,
block_a[0], block_a[1],
block_c[0], block_c[1],
p_grid_a[0], p_grid_a[1],
p_grid_c[0], p_grid_c[1]
);
// **************************************
// output the problem description
// **************************************
if (rank == 0) {
std::cout << "Running PDGEMR2D on the following problem:" << std::endl;
std::cout << params << std::endl;
}
result_correct = benchmark_pxgemr2d<double>(params, MPI_COMM_WORLD, n_rep,
algorithm,
costa_times,
scalapack_times,
test_correctness, exit_blacs);
} else if (type == "float") {
pxgemr2d_params<float> params(m, n,
block_a[0], block_a[1],
block_c[0], block_c[1],
p_grid_a[0], p_grid_a[1],
p_grid_c[0], p_grid_c[1]
);
// **************************************
// output the problem description
// **************************************
if (rank == 0) {
std::cout << "Running PSGEMR2D on the following problem:" << std::endl;
std::cout << params << std::endl;
}
result_correct = benchmark_pxgemr2d<float>(params, MPI_COMM_WORLD, n_rep,
algorithm,
costa_times,
scalapack_times,
test_correctness, exit_blacs);
} else if (type == "zfloat") {
pxgemr2d_params<std::complex<float>> params(m, n,
block_a[0], block_a[1],
block_c[0], block_c[1],
p_grid_a[0], p_grid_a[1],
p_grid_c[0], p_grid_c[1]
);
// **************************************
// output the problem description
// **************************************
if (rank == 0) {
std::cout << "Running PCGEMR2D on the following problem:" << std::endl;
std::cout << params << std::endl;
}
result_correct = benchmark_pxgemr2d<std::complex<float>>(params, MPI_COMM_WORLD, n_rep,
algorithm,
costa_times,
scalapack_times,
test_correctness, exit_blacs);
} else if (type == "zdouble") {
pxgemr2d_params<std::complex<double>> params(m, n,
block_a[0], block_a[1],
block_c[0], block_c[1],
p_grid_a[0], p_grid_a[1],
p_grid_c[0], p_grid_c[1]
);
// **************************************
// output the problem description
// **************************************
if (rank == 0) {
std::cout << "Running PZGEMR2D on the following problem:" << std::endl;
std::cout << params << std::endl;
}
result_correct = benchmark_pxgemr2d<std::complex<double>>(params, MPI_COMM_WORLD, n_rep,
algorithm,
costa_times,
scalapack_times,
test_correctness, exit_blacs);
} else {
throw std::runtime_error("COSTA(pxgemr2d_miniapp): unknown data type of matrix entries.");
}
} catch (const std::exception& e) {
// MPI is already finalized, but just in case
std::cout << e.what() << std::endl;
int flag = 0;
MPI_Finalized(&flag);
if (!flag) {
MPI_Abort(MPI_COMM_WORLD, -1);
MPI_Finalize();
}
return 0;
}
// *****************
// output times
// *****************
if (rank == 0) {
if (algorithm == "both" || algorithm == "costa") {
std::cout << "COSTA TIMES [ms] = ";
for (auto &time : costa_times) {
std::cout << time << " ";
}
std::cout << std::endl;
}
if (algorithm == "both" || algorithm == "scalapack") {
std::cout << "SCALAPACK TIMES [ms] = ";
for (auto &time : scalapack_times) {
std::cout << time << " ";
}
std::cout << std::endl;
}
}
if (test_correctness) {
int result = result_correct ? 0 : 1;
int global_result = 0;
MPI_Reduce(&result, &global_result, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (rank == 0) {
std::string yes_no = global_result == 0 ? "" : " NOT";
std::cout << "Result is" << yes_no << " CORRECT!" << std::endl;
}
}
MPI_Finalize();
return 0;
}