-
Notifications
You must be signed in to change notification settings - Fork 10
/
HDFIO.h
1063 lines (956 loc) · 42.4 KB
/
HDFIO.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef HDFIO_H
#define HDFIO_H
#include <hdf5.h>
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <cstring>
#include <cassert>
// we use H5_HAVE_PARALLEL defined in hdf5.h to signal whether we have MPI support or not
#ifdef H5_HAVE_PARALLEL
#include <mpi.h>
#else
#ifndef MPI_Comm
#define MPI_Comm int
#endif
#ifndef MPI_COMM_NULL
#define MPI_COMM_NULL 0
#endif
#ifndef MPI_COMM_WORLD
#define MPI_COMM_WORLD 0
#endif
#endif
namespace NameSpaceHDFIO {
static const unsigned int flash_str_len = 79; // string length for FLASH scalars and runtime parameters I/O
template<typename> struct FlashScalarsParametersStruct; // structure for FLASH scalars and runtime parameters I/O
template<> struct FlashScalarsParametersStruct<int> { char name[flash_str_len]; int value; };
template<> struct FlashScalarsParametersStruct<double> { char name[flash_str_len]; double value; };
template<> struct FlashScalarsParametersStruct<bool> { char name[flash_str_len]; bool value; };
template<> struct FlashScalarsParametersStruct<std::string> { char name[flash_str_len]; char value[flash_str_len]; };
// FLASHPropAssign template function; applies to string case (so we do a string-copy from string to c_str)
template<typename T, typename Td> inline void FLASHPropAssign(T val, Td * data) { strcpy(*data, val.c_str()); };
// FLASHPropAssign template functions that apply to int, double, bool cases
template<> inline void FLASHPropAssign<int> (int val, int * data) { *data = val; }; // simply assign
template<> inline void FLASHPropAssign<double> (double val, double * data) { *data = val; }; // simply assign
template<> inline void FLASHPropAssign<bool> (bool val, bool * data) { *data = val; }; // simply assign
}
/**
* HDFIO class
*
* This class represents an HDF5 object/file and provides basic HDF operations like opening a file,
* reading data form a dataset of that file, creating new HDF files and datasets and writing datasets into a file.
* There are also some functions for getting and setting different attributes, like the dimensionaltity of the HDF
* dataset to be written or the datasetnames inside an HDF5 file.
*
* @author Christoph Federrath
* @version 2007-2022
*/
class HDFIO
{
private:
std::string ClassSignature, Filename; // class signature and HDF5 filename
int Rank; // dimensionality of the field
hid_t File_id, Dataset_id, Dataspace_id; // HDF5 stuff
hsize_t HDFSize, HDFDims[4]; // HDF5 stuff; can maximally handle 4D datasets (but who would ever want more?)
herr_t HDF5_status, HDF5_error; // HDF5 stuff
int Verbose; // verbose level for printing to stdout
/// Constructors
public: HDFIO(void)
{
// empty constructor, so we can define a global HDFIO object in application code
Constructor("", 'r', MPI_COMM_NULL, 0);
};
public: HDFIO(const int verbose)
{
Constructor("", 'r', MPI_COMM_NULL, verbose);
};
public: HDFIO(const std::string filename)
{
Constructor(filename, 'r', MPI_COMM_NULL, 1);
};
public: HDFIO(const std::string filename, const int verbose)
{
Constructor(filename, 'r', MPI_COMM_NULL, verbose);
};
public: HDFIO(const std::string filename, const char read_write_char)
{
if (read_write_char == 'w')
Constructor(filename, read_write_char, MPI_COMM_WORLD, 1);
else
Constructor(filename, read_write_char, MPI_COMM_NULL, 1);
};
public: HDFIO(const std::string filename, const char read_write_char, const int verbose)
{
if (read_write_char == 'w')
Constructor(filename, read_write_char, MPI_COMM_WORLD, verbose);
else
Constructor(filename, read_write_char, MPI_COMM_NULL, verbose);
};
/// Destructor
public: ~HDFIO()
{
if (Verbose > 1) std::cout<<"HDFIO: destructor called."<<std::endl;
};
private: void Constructor(const std::string filename, const char read_write_char, MPI_Comm comm, const int verbose)
{
Filename = filename; // HDF5 filename
Verbose = verbose; // verbose (can be 0: no stdout, 1: default output, 2: more output)
ClassSignature = "HDFIO: "; // class signature, when this class is printing to stdout
Rank = 1; // dimensionality 1 (e.g., x only)
File_id = 0; // HDF5 file id 0
Dataset_id = 0; // HDF5 dataset id 0
Dataspace_id = 0; // HDF5 data space id 0
HDFSize = 0; // HDF5 buffer size 0
HDF5_status = 0; // HDF5 status 0
HDF5_error = -1; // HDF5 error -1
for (unsigned int i = 0; i < 4; i++) HDFDims[i] = 0; // set Dimensions to (0,0,0,0)
if (Filename != "") this->open(Filename, read_write_char, comm); // open file if provided in constructor
};
// get function signature for printing to stdout
private: std::string FuncSig(const std::string func_name)
{ return ClassSignature+func_name+": "; };
/**
* open an HDF5 file in serial mode
* @param Filename HDF5 filename
* @param read_write_char 'r': read only flag, 'w': write flag
*/
public: void open(const std::string Filename, const char read_write_char)
{
this->open(Filename, read_write_char, MPI_COMM_NULL); // open in serial mode
};
/**
* open an HDF5 file (overloaded)
* @param Filename HDF5 filename
* @param read_write_char 'r': read only flag, 'w': write flag
* @param comm: MPI communicator for parallel file I/O
*/
public: void open(const std::string Filename, const char read_write_char, MPI_Comm comm)
{
this->Filename = Filename;
hid_t plist_id = H5P_DEFAULT;
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) {
plist_id = H5Pcreate(H5P_FILE_ACCESS);
H5Pset_fapl_mpio(plist_id, comm, MPI_INFO_NULL);
assert( HDF5_status != HDF5_error );
}
#endif
switch (read_write_char)
{
case 'r':
{
// open HDF5 file in read only mode
File_id = H5Fopen(Filename.c_str(), H5F_ACC_RDONLY, plist_id);
assert( File_id != HDF5_error );
break;
}
case 'w':
{
// open HDF5 file in write mode
File_id = H5Fopen(Filename.c_str(), H5F_ACC_RDWR, plist_id);
assert( File_id != HDF5_error );
break;
}
default:
{
// open HDF5 file in read only mode
File_id = H5Fopen(Filename.c_str(), H5F_ACC_RDONLY, plist_id);
assert( File_id != HDF5_error );
break;
}
}
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) H5Pclose(plist_id);
#endif
};
/**
* close HDF5 file
*/
public: void close(void)
{
// close HDF5 file
HDF5_status = H5Fclose(File_id);
assert( HDF5_status != HDF5_error );
};
/**
* read data from a dataset
* @param *DataBuffer pointer to double/float/int array to which data is to be written
* @param Datasetname datasetname
* @param DataType (i.e. H5T_STD_I32LE)
*/
public: void read(void* const DataBuffer, const std::string Datasetname, const hid_t DataType)
{
this->read(DataBuffer, Datasetname, DataType, MPI_COMM_NULL); // serial mode
};
/**
* read data from a dataset
* @param *DataBuffer pointer to double/float/int array to which data is to be written
* @param Datasetname datasetname
* @param DataType (i.e. H5T_STD_I32LE)
* @param comm: MPI communicator for parallel file I/O
*/
public: void read(void* const DataBuffer, const std::string Datasetname, const hid_t DataType, MPI_Comm comm)
{
// get dimensional information from dataspace and update HDFSize
this->getDims(Datasetname);
// open dataset
Dataset_id = H5Dopen(File_id, Datasetname.c_str(), H5P_DEFAULT);
assert( Dataset_id != HDF5_error );
// open dataspace (to get dimensions)
Dataspace_id = H5Dget_space(Dataset_id);
assert( Dataspace_id != HDF5_error );
/// create property list for collective dataset i/o
hid_t plist_id = H5P_DEFAULT;
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) {
plist_id = H5Pcreate(H5P_DATASET_XFER);
H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
}
#endif
// read buffer /memspaceid //filespaceid
HDF5_status = H5Dread(Dataset_id, DataType, H5S_ALL, H5S_ALL, plist_id, DataBuffer);
assert( HDF5_status != HDF5_error );
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) {
HDF5_status = H5Pclose(plist_id);
assert( HDF5_status != HDF5_error );
}
#endif
HDF5_status = H5Sclose(Dataspace_id);
assert( HDF5_status != HDF5_error );
HDF5_status = H5Dclose(Dataset_id);
assert( HDF5_status != HDF5_error );
}; // read
/**
* read attribute from a dataset
* @param *DataBuffer pointer to double/float/int array to which data is to be written
* @param Datasetname name of dataset
* @param Attributename name of attribute to be read
* @param DataType (i.e. H5T_STD_I32LE)
*/
public: void read_attribute(void* const DataBuffer, const std::string Datasetname,
const std::string Attributename, const hid_t DataType)
{
this->read_attribute(DataBuffer, Datasetname, Attributename, DataType, MPI_COMM_NULL); // serial mode
};
/**
* read attribute from a dataset
* @param *DataBuffer pointer to double/float/int array to which data is to be written
* @param Datasetname name of dataset
* @param Attributename name of attribute to be read
* @param DataType (i.e. H5T_STD_I32LE)
* @param comm: MPI communicator for parallel file I/O
*/
public: void read_attribute(void* const DataBuffer, const std::string Datasetname,
const std::string Attributename, const hid_t DataType, MPI_Comm comm)
{
// open dataset
Dataset_id = H5Dopen(File_id, Datasetname.c_str(), H5P_DEFAULT);
assert( Dataset_id != HDF5_error );
/// create property list for collective dataset i/o
hid_t plist_id = H5P_DEFAULT;
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) {
plist_id = H5Pcreate(H5P_DATASET_XFER);
H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
}
#endif
/// open attribute
hid_t attr_id = H5Aopen(Dataset_id, Attributename.c_str(), plist_id);
/// read attribute
HDF5_status = H5Aread(attr_id, DataType, DataBuffer);
assert( HDF5_status != HDF5_error );
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) {
HDF5_status = H5Pclose(plist_id);
assert( HDF5_status != HDF5_error );
}
#endif
HDF5_status = H5Aclose(attr_id);
assert( HDF5_status != HDF5_error );
HDF5_status = H5Dclose(Dataset_id);
assert( HDF5_status != HDF5_error );
}; // read_attribute
/**
* read a slab of data from a dataset (overloaded)
* @param *DataBuffer pointer to double/float/int array to which data is to be written
* @param Datasetname datasetname
* @param DataType (i.e. H5T_STD_I32LE)
* @param offset (offset array)
* @param count (count array, i.e. the number of cells[dim] to be selected)
* @param out_rank (rank of the output array selected)
* @param out_offset (output offset array)
* @param out_count (output count array, i.e. the number of cells[dim] in output)
*/
public: void read_slab( void* const DataBuffer, const std::string Datasetname, const hid_t DataType,
const hsize_t offset[], const hsize_t count[],
const hsize_t out_rank, const hsize_t out_offset[], const hsize_t out_count[])
{
/// simply set total_out_count = out_count, in which case we selected a hyperslab that fits completely into the
/// total size of the output array. Use the overloaded function below, if output goes into an offset hyperslab
this->read_slab(DataBuffer, Datasetname, DataType, offset, count, out_rank, out_offset, out_count, out_count);
};
/**
* read a slab of data from a dataset (overloaded)
* @param *DataBuffer pointer to double/float/int array to which data is to be written
* @param Datasetname datasetname
* @param DataType (i.e. H5T_STD_I32LE)
* @param offset (offset array)
* @param count (count array, i.e. the number of cells[dim] to be selected)
* @param out_rank (rank of the output array selected)
* @param out_offset (output offset array)
* @param out_count (output count array, i.e. the number of cells[dim] in output)
* @param comm: MPI communicator for parallel file I/O
*/
public: void read_slab( void* const DataBuffer, const std::string Datasetname, const hid_t DataType,
const hsize_t offset[], const hsize_t count[],
const hsize_t out_rank, const hsize_t out_offset[], const hsize_t out_count[], MPI_Comm comm)
{
/// simply set total_out_count = out_count, in which case we selected a hyperslab that fits completely into the
/// total size of the output array. Use the overloaded function below, if output goes into an offset hyperslab
this->read_slab(DataBuffer, Datasetname, DataType, offset, count, out_rank, out_offset, out_count, out_count, comm);
};
/**
* read a slab of data from a dataset
* @param *DataBuffer pointer to double/float/int array to which data is to be written
* @param Datasetname datasetname
* @param DataType (i.e. H5T_STD_I32LE)
* @param offset (offset array)
* @param count (count array, i.e. the number of cells[dim] to be selected)
* @param out_rank (rank of the output array selected)
* @param out_offset (output offset array)
* @param out_count (output count array, i.e. the number of cells[dim] in output)
* @param total_out_count (total output count array, i.e. the total number of cells[dim] in output)
*/
public: void read_slab( void* const DataBuffer, const std::string Datasetname, const hid_t DataType,
const hsize_t offset[], const hsize_t count[],
const hsize_t out_rank, const hsize_t out_offset[], const hsize_t out_count[],
const hsize_t total_out_count[])
{
this->read_slab(DataBuffer, Datasetname, DataType, offset, count, out_rank, out_offset, out_count,
total_out_count, MPI_COMM_NULL); // serial mode
};
/**
* read a slab of data from a dataset
* @param *DataBuffer pointer to double/float/int array to which data is to be written
* @param Datasetname datasetname
* @param DataType (i.e. H5T_STD_I32LE)
* @param offset (offset array)
* @param count (count array, i.e. the number of cells[dim] to be selected)
* @param out_rank (rank of the output array selected)
* @param out_offset (output offset array)
* @param out_count (output count array, i.e. the number of cells[dim] in output)
* @param total_out_count (total output count array, i.e. the total number of cells[dim] in output)
* @param comm: MPI communicator for parallel file I/O
*/
public: void read_slab( void* const DataBuffer, const std::string Datasetname, const hid_t DataType,
const hsize_t offset[], const hsize_t count[],
const hsize_t out_rank, const hsize_t out_offset[], const hsize_t out_count[],
const hsize_t total_out_count[], MPI_Comm comm)
{
// get dimensional information from dataspace and update HDFSize
this->getDims(Datasetname);
// open dataset
Dataset_id = H5Dopen(File_id, Datasetname.c_str(), H5P_DEFAULT);
assert( Dataset_id != HDF5_error );
// open dataspace (to get dimensions)
Dataspace_id = H5Dget_space(Dataset_id);
assert( Dataspace_id != HDF5_error );
// select hyperslab
HDF5_status = H5Sselect_hyperslab(Dataspace_id, H5S_SELECT_SET, offset, NULL, count, NULL);
assert( HDF5_status != HDF5_error );
// create memspace
hid_t Memspace_id = H5Screate_simple(out_rank, total_out_count, NULL);
HDF5_status = H5Sselect_hyperslab(Memspace_id, H5S_SELECT_SET, out_offset, NULL, out_count, NULL);
assert( HDF5_status != HDF5_error );
/// create property list for collective dataset i/o
hid_t plist_id = H5P_DEFAULT;
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) {
plist_id = H5Pcreate(H5P_DATASET_XFER);
H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
}
#endif
// read buffer
HDF5_status = H5Dread(Dataset_id, DataType, Memspace_id, Dataspace_id, plist_id, DataBuffer );
assert( HDF5_status != HDF5_error );
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) {
HDF5_status = H5Pclose(plist_id);
assert( HDF5_status != HDF5_error );
}
#endif
HDF5_status = H5Sclose(Memspace_id);
assert( HDF5_status != HDF5_error );
HDF5_status = H5Sclose(Dataspace_id);
assert( HDF5_status != HDF5_error );
HDF5_status = H5Dclose(Dataset_id);
assert( HDF5_status != HDF5_error );
}; // read_slab
/**
* overwrite a slab of data from a dataset
* @param *DataBuffer pointer to double/float/int array to which data is to be written
* @param Datasetname datasetname
* @param DataType (i.e. H5T_STD_I32LE)
* @param offset (offset array)
* @param count (count array, i.e. the number of cells[dim] to be selected)
* @param out_rank (rank of the output array selected)
* @param out_offset (output offset array)
* @param out_count (output count array, i.e. the number of cells[dim] in output)
*/
public: void overwrite_slab(void* const DataBuffer, const std::string Datasetname, const hid_t DataType,
const hsize_t offset[], const hsize_t count[],
const hsize_t out_rank, const hsize_t out_offset[], const hsize_t out_count[])
{
this->overwrite_slab(DataBuffer, Datasetname, DataType,
offset, count, out_rank, out_offset, out_count, MPI_COMM_NULL); // serial mode
};
/**
* overwrite a slab of data from a dataset
* @param *DataBuffer pointer to double/float/int array to which data is to be written
* @param Datasetname datasetname
* @param DataType (i.e. H5T_STD_I32LE)
* @param offset (offset array)
* @param count (count array, i.e. the number of cells[dim] to be selected)
* @param out_rank (rank of the output array selected)
* @param out_offset (output offset array)
* @param out_count (output count array, i.e. the number of cells[dim] in output)
* @param comm: MPI communicator for parallel file I/O
*/
public: void overwrite_slab(void* const DataBuffer, const std::string Datasetname, const hid_t DataType,
const hsize_t offset[], const hsize_t count[],
const hsize_t out_rank, const hsize_t out_offset[], const hsize_t out_count[],
MPI_Comm comm)
{
// get dimensional information from dataspace and update HDFSize
this->getDims(Datasetname);
// open dataset
Dataset_id = H5Dopen(File_id, Datasetname.c_str(), H5P_DEFAULT);
assert( Dataset_id != HDF5_error );
// open dataspace (to get dimensions)
Dataspace_id = H5Dget_space(Dataset_id);
assert( Dataspace_id != HDF5_error );
// select hyperslab
HDF5_status = H5Sselect_hyperslab(Dataspace_id, H5S_SELECT_SET, offset, NULL, count, NULL);
assert( HDF5_status != HDF5_error );
// create memspace
hid_t Memspace_id = H5Screate_simple(out_rank, out_count, NULL);
HDF5_status = H5Sselect_hyperslab(Memspace_id, H5S_SELECT_SET, out_offset, NULL, out_count, NULL);
assert( HDF5_status != HDF5_error );
/// create property list for collective dataset i/o
hid_t plist_id = H5P_DEFAULT;
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) {
plist_id = H5Pcreate(H5P_DATASET_XFER);
H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
}
#endif
// overwrite dataset
HDF5_status = H5Dwrite(Dataset_id, DataType, Memspace_id, Dataspace_id, plist_id, DataBuffer);
assert( HDF5_status != HDF5_error );
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) {
HDF5_status = H5Pclose(plist_id);
assert( HDF5_status != HDF5_error );
}
#endif
HDF5_status = H5Sclose(Memspace_id);
assert( HDF5_status != HDF5_error );
HDF5_status = H5Sclose(Dataspace_id);
assert( HDF5_status != HDF5_error );
HDF5_status = H5Dclose(Dataset_id);
assert( HDF5_status != HDF5_error );
}; // overwrite_slab
/**
* overwrite data of an existing dataset (serial mode)
* @param *DataBuffer pointer to double/float/int array containing data to be written
* @param Datasetname datasetname
* @param DataType (i.e. H5T_STD_I32LE)
*/
public: void overwrite(const void* const DataBuffer, const std::string Datasetname, const hid_t DataType)
{
this->overwrite(DataBuffer, Datasetname, DataType, MPI_COMM_NULL);
};
/**
* overwrite data of an existing dataset (overloaded)
* @param *DataBuffer pointer to double/float/int array containing data to be written
* @param Datasetname datasetname
* @param DataType (i.e. H5T_STD_I32LE)
* @param comm: MPI communicator for parallel file I/O
*/
public: void overwrite(const void* const DataBuffer, const std::string Datasetname, const hid_t DataType, MPI_Comm comm)
{
// get dimensional information from dataspace and update HDFSize
this->getDims(Datasetname);
// open dataset
Dataset_id = H5Dopen(File_id, Datasetname.c_str(), H5P_DEFAULT);
assert( Dataset_id != HDF5_error );
// open dataspace (to get dimensions)
Dataspace_id = H5Dget_space(Dataset_id);
assert( Dataspace_id != HDF5_error );
/// create property list for collective dataset i/o
hid_t plist_id = H5P_DEFAULT;
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) {
plist_id = H5Pcreate(H5P_DATASET_XFER);
H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
}
#endif
// overwrite dataset
HDF5_status = H5Dwrite(Dataset_id, DataType, H5S_ALL, H5S_ALL, plist_id, DataBuffer);
assert( HDF5_status != HDF5_error );
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) {
HDF5_status = H5Pclose(plist_id);
assert( HDF5_status != HDF5_error );
}
#endif
HDF5_status = H5Sclose(Dataspace_id);
assert( HDF5_status != HDF5_error );
HDF5_status = H5Dclose(Dataset_id);
assert( HDF5_status != HDF5_error );
}; // overwrite
/**
* create new HDF5 file (serial mode)
* @param Filename HDF5 filename
*/
public: void create(const std::string Filename)
{
this->create(Filename, MPI_COMM_NULL);
};
/**
* create new HDF5 file (overloaded)
* @param Filename HDF5 filename
* @param comm: MPI communicator for parallel file I/O
*/
public: void create(const std::string Filename, MPI_Comm comm)
{
this->Filename = Filename;
hid_t plist_id = H5P_DEFAULT;
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) {
plist_id = H5Pcreate(H5P_FILE_ACCESS);
H5Pset_fapl_mpio(plist_id, comm, MPI_INFO_NULL);
}
#endif
// create HDF5 file (overwrite)
File_id = H5Fcreate(Filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, plist_id);
assert( File_id != HDF5_error );
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) H5Pclose(plist_id);
#endif
};
/**
* write HDF5 dataset (serial mode)
* @param DataBuffer int/float/double array containing the data
* @param Datasetname datasetname
* @param Dimensions dataset dimensions
* @param DataType (i.e. H5T_STD_I32LE)
*/
public: void write( const void* const DataBuffer, const std::string Datasetname,
const std::vector<int> Dimensions, const hid_t DataType)
{
this->write(DataBuffer, Datasetname, Dimensions, DataType, MPI_COMM_NULL);
};
/**
* write HDF5 dataset (overloaded)
* @param DataBuffer int/float/double array containing the data
* @param Datasetname datasetname
* @param Dimensions dataset dimensions
* @param DataType (i.e. H5T_STD_I32LE)
* @param comm: MPI communicator for parallel file I/O
*/
public: void write( const void* const DataBuffer, const std::string Datasetname,
const std::vector<int> Dimensions, const hid_t DataType, MPI_Comm comm)
{
this->setDims(Dimensions); // set dimensions
this->write(DataBuffer, Datasetname, DataType, comm); // call write
};
/**
* write HDF5 dataset (serial mode)
* @param *DataBuffer pointer to int/float/double array containing the data
* @param Datasetname datasetname
* @param DataType (i.e. H5T_IEEE_F32BE, H5T_STD_I32LE, ...)
*/
public: void write(const void* const DataBuffer, const std::string Datasetname, const hid_t DataType)
{
this->write(DataBuffer, Datasetname, DataType, MPI_COMM_NULL);
};
/**
* write HDF5 dataset (overloaded)
* @param *DataBuffer pointer to int/float/double array containing the data
* @param Datasetname datasetname
* @param DataType (i.e. H5T_IEEE_F32BE, H5T_STD_I32LE, ...)
* @param comm: MPI communicator for parallel file I/O
*/
public: void write(const void* const DataBuffer, const std::string Datasetname, const hid_t DataType, MPI_Comm comm)
{
// -------------- create dataspace
Dataspace_id = H5Screate_simple(Rank, HDFDims, NULL);
assert( Dataspace_id != HDF5_error );
// -------------- create dataset
Dataset_id = H5Dcreate(File_id, Datasetname.c_str(), DataType, Dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
assert( Dataset_id != HDF5_error );
/// create property list for collective dataset i/o
hid_t plist_id = H5P_DEFAULT;
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) {
plist_id = H5Pcreate(H5P_DATASET_XFER);
H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
}
#endif
// -------------- write dataset
HDF5_status = H5Dwrite(Dataset_id, DataType, H5S_ALL, H5S_ALL, plist_id, DataBuffer);
assert( HDF5_status != HDF5_error );
#ifdef H5_HAVE_PARALLEL
if (comm != MPI_COMM_NULL) {
HDF5_status = H5Pclose(plist_id);
assert( HDF5_status != HDF5_error );
}
#endif
// -------------- close dataset
HDF5_status = H5Dclose(Dataset_id);
assert( HDF5_status != HDF5_error );
// -------------- close dataspace
HDF5_status = H5Sclose(Dataspace_id);
assert( HDF5_status != HDF5_error );
}; // write
/**
* create empty HDF5 dataset
* @param Datasetname datasetname
* @param Dimensions dataset dimensions
* @param DataType (i.e. H5T_IEEE_F32BE, H5T_STD_I32LE, ...)
* @param comm: MPI communicator for parallel file I/O
*/
public: void create_dataset(const std::string Datasetname, const std::vector<int> Dimensions,
const hid_t DataType, MPI_Comm comm)
{
this->setDims(Dimensions); // set dimensions
// -------------- create dataspace
Dataspace_id = H5Screate_simple(Rank, HDFDims, NULL);
assert( Dataspace_id != HDF5_error );
// -------------- create dataset
Dataset_id = H5Dcreate(File_id, Datasetname.c_str(), DataType, Dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
assert( Dataset_id != HDF5_error );
// -------------- close dataset
HDF5_status = H5Dclose(Dataset_id);
assert( HDF5_status != HDF5_error );
// -------------- close dataspace
HDF5_status = H5Sclose(Dataspace_id);
assert( HDF5_status != HDF5_error );
};
/**
* delete HDF5 dataset
* @param Datasetname datasetname
*/
public: void delete_dataset(const std::string Datasetname)
{
std::vector<std::string> dsets_in_file = getDatasetnames();
bool dset_in_file = false; // see if the Datasetname exists in the file
for (unsigned int i = 0; i < dsets_in_file.size(); i++) {
if (dsets_in_file[i] == Datasetname) { dset_in_file = true; break; }
}
if (dset_in_file) { // if the Datasetname is in the file, delete it
HDF5_status = H5Ldelete(File_id, Datasetname.c_str(), H5P_DEFAULT); // delete dataset
assert( HDF5_status != HDF5_error );
}
};
/**
* delete all HDF5 datasets
*/
public: void delete_datasets(void)
{
std::vector<std::string> dsets_in_file = getDatasetnames();
for (unsigned int i = 0; i < dsets_in_file.size(); i++) {
this->delete_dataset(dsets_in_file[i]);
}
};
/**
* set the rank of a dataset
* @param Rank the rank, dimensionality (0,1,2)
*/
public: void setRank(const int Rank)
{
this->Rank = Rank;
};
/**
* set array dimension in different directions
* @param dim dimension of the array
*/
public: void setDimX(const int dim_x)
{
HDFDims[0] = static_cast<hsize_t>(dim_x);
};
public: void setDimY(const int dim_y)
{
HDFDims[1] = static_cast<hsize_t>(dim_y);
};
public: void setDimZ(const int dim_z)
{
HDFDims[2] = static_cast<hsize_t>(dim_z);
};
public: void setDims(const std::vector<int> Dimensions)
{
Rank = Dimensions.size();
for(int i = 0; i < Rank; i++)
HDFDims[i] = static_cast<hsize_t>(Dimensions[i]);
};
/**
* get the rank of the current dataset
* @return int dimensionality
*/
public: int getRank(void) const
{
return Rank;
};
/**
* get the rank of a dataset with name datasetname
* @param Datasetname datasetname
* @return int dimensionality
*/
public: int getRank(const std::string Datasetname)
{
// get dimensional information from dataspace and update HDFSize
this->getDims(Datasetname);
return Rank;
};
/**
* get array dimension in different directions
* @return dimension of the array
*/
public: int getDimX(void) const
{
return static_cast<int>(HDFDims[0]);
};
public: int getDimY(void) const
{
return static_cast<int>(HDFDims[1]);
};
public: int getDimZ(void) const
{
return static_cast<int>(HDFDims[2]);
};
/**
* get dataset size of dataset with datasetname
* @param Datasetname datasetname
* @return size of the dataset
*/
public: int getSize(const std::string Datasetname)
{
// open dataset
Dataset_id = H5Dopen(File_id, Datasetname.c_str(), H5P_DEFAULT);
if (Dataset_id == HDF5_error)
{
std::cout << "HDFIO: getSize(): CAUTION: Datasetname '" << Datasetname << "' does not exists in file '"
<<this->getFilename()<<"'. Continuing..." << std::endl;
return 0;
}
assert( Dataset_id != HDF5_error );
// open dataspace
Dataspace_id = H5Dget_space(Dataset_id);
assert( Dataspace_id != HDF5_error );
// get dimensional information from dataspace
hsize_t HDFxdims[4], HDFmaxdims[4];
Rank = H5Sget_simple_extent_dims(Dataspace_id, HDFxdims, HDFmaxdims);
// from the dimensional info, calculate the size of the buffer.
HDFSize = 1;
for (int i = 0; i < Rank; i++) {
HDFDims[i] = HDFxdims[i];
HDFSize *= HDFDims[i];
}
HDF5_status = H5Sclose(Dataspace_id);
assert( HDF5_status != HDF5_error );
HDF5_status = H5Dclose(Dataset_id);
assert( HDF5_status != HDF5_error );
return static_cast<int>(HDFSize);
}; // getSize
/**
* get array dimension in different directions and update HDFSize
* @param Datasetname datasetname for which dimensional info is read
* @return dimension of the array
*/
public: std::vector<int> getDims(const std::string Datasetname)
{
// open dataset
Dataset_id = H5Dopen(File_id, Datasetname.c_str(), H5P_DEFAULT);
assert( Dataset_id != HDF5_error );
// open dataspace
Dataspace_id = H5Dget_space(Dataset_id);
assert( Dataspace_id != HDF5_error );
// get dimensional information from dataspace
hsize_t HDFxdims[4], HDFmaxdims[4];
Rank = H5Sget_simple_extent_dims(Dataspace_id, HDFxdims, HDFmaxdims);
// from the dimensional info, calculate the size of the buffer.
HDFSize = 1;
for (int i = 0; i < Rank; i++) {
HDFDims[i] = HDFxdims[i];
HDFSize *= HDFDims[i];
}
HDF5_status = H5Sclose(Dataspace_id);
assert( HDF5_status != HDF5_error );
HDF5_status = H5Dclose(Dataset_id);
assert( HDF5_status != HDF5_error );
std::vector<int> ReturnDims(Rank);
for(int i = 0; i < Rank; i++)
ReturnDims[i] = static_cast<int>(HDFDims[i]);
return ReturnDims;
}; // getDims
/**
* get HDF5 file ID
* @return File_id
*/
public: hid_t getFileID(void)
{
return File_id;
};
/**
* get HDF5 filename
* @return filename
*/
public: std::string getFilename(void)
{
return Filename;
};
/**
* get number of datasets in HDF5 file
* @return the number of datasets
*/
public: int getNumberOfDatasets(void)
{
hsize_t *NumberofObjects = new hsize_t[1];
H5Gget_num_objs(File_id, NumberofObjects);
int returnNumber = static_cast<int>(NumberofObjects[0]);
delete [] NumberofObjects;
return returnNumber;
};
/**
* get HDF5 datasetname
* @param datasetnumber integer number identifying the dataset
* @return datasetname
*/
public: std::string getDatasetname(const int datasetnumber)
{
char *Name = new char[256];
H5Gget_objname_by_idx(File_id, datasetnumber, Name, 256);
std::string returnName = static_cast<std::string>(Name);
delete [] Name;
return returnName;
};
/**
* getDatasetnames
* @return vector<string> datasetnames
*/
public: std::vector<std::string> getDatasetnames(void)
{
int nsets = this->getNumberOfDatasets();
std::vector<std::string> ret(nsets);
for (int i=0; i<nsets; i++)
{
ret[i] = this->getDatasetname(i);
if (Verbose > 1) std::cout << "dataset in file: " << ret[i] << std::endl;
}
return ret;
};
// ************************************************************************* //
// ************** reading FLASH scalars or runtime parameters ************** //
// ************************************************************************* //
/// returns FLASH integer, real, logical, string scalars or runtime parameters as maps
public: std::map<std::string, int> ReadFlashIntegerScalars()
{ return GetFLASHProps<int>("integer scalars"); };
public: std::map<std::string, int> ReadFlashIntegerParameters()
{ return GetFLASHProps<int>("integer runtime parameters"); };
public: std::map<std::string, double> ReadFlashRealScalars()
{ return GetFLASHProps<double>("real scalars"); };
public: std::map<std::string, double> ReadFlashRealParameters()
{ return GetFLASHProps<double>("real runtime parameters"); };
public: std::map<std::string, bool> ReadFlashLogicalScalars()
{ return GetFLASHProps<bool>("logical scalars"); };
public: std::map<std::string, bool> ReadFlashLogicalParameters()
{ return GetFLASHProps<bool>("logical runtime parameters"); };
public: std::map<std::string, std::string> ReadFlashStringScalars()
{ return GetFLASHProps<std::string>("string scalars"); };
public: std::map<std::string, std::string> ReadFlashStringParameters()
{ return GetFLASHProps<std::string>("string runtime parameters"); };
// returns map of FLASH integer, real, logical, string scalars or runtime parameters
private: template<typename T> std::map<std::string, T> GetFLASHProps(std::string dsetname)
{
std::map<std::string, T> ret; // return object
int nProps = this->getDims(dsetname)[0];
hid_t h5string = H5Tcopy(H5T_C_S1); H5Tset_size(h5string, NameSpaceHDFIO::flash_str_len); hid_t dtype;
if (typeid(T) == typeid(int)) dtype = H5T_NATIVE_INT;
if (typeid(T) == typeid(double)) dtype = H5T_NATIVE_DOUBLE;
if (typeid(T) == typeid(bool)) dtype = H5T_NATIVE_HBOOL;
if (typeid(T) == typeid(std::string)) dtype = H5Tcopy(h5string);
hid_t datatype = H5Tcreate(H5T_COMPOUND, sizeof(NameSpaceHDFIO::FlashScalarsParametersStruct<T>));
H5Tinsert(datatype, "name", HOFFSET(NameSpaceHDFIO::FlashScalarsParametersStruct<T>, name), h5string);
H5Tinsert(datatype, "value", HOFFSET(NameSpaceHDFIO::FlashScalarsParametersStruct<T>, value), dtype);
NameSpaceHDFIO::FlashScalarsParametersStruct<T> * data = new NameSpaceHDFIO::FlashScalarsParametersStruct<T>[nProps];
this->read(data, dsetname, datatype);
for (int i = 0; i < nProps; i++) {
std::string pname = Trim((std::string)data[i].name);
ret[pname] = (T)data[i].value;
if (Verbose > 1) std::cout<<FuncSig(__func__)<<"name, value = '"<<pname<<"', '"<<ret[pname]<<"'"<<std::endl;
}