Skip to content

Commit

Permalink
Support customizable units in output
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalobg committed Mar 8, 2024
1 parent 8758066 commit cb7a4f5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 38 deletions.
51 changes: 51 additions & 0 deletions src/Unit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once
#include <iostream>

enum { MegaByte, GigaByte, MibiByte, GibiByte };

// Units for output:
struct Unit {
int value;
Unit(int v) : value(v) {}
double fmt(double bytes) {
switch(value) {
case MibiByte: return pow(2.0, -20.0) * bytes;
case MegaByte: return 1.0E-6 * bytes;
case GibiByte: return pow(2.0, -30.0) * bytes;
case GigaByte: return 1.0E-9 * bytes;
default: std::cerr << "Unimplemented!" << std::endl; abort();
}
}
char const* str() {
switch(value) {
case MibiByte: return "MiB";
case MegaByte: return "MB";
case GibiByte: return "GiB";
case GigaByte: return "GB";
default: std::cerr << "Unimplemented!" << std::endl; abort();
}
}
Unit kibi() {
switch(value) {
case MegaByte: return Unit(MibiByte);
case GigaByte: return Unit(GibiByte);
default: return *this;
}
}
Unit byte() {
switch(value) {
case MibiByte: return Unit(MegaByte);
case GibiByte: return Unit(GigaByte);
default: return *this;
}
}
char const* lower() {
switch(value) {
case MibiByte: return "mibytes";
case MegaByte: return "mbytes";
case GibiByte: return "gibytes";
case GigaByte: return "gbytes";
default: std::cerr << "Unimplemented!" << std::endl; abort();
}
}
};
64 changes: 26 additions & 38 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "Stream.h"
#include "StreamModels.h"
#include "Unit.h"

using namespace std;

Expand All @@ -27,7 +28,7 @@ unsigned int num_times = 100;
unsigned int deviceIndex = 0;
bool use_float = false;
bool output_as_csv = false;
bool mibibytes = false;
Unit unit = MegaByte;
string csv_separator = ",";

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

// Units for output:
enum class Unit { Mega, Giga };

void parseArguments(int argc, char *argv[]);

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

// Formatting utilities:
auto fmt_unit = [](double bytes, Unit unit = Unit::Mega) {
switch(unit) {
case Unit::Mega: return (mibibytes? pow(2.0, -20.0) : 1.0E-6) * bytes;
case Unit::Giga: return (mibibytes? pow(2.0, -30.0) : 1.0E-9) * bytes;
default: cerr << "Unimplemented!" << endl; abort();
}
};
auto unit_label = [](Unit unit = Unit::Mega) {
switch(unit) {
case Unit::Mega: return mibibytes? "MiB" : "MB";
case Unit::Giga: return mibibytes? "GiB" : "GB";
default: cerr << "Unimplemented!" << endl; abort();
}
};
auto fmt_bw = [&](size_t weight, double dt, Unit unit = Unit::Mega) {
return fmt_unit((weight * sizeof(T) * ARRAY_SIZE)/dt, unit);
auto fmt_bw = [&](size_t weight, double dt) {
return unit.fmt((weight * sizeof(T) * ARRAY_SIZE)/dt);
};
// Output formatting:
auto fmt_csv_header = [] {
cout
<< "function" << csv_separator
<< "num_times" << csv_separator
<< "n_elements" << csv_separator
<< "sizeof" << csv_separator
<< ((mibibytes) ? "max_mibytes_per_sec" : "max_mbytes_per_sec") << csv_separator
<< "max_" << unit.lower() << "_per_sec" << csv_separator
<< "min_runtime" << csv_separator
<< "max_runtime" << csv_separator
<< "avg_runtime" << endl;
Expand Down Expand Up @@ -224,10 +207,8 @@ void run()

size_t nbytes = ARRAY_SIZE*sizeof(T);
cout << setprecision(1) << fixed
<< "Array size: " << fmt_unit(nbytes, Unit::Mega) << " " << unit_label(Unit::Mega)
<< " (=" << fmt_unit(nbytes, Unit::Giga) << " " << unit_label(Unit::Giga) << ")" << endl;
cout << "Total size: " << fmt_unit(3.0*nbytes, Unit::Mega) << " " << unit_label(Unit::Mega)
<< " (=" << fmt_unit(3.0*nbytes, Unit::Giga) << " " << unit_label(Unit::Giga) << ")" << endl;
<< "Array size: " << unit.fmt(nbytes) << " " << unit.str() << endl;
cout << "Total size: " << unit.fmt(3.0*nbytes) << " " << unit.str() << endl;
cout.precision(ss);
}

Expand Down Expand Up @@ -256,22 +237,14 @@ void run()
{
cout << "Init: "
<< setw(7)
<< initElapsedS
<< " s (="
<< initBWps
<< (mibibytes ? " MiBytes/sec" : " MBytes/sec")
<< ")" << endl;
<< initElapsedS << " s (=" << initBWps << " " << unit.str() << "/s" << ")" << endl;
cout << "Read: "
<< setw(7)
<< readElapsedS
<< " s (="
<< readBWps
<< (mibibytes ? " MiBytes/sec" : " MBytes/sec")
<< ")" << endl;
<< readElapsedS << " s (=" << readBWps << " " << unit.str() << "/s" << ")" << endl;

cout
<< left << setw(12) << "Function"
<< left << setw(12) << ((mibibytes) ? "MiBytes/sec" : "MBytes/sec")
<< left << setw(12) << (string(unit.str()) + "/s")
<< left << setw(12) << "Min (sec)"
<< left << setw(12) << "Max"
<< left << setw(12) << "Average"
Expand Down Expand Up @@ -459,7 +432,19 @@ void parseArguments(int argc, char *argv[])
}
else if (!string("--mibibytes").compare(argv[i]))
{
mibibytes = true;
unit = Unit(MibiByte);
}
else if (!string("--megabytes").compare(argv[i]))
{
unit = Unit(MegaByte);
}
else if (!string("--gibibytes").compare(argv[i]))
{
unit = Unit(GibiByte);
}
else if (!string("--gigabytes").compare(argv[i]))
{
unit = Unit(GigaByte);
}
else if (!string("--help").compare(argv[i]) ||
!string("-h").compare(argv[i]))
Expand All @@ -476,7 +461,10 @@ void parseArguments(int argc, char *argv[])
cout << " -o --only NAME Only run one benchmark (see --print-names)" << endl;
cout << " --print-names Prints all available benchmark names" << endl;
cout << " --csv Output as csv table" << endl;
cout << " --megabytes Use MB=10^6 for bandwidth calculation (default)" << endl;
cout << " --mibibytes Use MiB=2^20 for bandwidth calculation (default MB=10^6)" << endl;
cout << " --gigibytes Use GiB=2^30 for bandwidth calculation (default MB=10^6)" << endl;
cout << " --gigabytes Use GB=10^9 for bandwidth calculation (default MB=10^6)" << endl;
cout << endl;
exit(EXIT_SUCCESS);
}
Expand Down

0 comments on commit cb7a4f5

Please sign in to comment.