-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathimb.cpp
370 lines (331 loc) · 15 KB
/
imb.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/****************************************************************************
* *
* Copyright (C) 2024 Intel Corporation *
* *
*****************************************************************************
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***************************************************************************/
#include <mpi.h>
#include <stdexcept>
#include <fstream>
#include <algorithm>
#include "smart_ptr.h"
#include "args_parser.h"
#include "benchmark.h"
#include "benchmark_suites_collection.h"
#include "utils.h"
#include "scope.h"
#include "benchmark_suite.h"
using namespace std;
extern void check_parser();
int main(int argc, char * *argv)
{
bool no_mpi_init_flag = true;
int return_value = 0;
int rank = 0, size = 0;
const char *program_name = "Intel(R) MPI Benchmarks 2021.8";
std::ostringstream output;
// Some unit tests for args parser
#if 0
check_parser();
return 1;
#endif
try {
// Allow very first init steps for each suite -- each benchmark
// is allowed to init flags and do other fundamental things before MPI_Init and
// even args parsing
BenchmarkSuitesCollection::init_registered_suites();
// Do basic initialisation of exapected args
//args_parser parser(argc, argv, "/", ':');
//args_parser parser(argc, argv, "--", '=');
args_parser parser(argc, argv, "-", ' ', output);
parser.set_program_name(program_name);
parser.set_flag(args_parser::ALLOW_UNEXPECTED_ARGS);
parser.add<string>("thread_level", "single").
set_caption("single|funneled|serialized|multiple|nompinit").
set_description(
"Sets up the type of MPI_Init call to use:\n"
"single: MPI_Init\n"
"funneled: MPI_Init_thread with MPI_THREAD_FUNNELED\n"
"serialized: MPI_Init_thread with MPI_THREAD_SERIALIZED\n"
"multiple: MPI_Init_thread with MPI_THREAD_MULTIPLE\n"
"nompiinit: don't call MPI_Init (the MPI_Init call may be made then in error case\n"
"to prevent rubbish output\n");
parser.add<string>("input", "").set_caption("filename").
set_description(
"The argument after -input is a filename is any text file containing, line by line,\n"
"benchmark names facilitates running particular benchmarks as compared to\n"
"using the command line.\n"
"\n"
"default:\n"
"no input file exists\n");
parser.add_vector<string>("include", "").set_caption("benchmark[,benchmark,[...]").
set_description("The argument after -include is one or more benchmark names separated by comma");
parser.add_vector<string>("exclude", "").set_caption("benchmark[,benchmark,[...]").
set_description("The argument after -exclude is one or more benchmark names separated by comma");
// Extra non-option arguments
parser.set_current_group("EXTRA_ARGS");
parser.add_vector<string>("(benchmarks)", "").set_caption("benchmark[,benchmark,[...]]");
parser.set_default_current_group();
// Now fill in bechmark suite related args
if (!BenchmarkSuitesCollection::declare_args(parser, output)) {
throw runtime_error("one or more benchmark suites failed on options declaration stage");
}
// "system" option args to do special things, not dumped to files
parser.set_current_group("SYS");
#ifdef WITH_YAML_CPP
parser.add<string>("dump", "").set_caption("config.yaml").
set_description(
"Dump the YAML config file with the set of actual options for\n"
"the benchmark session. Parameter sets up the config file name\n");
parser.add<string>("load", "").set_caption("config.yaml").
set_description(
"Load session options from YAML config file given as a parameter\n");
#endif
parser.add_flag("list").
set_description(
"Prints out all the benchmark names available in this IMB build.\n"
"The information about the benchmarks suite each benchmark belongs to\n"
"and the benchmark description (if available) is printed out also\n");
parser.set_default_current_group();
if (!parser.parse()) {
throw 1;
}
#ifdef WITH_YAML_CPP
string infile;
infile = parser.get<string>("load");
if (infile != "") {
ifstream in(infile.c_str(), ios_base::in);
parser.load(in);
if (!parser.parse()) {
throw runtime_error("input config file parse error");
}
}
string outfile;
outfile = parser.get<string>("dump");
if (outfile != "") {
string out;
out = parser.dump();
ofstream of(outfile.c_str(), ios_base::out);
of << out;
}
#endif
vector<string> requested_benchmarks, to_include, to_exclude;
parser.get<string>("(benchmarks)", requested_benchmarks);
parser.get_unknown_args(requested_benchmarks);
parser.get<string>("include", to_include);
parser.get<string>("exclude", to_exclude);
string filename = parser.get<string>("input");
if (filename != "") {
FILE *t = fopen(filename.c_str(), "r");
if (t == NULL) {
throw runtime_error("can't open a file given in -input option");
}
char input_line[IMB_INPUT_ARG_LEN], name[IMB_INPUT_NAME_LEN];
while (fgets(input_line, IMB_INPUT_ARG_LEN, t)) {
if (input_line[0] != '#' && strlen(input_line) > 0) {
sscanf(input_line, "%31s[^\n]", name);
requested_benchmarks.push_back(name);
}
}
fclose(t);
}
// Complete benchmark list filling in: combine -input, -include, -exclude options,
// make sure all requested benchmarks are found
vector<string> default_benchmarks, all_benchmarks;
vector<string> actual_benchmark_list;
vector<string> benchmarks_to_run;
vector<string> missing;
map<string, set<string> > by_suite;
BenchmarkSuitesCollection::get_full_list(all_benchmarks, by_suite);
BenchmarkSuitesCollection::get_default_list(default_benchmarks);
if (parser.get<bool>("list")) {
output << program_name << endl;
output << "List of benchmarks:" << endl;
for (map<string, set<string> >::iterator it_s = by_suite.begin();
it_s != by_suite.end(); ++it_s) {
set<string> &benchmarks = it_s->second;
string sn = it_s->first;
if (sn == "__generic__")
continue;
output << sn << ":" << endl;
for (set<string>::iterator it_b = benchmarks.begin();
it_b != benchmarks.end(); ++it_b) {
smart_ptr<Benchmark> b = BenchmarkSuitesCollection::create(*it_b);
if (b.get() == NULL) exit(1);
string bn = b->get_name();
vector<string> comments = b->get_comments();
output << " " << bn;
if (!b->is_default()) output << " (non-default)";
output << endl;
for (size_t i = 0; i < comments.size(); i++)
output << " " << comments[i] << endl;
}
}
throw 0;
}
{
using namespace set_operations;
preprocess_list(requested_benchmarks);
preprocess_list(to_include);
preprocess_list(to_exclude);
preprocess_list(all_benchmarks);
preprocess_list(default_benchmarks);
if (requested_benchmarks.size() != 0) {
combine(to_include, requested_benchmarks);
} else {
combine(actual_benchmark_list, default_benchmarks);
}
exclude(to_include, to_exclude);
exclude(actual_benchmark_list, to_exclude);
combine(to_include, actual_benchmark_list);
actual_benchmark_list = to_include;
missing = actual_benchmark_list;
exclude(missing, all_benchmarks);
if (missing.size() != 0) {
exclude(actual_benchmark_list, missing);
if (actual_benchmark_list.size() == 0) {
combine(actual_benchmark_list, default_benchmarks);
}
}
// Change benchmark names to their canonical form
all_benchmarks.resize(0);
by_suite.clear();
BenchmarkSuitesCollection::get_full_list(all_benchmarks, by_suite);
for (size_t i = 0; i < actual_benchmark_list.size(); i++) {
string b = to_lower(actual_benchmark_list[i]);
for (size_t i = 0; i < all_benchmarks.size(); i++) {
if (to_lower(all_benchmarks[i]) == b) {
benchmarks_to_run.push_back(all_benchmarks[i]);
}
}
}
}
// Do aproppriate MPI_Init call
string mpi_init_mode = parser.get<string>("thread_level");
int required_mode, provided_mode;
if (mpi_init_mode == "single") {
no_mpi_init_flag = false;
required_mode = MPI_THREAD_SINGLE;
} else if (mpi_init_mode == "funneled") {
no_mpi_init_flag = false;
required_mode = MPI_THREAD_FUNNELED;
} else if (mpi_init_mode == "serialized") {
no_mpi_init_flag = false;
required_mode = MPI_THREAD_SERIALIZED;
} else if (mpi_init_mode == "multiple") {
no_mpi_init_flag = false;
required_mode = MPI_THREAD_MULTIPLE;
} else if (mpi_init_mode == "nompiinit") {
;
} else {
throw logic_error("wrong value of `thread_level' option");
}
if (!no_mpi_init_flag) {
MPI_Init_thread(&argc, (char ***)&argv, required_mode, &provided_mode);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (required_mode > provided_mode) {
throw logic_error("can't setup a required MPI threading mode");
}
}
//---------------------------------------------------------------------
// ACTUAL BENCHMARKING
//
// 1, Preparation phase on suite level
if (!BenchmarkSuitesCollection::prepare(parser, benchmarks_to_run, missing, output)) {
throw logic_error("One or more benchmark suites failed at preparation stage");
}
{
using namespace set_operations;
vector<string> benchmarks_to_exclude = benchmarks_to_run;
all_benchmarks.clear();
BenchmarkSuitesCollection::get_full_list(all_benchmarks, by_suite);
exclude(benchmarks_to_exclude, all_benchmarks);
exclude(benchmarks_to_run, benchmarks_to_exclude);
}
if (rank == 0) {
cout << output.str();
output.str("");
output.clear();
}
// 2. All benchmarks wrappers constructors, initializers and scope definition
typedef pair<smart_ptr<Benchmark>, smart_ptr<Scope> > item;
typedef vector<item> running_sequence;
running_sequence sequence;
for (vector<string>::iterator it = benchmarks_to_run.begin();
it != benchmarks_to_run.end(); ++it) {
string bn = *it;
smart_ptr<Benchmark> b = BenchmarkSuitesCollection::create(*it);
if (b.get() == NULL) {
throw logic_error("benchmark creator failed!");
}
b->init();
smart_ptr<Scope> scope = b->get_scope();
sequence.push_back(item(b, scope));
}
// 3. Actual running cycle
for (running_sequence::iterator it = sequence.begin(); it != sequence.end(); ++it) {
smart_ptr<Benchmark> &b = it->first;
smart_ptr<Scope> &scope = it->second;
for (Scope::iterator s = scope->begin(); s != scope->end(); ++s)
b->run(*s);
}
// 4. Finalize cycle
for (running_sequence::iterator it = sequence.begin(); it != sequence.end(); ++it) {
smart_ptr<Benchmark> &b = it->first;
b->finalize();
}
// 5. Final steps on suite-level
BenchmarkSuitesCollection::finalize(benchmarks_to_run, output);
if (rank == 0) {
cout << output.str();
output.str("");
output.clear();
}
}
catch(exception &ex) {
if (no_mpi_init_flag) {
MPI_Init(NULL, NULL);
no_mpi_init_flag = false;
}
if (!no_mpi_init_flag && rank == 0) {
cout << "EXCEPTION: " << ex.what() << endl;
cout << output.str();
}
return_value = 1;
}
catch(int ret) {
if (no_mpi_init_flag) {
MPI_Init(NULL, NULL);
no_mpi_init_flag = false;
}
if (!no_mpi_init_flag && rank == 0) {
cout << output.str();
}
return_value = ret;
}
if (!no_mpi_init_flag)
MPI_Finalize();
return return_value;
}