Move the HumanSize function to Logging.cxx as it is only used by the logger

This commit is contained in:
Robert Maynard 2019-01-04 16:40:56 -05:00
parent 9cb099a83d
commit 543255c374
4 changed files with 77 additions and 72 deletions

@ -33,6 +33,7 @@
#include <vtkm/testing/Testing.h>
#include <iomanip>
#include <iostream>
#include <sstream>
@ -83,8 +84,9 @@ struct MeasureCopySpeed
vtkm::UInt64 actualSize = sizeof(ValueType);
actualSize *= static_cast<vtkm::UInt64>(this->Source.GetNumberOfValues());
std::ostringstream out;
out << "Copying " << HumanSize(this->NumBytes) << " (actual=" << HumanSize(actualSize)
<< ") of " << vtkm::testing::TypeName<ValueType>::Name() << "\n";
out << "Copying " << vtkm::cont::GetHumanReadableSize(this->NumBytes)
<< " (actual=" << vtkm::cont::GetHumanReadableSize(actualSize) << ") of "
<< vtkm::testing::TypeName<ValueType>::Name() << "\n";
return out.str();
}
};
@ -123,14 +125,14 @@ void BenchmarkValueType()
{
bench.GatherSamples(functor);
vtkm::Float64 speed = static_cast<Float64>(size) / stats::Mean(bench.GetSamples());
speedStr = HumanSize(static_cast<UInt64>(speed)) + std::string("/s");
speedStr = vtkm::cont::GetHumanReadableSize(static_cast<UInt64>(speed)) + std::string("/s");
}
catch (vtkm::cont::ErrorBadAllocation&)
{
speedStr = "[allocation too large]";
}
PrintRow(std::cout, HumanSize(size), speedStr);
PrintRow(std::cout, vtkm::cont::GetHumanReadableSize(size), speedStr);
}
std::cout << "\n";

