-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathch_frb_l1.hpp
293 lines (227 loc) · 8 KB
/
ch_frb_l1.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#ifndef _CH_FRB_L1_HPP
#define _CH_FRB_L1_HPP
#include <string>
#include <vector>
#include <cstring>
#include <stdexcept>
#include <unordered_set>
#include <yaml-cpp/yaml.h>
namespace ch_frb_l1 {
#if 0
} // compiler pacifier
#endif
// -------------------------------------------------------------------------------------------------
//
// file_utils.cpp
extern bool file_exists(const std::string &filename);
extern bool is_directory(const std::string &filename);
extern bool is_empty_directory(const std::string &dirname);
extern std::vector<std::string> listdir(const std::string &dirname);
extern size_t disk_space_used(const std::string &dirname);
// Note: umask will be applied to 'mode'
extern void makedir(const std::string &filename, bool throw_exception_if_directory_exists=true, mode_t mode=0777);
// "devname" should be either "ssd" or "nfs".
//
// There used to be an arbitrary string 'acqdir_base' here, but I got nervous since if someone accidentally specified
// a directory in the node's root filesystem (as opposed to local SSD or "large" NFS server), it would blow up the
// poor head node with more NFS traffic than it can handle!
//
// The only purpose of the 'stream_ids' argument is to check that /frb-archiver-X is mounted, for all X in stream_ids.
//
// If "new_acq" is false, assume that we're not starting a new
// acquisition, so it's okay if the directories are not empty. This
// is required, eg, if we're turning on or off different beams within
// an acq.
extern std::string acqname_to_filename_pattern(const std::string &devname,
const std::string &acqname,
const std::vector<int> &stream_ids,
const std::vector<int> &beam_ids,
bool new_acq=true);
// Converts "/local/acq_data/(ACQNAME)/beam_(BEAM)/chunk_(CHUNK).msg"
// to "/local/acq_data/(ACQNAME)"
extern std::string acq_pattern_to_dir(const std::string &pattern);
// -------------------------------------------------------------------------------------------------
//
// Inlines
inline int xdiv(int num, int den)
{
assert((num > 0) && (den > 0) && (num % den == 0));
return num / den;
}
inline bool is_power_of_two(int n)
{
assert(n >= 1);
return (n & (n-1)) == 0;
}
inline int round_up_to_power_of_two(double x)
{
assert(x > 0.0 && x < 1.0e9);
return 1 << int(ceil(log2(x)));
}
inline int round_down_to_power_of_two(double x)
{
assert(x >= 1.0 && x < 1.0e9);
return 1 << int(floor(log2(x)));
}
inline std::vector<int> vrange(int lo, int hi)
{
assert(lo <= hi);
std::vector<int> ret(hi-lo);
for (int i = lo; i < hi; i++)
ret[i-lo] = i;
return ret;
}
inline std::vector<int> vrange(int n)
{
return vrange(0,n);
}
template<typename T>
inline std::vector<T> vconcat(const std::vector<T> &v1, const std::vector<T> &v2)
{
size_t n1 = v1.size();
size_t n2 = v2.size();
std::vector<T> ret(n1+n2);
memcpy(&ret[0], &v1[0], n1 * sizeof(T));
memcpy(&ret[n1], &v2[0], n2 * sizeof(T));
return ret;
}
template<typename T>
inline bool vcontains(const std::vector<T> &v, T x)
{
for (size_t i = 0; i < v.size(); i++) {
if (v[i] == x)
return true;
}
return false;
}
// -------------------------------------------------------------------------------------------------
//
// yaml_paramfile
struct yaml_paramfile {
const std::string filename;
YAML::Node yaml;
int verbosity;
std::unordered_set<std::string> all_keys;
mutable std::unordered_set<std::string> requested_keys;
// The 'verbosity' constructor argument has the following meaning:
// 0 = quiet
// 1 = announce default values for all unspecified parameters
// 2 = announce all parameters
yaml_paramfile(const std::string &filename, int verbosity=0);
bool has_param(const std::string &k) const;
bool check_for_unused_params(bool fatal=true) const;
// For debugging and message-printing
virtual void _die(const std::string &txt) const; // by default, throws an exception
virtual void _print(const std::string &txt) const; // by default, prints to cout
template<typename T> static inline std::string type_name();
template<typename T> static inline std::string stringify(const T &x);
template<typename T> static inline std::string stringify(const std::vector<T> &x);
// _read_scalar1(): helper for read_scalar(), assumes key exists
template<typename T>
T _read_scalar1(const std::string &k) const
{
try {
return yaml[k].as<T> ();
}
catch (...) { }
_die(std::string("expected '") + k + std::string("' to have type ") + type_name<T>());
throw std::runtime_error("yaml_paramfile::_die() returned?!");
}
// _read_scalar2(): helper for read_scalar(), assumes key exists
template<typename T>
T _read_scalar2(const std::string &k) const
{
T ret = _read_scalar1<T> (k);
requested_keys.insert(k);
if (verbosity >= 2)
_print(k + " = " + stringify(ret) + "\n");
return ret;
}
// "Vanilla" version of read_scalar()
template<typename T>
T read_scalar(const std::string &k) const
{
if (!has_param(k))
_die("parameter '" + k + "' not found");
return _read_scalar2<T> (k);
}
// This version of read_scalar() has a default value, which is returned if the key is not found.
template<typename T>
T read_scalar(const std::string &k, T default_val) const
{
if (!has_param(k)) {
if (verbosity >= 1)
_print("parameter '" + k + "' not found, using default value " + stringify(default_val) + "\n");
return default_val;
}
return _read_scalar2<T> (k);
}
// _read_vector1(): helper for read_vector(), assumes key exists
// Automatically converts a scalar to length-1 vector.
template<typename T>
std::vector<T> _read_vector1(const std::string &k) const
{
try {
return yaml[k].as<std::vector<T>> ();
}
catch (...) { }
try {
return { yaml[k].as<T>() };
}
catch (...) { }
_die("expected '" + k + "' to have type " + type_name<T>() + ", or be a list of " + type_name<T>() + "s");
throw std::runtime_error("yaml_paramfile::_die() returned?!");
}
// _read_vector2(): helper for read_vector(), assumes key exists
template<typename T>
std::vector<T> _read_vector2(const std::string &k) const
{
std::vector<T> ret = _read_vector1<T> (k);
requested_keys.insert(k);
if (verbosity >= 2)
_print(k + " = " + stringify(ret) + "\n");
return ret;
}
// "Vanilla" version of read_vector().
// Automatically converts a scalar to length-1 vector.
template<typename T>
std::vector<T> read_vector(const std::string &k) const
{
if (!has_param(k))
_die("parameter '" + k + "' not found");
return _read_vector2<T> (k);
}
// This version of read_vector() has a default value, which is returned if the key is not found.
template<typename T>
std::vector<T> read_vector(const std::string &k, const std::vector<T> default_val) const
{
if (!has_param(k)) {
if (verbosity >= 1)
_print("parameter '" + k + "' not found, using default value " + stringify(default_val) + "\n");
return default_val;
}
return _read_vector2<T> (k);
}
};
template<> inline std::string yaml_paramfile::type_name<int> () { return "int"; }
template<> inline std::string yaml_paramfile::type_name<bool> () { return "bool"; }
template<> inline std::string yaml_paramfile::type_name<float> () { return "float"; }
template<> inline std::string yaml_paramfile::type_name<double> () { return "double"; }
template<> inline std::string yaml_paramfile::type_name<std::string> () { return "string"; }
template<typename T> inline std::string yaml_paramfile::stringify(const T &x)
{
std::stringstream ss;
ss << x;
return ss.str();
}
template<typename T> inline std::string yaml_paramfile::stringify(const std::vector<T> &x)
{
std::stringstream ss;
ss << "[ ";
for (size_t i = 0; i < x.size(); i++)
ss << (i ? ", " : "") << x[i];
ss << " ]";
return ss.str();
}
} // namespace ch_frb_l1
#endif // _CH_FRB_L1_HPP