Skip to content

Commit 028c254

Browse files
committed
Add option to silence errors
1 parent fd952e5 commit 028c254

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

src/main.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// source code
66

77
#include <algorithm>
8+
#include <array>
89
#include <chrono>
910
#include <cmath>
1011
#include <cstring>
@@ -27,6 +28,7 @@ unsigned int deviceIndex = 0;
2728
bool use_float = false;
2829
bool output_as_csv = false;
2930
Unit unit = MegaByte;
31+
bool silence_errors = false;
3032
std::string csv_separator = ",";
3133

3234
// Benchmarks:
@@ -243,7 +245,7 @@ void run()
243245

244246
std::cout
245247
<< std::left << std::setw(12) << "Function"
246-
<< std::left << std::setw(12) << (string(unit.str()) + "/s")
248+
<< std::left << std::setw(12) << (std::string(unit.str()) + "/s")
247249
<< std::left << std::setw(12) << "Min (sec)"
248250
<< std::left << std::setw(12) << "Max"
249251
<< std::left << std::setw(12) << "Average"
@@ -307,26 +309,37 @@ void check_solution(const unsigned int ntimes, std::vector<T>& a, std::vector<T>
307309

308310
long double epsi = std::numeric_limits<T>::epsilon() * 100.0;
309311

310-
if (errA > epsi)
312+
bool failed = false;
313+
if (errA > epsi) {
314+
failed = true;
311315
std::cerr
312316
<< "Validation failed on a[]. Average error " << errA
313317
<< std::endl;
314-
if (errB > epsi)
318+
}
319+
if (errB > epsi) {
320+
failed = true;
315321
std::cerr
316322
<< "Validation failed on b[]. Average error " << errB
317323
<< std::endl;
318-
if (errC > epsi)
324+
}
325+
if (errC > epsi) {
326+
failed = true;
319327
std::cerr
320328
<< "Validation failed on c[]. Average error " << errC
321329
<< std::endl;
330+
}
322331
// Check sum to 8 decimal places
323-
if (selection == Benchmark::All && errSum > 1.0E-8)
332+
if (selection == Benchmark::All && errSum > epsi) {
333+
failed = true;
324334
std::cerr
325335
<< "Validation failed on sum. Error " << errSum
326336
<< std::endl << std::setprecision(15)
327337
<< "Sum was " << sum << " but should be " << goldSum
328338
<< std::endl;
339+
}
329340

341+
if (failed && !silence_errors)
342+
std::exit(EXIT_FAILURE);
330343
}
331344

332345
int parseUInt(const char *str, unsigned int *output)
@@ -401,7 +414,7 @@ void parseArguments(int argc, char *argv[])
401414
std::cerr << "Expected benchmark name after --only" << std::endl;
402415
exit(EXIT_FAILURE);
403416
}
404-
auto key = string(argv[i]);
417+
auto key = std::string(argv[i]);
405418
if (key == "Classic")
406419
{
407420
selection = Benchmark::Classic;
@@ -413,7 +426,7 @@ void parseArguments(int argc, char *argv[])
413426
else
414427
{
415428
auto p = find_if(labels.begin(), labels.end(), [&](char const* label) {
416-
return string(label) == key;
429+
return std::string(label) == key;
417430
});
418431
if (p == labels.end()) {
419432
std::cerr << "Unknown benchmark name \"" << argv[i] << "\" after --only" << std::endl;
@@ -433,18 +446,22 @@ void parseArguments(int argc, char *argv[])
433446
{
434447
unit = Unit(MibiByte);
435448
}
436-
else if (!string("--megabytes").compare(argv[i]))
449+
else if (!std::string("--megabytes").compare(argv[i]))
437450
{
438451
unit = Unit(MegaByte);
439452
}
440-
else if (!string("--gibibytes").compare(argv[i]))
453+
else if (!std::string("--gibibytes").compare(argv[i]))
441454
{
442455
unit = Unit(GibiByte);
443456
}
444-
else if (!string("--gigabytes").compare(argv[i]))
457+
else if (!std::string("--gigabytes").compare(argv[i]))
445458
{
446459
unit = Unit(GigaByte);
447460
}
461+
else if (!std::string("--silence-errors").compare(argv[i]))
462+
{
463+
silence_errors = true;
464+
}
448465
else if (!std::string("--help").compare(argv[i]) ||
449466
!std::string("-h").compare(argv[i]))
450467
{
@@ -464,13 +481,14 @@ void parseArguments(int argc, char *argv[])
464481
std::cout << " --mibibytes Use MiB=2^20 for bandwidth calculation (default MB=10^6)" << std::endl;
465482
std::cout << " --gigibytes Use GiB=2^30 for bandwidth calculation (default MB=10^6)" << std::endl;
466483
std::cout << " --gigabytes Use GB=10^9 for bandwidth calculation (default MB=10^6)" << std::endl;
484+
std::cout << " --silence-errors Ignores validation errors." << std::endl;
467485
std::cout << std::endl;
468486
std::exit(EXIT_SUCCESS);
469487
}
470488
else
471489
{
472490
std::cerr << "Unrecognized argument '" << argv[i] << "' (try '--help')"
473-
<< std::std::endl;
491+
<< std::endl;
474492
std::exit(EXIT_FAILURE);
475493
}
476494
}

0 commit comments

Comments
 (0)