@ -295,7 +295,9 @@ private:
vtkm::Id arraySize = Config.ComputeSize<Value>();
std::stringstream description;
description << "Copy " << arraySize << " values ("
<< HumanSize(static_cast<vtkm::UInt64>(arraySize) * sizeof(Value)) << ")";
<< vtkm::cont::GetHumanReadableSize(static_cast<vtkm::UInt64>(arraySize) *
sizeof(Value))
<< ")";
return description.str();
}
};
@ -340,8 +342,9 @@ private:
vtkm::Id arraySize = Config.ComputeSize<Value>();
std::stringstream description;
description << "CopyIf on " << arraySize << " values ("
<< HumanSize(static_cast<vtkm::UInt64>(arraySize) * sizeof(Value)) << ") with "
<< PERCENT_VALID << "% valid values";
<< vtkm::cont::GetHumanReadableSize(static_cast<vtkm::UInt64>(arraySize) *
sizeof(Value))
<< ") with " << PERCENT_VALID << "% valid values";
return description.str();
}
};
@ -396,8 +399,9 @@ private:
vtkm::Id arraySize = Config.ComputeSize<Value>();
std::stringstream description;
description << "LowerBounds on " << arraySize << " input values ("
<< "(" << HumanSize(static_cast<vtkm::UInt64>(arraySize) * sizeof(Value)) << ") ("
<< PERCENT_VALUES << "% configuration)";
<< "(" << vtkm::cont::GetHumanReadableSize(static_cast<vtkm::UInt64>(arraySize) *
sizeof(Value))
<< ") (" << PERCENT_VALUES << "% configuration)";
return description.str();
}
};
@ -454,7 +458,9 @@ private:
vtkm::Id arraySize = Config.ComputeSize<Value>();
std::stringstream description;
description << "Reduce on " << arraySize << " values ("
<< HumanSize(static_cast<vtkm::UInt64>(arraySize) * sizeof(Value)) << ")";
<< vtkm::cont::GetHumanReadableSize(static_cast<vtkm::UInt64>(arraySize) *
sizeof(Value))
<< ")";
return description.str();
}
};
@ -499,8 +505,9 @@ private:
vtkm::Id arraySize = Config.ComputeSize<Value>();
std::stringstream description;
description << "ReduceByKey on " << arraySize << " values ("
<< HumanSize(static_cast<vtkm::UInt64>(arraySize) * sizeof(Value)) << ") with "
<< N_KEYS << " (" << PERCENT_KEYS << "%) distinct vtkm::Id keys";
<< vtkm::cont::GetHumanReadableSize(static_cast<vtkm::UInt64>(arraySize) *
sizeof(Value))
<< ") with " << N_KEYS << " (" << PERCENT_KEYS << "%) distinct vtkm::Id keys";
return description.str();
}
};
@ -546,7 +553,9 @@ private:
vtkm::Id arraySize = Config.ComputeSize<Value>();
std::stringstream description;
description << "ScanInclusive on " << arraySize << " values ("
<< HumanSize(static_cast<vtkm::UInt64>(arraySize) * sizeof(Value)) << ")";
<< vtkm::cont::GetHumanReadableSize(static_cast<vtkm::UInt64>(arraySize) *
sizeof(Value))
<< ")";
return description.str();
}
};
@ -582,7 +591,9 @@ private:
vtkm::Id arraySize = Config.ComputeSize<Value>();
std::stringstream description;
description << "ScanExclusive on " << arraySize << " values ("
<< HumanSize(static_cast<vtkm::UInt64>(arraySize) * sizeof(Value)) << ")";
<< vtkm::cont::GetHumanReadableSize(static_cast<vtkm::UInt64>(arraySize) *
sizeof(Value))
<< ")";
return description.str();
}
};
@ -624,7 +635,9 @@ private:
vtkm::Id arraySize = Config.ComputeSize<Value>();
std::stringstream description;
description << "Sort on " << arraySize << " random values ("
<< HumanSize(static_cast<vtkm::UInt64>(arraySize) * sizeof(Value)) << ")";
<< vtkm::cont::GetHumanReadableSize(static_cast<vtkm::UInt64>(arraySize) *
sizeof(Value))
<< ")";
return description.str();
}
};
@ -677,8 +690,9 @@ private:
vtkm::Id arraySize = Config.ComputeSize<Value>();
std::stringstream description;
description << "SortByKey on " << arraySize << " random values ("
<< HumanSize(static_cast<vtkm::UInt64>(arraySize) * sizeof(Value)) << ") with "
<< N_KEYS << " (" << PERCENT_KEYS << "%) different vtkm::Id keys";
<< vtkm::cont::GetHumanReadableSize(static_cast<vtkm::UInt64>(arraySize) *
sizeof(Value))
<< ") with " << N_KEYS << " (" << PERCENT_KEYS << "%) different vtkm::Id keys";
return description.str();
}
};
@ -734,7 +748,9 @@ private:
vtkm::Id arraySize = Config.ComputeSize<Value>();
std::stringstream description;
description << "StableSortIndices::Sort on " << arraySize << " random values ("
<< HumanSize(static_cast<vtkm::UInt64>(arraySize) * sizeof(Value)) << ")";
<< vtkm::cont::GetHumanReadableSize(static_cast<vtkm::UInt64>(arraySize) *
sizeof(Value))
<< ")";
return description.str();
}
};
@ -781,8 +797,9 @@ private:
vtkm::Id arraySize = Config.ComputeSize<Value>();
std::stringstream description;
description << "StableSortIndices::Unique on " << arraySize << " values ("
<< HumanSize(static_cast<vtkm::UInt64>(arraySize) * sizeof(Value)) << ") with "
<< this->N_VALID << " (" << PERCENT_VALID << "%) valid values";
<< vtkm::cont::GetHumanReadableSize(static_cast<vtkm::UInt64>(arraySize) *
sizeof(Value))
<< ") with " << this->N_VALID << " (" << PERCENT_VALID << "%) valid values";
return description.str();
}
};
@ -837,8 +854,9 @@ private:
vtkm::Id arraySize = Config.ComputeSize<Value>();
std::stringstream description;
description << "Unique on " << arraySize << " values ("
<< HumanSize(static_cast<vtkm::UInt64>(arraySize) * sizeof(Value)) << ") with "
<< N_VALID << " (" << PERCENT_VALID << "%) valid values";
<< vtkm::cont::GetHumanReadableSize(static_cast<vtkm::UInt64>(arraySize) *
sizeof(Value))
<< ") with " << N_VALID << " (" << PERCENT_VALID << "%) valid values";
return description.str();
}
};
@ -894,7 +912,9 @@ private:
std::stringstream description;
description << "UpperBounds on " << arraySize << " input and " << N_VALS << " ("
<< PERCENT_VALS << "%) values (input array size: "
<< HumanSize(static_cast<vtkm::UInt64>(arraySize) * sizeof(Value)) << ")";
<< vtkm::cont::GetHumanReadableSize(static_cast<vtkm::UInt64>(arraySize) *
sizeof(Value))
<< ")";
return description.str();
}
};

