-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.hpp
195 lines (155 loc) · 5.12 KB
/
utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) Michael M. Magruder (https://github.com/mikemag)
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#pragma once
#include <climits>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <nlohmann/json.hpp>
#include <sstream>
#include <type_traits>
#include <unordered_map>
#include <vector>
#if defined(__CUDA_ARCH__)
#define CUDA_HOST_AND_DEVICE __device__ __host__
#else
#define CUDA_HOST_AND_DEVICE
#endif
using json = nlohmann::json;
// This embodies my love/hate relationship with C++. Simple shit just isn't simple :(
class comma_numpunct : public std::numpunct<char> {
protected:
virtual char do_thousands_sep() const { return ','; }
virtual std::string do_grouping() const { return "\03"; }
};
template <typename T>
std::string commaString(T i) {
std::locale comma_locale(std::locale(), new comma_numpunct());
std::stringstream ss;
ss.imbue(comma_locale);
ss << std::setprecision(2) << std::fixed << i;
return ss.str();
}
std::string commaString(float v);
std::string hexString(uint32_t v);
// A constexpr pow()
template <typename T>
constexpr T constPow(T num, T pow) {
return pow == 0 ? 1 : num * constPow(num, pow - 1);
}
// Round up to the next power of two
template <typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type,
typename = typename std::enable_if<std::is_unsigned<T>::value>::type>
CUDA_HOST_AND_DEVICE constexpr T nextPowerOfTwo(T value, unsigned maxb = sizeof(T) * CHAR_BIT, unsigned curb = 1) {
return maxb <= curb ? value : nextPowerOfTwo(((value - 1) | ((value - 1) >> curb)) + 1, maxb, curb << 1);
}
// Little constexprs which can be used from host and device. These should be in cuda::std, but aren't :(
namespace cudaExtra::std {
template <class T>
CUDA_HOST_AND_DEVICE static constexpr const T &min(const T &a, const T &b) {
return (b < a) ? b : a;
}
template <class T>
CUDA_HOST_AND_DEVICE static constexpr const T &clamp(const T &v, const T &lo, const T &hi) {
return v < lo ? lo : hi < v ? hi : v;
}
} // namespace cudaExtra::std
// Get basic info about the OS, hardware, machine, etc.
class OSInfo {
public:
std::map<std::string, json> info;
OSInfo();
std::ostream &dump(std::ostream &stream) const {
for (const auto &a : info) {
stream << a.first << ": " << a.second << std::endl;
}
return stream;
}
template <typename T>
T macOSSysctlByName(const std::string &name);
};
template <bool enabled>
std::ostream &operator<<(std::ostream &stream, const OSInfo &r) {
return r.dump(stream);
}
// Get basic info about the GPU, if present
class GPUInfo {
public:
std::map<std::string, json> info;
GPUInfo();
bool hasGPU() const { return hasGPU_; }
std::ostream &dump(std::ostream &stream) const {
for (const auto &a : info) {
stream << a.first << ": " << a.second << std::endl;
}
return stream;
}
private:
int _ConvertSMVer2Cores(int major, int minor);
void dumpDeviceInfo();
void loadDeviceInfo();
bool hasGPU_ = false;
};
template <bool enabled>
std::ostream &operator<<(std::ostream &stream, const GPUInfo &r) {
return r.dump(stream);
}
// A way to record various stats about a game, so we can write a nice json file of results.
// Note: the file is an array of objects. The first object is overall system info, then one object for each run
// completed. Each object is on its own line, and the array start and end are on their own lines. If we're interrupted
// while writing, the worst case is the last line is broken. That may be data for a completed run, and that's lost. Or
// it may be we're just missing the close "]" for the array. In either case, the other data is still readable a line at
// a time, and readers can deal with an incomplete file. This allows for interrupting and restarting runs.
class StatsRecorder {
public:
OSInfo osInfo;
GPUInfo gpuInfo;
explicit StatsRecorder(const std::string &filename) : filename(filename) {}
~StatsRecorder();
void newRun();
void add(const std::string &name, const json &value) { run[name] = value; }
private:
std::ofstream js;
std::string filename;
std::unordered_map<std::string, json> run;
void emitSysInfo();
};
// Counters for various experiments, no overhead if not enabled.
template <bool enabled>
class ExperimentCounter {
uint64_t value = 0;
const char *name;
public:
explicit ExperimentCounter(const char *name) : name(name) {}
ExperimentCounter<enabled> &operator++() {
if (enabled) {
++value;
}
return *this;
}
ExperimentCounter<enabled> &operator+=(int64_t rhs) {
if (enabled) {
value += rhs;
}
return *this;
}
std::ostream &dump(std::ostream &stream) const {
stream << name << ": ";
if (enabled) {
stream << commaString(value);
} else {
stream << "counter disabled";
}
return stream;
}
void record(StatsRecorder &sr) { sr.add(name, value); }
};
template <bool enabled>
std::ostream &operator<<(std::ostream &stream, const ExperimentCounter<enabled> &r) {
return r.dump(stream);
}
std::map<int, int> &getACrCache();