18
18
19
19
#include " Stream.h"
20
20
#include " StreamModels.h"
21
+ #include " Unit.h"
21
22
22
23
using namespace std ;
23
24
@@ -27,7 +28,7 @@ unsigned int num_times = 100;
27
28
unsigned int deviceIndex = 0 ;
28
29
bool use_float = false ;
29
30
bool output_as_csv = false ;
30
- bool mibibytes = false ;
31
+ Unit unit = MegaByte ;
31
32
string csv_separator = " ," ;
32
33
33
34
// Benchmarks:
@@ -86,9 +87,6 @@ void check_solution(const unsigned int ntimes, vector<T>& a, vector<T>& b, vecto
86
87
template <typename T>
87
88
void run ();
88
89
89
- // Units for output:
90
- enum class Unit { Mega, Giga };
91
-
92
90
void parseArguments (int argc, char *argv[]);
93
91
94
92
int main (int argc, char *argv[])
@@ -155,31 +153,16 @@ void run()
155
153
streamsize ss = cout.precision ();
156
154
157
155
// 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);
174
158
};
175
- // Output formatting:
176
159
auto fmt_csv_header = [] {
177
160
cout
178
161
<< " function" << csv_separator
179
162
<< " num_times" << csv_separator
180
163
<< " n_elements" << csv_separator
181
164
<< " sizeof" << csv_separator
182
- << ((mibibytes) ? " max_mibytes_per_sec " : " max_mbytes_per_sec " ) << csv_separator
165
+ << " max_ " << unit. lower () << " _per_sec " << csv_separator
183
166
<< " min_runtime" << csv_separator
184
167
<< " max_runtime" << csv_separator
185
168
<< " avg_runtime" << endl;
@@ -224,10 +207,8 @@ void run()
224
207
225
208
size_t nbytes = ARRAY_SIZE*sizeof (T);
226
209
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;
231
212
cout.precision (ss);
232
213
}
233
214
@@ -256,22 +237,14 @@ void run()
256
237
{
257
238
cout << " Init: "
258
239
<< setw (7 )
259
- << initElapsedS
260
- << " s (="
261
- << initBWps
262
- << (mibibytes ? " MiBytes/sec" : " MBytes/sec" )
263
- << " )" << endl;
240
+ << initElapsedS << " s (=" << initBWps << " " << unit.str () << " /s" << " )" << endl;
264
241
cout << " Read: "
265
242
<< setw (7 )
266
- << readElapsedS
267
- << " s (="
268
- << readBWps
269
- << (mibibytes ? " MiBytes/sec" : " MBytes/sec" )
270
- << " )" << endl;
243
+ << readElapsedS << " s (=" << readBWps << " " << unit.str () << " /s" << " )" << endl;
271
244
272
245
cout
273
246
<< left << setw (12 ) << " Function"
274
- << left << setw (12 ) << ((mibibytes) ? " MiBytes/sec " : " MBytes/sec " )
247
+ << left << setw (12 ) << (string (unit. str ()) + " /s " )
275
248
<< left << setw (12 ) << " Min (sec)"
276
249
<< left << setw (12 ) << " Max"
277
250
<< left << setw (12 ) << " Average"
@@ -459,7 +432,19 @@ void parseArguments(int argc, char *argv[])
459
432
}
460
433
else if (!string (" --mibibytes" ).compare (argv[i]))
461
434
{
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);
463
448
}
464
449
else if (!string (" --help" ).compare (argv[i]) ||
465
450
!string (" -h" ).compare (argv[i]))
@@ -476,7 +461,10 @@ void parseArguments(int argc, char *argv[])
476
461
cout << " -o --only NAME Only run one benchmark (see --print-names)" << endl;
477
462
cout << " --print-names Prints all available benchmark names" << endl;
478
463
cout << " --csv Output as csv table" << endl;
464
+ cout << " --megabytes Use MB=10^6 for bandwidth calculation (default)" << endl;
479
465
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;
480
468
cout << endl;
481
469
exit (EXIT_SUCCESS);
482
470
}
0 commit comments