-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_hdf5.hpp
450 lines (399 loc) · 11 KB
/
simple_hdf5.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#ifndef SIMPLE_HDF5_H_INCLUDED
#define SIMPLE_HDF5_H_INCLUDED
#include <H5Dpublic.h>
#include <H5Epublic.h>
#include <H5Fpublic.h>
#include <H5Gpublic.h>
#include <H5Tpublic.h>
#include <H5Spublic.h>
#include <H5Apublic.h>
#include <H5Ppublic.h>
#include <sstream>
template <typename T>
T clamp(const T& v, const T& a, const T& b)
{
return std::max(a, std::min(v, b));
}
namespace hdf5 {
/// @brief Temporarily disable HDF5 error printing
class mute_errors {
public:
mute_errors() {
H5Eget_auto2(H5E_DEFAULT, &old_func, &old_client_data);
H5Eset_auto2(H5E_DEFAULT, NULL, NULL);
}
~mute_errors() {
H5Eset_auto2(H5E_DEFAULT, old_func, old_client_data);
}
private:
herr_t (*old_func)(hid_t,void*);
void *old_client_data;
};
/// @brief A reference-counted handle
class handle {
public:
typedef int (*close_func)(hid_t);
/// @brief Initialize empty handle
handle() : id_(-1), close_(NULL) {}
/// @brief Initialize handle
/// @param[in] id Handle to adopt
/// @param[in] close Function to release handle
/// The close function will be called to release the object when the
/// reference count drops to 0
handle(hid_t id, close_func close) : id_(id), close_(close)
{
if (id_ < 0) {
close_ = NULL;
throw std::runtime_error("A library call failed!");
}
}
/// @brief Copy handle, increasing reference count
handle(const handle &other) : id_(other.id_), close_(other.close_)
{
if (id_ > 0) {
incref();
}
}
/// @brief Move handle, keeping reference count constant
handle(handle &&other) : id_(other.id_), close_(other.close_)
{
other.id_ = -1;
other.close_ = NULL;
}
/// @brief Destroy handle
/// If this is the last remaining reference to the held object, it will be
/// freed.
~handle()
{
if (refcount() > 1) {
decref();
} else if (close_ != NULL && id_ > 0) {
(*close_)(id_);
}
}
operator hid_t() const { return id_; }
/// @brief Is this handle still valid?
explicit operator bool() const { return id_ > 0; }
/// @brief Get the path name
std::string name() const
{
mute_errors muzzle;
std::string name;
ssize_t size = H5Iget_name(*this, NULL, 0);
if (size > 0) {
name.resize(size+1);
H5Iget_name(*this, &name[0], size+1);
}
return name;
}
private:
hid_t id_;
close_func close_;
int refcount() { return id_ > 0 ? H5Iget_ref(id_) : 0; }
void incref() { H5Iinc_ref(id_); }
void decref() { H5Idec_ref(id_); }
};
class Dataspace : public handle {
public:
Dataspace(std::vector<hsize_t> &&dims) :
handle(create(dims), H5Sclose)
{}
bool operator==(const Dataspace &other) const {
if (hid_t(*this) == hid_t(other))
return true;
std::vector<hsize_t> extent(this->get_extent()), other_extent(other.get_extent());
return (extent.size() == other_extent.size()) &&
std::equal(extent.begin(),extent.end(),other_extent.begin());
}
private:
std::vector<hsize_t> get_extent() const
{
std::vector<hsize_t> dims;
int size = H5Sget_simple_extent_ndims(*this);
if (size > 0) {
dims.resize(size);
H5Sget_simple_extent_dims(*this, dims.data(), NULL);
}
return dims;
}
static hid_t create(std::vector<hsize_t> &dims)
{
if (dims.size() == 0) {
return H5Screate(H5S_SCALAR);
} else {
return H5Screate_simple(dims.size(), dims.data(), NULL);
}
}
};
class Datatype : public handle {
public:
using handle::handle;
};
/// @brief Return the corresponding HDF5 data type
/// Specialize this for custom types
template <typename T>
Datatype
get_datatype(const T&);
template <>
Datatype
get_datatype(const double&) { return Datatype(H5T_NATIVE_DOUBLE, NULL); }
template <>
Datatype
get_datatype(const unsigned long&) { return Datatype(H5T_NATIVE_ULONG, NULL); }
template <>
Datatype
get_datatype(const std::string &v)
{
hid_t dt = H5Tcopy(H5T_C_S1);
H5Tset_size(dt, v.size()+1);
H5Tset_strpad(dt, H5T_STR_NULLTERM);
return Datatype(dt, H5Tclose);
}
template<>
Datatype
get_datatype(const std::vector<double> &v)
{ return get_datatype(double()); }
/// @brief Return the dimensions of the given object
template <typename T>
typename std::enable_if<!std::is_pod<T>::value, std::vector<hsize_t> >::type
get_shape(const T&);
template <typename T>
typename std::enable_if<std::is_pod<T>::value, std::vector<hsize_t> >::type
get_shape(const T &v)
{ return std::vector<hsize_t>(); }
template <>
std::vector<hsize_t>
get_shape(const std::string &v)
{ return std::vector<hsize_t>(); }
template <>
std::vector<hsize_t>
get_shape(const std::vector<double> &v)
{ return std::vector<hsize_t> {v.size()}; }
/// @brief Calculate an optimal chunk shape of the given size
template <typename T>
std::vector<hsize_t>
get_chunk_shape(const T& data, hsize_t max_chunk_size=size_t(2)<<15)
{
std::vector<hsize_t> shape(get_shape(data));
std::vector<hsize_t> chunk(shape.size());
hsize_t size = H5Tget_size(get_datatype(data));
for(hsize_t d : shape)
size *= d;
hsize_t chunk_size(std::min(max_chunk_size, size));
hsize_t cum_size = 1;
for (int i=shape.size()-1; i >= 0; i--) {
chunk[i] = clamp(chunk_size/cum_size, hsize_t(1), shape[i]);
cum_size *= shape[i];
}
return chunk;
}
/// @brief Return a pointer to the beginning of the object's continguous backing store
template <typename T>
const typename std::enable_if<!std::is_pod<T>::value, void>::type*
get_data(const T&);
template <typename T>
const typename std::enable_if<std::is_pod<T>::value, void>::type*
get_data(const T &v)
{ return &v; }
template <>
const void*
get_data(const std::string &v)
{ return v.c_str(); }
template<>
const void*
get_data(const std::vector<double> &v)
{ return v.data(); }
/// @brief A named path
class Node : public handle {
public:
using handle::handle;
class AttributeSet;
class AttributeProxy {
private:
AttributeProxy(handle &node, const std::string &name) : name_(name), node_(node)
{}
friend class AttributeSet;
public:
template <typename T>
T operator=(const T &value)
{
if (H5Aexists(node_, name_.c_str()) > 0) {
H5Adelete(node_, name_.c_str());
}
Dataspace dspace(get_shape(value));
Datatype dtype(get_datatype(value));
hid_t attr_id = H5Acreate2(node_, name_.c_str(), dtype, dspace, H5P_DEFAULT, H5P_DEFAULT);
H5Awrite(attr_id, dtype, get_data(value));
return value;
}
private:
std::string name_;
handle node_;
};
class AttributeSet {
private:
AttributeSet(handle parent) : parent_(parent)
{}
friend class Node;
public:
AttributeProxy operator[](const std::string &name)
{
return AttributeProxy(parent_, name);
}
private:
handle parent_;
};
AttributeSet attrs() const { return AttributeSet(*this); }
private:
handle parent_;
};
/// @brief A group
class Group : public Node {
public:
using Node::Node;
hsize_t num_children()
{
hsize_t n;
H5Gget_num_objs(*this, &n);
return n;
}
};
/// @brief A property list specification
class PropertyListClass : public handle {
public:
using handle::handle;
};
/// @brief A generic property list
class PropertyList : public handle {
public:
PropertyList() : handle(H5P_DEFAULT, NULL)
{}
protected:
PropertyList(hid_t type) : handle(H5Pcreate(type), H5Pclose)
{}
};
/// @brief Settings for dataset creation
class DatasetCreationProperties : public PropertyList {
public:
DatasetCreationProperties() : PropertyList(H5P_DATASET_CREATE) {}
void set_chunk(const std::vector<hsize_t> &chunk) { H5Pset_chunk(*this, chunk.size(), chunk.data()); }
void set_deflate(unsigned int complevel) { H5Pset_deflate(*this, complevel); }
void set_shuffle() { H5Pset_shuffle(*this); }
};
/// @brief A dataset
class Dataset : public Node {
public:
/// @brief Create a dataset
Dataset(Group group, const std::string &name, Datatype dtype, Dataspace dspace,
PropertyList link=PropertyList(), PropertyList creation=PropertyList(),
PropertyList access=PropertyList()) :
Node(H5Dcreate2(group, name.c_str(), dtype, dspace, link, creation, access), H5Dclose),
parent_(group)
{
// TODO: check return value
}
/// @brief Write data to dataset
template <typename T>
void write(const T& data)
{
Datatype dtype(get_datatype(data));
Dataspace dspace(get_shape(data));
// TODO: check return value
H5Dwrite(*this, dtype, dspace, H5S_ALL, H5P_DEFAULT, get_data(data));
}
private:
Group parent_;
};
class File : public handle {
public:
using handle::handle;
enum access { read, write, append };
public:
Group create_group(const std::string &where, const std::string &name, bool overwrite=false)
{
Group root = File::get_group(where);
if (overwrite) {
mute_errors muzzle;
if (H5Gunlink(root, name.c_str()) < 0)
H5Eclear2(H5E_DEFAULT);
}
return Group(H5Gcreate(root, name.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT), H5Gclose);
}
template <typename T>
Dataset create_carray(const std::string &where, const std::string &name, const T& object, bool overwrite=false)
{
Group group(H5Gopen(*this, where.c_str(), H5P_DEFAULT), H5Gclose);
return create_carray(group, name, object, overwrite);
}
/// @brief Create a chunked, compressed array
/// @param[in] where parent group
/// @param[in] name name of array
/// @param[in] object object to store. This determines the type and shape of the resulting array
/// @param[in] overwrite overwrite the existing dataset
template <typename T>
Dataset create_carray(Group where, const std::string &name, const T& object, bool overwrite=false)
{
Dataspace dspace(get_shape(object));
Datatype dtype(get_datatype(object));
DatasetCreationProperties plist;
plist.set_chunk(get_chunk_shape(object));
plist.set_shuffle();
plist.set_deflate(6);
if (overwrite) {
mute_errors muzzle;
if (H5Gunlink(where, name.c_str()) < 0)
H5Eclear2(H5E_DEFAULT);
}
Dataset dataset(where, name, dtype, dspace, PropertyList(), plist, PropertyList());
dataset.write(object);
return dataset;
}
private:
Group get_group(const std::string &where)
{
mute_errors muzzle;
hid_t gid = H5Gopen(*this, where.c_str(), H5P_DEFAULT);
if (gid < 0) {
// Node does not exist. Walk the requested path, creating parents
// as necessary.
hid_t parent = *this;
std::istringstream iss(where);
std::string chunk;
while (std::getline(iss, chunk, '/')) {
if (chunk.size() == 0)
continue;
gid = H5Gopen(parent, chunk.c_str(), H5P_DEFAULT);
if (gid < 0) {
gid = H5Gcreate(parent, chunk.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
}
H5Gclose(parent);
parent = gid;
}
H5Eclear2(H5E_DEFAULT);
}
return Group(gid, H5Gclose);
}
};
/// @brief Open a file for reading or writing
File open_file(const std::string &fname, File::access mode=File::read)
{
mute_errors muzzle;
hid_t id;
if (mode == File::read) {
id = H5Fopen(fname.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
} else if (mode == File::write) {
id = H5Fcreate(fname.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
} else {
id = H5Fopen(fname.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
if (id < 0) {
H5Eclear2(H5E_DEFAULT);
id = H5Fcreate(fname.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
}
}
if (id < 0) {
throw std::runtime_error("Couldn't create file");
}
return File(id, H5Fclose);
}
}
#endif // SIMPLE_HDF5_H_INCLUDED