NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
format.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the
12  * names of its contributors may be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  ******************************************************************************/
27 
28 /******************************************************************************
29  *
30  * Code and text by Sean Baxter, NVIDIA Research
31  * See http://nvlabs.github.io/moderngpu for repository and documentation.
32  *
33  ******************************************************************************/
34 
35 #pragma once
36 
37 #include "static.h"
38 #include <vector>
39 #include <cstdio>
40 #include <algorithm>
41 #include <string>
42 
43 namespace mgpu {
44 
45 // Like sprintf but dynamically allocates sufficient output to hold the entire
46 // text.
47 std::string stringprintf(const char* format, ...);
48 
49 // Returns xxx.xx(K|M|B)
50 std::string FormatInteger(int64 x);
51 
52 const char* TypeIdString(const std::type_info& ti);
53 
54 template<typename T>
55 const char* TypeIdName() {
56  return TypeIdString(typeid(T));
57 }
58 
60  const char* format;
61  FormatOpPrintf(const char* f) : format(f) { }
62 
63  template<typename T>
64  std::string operator()(int index, T x) const {
65  return stringprintf(format, x);
66  }
67 };
68 
70  const char* format;
71  FormatOpMaskBit(const char* f) : format(f) { }
72 
73  std::string operator()(int index, int x) const {
74  return stringprintf(format, (0x80000000 & x) ? '*' : ' ',
75  0x7fffffff & x);
76  }
77 };
78 
80  const char* format;
81  const int* marks;
82  int numMarks;
83 
84  FormatOpMarkArray(const char* f, const int* m, int n) :
85  format(f), marks(m), numMarks(n) { }
86 
87  std::string operator()(int index, int x) const {
88  // Search for index in the array of marks.
89  bool mark = std::binary_search(marks, marks + numMarks, index);
90  return stringprintf(format, mark ? '*' : ' ', x);
91  }
92 };
93 
94 template<typename T, typename Op>
95 std::string FormatArrayOp(const T* data, size_t count, Op op, int numCols) {
96  std::string s;
97  size_t numRows = MGPU_DIV_UP(count, numCols);
98  for(size_t row(0); row < numRows; ++row) {
99  size_t left = row * numCols;
100  s.append(stringprintf("%5d: ", left));
101 
102  for(size_t col(left); col < std::min(left + numCols, count); ++col) {
103  s.append(op(col, data[col]));
104  s.push_back(' ');
105  }
106  s.push_back('\n');
107  }
108  return s;
109 }
110 
111 template<typename T>
112 std::string FormatArray(const T* data, size_t count, const char* format,
113  int numCols) {
114  return FormatArrayOp(data, count, FormatOpPrintf(format), numCols);
115 }
116 
117 template<typename T>
118 std::string FormatArray(const std::vector<T>& data, const char* format,
119  int numCols) {
120  return FormatArray(&data[0], (int)data.size(), format, numCols);
121 }
122 template<typename T, typename Op>
123 std::string FormatArrayOp(const std::vector<T>& data, Op op, int numCols) {
124  return FormatArrayOp(&data[0], (int)data.size(), op, numCols);
125 }
126 
127 template<typename T>
128 void PrintArray(const T* data, size_t count, const char* format, int numCols) {
129  std::string s = FormatArray(data, count, format, numCols);
130  printf("%s", s.c_str());
131 }
132 
133 template<typename T>
134 void PrintArray(const std::vector<T>& data, const char* format, int numCols) {
135  std::string s = FormatArray(data, format, numCols);
136  printf("%s", s.c_str());
137 }
138 
139 template<typename T, typename Op>
140 void PrintArrayOp(const std::vector<T>& data, Op op, int numCols) {
141  std::string s = FormatArrayOp(data, op, numCols);
142  printf("%s", s.c_str());
143 }
144 
145 
146 
147 
148 } // namespace mgpu