-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContainerInterface.h
700 lines (581 loc) · 24.6 KB
/
ContainerInterface.h
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#ifndef HDF5_CONTAINERINTERFACE_H_
#define HDF5_CONTAINERINTERFACE_H_
#include <hdf5.h>
#include "Exception.h"
#include <vector>
#include <list>
#include <map>
namespace hdf5 {
template<typename T> struct ContainerInterface {
typedef T Container;
/**
* Get the internal HDF5 type of the elements stored in the container
* @return HDF5 type identifier of element type
*/
static hid_t hdfElementType() { return -1; }
/**
* Creates an HDF5 dataspace fitting the container.
* @param src The container we want to convert to/from a HDF5 dataset
* @returns HDF5 dataspace identifier fitting the container
*/
static hid_t hdfSpace(const Container& src) { return -1; }
/**
* Performs the writing of the src container to the defined dataset
* @param src Container to write
* @param dataSet HDF5 identifier for the target dataset
* @param dataSpace HDF5 identifier fitting the container
*/
static void write(const Container& src, hid_t dataSet, hid_t dataSpace);
/**
* Reads the data from the dataSet into the dst container
* @param src Container to store the data from file
* @param dataSet HDF5 identifier for the target dataset
* @param dataSpace HDF5 identifier fitting the container
*/
static void read(Container& dst, hid_t dataSet, hid_t dataSpace);
};
/*
* Implementation for STL Containers
*/
// std::vector<ElementType>
template<typename ElementType, typename Allocator> struct ContainerInterface<std::vector<ElementType, Allocator> > {
typedef typename std::vector<ElementType, Allocator> Container;
static hid_t hdfElementType() { return DataType<ElementType>::hdfType(); }
static hid_t hdfSpace(const Container& src) {
hsize_t dims[] = { src.size(), };
// hsize_t maxDims[] = { H5S_UNLIMITED, };
return H5Screate_simple(1, dims, 0);
}
/**
* Check if the type of the provided dataset and the container matches
* @param src
* @param dataSet
* @param hdfMemLayout
* @return
*/
static bool checkCompatibility(const Container& src, hid_t dataSet, hid_t hdfMemLayout) {
const size_t NumDims = 1;
// check if type of the target dataset and the one stored in this container matches
htri_t type = H5Tequal(DataType<ElementType>::hdfType(), H5Dget_type(dataSet));
if (type < 1) {
throw Exception("HDF5 and Container element type declaration does not match");
}
// check if rank matches
int rank = H5Sget_simple_extent_ndims(hdfMemLayout);
if (rank < 0) {
throw Exception("Could not get dimensionality of dataset");
}
if ( (size_t)rank != NumDims) {
throw Exception("Rank of source and destination does not match! STL vectors are just of rank 1.");
}
// check dimensions
hsize_t* dims = new hsize_t[rank];
if (H5Sget_simple_extent_dims(hdfMemLayout, dims, 0) < 0) {
delete dims;
throw Exception("Could not retrieve size of dimensions");
}
bool sizeFit = dims[0] == src.size();
// freeing memory
free(dims);
if (!sizeFit) {
throw Exception("Dimensions between dataset and provided container does not match");
}
return true;
}
/**
* Performs the writing of the src container to the defined dataset
* @param src Container to write
* @param dataSet HDF5 identifier for the target dataset
* @param dataSpace HDF5 identifier fitting the container
*/
static void write(const Container& src, hid_t dataSet, hid_t dataSpace) {
// std::cout << "hdf5::ContainerInterface< std::vector<..> >::write()" << std::endl;
// checking compatibility of hdf5 target and the c++ src object
if (!checkCompatibility(src, dataSet, dataSpace)) {
throw Exception("hdf5::ContainerInterface< std::vector<..> >::write(): Type compatibility check failed");
}
// check type of elements stored in container
if (DataType<ElementType>::isStructType() && !DataType<ElementType>::isPOD()) {
// ok that's no very efficient but we have to copy the elements twice
// firstly we have to get the POD equivalent of the source type and fill it
// before writing data out
typedef typename DataType<ElementType>::PODType POD;
typedef typename std::vector<POD> DstContainer;
size_t nElements = src.size();
DstContainer dst;
dst.resize(nElements);
for (size_t i = 0; i < nElements; ++i) {
DataType<ElementType>::assignToPOD(src[i], dst[i]);
}
H5Dwrite(dataSet, DataType<ElementType>::hdfType(), H5S_ALL, H5S_ALL, H5P_DEFAULT, dst.data());
for (size_t i = 0; i < nElements; ++i) {
DataType<ElementType>::freePOD(dst[i]);
}
}
else {
// this allows simple write
H5Dwrite(dataSet, DataType<ElementType>::hdfType(), H5S_ALL, H5S_ALL, H5P_DEFAULT, src.data());
}
}
/**
* Reads the data from the dataSet into the dst container
* @param src Container to store the data from file
* @param dataSet HDF5 identifier for the target dataset
* @param dataSpace HDF5 identifier fitting the container
*/
static void read(Container& dst, hid_t dataSet, hid_t dataSpace) {
const size_t NumDims = 1;
// std::cout << "hdf5::ContainerInterface< std::vector<..> >::read()" << std::endl;
if (NumDims != H5Sget_simple_extent_ndims(dataSpace)) {
throw Exception("hdf5::ContainerInterface< std::vector<..> >::read(): Dimensions of HDF5 and target container does not match");
}
// resize vector to be fitting for data from file
hsize_t* dims = new hsize_t[NumDims];
H5Sget_simple_extent_dims(dataSpace, dims, 0);
dst.resize(dims[0]);
delete dims;
// checking compatibility of hdf5 target and the c++ src object
if (!checkCompatibility(dst, dataSet, dataSpace)) {
throw Exception("hdf5::ContainerInterface< std::vector<..> >::read(): Type compatibility check failed");
}
// check type of elements stored in container
// data can only be read to a POD structure therefore with use this...
typedef typename DataType<ElementType>::PODType POD;
POD* rawData = (POD*) malloc( DataType<ElementType>::size() * dst.size());
herr_t status = H5Dread(dataSet, DataType<ElementType>::hdfType(), H5S_ALL, H5S_ALL, H5P_DEFAULT, rawData);
if (status < 0) {
free(rawData);
throw Exception("hdf5::ContainerInterface< std::vector<..> >::read(): Error while reading data from file");
}
for (size_t i = 0; i < dst.size(); ++i) {
// .. it target is not a POD we need to translate ..
DataType<ElementType>::assignFromPOD(rawData[i], dst[i]);
DataType<ElementType>::freePOD(rawData[i]);
}
free(rawData);
}
};
// std::list<ElementType>
template<typename ElementType, typename Allocator> struct ContainerInterface<std::list<ElementType, Allocator> > {
typedef typename std::list<ElementType, Allocator> Container;
static hid_t hdfElementType() { return DataType<ElementType>::hdfType(); }
static hid_t hdfSpace(const Container& src) {
hsize_t dims[] = { src.size(), };
// hsize_t maxDims[] = { H5S_UNLIMITED, };
return H5Screate_simple(1, dims, 0);
}
/**
* Check if the type of the provided dataset and the container matches
* @param src
* @param dataSet
* @param hdfMemLayout
* @return
*/
static bool checkCompatibility(const Container& src, hid_t dataSet, hid_t hdfMemLayout) {
const size_t NumDims = 1;
// check if type of the target dataset and the one stored in this container matches
htri_t type = H5Tequal(DataType<ElementType>::hdfType(), H5Dget_type(dataSet));
if (type < 1) {
throw Exception("HDF5 and Container element type declaration does not match");
}
// check if rank matches
int rank = H5Sget_simple_extent_ndims(hdfMemLayout);
if (rank < 0) {
throw Exception("Could not get dimensionality of dataset");
}
if ( (size_t)rank != NumDims) {
throw Exception("Rank of source and destination does not match! STL vectors are just of rank 1.");
}
// check dimensions
hsize_t* dims = new hsize_t[rank];
if (H5Sget_simple_extent_dims(hdfMemLayout, dims, 0) < 0) {
delete dims;
throw Exception("Could not retrieve size of dimensions");
}
bool sizeFit = dims[0] == src.size();
// freeing memory
free(dims);
if (!sizeFit) {
throw Exception("Dimensions between dataset and provided container does not match");
}
return true;
}
/**
* Performs the writing of the src container to the defined dataset
* @param src Container to write
* @param dataSet HDF5 identifier for the target dataset
* @param dataSpace HDF5 identifier fitting the container
*/
static void write(const Container& src, hid_t dataSet, hid_t dataSpace) {
// std::cout << "hdf5::ContainerInterface< std::list<..> >::write()" << std::endl;
// checking compatibility of hdf5 target and the c++ src object
if (!checkCompatibility(src, dataSet, dataSpace)) {
throw Exception("hdf5::ContainerInterface< std::list<..> >::write(): Type compatibility check failed");
}
// check type of elements stored in container
typedef typename DataType<ElementType>::PODType POD;
size_t nElements = src.size();
size_t item = 0;
POD* tmp = (POD*) malloc( DataType<ElementType>::size() * nElements);
for (typename Container::const_iterator it = src.begin(); it != src.end(); ++it) {
DataType<ElementType>::assignToPOD(*it, tmp[item]);
++item;
}
H5Dwrite(dataSet, DataType<ElementType>::hdfType(), H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp);
for (size_t i = 0; i < item; ++i) {
DataType<POD>::freePOD(tmp[i]);
}
free(tmp);
}
/**
* Reads the data from the dataSet into the dst container
* @param src Container to store the data from file
* @param dataSet HDF5 identifier for the target dataset
* @param dataSpace HDF5 identifier fitting the container
*/
static void read(Container& dst, hid_t dataSet, hid_t dataSpace) {
const size_t NumDims = 1;
// std::cout << "hdf5::ContainerInterface< std::list<..> >::read()" << std::endl;
if (NumDims != H5Sget_simple_extent_ndims(dataSpace)) {
throw Exception("hdf5::ContainerInterface< std::list<..> >::read(): Dimensions of HDF5 and target container does not match");
}
// resize vector to be fitting for data from file
hsize_t* dims = new hsize_t[NumDims];
H5Sget_simple_extent_dims(dataSpace, dims, 0);
size_t nElements = dst.resize(dims[0]);
delete dims;
// check type of elements stored in container
// data can only be read to a POD structure therefore with use this...
typedef typename DataType<ElementType>::PODType POD;
POD* rawData = (POD*) malloc( DataType<ElementType>::size() * dst.size());
herr_t status = H5Dread(dataSet, DataType<ElementType>::hdfType(), H5S_ALL, H5S_ALL, H5P_DEFAULT, rawData);
if (status < 0) {
free(rawData);
throw Exception("hdf5::ContainerInterface< std::vector<..> >::read(): Error while reading data from file");
}
for (size_t i = 0; i < dst.size(); ++i) {
ElementType tmp;
DataType<ElementType>::assignFromPOD(rawData[i], tmp);
DataType<ElementType>::freePOD(rawData[i]);
dst.push_back(tmp);
}
free(rawData);
}
};
// Everything we need for converting std::map<Key,Value> back and forth...
template<typename Key, typename Value> struct POD_STL_MAP {
Key k;
Value v;
};
template<typename Key, typename Value> struct DataType< POD_STL_MAP<Key, Value> > {
typedef POD_STL_MAP<Key, Value> ElementType;
typedef ElementType PODType;
static hid_t hdfType() {
hid_t t = H5Tcreate(H5T_COMPOUND, size());
H5Tinsert(t, "Key", HOFFSET(PODType, k), DataType<Key>::hdfType());
H5Tinsert(t, "Value", HOFFSET(PODType, v), DataType<Value>::hdfType());
return t;
}
static hid_t isStructType() { return true; }
static hid_t isPOD() { return true; }
static hsize_t size() { return sizeof(ElementType); }
static void assignToPOD(const ElementType& in, PODType& out) { }
static void assignFromPOD(const PODType& in, ElementType& out) {};
};
template<typename Key, typename Value, typename Compare, typename Allocator> struct ContainerInterface< std::map<Key, Value, Compare, Allocator> > {
typedef typename std::map<Key, Value, Compare, Allocator> Container;
// typedef POD_STL_MAP<Key, Value> Element;
// POD Types for storage
typedef typename DataType<Key>::PODType KeyPOD;
typedef typename DataType<Value>::PODType ValuePOD;
typedef POD_STL_MAP<KeyPOD, ValuePOD> ElementPOD;
static hid_t hdfElementType() { return DataType< ElementPOD >::hdfType(); }
static hid_t hdfSpace(const Container& src) {
hsize_t dims[] = { src.size(), };
return H5Screate_simple(1, dims, 0);
}
static bool checkCompatibility(const Container& src, hid_t dataSet, hid_t hdfMemLayout, bool sizeTest = true) {
const size_t NumDims = 1;
// check if type of the target dataset and the one stored in this container matches
htri_t type = H5Tequal(DataType<ElementPOD>::hdfType(), H5Dget_type(dataSet));
if (type < 1) {
throw Exception("HDF5 and Container element type declaration does not match");
}
// check if rank matches
int rank = H5Sget_simple_extent_ndims(hdfMemLayout);
if (rank < 0) {
throw Exception("Could not get dimensionality of dataset");
}
if ( (size_t)rank != NumDims) {
throw Exception("Rank of source and destination does not match! STL vectors are just of rank 1.");
}
// check dimensions
hsize_t* dims = new hsize_t[rank];
if (H5Sget_simple_extent_dims(hdfMemLayout, dims, 0) < 0) {
delete dims;
throw Exception("Could not retrieve size of dimensions");
}
bool sizeFit = dims[0] == src.size();
// freeing memory
free(dims);
if (!sizeFit && sizeTest) {
throw Exception("Dimensions between dataset and provided container does not match");
}
return true;
}
static void write(const Container& src, hid_t dataSet, hid_t dataSpace) {
// checking compatibility of hdf5 target and the c++ src object
if (!checkCompatibility(src, dataSet, dataSpace)) {
throw Exception("hdf5::ContainerInterface< std::map<..> >::write(): Type compatibility check failed");
}
ElementPOD* buffer = (ElementPOD*) malloc(src.size() * DataType<ElementPOD>::size());
size_t iBuf = 0;
for (typename Container::const_iterator it = src.begin(); it != src.end(); ++it) {
DataType<Key>::assignToPOD(it->first, buffer[iBuf].k);
DataType<Value>::assignToPOD(it->second, buffer[iBuf].v);
++iBuf;
}
H5Dwrite(dataSet, DataType<ElementPOD>::hdfType(), H5S_ALL, H5S_ALL, H5P_DEFAULT, buffer);
// first free POD if necessary (will be handled by each type handler)
for (size_t i = 0; i < iBuf; ++i) {
DataType<Key>::freePOD(buffer[i].k);
DataType<Value>::freePOD(buffer[i].v);
}
free(buffer);
}
static void read(Container& dst, hid_t dataSet, hid_t dataSpace) {
const size_t NumDims = 1;
// std::cout << "hdf5::ContainerInterface< std::mapr<..> >::read()" << std::endl;
if (static_cast<int>(NumDims) != H5Sget_simple_extent_ndims(dataSpace)) {
throw Exception("hdf5::ContainerInterface< std::map<..> >::read(): Dimensions of HDF5 and target container does not match");
}
// resize vector to be fitting for data from file
hsize_t* dims = new hsize_t[NumDims];
H5Sget_simple_extent_dims(dataSpace, dims, 0);
size_t nElements = dims[0];
delete dims;
// checking compatibility of hdf5 target and the c++ src object
if (!checkCompatibility(dst, dataSet, dataSpace, false)) {
throw Exception("hdf5::ContainerInterface< std::map<..> >::read(): Type compatibility check failed");
}
// check if key or value is not a POD
hsize_t normalLen = nElements * DataType<ElementPOD>::size();
hsize_t variableLen;
herr_t status = H5Dvlen_get_buf_size(dataSet, DataType<ElementPOD>::hdfType(), dataSpace, &variableLen);
if (status < 0) {
throw Exception("hdf5::ContainerInterface< std::map<..> >::read(): Error while determining necessary memory buffer size");
}
ElementPOD* buffer = (ElementPOD*) malloc( std::max(variableLen, normalLen) );
status = H5Dread(dataSet, DataType<ElementPOD>::hdfType(), H5S_ALL, H5S_ALL, H5P_DEFAULT, buffer);
if (status < 0) {
free(buffer);
throw Exception("hdf5::ContainerInterface< std::map<..> >::read(): Error while reading data from file");
}
for (size_t i = 0; i < nElements; ++i) {
Key k;
Value v;
DataType<Key>::assignFromPOD(buffer[i].k, k);
DataType<Value>::assignFromPOD(buffer[i].v, v);
dst[k] = v;
DataType<Key>::freePOD(buffer[i].k);
DataType<Value>::freePOD(buffer[i].v);
}
// std::cout << "ContainerInterface< std::map<Key, Value, ..> >::read(..): freeing buffer" << std::endl;
free(buffer);
}
};
// boost multi_array
template<typename ElementType, std::size_t NumDims, typename Allocator> struct ContainerInterface< boost::multi_array<ElementType, NumDims, Allocator> > {
typedef typename boost::multi_array<ElementType, NumDims, Allocator> Container;
typedef typename std::vector<size_t> Coordinate;
static hid_t hdfElementType() { return DataType<ElementType>::hdfType(); }
static hid_t hdfSpace(const Container& src) {
// std::cout << " ContainerInterface<boost::multi_array...>::hdfSpace(): Rank=" << NumDims << std::endl;
hsize_t* dims = new hsize_t[NumDims];
hsize_t* maxDims = new hsize_t[NumDims];
for (size_t i = 0; i < NumDims; ++i) {
dims[i] = src.shape()[i];
maxDims[i] = H5S_UNLIMITED;
// std::cout << " ContainerInterface<boost::multi_array...>::hdfSpace(): dims[" << i << "]=" << dims[i] << ", maxDims[" << i << "]=" << maxDims[i] << std::endl;
}
// H5S_UNLIMITED leads to HDF5 C lib errors therefore currently only fixed size is supported
hid_t spaceId = H5Screate_simple(NumDims, dims, 0);
free(maxDims);
free(dims);
// std::cout << " ContainerInterface<boost::multi_array...>::hdfSpace(): spaceId=" << spaceId << std::endl;
return spaceId;
}
static Coordinate getArrayExtents(const Container& src, size_t& nElements) {
Coordinate dimXX(NumDims);
nElements = 1.;
for (hsize_t iDim = 0; iDim < NumDims; ++iDim) {
dimXX[iDim] = static_cast<size_t>(src.shape()[iDim]);
nElements *= src.shape()[iDim];
}
return dimXX;
}
static Coordinate getArrayCoordinate(const Container& src, size_t i) {
Coordinate x(NumDims);
size_t iBackup = i;
// calculate coordinate in multi_array
for (size_t dim = 0; dim < NumDims; ++dim) {
size_t nom = 1;
for (size_t ii = dim+1; ii < NumDims; ++ii)
nom *= src.shape()[ii];
size_t tmp = iBackup / nom;
x[dim] = tmp;
iBackup -= tmp*nom;
}
return x;
}
static bool checkCompatibility(const Container& src, hid_t dataSet, hid_t hdfMemLayout) {
using namespace std;
// type match
//TODO: %%%%%%%%%%%%%%%%%%%%%%%%55
// htri_t type = H5Tequal(DataType<ElementType>::hdfType(), H5Dget_type(dataSet));
// if (type < 1) {
// cout << "Type on file:" << endl;
// hid_t t = H5Dget_type(dataSet);
// if (H5Tget_class(t) == H5T_COMPOUND) {
// size_t nMembers = H5Tget_nmembers(t);
// for (size_t iMember = 0; iMember < nMembers; ++iMember) {
// hid_t memberType = H5Tget_member_type(t, iMember);
// size_t memberOffset = H5Tget_member_offset(t, iMember);
// char* memberName = H5Tget_member_name(t, iMember);
// cout << iMember << ": " << memberName <<
// ", " << getTypeClassName(memberType) <<
// " @ offset=" << memberOffset << endl;
// free(memberName);
// }
// }
//
// cout << "type in memory:" << endl;
// t = DataType<ElementType>::hdfType();
// if (H5Tget_class(t) == H5T_COMPOUND) {
// size_t nMembers = H5Tget_nmembers(t);
// for (size_t iMember = 0; iMember < nMembers; ++iMember) {
// hid_t memberType = H5Tget_member_type(t, iMember);
// size_t memberOffset = H5Tget_member_offset(t, iMember);
// char* memberName = H5Tget_member_name(t, iMember);
// cout << iMember << ": " << memberName <<
// ", " << getTypeClassName(memberType) <<
// " @ offset=" << memberOffset << endl;
// free(memberName);
// }
// }
//
// throw Exception("HDF5 and Container element type declaration does not match");
// }
// check if rank matches
int rank = H5Sget_simple_extent_ndims(hdfMemLayout);
if (rank < 0) {
throw Exception("Could not get dimensionality of dataset");
}
if ( (size_t)rank != NumDims) {
throw Exception("Rank of source and destination does not match!");
}
// check dimensions
hsize_t* dims = new hsize_t[rank];
if (H5Sget_simple_extent_dims(hdfMemLayout, dims, 0) < 0) {
delete dims;
throw Exception("Could not retrieve size of dimension");
}
bool sizeFit = true;
for (size_t iDim = 0; iDim < NumDims; ++iDim) {
sizeFit &= (dims[iDim] == src.shape()[iDim]);
}
// freeing memory
free(dims);
if (!sizeFit) {
throw Exception("Dimensions between dataset and provided container does not match");
}
return true;
}
/**
* Performs the writing of the src container to the defined dataset
* @param src Container to write
* @param dataSet HDF5 identifier for the target dataset
* @param dataSpace HDF5 identifier fitting the container
*/
static void write(const Container& src, hid_t ds, hid_t space) {
// std::cout << "boost::multi_array::write()" << std::endl;
if (!checkCompatibility(src, ds, space)) {
throw Exception("ContainerInterface< boost::multi_array<...> >::write(): Type compatibility check failed");
}
// check DataType
if (DataType<ElementType>::isStructType() && !DataType<ElementType>::isPOD()) {
// ok that's no very efficient but we have to copy the elements twice
// firstly we have to get the POD equivalent of the source type and fill it
// before writing data out
typedef typename DataType<ElementType>::PODType POD;
typedef typename boost::multi_array<POD, NumDims> DstContainer;
size_t nElements = 1.;
Coordinate dim = getArrayExtents(src, nElements);
// this relies on a fitting copy constructor
// otherwise e.g. gcc would also perform on non-pod but other compilers
// fail because it is not standard compliant
DstContainer dst;
dst.resize(dim);
// assuming c-data order
for (size_t i = 0; i < nElements; ++i) {
Coordinate x = getArrayCoordinate(src, i);
DataType<ElementType>::assignToPOD(src(x), dst(x));
}
H5Dwrite(ds, DataType<ElementType>::hdfType(), H5S_ALL, H5S_ALL, H5P_DEFAULT, dst.data());
for (size_t i = 0; i < nElements; ++i) {
Coordinate x = getArrayCoordinate(src, i);
DataType<POD>::freePOD(dst(x));
}
}
else {
// this allows simple write
H5Dwrite(ds, DataType<ElementType>::hdfType(), H5S_ALL, H5S_ALL, H5P_DEFAULT, src.data());
}
};
/**
* Reads the data from the dataSet into the dst container
* @param src Container to store the data from file
* @param dataSet HDF5 identifier for the target dataset
* @param dataSpace HDF5 identifier fitting the container
*/
static void read(Container& dst, hid_t ds, hid_t space) {
// get dimensions (no checking required all will be done by checkCompatibility)
if (NumDims != H5Sget_simple_extent_ndims(space)) {
throw Exception("ContainerInterface< boost::multi_array<...> >::read(): Dimensions of HDF5 and target container does not match");
}
Coordinate dimXX(NumDims);
size_t nElements = 1.;
{
hsize_t* dims = new hsize_t[NumDims];
H5Sget_simple_extent_dims(space, dims, 0);
for (hsize_t iDim = 0; iDim < NumDims; ++iDim) {
dimXX[iDim] = static_cast<size_t>(dims[iDim]);
nElements *= dims[iDim];
}
delete dims;
}
// resize multi_array according to source dimensions
dst.resize(dimXX);
if (!checkCompatibility(dst, ds, space)) {
throw Exception("ContainerInterface< boost::multi_array<...> >::read(): Type compatibility check failed");
}
// check type of elements stored in container
// data can only be read to a POD structure therefore with use this...
typedef typename DataType<ElementType>::PODType POD;
POD* rawData = (POD*) malloc( DataType<ElementType>::size() * nElements);
herr_t status = H5Dread(ds, DataType<ElementType>::hdfType(), H5S_ALL, H5S_ALL, H5P_DEFAULT, rawData);
if (status < 0) {
free(rawData);
throw Exception("hdf5::ContainerInterface< boost::multi_array<..> >::read(): Error while reading data from file");
}
for (size_t i = 0; i < nElements; ++i) {
// assuming c-data order
Coordinate x = getArrayCoordinate(dst, i);
DataType<ElementType>::assignFromPOD(rawData[i], dst(x));
DataType<ElementType>::freePOD(rawData[i]);
}
free(rawData);
}
};
};
#endif