@ -37,9 +37,9 @@
#endif // VTKM_ENABLE_LOGGING
#include <vtkm/testing/Testing.h> // for HumanSize
#include <cassert>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <type_traits>
#include <unordered_map>
@ -215,6 +215,36 @@ std::string GetStackTrace(vtkm::Int32 skip)
return result;
}
namespace
{
/// Convert a size in bytes to a human readable string (e.g. "64 bytes",
/// "1.44 MiB", "128 GiB", etc). @a prec controls the fixed point precision
/// of the stringified number.
inline VTKM_CONT std::string HumanSize(vtkm::UInt64 bytes, int prec = 2)
{
std::int64_t current = bytes;
std::int64_t previous = bytes;
constexpr const char* units[] = { "bytes", "KiB", "MiB", "GiB", "TiB", "PiB" };
//this way reduces the number of float divisions we do
int i = 0;
while (current > 1024)
{
previous = current;
current = current >> 10; //shift up by 1024
++i;
}
const double bytesf =
(i == 0) ? static_cast<double>(previous) : static_cast<double>(previous) / 1024.;
std::ostringstream out;
out << std::fixed << std::setprecision(prec) << bytesf << " " << units[i];
return out.str();
}
}
VTKM_CONT
std::string GetHumanReadableSize(vtkm::UInt64 bytes, int prec)
{

@ -33,7 +33,6 @@
#include <vtkm/VecTraits.h>
#include <exception>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
@ -697,50 +696,4 @@ static inline VTKM_CONT bool test_equal_portals(const PortalType1& portal1,
return true;
}
/// Convert a size in bytes to a human readable string (e.g. "64 bytes",
/// "1.44 MiB", "128 GiB", etc). @a prec controls the fixed point precision
/// of the stringified number.
static inline VTKM_CONT std::string HumanSize(vtkm::UInt64 bytes, int prec = 2)
{
std::string suffix = "bytes";
// Might truncate, but it really doesn't matter unless the precision arg
// is obscenely huge.
vtkm::Float64 bytesf = static_cast<vtkm::Float64>(bytes);
if (bytesf >= 1024.)
{
bytesf /= 1024.;
suffix = "KiB";
}
if (bytesf >= 1024.)
{
bytesf /= 1024.;
suffix = "MiB";
}
if (bytesf >= 1024.)
{
bytesf /= 1024.;
suffix = "GiB";
}
if (bytesf >= 1024.)
{
bytesf /= 1024.;
suffix = "TiB";
}
if (bytesf >= 1024.)
{
bytesf /= 1024.;
suffix = "PiB"; // Dream big...
}
std::ostringstream out;
out << std::fixed << std::setprecision(prec) << bytesf << " " << suffix;
return out.str();
}
#endif //vtk_m_testing_Testing_h