Home | History | Annotate | Download | only in lib
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #ifndef TENSORFLOW_STREAM_EXECUTOR_LIB_HUMAN_READABLE_H_
     17 #define TENSORFLOW_STREAM_EXECUTOR_LIB_HUMAN_READABLE_H_
     18 
     19 #include <assert.h>
     20 #include <limits>
     21 
     22 #include "tensorflow/stream_executor/lib/stringprintf.h"
     23 #include "tensorflow/stream_executor/platform/port.h"
     24 
     25 namespace perftools {
     26 namespace gputools {
     27 namespace port {
     28 
     29 class HumanReadableNumBytes {
     30  public:
     31   static string ToString(int64 num_bytes) {
     32     if (num_bytes == std::numeric_limits<int64>::min()) {
     33       // Special case for number with not representable nagation.
     34       return "-8E";
     35     }
     36 
     37     const char* neg_str = GetNegStr(&num_bytes);
     38 
     39     // Special case for bytes.
     40     if (num_bytes < 1024LL) {
     41       // No fractions for bytes.
     42       return port::Printf("%s%lldB", neg_str, num_bytes);
     43     }
     44 
     45     static const char units[] = "KMGTPE";  // int64 only goes up to E.
     46     const char* unit = units;
     47     while (num_bytes >= (1024LL) * (1024LL)) {
     48       num_bytes /= (1024LL);
     49       ++unit;
     50       assert(unit < units + sizeof(units));
     51     }
     52 
     53     return port::Printf(((*unit == 'K') ? "%s%.1f%c" : "%s%.2f%c"), neg_str,
     54                         num_bytes / 1024.0, *unit);
     55   }
     56 
     57  private:
     58   template <typename T>
     59   static const char* GetNegStr(T* value) {
     60     if (*value < 0) {
     61       *value = -(*value);
     62       return "-";
     63     } else {
     64       return "";
     65     }
     66   }
     67 };
     68 
     69 }  // namespace port
     70 }  // namespace gputools
     71 }  // namespace perftools
     72 
     73 #endif  // TENSORFLOW_STREAM_EXECUTOR_LIB_HUMAN_READABLE_H_
     74