From 543255c374dd39316ee0c37d7a3920e55f9b0606 Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Fri, 4 Jan 2019 16:40:56 -0500 Subject: [PATCH] Move the HumanSize function to Logging.cxx as it is only used by the logger --- benchmarking/BenchmarkCopySpeeds.cxx | 10 +++-- benchmarking/BenchmarkDeviceAdapter.cxx | 58 +++++++++++++++++-------- vtkm/cont/Logging.cxx | 34 ++++++++++++++- vtkm/testing/Testing.h | 47 -------------------- 4 files changed, 77 insertions(+), 72 deletions(-) diff --git a/benchmarking/BenchmarkCopySpeeds.cxx b/benchmarking/BenchmarkCopySpeeds.cxx index 034106dec..6b56f7025 100644 --- a/benchmarking/BenchmarkCopySpeeds.cxx +++ b/benchmarking/BenchmarkCopySpeeds.cxx @@ -33,6 +33,7 @@ #include +#include #include #include @@ -83,8 +84,9 @@ struct MeasureCopySpeed vtkm::UInt64 actualSize = sizeof(ValueType); actualSize *= static_cast(this->Source.GetNumberOfValues()); std::ostringstream out; - out << "Copying " << HumanSize(this->NumBytes) << " (actual=" << HumanSize(actualSize) - << ") of " << vtkm::testing::TypeName::Name() << "\n"; + out << "Copying " << vtkm::cont::GetHumanReadableSize(this->NumBytes) + << " (actual=" << vtkm::cont::GetHumanReadableSize(actualSize) << ") of " + << vtkm::testing::TypeName::Name() << "\n"; return out.str(); } }; @@ -123,14 +125,14 @@ void BenchmarkValueType() { bench.GatherSamples(functor); vtkm::Float64 speed = static_cast(size) / stats::Mean(bench.GetSamples()); - speedStr = HumanSize(static_cast(speed)) + std::string("/s"); + speedStr = vtkm::cont::GetHumanReadableSize(static_cast(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"; diff --git a/benchmarking/BenchmarkDeviceAdapter.cxx b/benchmarking/BenchmarkDeviceAdapter.cxx index f185f99c2..3cb5dd5af 100644 --- a/benchmarking/BenchmarkDeviceAdapter.cxx +++ b/benchmarking/BenchmarkDeviceAdapter.cxx @@ -295,7 +295,9 @@ private: vtkm::Id arraySize = Config.ComputeSize(); std::stringstream description; description << "Copy " << arraySize << " values (" - << HumanSize(static_cast(arraySize) * sizeof(Value)) << ")"; + << vtkm::cont::GetHumanReadableSize(static_cast(arraySize) * + sizeof(Value)) + << ")"; return description.str(); } }; @@ -340,8 +342,9 @@ private: vtkm::Id arraySize = Config.ComputeSize(); std::stringstream description; description << "CopyIf on " << arraySize << " values (" - << HumanSize(static_cast(arraySize) * sizeof(Value)) << ") with " - << PERCENT_VALID << "% valid values"; + << vtkm::cont::GetHumanReadableSize(static_cast(arraySize) * + sizeof(Value)) + << ") with " << PERCENT_VALID << "% valid values"; return description.str(); } }; @@ -396,8 +399,9 @@ private: vtkm::Id arraySize = Config.ComputeSize(); std::stringstream description; description << "LowerBounds on " << arraySize << " input values (" - << "(" << HumanSize(static_cast(arraySize) * sizeof(Value)) << ") (" - << PERCENT_VALUES << "% configuration)"; + << "(" << vtkm::cont::GetHumanReadableSize(static_cast(arraySize) * + sizeof(Value)) + << ") (" << PERCENT_VALUES << "% configuration)"; return description.str(); } }; @@ -454,7 +458,9 @@ private: vtkm::Id arraySize = Config.ComputeSize(); std::stringstream description; description << "Reduce on " << arraySize << " values (" - << HumanSize(static_cast(arraySize) * sizeof(Value)) << ")"; + << vtkm::cont::GetHumanReadableSize(static_cast(arraySize) * + sizeof(Value)) + << ")"; return description.str(); } }; @@ -499,8 +505,9 @@ private: vtkm::Id arraySize = Config.ComputeSize(); std::stringstream description; description << "ReduceByKey on " << arraySize << " values (" - << HumanSize(static_cast(arraySize) * sizeof(Value)) << ") with " - << N_KEYS << " (" << PERCENT_KEYS << "%) distinct vtkm::Id keys"; + << vtkm::cont::GetHumanReadableSize(static_cast(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(); std::stringstream description; description << "ScanInclusive on " << arraySize << " values (" - << HumanSize(static_cast(arraySize) * sizeof(Value)) << ")"; + << vtkm::cont::GetHumanReadableSize(static_cast(arraySize) * + sizeof(Value)) + << ")"; return description.str(); } }; @@ -582,7 +591,9 @@ private: vtkm::Id arraySize = Config.ComputeSize(); std::stringstream description; description << "ScanExclusive on " << arraySize << " values (" - << HumanSize(static_cast(arraySize) * sizeof(Value)) << ")"; + << vtkm::cont::GetHumanReadableSize(static_cast(arraySize) * + sizeof(Value)) + << ")"; return description.str(); } }; @@ -624,7 +635,9 @@ private: vtkm::Id arraySize = Config.ComputeSize(); std::stringstream description; description << "Sort on " << arraySize << " random values (" - << HumanSize(static_cast(arraySize) * sizeof(Value)) << ")"; + << vtkm::cont::GetHumanReadableSize(static_cast(arraySize) * + sizeof(Value)) + << ")"; return description.str(); } }; @@ -677,8 +690,9 @@ private: vtkm::Id arraySize = Config.ComputeSize(); std::stringstream description; description << "SortByKey on " << arraySize << " random values (" - << HumanSize(static_cast(arraySize) * sizeof(Value)) << ") with " - << N_KEYS << " (" << PERCENT_KEYS << "%) different vtkm::Id keys"; + << vtkm::cont::GetHumanReadableSize(static_cast(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(); std::stringstream description; description << "StableSortIndices::Sort on " << arraySize << " random values (" - << HumanSize(static_cast(arraySize) * sizeof(Value)) << ")"; + << vtkm::cont::GetHumanReadableSize(static_cast(arraySize) * + sizeof(Value)) + << ")"; return description.str(); } }; @@ -781,8 +797,9 @@ private: vtkm::Id arraySize = Config.ComputeSize(); std::stringstream description; description << "StableSortIndices::Unique on " << arraySize << " values (" - << HumanSize(static_cast(arraySize) * sizeof(Value)) << ") with " - << this->N_VALID << " (" << PERCENT_VALID << "%) valid values"; + << vtkm::cont::GetHumanReadableSize(static_cast(arraySize) * + sizeof(Value)) + << ") with " << this->N_VALID << " (" << PERCENT_VALID << "%) valid values"; return description.str(); } }; @@ -837,8 +854,9 @@ private: vtkm::Id arraySize = Config.ComputeSize(); std::stringstream description; description << "Unique on " << arraySize << " values (" - << HumanSize(static_cast(arraySize) * sizeof(Value)) << ") with " - << N_VALID << " (" << PERCENT_VALID << "%) valid values"; + << vtkm::cont::GetHumanReadableSize(static_cast(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(arraySize) * sizeof(Value)) << ")"; + << vtkm::cont::GetHumanReadableSize(static_cast(arraySize) * + sizeof(Value)) + << ")"; return description.str(); } }; diff --git a/vtkm/cont/Logging.cxx b/vtkm/cont/Logging.cxx index 75c41007d..b1088de85 100644 --- a/vtkm/cont/Logging.cxx +++ b/vtkm/cont/Logging.cxx @@ -37,9 +37,9 @@ #endif // VTKM_ENABLE_LOGGING -#include // for HumanSize - #include +#include +#include #include #include #include @@ -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(previous) : static_cast(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) { diff --git a/vtkm/testing/Testing.h b/vtkm/testing/Testing.h index b903fd406..63775fd7e 100644 --- a/vtkm/testing/Testing.h +++ b/vtkm/testing/Testing.h @@ -33,7 +33,6 @@ #include #include -#include #include #include #include @@ -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(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