From f41959ccb2d9d4c722fe8fc3351401d53bcf4900 Mon Sep 17 00:00:00 2001 From: Manjunath Kudlur Date: Fri, 6 Nov 2015 16:27:58 -0800 Subject: TensorFlow: Initial commit of TensorFlow library. TensorFlow is an open source software library for numerical computation using data flow graphs. Base CL: 107276108 --- tensorflow/stream_executor/lib/human_readable.h | 58 +++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tensorflow/stream_executor/lib/human_readable.h (limited to 'tensorflow/stream_executor/lib/human_readable.h') diff --git a/tensorflow/stream_executor/lib/human_readable.h b/tensorflow/stream_executor/lib/human_readable.h new file mode 100644 index 0000000000..78df4a4a70 --- /dev/null +++ b/tensorflow/stream_executor/lib/human_readable.h @@ -0,0 +1,58 @@ +#ifndef TENSORFLOW_STREAM_EXECUTOR_LIB_HUMAN_READABLE_H_ +#define TENSORFLOW_STREAM_EXECUTOR_LIB_HUMAN_READABLE_H_ + +#include +#include + +#include "tensorflow/stream_executor/lib/stringprintf.h" +#include "tensorflow/stream_executor/platform/port.h" + +namespace perftools { +namespace gputools { +namespace port { + +class HumanReadableNumBytes { + public: + static string ToString(int64 num_bytes) { + if (num_bytes == std::numeric_limits::min()) { + // Special case for number with not representable nagation. + return "-8E"; + } + + const char* neg_str = GetNegStr(&num_bytes); + + // Special case for bytes. + if (num_bytes < 1024LL) { + // No fractions for bytes. + return port::Printf("%s%lldB", neg_str, num_bytes); + } + + static const char units[] = "KMGTPE"; // int64 only goes up to E. + const char* unit = units; + while (num_bytes >= (1024LL) * (1024LL)) { + num_bytes /= (1024LL); + ++unit; + assert(unit < units + sizeof(units)); + } + + return port::Printf(((*unit == 'K') ? "%s%.1f%c" : "%s%.2f%c"), neg_str, + num_bytes / 1024.0, *unit); + } + + private: + template + static const char* GetNegStr(T* value) { + if (*value < 0) { + *value = -(*value); + return "-"; + } else { + return ""; + } + } +}; + +} // namespace port +} // namespace gputools +} // namespace perftools + +#endif // TENSORFLOW_STREAM_EXECUTOR_LIB_HUMAN_READABLE_H_ -- cgit v1.2.3