Skip to content

Commit cb7a4f5

Browse files
committed
Support customizable units in output
1 parent 8758066 commit cb7a4f5

File tree

2 files changed

+77
-38
lines changed

2 files changed

+77
-38
lines changed

src/Unit.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#pragma once
2+
#include <iostream>
3+
4+
enum { MegaByte, GigaByte, MibiByte, GibiByte };
5+
6+
// Units for output:
7+
struct Unit {
8+
int value;
9+
Unit(int v) : value(v) {}
10+
double fmt(double bytes) {
11+
switch(value) {
12+
case MibiByte: return pow(2.0, -20.0) * bytes;
13+
case MegaByte: return 1.0E-6 * bytes;
14+
case GibiByte: return pow(2.0, -30.0) * bytes;
15+
case GigaByte: return 1.0E-9 * bytes;
16+
default: std::cerr << "Unimplemented!" << std::endl; abort();
17+
}
18+
}
19+
char const* str() {
20+
switch(value) {
21+
case MibiByte: return "MiB";
22+
case MegaByte: return "MB";
23+
case GibiByte: return "GiB";
24+
case GigaByte: return "GB";
25+
default: std::cerr << "Unimplemented!" << std::endl; abort();
26+
}
27+
}
28+
Unit kibi() {
29+
switch(value) {
30+
case MegaByte: return Unit(MibiByte);
31+
case GigaByte: return Unit(GibiByte);
32+
default: return *this;
33+
}
34+
}
35+
Unit byte() {
36+
switch(value) {
37+
case MibiByte: return Unit(MegaByte);
38+
case GibiByte: return Unit(GigaByte);
39+
default: return *this;
40+
}
41+
}
42+
char const* lower() {
43+
switch(value) {
44+
case MibiByte: return "mibytes";
45+
case MegaByte: return "mbytes";
46+
case GibiByte: return "gibytes";
47+
case GigaByte: return "gbytes";
48+
default: std::cerr << "Unimplemented!" << std::endl; abort();
49+
}
50+
}
51+
};

src/main.cpp

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "Stream.h"
2020
#include "StreamModels.h"
21+
#include "Unit.h"
2122

2223
using namespace std;
2324

@@ -27,7 +28,7 @@ unsigned int num_times = 100;
2728
unsigned int deviceIndex = 0;
2829
bool use_float = false;
2930
bool output_as_csv = false;
30-
bool mibibytes = false;
31+
Unit unit = MegaByte;
3132
string csv_separator = ",";
3233

