-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpxtran_miniapp.cpp
302 lines (268 loc) · 11.6 KB
/
pxtran_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
294
295
296
297
298
299
300
301
302
// from std
#include "../utils/pxtran_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 PXTRAN MINIAPP",
"A miniapp computing: `C = alpha*A^T + beta*C` and comparing the performance of COSTA (with scalapack wrappers) 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",
"processor 2D-decomposition.",
cxxopts::value<std::vector<int>>()->default_value("1,1"))
("alpha",
"alpha parameter in C = alpha*A^T + beta*C",
cxxopts::value<int>()->default_value("1"))
("beta",
"beta parameter in C = alpha*A^T + beta*C",
cxxopts::value<int>()->default_value("0"))
("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 = result["p_grid"].as<std::vector<int>>();
auto al = result["alpha"].as<int>();
auto be = result["beta"].as<int>();
// check if alpha and beta take correct values
if ((al != 0 && al != 1) || (be != 0 && be != 1)) {
std::cout << "COSTA (pxtran_miniapp.cpp): ERROR: in this miniapp, \
--alpha and --beta options can only take values 0 or 1 corresponding to \
the zero and the unit elements (respectively), of the chosen data type. \
This is not a requirement of COSTA pxtran wrapper, but just of this miniapp. \
These elements are chosen because they are well defined also for complex data-types."
<< std::endl;
return 0;
}
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 (pxtran_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 (pxtran_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(pxtran_miniapp.cpp): WARNING: correctness checking enabled, setting `n_rep` to 1." << std::endl;
if (algorithm != "both") {
std::cout << "COSTA(pxtran_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);
// check if processor grid corresponds to P
if (p_grid[0] * p_grid[1] != P) {
p_grid[0] = 1;
p_grid[1] = P;
if (rank == 0) {
std::cout << "COSTA(pxtran_miniapp.cpp): warning: number of processors in the grid must be equal to P, setting grid to 1xP instead." << std::endl;
}
}
// *******************************
// perform the multiplication
// ******************************
// no blacs functions will be invoked afterwards
bool exit_blacs = true;
try {
if (type == "double") {
double alpha = double{1.0 * al};
double beta = double{1.0 * be};
pxtran_op_params<double> params(m, n,
block_a[0], block_a[1],
block_c[0], block_c[1],
p_grid[0], p_grid[1],
alpha, beta);
// **************************************
// output the problem description
// **************************************
if (rank == 0) {
std::cout << "Running PDTRAN on the following problem:" << std::endl;
std::cout << params << std::endl;
}
result_correct = benchmark_pxtran<double>(params, MPI_COMM_WORLD, n_rep,
algorithm,
costa_times, scalapack_times,
test_correctness, exit_blacs);
} else if (type == "float") {
float alpha = float{1.0f * al};
float beta = float{1.0f * be};
pxtran_op_params<float> params(m, n,
block_a[0], block_a[1],
block_c[0], block_c[1],
p_grid[0], p_grid[1],
alpha, beta);
// **************************************
// output the problem description
// **************************************
if (rank == 0) {
std::cout << "Running PSTRAN on the following problem:" << std::endl;
std::cout << params << std::endl;
}
result_correct = benchmark_pxtran<float>(params, MPI_COMM_WORLD, n_rep,
algorithm,
costa_times, scalapack_times,
test_correctness, exit_blacs);
} else if (type == "zfloat") {
std::complex<float> alpha = std::complex<float>{1.0f * al};
std::complex<float> beta = std::complex<float>{1.0f * be};
pxtran_op_params<std::complex<float>> params(m, n,
block_a[0], block_a[1],
block_c[0], block_c[1],
p_grid[0], p_grid[1],
alpha, beta);
// **************************************
// output the problem description
// **************************************
if (rank == 0) {
std::cout << "Running PCTRANU on the following problem:" << std::endl;
std::cout << params << std::endl;
}
result_correct = benchmark_pxtran<std::complex<float>>(params, MPI_COMM_WORLD, n_rep,
algorithm,
costa_times, scalapack_times,
test_correctness, exit_blacs);
} else if (type == "zdouble") {
std::complex<double> alpha = std::complex<double>{1.0 * al};
std::complex<double> beta = std::complex<double>{1.0 * be};
pxtran_op_params<std::complex<double>> params(m, n,
block_a[0], block_a[1],
block_c[0], block_c[1],
p_grid[0], p_grid[1],
alpha, beta);
// **************************************
// output the problem description
// **************************************
if (rank == 0) {
std::cout << "Running PZTRANU on the following problem:" << std::endl;
std::cout << params << std::endl;
}
result_correct = benchmark_pxtran<std::complex<double>>(params, MPI_COMM_WORLD, n_rep,
algorithm,
costa_times, scalapack_times,
test_correctness, exit_blacs);
} else {
throw std::runtime_error("COSTA(pxtran_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;
}