diff options
author | Manjunath Kudlur <keveman@gmail.com> | 2015-11-06 16:27:58 -0800 |
---|---|---|
committer | Manjunath Kudlur <keveman@gmail.com> | 2015-11-06 16:27:58 -0800 |
commit | f41959ccb2d9d4c722fe8fc3351401d53bcf4900 (patch) | |
tree | ef0ca22cb2a5ac4bdec9d080d8e0788a53ed496d /tensorflow/stream_executor/lib/human_readable.h |
TensorFlow: Initial commit of TensorFlow library.
TensorFlow is an open source software library for numerical computation
using data flow graphs.
Base CL: 107276108
Diffstat (limited to 'tensorflow/stream_executor/lib/human_readable.h')
-rw-r--r-- | tensorflow/stream_executor/lib/human_readable.h | 58 |
1 files changed, 58 insertions, 0 deletions
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 <assert.h> +#include <limits> + +#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<int64>::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 <typename T> + 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_ |