3334
// Benchmarks:
@@ -86,9 +87,6 @@ void check_solution(const unsigned int ntimes, vector<T>& a, vector<T>& b, vecto
8687
template <typename T>
8788
void run();
8889

89-
// Units for output:
90-
enum class Unit { Mega, Giga };
91-
9290
void parseArguments(int argc, char *argv[]);
9391

9492
int main(int argc, char *argv[])
@@ -155,31 +153,16 @@ void run()
155153
streamsize ss = cout.precision();
156154

157155
// Formatting utilities:
158-
auto fmt_unit = [](double bytes, Unit unit = Unit::Mega) {
159-
switch(unit) {
160-
case Unit::Mega: return (mibibytes? pow(2.0, -20.0) : 1.0E-6) * bytes;
161-
case Unit::Giga: return (mibibytes? pow(2.0, -30.0) : 1.0E-9) * bytes;
162-
default: cerr << "Unimplemented!" << endl; abort();
163-
}
164-
};
165-
auto unit_label = [](Unit unit = Unit::Mega) {
166-
switch(unit) {
167-
case Unit::Mega: return mibibytes? "MiB" : "MB";
168-
case Unit::Giga: return mibibytes? "GiB" : "GB";
169-
default: cerr << "Unimplemented!" << endl; abort();
170-
}
171-
};
172-
auto fmt_bw = [&](size_t weight, double dt, Unit unit = Unit::Mega) {
173-
return fmt_unit((weight * sizeof(T) * ARRAY_SIZE)/dt, unit);
156+
auto fmt_bw = [&](size_t weight, double dt) {
157+
return unit.fmt((weight * sizeof(T) * ARRAY_SIZE)/dt);
174158
};
175-
// Output formatting:
176159
auto fmt_csv_header = [] {
177160
cout
178161
<< "function" << csv_separator
179162
<< "num_times" << csv_separator
180163
<< "n_elements" << csv_separator
181164
<< "sizeof" << csv_separator
182-
<< ((mibibytes) ? "max_mibytes_per_sec" : "max_mbytes_per_sec") << csv_separator
165+
<< "max_" << unit.lower() << "_per_sec" << csv_separator
183166
<< "min_runtime" << csv_separator
184167
<< "max_runtime" << csv_separator
185168
<< "avg_runtime" << endl;
@@ -224,10 +207,8 @@ void run()
224207

225208
size_t nbytes = ARRAY_SIZE*sizeof(T);
226209
cout << setprecision(1) << fixed
227-
<< "Array size: " << fmt_unit(nbytes, Unit::Mega) << " " << unit_label(Unit::Mega)
228-
<< " (=" << fmt_unit(nbytes, Unit::Giga) << " " << unit_label(Unit::Giga) << ")" << endl;
229-
cout << "Total size: " << fmt_unit(3.0*nbytes, Unit::Mega) << " " << unit_label(Unit::Mega)
230-
<< " (=" << fmt_unit(3.0*nbytes, Unit::Giga) << " " << unit_label(Unit::Giga) << ")" << endl;
210+
<< "Array size: " << unit.fmt(nbytes) << " " << unit.str() << endl;
211+
cout << "Total size: " << unit.fmt(3.0*nbytes) << " " << unit.str() << endl;
231212
cout.precision(ss);
232213
}
233214

@@ -256,22 +237,14 @@ void run()
256237
{
257238
cout << "Init: "
258239
<< setw(7)
259-
<< initElapsedS
260-
<< " s (="
261-
<< initBWps
262-
<< (mibibytes ? " MiBytes/sec" : " MBytes/sec")
263-
<< ")" << endl;
240+
<< initElapsedS << " s (=" << initBWps << " " << unit.str() << "/s" << ")" << endl;
264241
cout << "Read: "
265242
<< setw(7)
266-
<< readElapsedS
267-
<< " s (="
268-
<< readBWps
269-
<< (mibibytes ? " MiBytes/sec" : " MBytes/sec")
270-
<< ")" << endl;
243+
<< readElapsedS << " s (=" << readBWps << " " << unit.str() << "/s" << ")" << endl;
271244

272245
cout
273246
<< left << setw(12) << "Function"
274-
<< left << setw(12) << ((mibibytes) ? "MiBytes/sec" : "MBytes/sec")
247+
<< left << setw(12) << (string(unit.str()) + "/s")
275248
<< left << setw(12) << "Min (sec)"
276249
<< left << setw(12) << "Max"
277250
<< left << setw(12) << "Average"
@@ -459,7 +432,19 @@ void parseArguments(int argc, char *argv[])
459432
}
460433
else if (!string("--mibibytes").compare(argv[i]))
461434
{
462-
mibibytes = true;
435+
unit = Unit(MibiByte);
436+
}
437+
else if (!string("--megabytes").compare(argv[i]))
438+
{
439+
unit = Unit(MegaByte);
440+
}
441+
else if (!string("--gibibytes").compare(argv[i]))
442+
{
443+
unit = Unit(GibiByte);
444+
}
445+
else if (!string("--gigabytes").compare(argv[i]))
446+
{
447+
unit = Unit(GigaByte);
463448
}
464449
else if (!string("--help").compare(argv[i]) ||
465450
!string("-h").compare(argv[i]))
@@ -476,7 +461,10 @@ void parseArguments(int argc, char *argv[])
476461
cout << " -o --only NAME Only run one benchmark (see --print-names)" << endl;
477462
cout << " --print-names Prints all available benchmark names" << endl;
478463
cout << " --csv Output as csv table" << endl;
464+
cout << " --megabytes Use MB=10^6 for bandwidth calculation (default)" << endl;
479465
cout << " --mibibytes Use MiB=2^20 for bandwidth calculation (default MB=10^6)" << endl;
466+
cout << " --gigibytes Use GiB=2^30 for bandwidth calculation (default MB=10^6)" << endl;
467+
cout << " --gigabytes Use GB=10^9 for bandwidth calculation (default MB=10^6)" << endl;
480468
cout << endl;
481469
exit(EXIT_SUCCESS);
482470
}

0 commit comments

Comments
 (0)