NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mgpuutil.cpp
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 #include "util/format.h"
36 #include <vector_types.h>
37 #include <cstdarg>
38 #include <map>
39 
40 #define MGPU_RAND_NS std::tr1
41 
42 #ifdef _MSC_VER
43 #include <random>
44 #else
45 #include <tr1/random>
46 #endif
47 
48 namespace mgpu {
49 
51 // String formatting utilities.
52 
53 std::string stringprintf(const char* format, ...) {
54  va_list args;
55  va_start(args, format);
56  int len = vsnprintf(0, 0, format, args);
57  va_end(args);
58 
59  // allocate space.
60  std::string text;
61  text.resize(len);
62 
63  va_start(args, format);
64  vsnprintf(&text[0], len + 1, format, args);
65  va_end(args);
66 
67  return text;
68 }
69 
70 std::string FormatInteger(int64 x) {
71  std::string s;
72  if(x < 1000)
73  s = stringprintf("%6d", (int)x);
74  else if(x < 1000000) {
75  if(0 == (x % 1000))
76  s = stringprintf("%5dK", (int)(x / 1000));
77  else
78  s = stringprintf("%5.1lfK", x / 1.0e3);
79  } else if(x < 1000000000ll) {
80  if(0 == (x % 1000000ll))
81  s = stringprintf("%5dM", (int)(x / 1000000));
82  else
83  s = stringprintf("%5.1lfM", x / 1.0e6);
84  } else {
85  if(0 == (x % 1000000000ll))
86  s = stringprintf("%5dB", (int)(x / 1000000000ll));
87  else
88  s = stringprintf("%5.1lfB", x / 1.0e9);
89  }
90  return s;
91 }
92 
93 class TypeIdMap {
94  typedef std::map<std::string, const char*> Map;
95  Map _map;
96 
97  void Insert(const std::type_info& ti, const char* name) {
98  _map[ti.name()] = name;
99  }
100 public:
102  Insert(typeid(char), "char");
103  Insert(typeid(byte), "byte");
104  Insert(typeid(short), "short");
105  Insert(typeid(ushort), "ushort");
106  Insert(typeid(int), "int");
107  Insert(typeid(int64), "int64");
108  Insert(typeid(uint), "uint");
109  Insert(typeid(uint64), "uint64");
110  Insert(typeid(float), "float");
111  Insert(typeid(double), "double");
112  Insert(typeid(int2), "int2");
113  Insert(typeid(int3), "int3");
114  Insert(typeid(int4), "int4");
115  Insert(typeid(uint2), "uint2");
116  Insert(typeid(uint3), "uint3");
117  Insert(typeid(uint4), "uint4");
118  Insert(typeid(float2), "float2");
119  Insert(typeid(float3), "float3");
120  Insert(typeid(float4), "float4");
121  Insert(typeid(double2), "double2");
122  Insert(typeid(double3), "double3");
123  Insert(typeid(double4), "double4");
124  Insert(typeid(char*), "char*");
125  }
126  const char* name(const std::type_info& ti) {
127  const char* n = ti.name();
128  Map::iterator it = _map.find(n);
129  if(it != _map.end())
130  n = it->second;
131  return n;
132  }
133 };
134 
135 const char* TypeIdString(const std::type_info& ti) {
136  static TypeIdMap typeIdMap;
137  return typeIdMap.name(ti);
138 }
139 
141 // Random number generators.
142 
144 
145 int Rand(int min, int max) {
146  MGPU_RAND_NS::uniform_int<int> r(min, max);
147  return r(mt19937);
148 }
150  MGPU_RAND_NS::uniform_int<int64> r(min, max);
151  return r(mt19937);
152 }
154  MGPU_RAND_NS::uniform_int<uint> r(min, max);
155  return r(mt19937);
156 }
158  MGPU_RAND_NS::uniform_int<uint64> r(min, max);
159  return r(mt19937);
160 }
161 float Rand(float min, float max) {
162  MGPU_RAND_NS::uniform_real<float> r(min, max);
163  return r(mt19937);
164 }
165 double Rand(double min, double max) {
166  MGPU_RAND_NS::uniform_real<double> r(min, max);
167  return r(mt19937);
168 }
169 
170 } // namespace mgpu