-
Notifications
You must be signed in to change notification settings - Fork 51
/
HDF5IOHandler.cpp
1544 lines (1407 loc) · 59.9 KB
/
HDF5IOHandler.cpp
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
/* Copyright 2017-2019 Fabian Koller
*
* This file is part of openPMD-api.
*
* openPMD-api is free software: you can redistribute it and/or modify
* it under the terms of of either the GNU General Public License or
* the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* openPMD-api is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License and the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with openPMD-api.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "openPMD/IO/HDF5/HDF5IOHandler.hpp"
#include "openPMD/IO/HDF5/HDF5IOHandlerImpl.hpp"
#if openPMD_HAVE_HDF5
# include "openPMD/auxiliary/Filesystem.hpp"
# include "openPMD/auxiliary/StringManip.hpp"
# include "openPMD/backend/Attribute.hpp"
# include "openPMD/IO/IOTask.hpp"
# include "openPMD/IO/HDF5/HDF5Auxiliary.hpp"
# include "openPMD/IO/HDF5/HDF5FilePosition.hpp"
#endif
#include <cstring>
#include <future>
#include <iostream>
#include <string>
#include <vector>
namespace openPMD
{
#if openPMD_HAVE_HDF5
# if openPMD_USE_VERIFY
# define VERIFY(CONDITION, TEXT) { if(!(CONDITION)) throw std::runtime_error((TEXT)); }
# else
# define VERIFY(CONDITION, TEXT) do{ (void)sizeof(CONDITION); } while( 0 )
# endif
HDF5IOHandlerImpl::HDF5IOHandlerImpl(AbstractIOHandler* handler)
: AbstractIOHandlerImpl(handler),
m_datasetTransferProperty{H5P_DEFAULT},
m_fileAccessProperty{H5P_DEFAULT},
m_H5T_BOOL_ENUM{H5Tenum_create(H5T_NATIVE_INT8)}
{
VERIFY(m_H5T_BOOL_ENUM >= 0, "[HDF5] Internal error: Failed to create HDF5 enum");
std::string t{"TRUE"};
std::string f{"FALSE"};
int64_t tVal = 1;
int64_t fVal = 0;
herr_t status;
status = H5Tenum_insert(m_H5T_BOOL_ENUM, t.c_str(), &tVal);
VERIFY(status == 0, "[HDF5] Internal error: Failed to insert into HDF5 enum");
status = H5Tenum_insert(m_H5T_BOOL_ENUM, f.c_str(), &fVal);
VERIFY(status == 0, "[HDF5] Internal error: Failed to insert into HDF5 enum");
}
HDF5IOHandlerImpl::~HDF5IOHandlerImpl()
{
herr_t status;
status = H5Tclose(m_H5T_BOOL_ENUM);
if( status < 0 )
std::cerr << "Internal error: Failed to close HDF5 enum\n";
while( !m_openFileIDs.empty() )
{
auto file = m_openFileIDs.begin();
status = H5Fclose(*file);
if( status < 0 )
std::cerr << "Internal error: Failed to close HDF5 file (serial)\n";
m_openFileIDs.erase(file);
}
if( m_datasetTransferProperty != H5P_DEFAULT )
{
status = H5Pclose(m_datasetTransferProperty);
if( status < 0 )
std::cerr << "Internal error: Failed to close HDF5 dataset transfer property\n";
}
if( m_fileAccessProperty != H5P_DEFAULT )
{
status = H5Pclose(m_fileAccessProperty);
if( status < 0 )
std::cerr << "Internal error: Failed to close HDF5 file access property\n";
}
}
void
HDF5IOHandlerImpl::createFile(Writable* writable,
Parameter< Operation::CREATE_FILE > const& parameters)
{
if( m_handler->accessTypeBackend == AccessType::READ_ONLY )
throw std::runtime_error("[HDF5] Creating a file in read-only mode is not possible.");
if( !writable->written )
{
if( !auxiliary::directory_exists(m_handler->directory) )
{
bool success = auxiliary::create_directories(m_handler->directory);
VERIFY(success, "[HDF5] Internal error: Failed to create directories during HDF5 file creation");
}
std::string name = m_handler->directory + parameters.name;
if( !auxiliary::ends_with(name, ".h5") )
name += ".h5";
unsigned flags;
if( m_handler->accessTypeBackend == AccessType::CREATE )
flags = H5F_ACC_TRUNC;
else
flags = H5F_ACC_EXCL;
hid_t id = H5Fcreate(name.c_str(),
flags,
H5P_DEFAULT,
m_fileAccessProperty);
VERIFY(id >= 0, "[HDF5] Internal error: Failed to create HDF5 file");
writable->written = true;
writable->abstractFilePosition = std::make_shared< HDF5FilePosition >("/");
m_fileIDs[writable] = id;
m_openFileIDs.insert(id);
}
}
void
HDF5IOHandlerImpl::createPath(Writable* writable,
Parameter< Operation::CREATE_PATH > const& parameters)
{
if( m_handler->accessTypeBackend == AccessType::READ_ONLY )
throw std::runtime_error("[HDF5] Creating a path in a file opened as read only is not possible.");
if( !writable->written )
{
/* Sanitize path */
std::string path = parameters.path;
if( auxiliary::starts_with(path, '/') )
path = auxiliary::replace_first(path, "/", "");
if( !auxiliary::ends_with(path, '/') )
path += '/';
/* Open H5Object to write into */
Writable* position;
if( writable->parent )
position = writable->parent;
else
position = writable; /* root does not have a parent but might still have to be written */
auto res = m_fileIDs.find(position);
hid_t node_id = H5Gopen(res->second,
concrete_h5_file_position(position).c_str(),
H5P_DEFAULT);
VERIFY(node_id >= 0, "[HDF5] Internal error: Failed to open HDF5 group during path creation");
/* Create the path in the file */
std::stack< hid_t > groups;
groups.push(node_id);
for( std::string const& folder : auxiliary::split(path, "/", false) )
{
hid_t group_id = H5Gcreate(groups.top(),
folder.c_str(),
H5P_DEFAULT,
H5P_DEFAULT,
H5P_DEFAULT);
VERIFY(group_id >= 0, "[HDF5] Internal error: Failed to create HDF5 group during path creation");
groups.push(group_id);
}
/* Close the groups */
herr_t status;
while( !groups.empty() )
{
status = H5Gclose(groups.top());
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 group during path creation");
groups.pop();
}
writable->written = true;
writable->abstractFilePosition = std::make_shared< HDF5FilePosition >(path);
m_fileIDs[writable] = res->second;
}
}
void
HDF5IOHandlerImpl::createDataset(Writable* writable,
Parameter< Operation::CREATE_DATASET > const& parameters)
{
if( m_handler->accessTypeBackend == AccessType::READ_ONLY )
throw std::runtime_error("[HDF5] Creating a dataset in a file opened as read only is not possible.");
if( !writable->written )
{
/* Sanitize name */
std::string name = parameters.name;
if( auxiliary::starts_with(name, '/') )
name = auxiliary::replace_first(name, "/", "");
if( auxiliary::ends_with(name, '/') )
name = auxiliary::replace_last(name, "/", "");
/* Open H5Object to write into */
auto res = m_fileIDs.find(writable);
if( res == m_fileIDs.end() )
res = m_fileIDs.find(writable->parent);
hid_t node_id = H5Gopen(res->second,
concrete_h5_file_position(writable).c_str(),
H5P_DEFAULT);
VERIFY(node_id >= 0, "[HDF5] Internal error: Failed to open HDF5 group during dataset creation");
Datatype d = parameters.dtype;
if( d == Datatype::UNDEFINED )
{
// TODO handle unknown dtype
std::cerr << "Datatype::UNDEFINED caught during dataset creation (serial HDF5)" << std::endl;
d = Datatype::BOOL;
}
Attribute a(0);
a.dtype = d;
std::vector< hsize_t > dims;
for( auto const& val : parameters.extent )
dims.push_back(static_cast< hsize_t >(val));
hid_t space = H5Screate_simple(static_cast< int >(dims.size()), dims.data(), dims.data());
VERIFY(space >= 0, "[HDF5] Internal error: Failed to create dataspace during dataset creation");
std::vector< hsize_t > chunkDims;
for( auto const& val : parameters.chunkSize )
chunkDims.push_back(static_cast< hsize_t >(val));
/* enable chunking on the created dataspace */
hid_t datasetCreationProperty = H5Pcreate(H5P_DATASET_CREATE);
herr_t status;
//status = H5Pset_chunk(datasetCreationProperty, chunkDims.size(), chunkDims.data());
//VERIFY(status == 0, "[HDF5] Internal error: Failed to set chunk size during dataset creation");
std::string const& compression = parameters.compression;
if( !compression.empty() )
std::cerr << "Compression not yet implemented in HDF5 backend."
<< std::endl;
/*
{
std::vector< std::string > args = auxiliary::split(compression, ":");
std::string const& format = args[0];
if( (format == "zlib" || format == "gzip" || format == "deflate")
&& args.size() == 2 )
{
status = H5Pset_deflate(datasetCreationProperty, std::stoi(args[1]));
VERIFY(status == 0, "[HDF5] Internal error: Failed to set deflate compression during dataset creation");
} else if( format == "szip" || format == "nbit" || format == "scaleoffset" )
std::cerr << "Compression format " << format
<< " not yet implemented. Data will not be compressed!"
<< std::endl;
else
std::cerr << "Compression format " << format
<< " unknown. Data will not be compressed!"
<< std::endl;
}
*/
std::string const& transform = parameters.transform;
if( !transform.empty() )
std::cerr << "Custom transform not yet implemented in HDF5 backend."
<< std::endl;
hid_t datatype = getH5DataType(a);
VERIFY(datatype >= 0, "[HDF5] Internal error: Failed to get HDF5 datatype during dataset creation");
hid_t group_id = H5Dcreate(node_id,
name.c_str(),
datatype,
space,
H5P_DEFAULT,
datasetCreationProperty,
H5P_DEFAULT);
VERIFY(group_id >= 0, "[HDF5] Internal error: Failed to create HDF5 group during dataset creation");
status = H5Dclose(group_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 dataset during dataset creation");
status = H5Tclose(datatype);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 datatype during dataset creation");
status = H5Pclose(datasetCreationProperty);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 dataset creation property during dataset creation");
status = H5Sclose(space);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 dataset space during dataset creation");
status = H5Gclose(node_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 group during dataset creation");
writable->written = true;
writable->abstractFilePosition = std::make_shared< HDF5FilePosition >(name);
m_fileIDs[writable] = res->second;
}
}
void
HDF5IOHandlerImpl::extendDataset(Writable* writable,
Parameter< Operation::EXTEND_DATASET > const& parameters)
{
if( m_handler->accessTypeBackend == AccessType::READ_ONLY )
throw std::runtime_error("[HDF5] Extending a dataset in a file opened as read only is not possible.");
if( !writable->written )
throw std::runtime_error("[HDF5] Extending an unwritten Dataset is not possible.");
auto res = m_fileIDs.find(writable->parent);
hid_t node_id, dataset_id;
node_id = H5Gopen(res->second,
concrete_h5_file_position(writable->parent).c_str(),
H5P_DEFAULT);
VERIFY(node_id >= 0, "[HDF5] Internal error: Failed to open HDF5 group during dataset extension");
/* Sanitize name */
std::string name = parameters.name;
if( auxiliary::starts_with(name, '/') )
name = auxiliary::replace_first(name, "/", "");
if( !auxiliary::ends_with(name, '/') )
name += '/';
dataset_id = H5Dopen(node_id,
name.c_str(),
H5P_DEFAULT);
VERIFY(dataset_id >= 0, "[HDF5] Internal error: Failed to open HDF5 dataset during dataset extension");
std::vector< hsize_t > size;
for( auto const& val : parameters.extent )
size.push_back(static_cast< hsize_t >(val));
herr_t status;
status = H5Dset_extent(dataset_id, size.data());
VERIFY(status == 0, "[HDF5] Internal error: Failed to extend HDF5 dataset during dataset extension");
status = H5Dclose(dataset_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 dataset during dataset extension");
status = H5Gclose(node_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 group during dataset extension");
}
void
HDF5IOHandlerImpl::openFile(Writable* writable,
Parameter< Operation::OPEN_FILE > const& parameters)
{
//TODO check if file already open
//not possible with current implementation
//quick idea - map with filenames as key
if( !auxiliary::directory_exists(m_handler->directory) )
throw no_such_file_error("[HDF5] Supplied directory is not valid: " + m_handler->directory);
std::string name = m_handler->directory + parameters.name;
if( !auxiliary::ends_with(name, ".h5") )
name += ".h5";
unsigned flags;
AccessType at = m_handler->accessTypeBackend;
if( at == AccessType::READ_ONLY )
flags = H5F_ACC_RDONLY;
else if( at == AccessType::READ_WRITE || at == AccessType::CREATE )
flags = H5F_ACC_RDWR;
else
throw std::runtime_error("[HDF5] Unknown file AccessType");
hid_t file_id;
file_id = H5Fopen(name.c_str(),
flags,
m_fileAccessProperty);
if( file_id < 0 )
throw no_such_file_error("[HDF5] Failed to open HDF5 file " + name);
writable->written = true;
writable->abstractFilePosition = std::make_shared< HDF5FilePosition >("/");
m_fileIDs.erase(writable);
m_fileIDs.insert({writable, file_id});
m_openFileIDs.insert(file_id);
}
void
HDF5IOHandlerImpl::openPath(Writable* writable,
Parameter< Operation::OPEN_PATH > const& parameters)
{
auto res = m_fileIDs.find(writable->parent);
hid_t node_id, path_id;
node_id = H5Gopen(res->second,
concrete_h5_file_position(writable->parent).c_str(),
H5P_DEFAULT);
VERIFY(node_id >= 0, "[HDF5] Internal error: Failed to open HDF5 group during path opening");
/* Sanitize path */
std::string path = parameters.path;
if( auxiliary::starts_with(path, '/') )
path = auxiliary::replace_first(path, "/", "");
if( !auxiliary::ends_with(path, '/') )
path += '/';
path_id = H5Gopen(node_id,
path.c_str(),
H5P_DEFAULT);
VERIFY(path_id >= 0, "[HDF5] Internal error: Failed to open HDF5 group during path opening");
herr_t status;
status = H5Gclose(path_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 group during path opening");
status = H5Gclose(node_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 group during path opening");
writable->written = true;
writable->abstractFilePosition = std::make_shared< HDF5FilePosition >(path);
m_fileIDs.erase(writable);
m_fileIDs.insert({writable, res->second});
}
void
HDF5IOHandlerImpl::openDataset(Writable* writable,
Parameter< Operation::OPEN_DATASET > & parameters)
{
auto res = m_fileIDs.find(writable->parent);
hid_t node_id, dataset_id;
node_id = H5Gopen(res->second,
concrete_h5_file_position(writable->parent).c_str(),
H5P_DEFAULT);
VERIFY(node_id >= 0, "[HDF5] Internal error: Failed to open HDF5 group during dataset opening");
/* Sanitize name */
std::string name = parameters.name;
if( auxiliary::starts_with(name, '/') )
name = auxiliary::replace_first(name, "/", "");
if( !auxiliary::ends_with(name, '/') )
name += '/';
dataset_id = H5Dopen(node_id,
name.c_str(),
H5P_DEFAULT);
VERIFY(dataset_id >= 0, "[HDF5] Internal error: Failed to open HDF5 dataset during dataset opening");
hid_t dataset_type, dataset_space;
dataset_type = H5Dget_type(dataset_id);
dataset_space = H5Dget_space(dataset_id);
H5S_class_t dataset_class = H5Sget_simple_extent_type(dataset_space);
using DT = Datatype;
Datatype d;
if( dataset_class == H5S_SIMPLE || dataset_class == H5S_SCALAR )
{
if( H5Tequal(dataset_type, H5T_NATIVE_CHAR) )
d = DT::CHAR;
else if( H5Tequal(dataset_type, H5T_NATIVE_UCHAR) )
d = DT::UCHAR;
else if( H5Tequal(dataset_type, H5T_NATIVE_SHORT) )
d = DT::SHORT;
else if( H5Tequal(dataset_type, H5T_NATIVE_INT) )
d = DT::INT;
else if( H5Tequal(dataset_type, H5T_NATIVE_LONG) )
d = DT::LONG;
else if( H5Tequal(dataset_type, H5T_NATIVE_LLONG) )
d = DT::LONGLONG;
else if( H5Tequal(dataset_type, H5T_NATIVE_FLOAT) )
d = DT::FLOAT;
else if( H5Tequal(dataset_type, H5T_NATIVE_DOUBLE) )
d = DT::DOUBLE;
else if( H5Tequal(dataset_type, H5T_NATIVE_USHORT) )
d = DT::USHORT;
else if( H5Tequal(dataset_type, H5T_NATIVE_UINT) )
d = DT::UINT;
else if( H5Tequal(dataset_type, H5T_NATIVE_ULONG) )
d = DT::ULONG;
else if( H5Tequal(dataset_type, H5T_NATIVE_ULLONG) )
d = DT::ULONGLONG;
else if( H5Tget_class(dataset_type) == H5T_STRING )
d = DT::STRING;
else
throw std::runtime_error("[HDF5] Unknown dataset type");
} else
throw std::runtime_error("[HDF5] Unsupported dataset class");
auto dtype = parameters.dtype;
*dtype = d;
int ndims = H5Sget_simple_extent_ndims(dataset_space);
std::vector< hsize_t > dims(ndims, 0);
std::vector< hsize_t > maxdims(ndims, 0);
H5Sget_simple_extent_dims(dataset_space,
dims.data(),
maxdims.data());
Extent e;
for( auto const& val : dims )
e.push_back(val);
auto extent = parameters.extent;
*extent = e;
herr_t status;
status = H5Sclose(dataset_space);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 dataset space during dataset opening");
status = H5Tclose(dataset_type);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 dataset type during dataset opening");
status = H5Dclose(dataset_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 dataset during dataset opening");
status = H5Gclose(node_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 group during dataset opening");
writable->written = true;
writable->abstractFilePosition = std::make_shared< HDF5FilePosition >(name);
m_fileIDs[writable] = res->second;
}
void
HDF5IOHandlerImpl::deleteFile(Writable* writable,
Parameter< Operation::DELETE_FILE > const& parameters)
{
if( m_handler->accessTypeBackend == AccessType::READ_ONLY )
throw std::runtime_error("[HDF5] Deleting a file opened as read only is not possible.");
if( writable->written )
{
hid_t file_id = m_fileIDs[writable];
herr_t status = H5Fclose(file_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 file during file deletion");
std::string name = m_handler->directory + parameters.name;
if( !auxiliary::ends_with(name, ".h5") )
name += ".h5";
if( !auxiliary::file_exists(name) )
throw std::runtime_error("[HDF5] File does not exist: " + name);
auxiliary::remove_file(name);
writable->written = false;
writable->abstractFilePosition.reset();
m_openFileIDs.erase(file_id);
m_fileIDs.erase(writable);
}
}
void
HDF5IOHandlerImpl::deletePath(Writable* writable,
Parameter< Operation::DELETE_PATH > const& parameters)
{
if( m_handler->accessTypeBackend == AccessType::READ_ONLY )
throw std::runtime_error("[HDF5] Deleting a path in a file opened as read only is not possible.");
if( writable->written )
{
/* Sanitize path */
std::string path = parameters.path;
if( auxiliary::starts_with(path, '/') )
path = auxiliary::replace_first(path, "/", "");
if( !auxiliary::ends_with(path, '/') )
path += '/';
/* Open H5Object to delete in
* Ugly hack: H5Ldelete can't delete "."
* Work around this by deleting from the parent
*/
auto res = m_fileIDs.find(writable);
if( res == m_fileIDs.end() )
res = m_fileIDs.find(writable->parent);
hid_t node_id = H5Gopen(res->second,
concrete_h5_file_position(writable->parent).c_str(),
H5P_DEFAULT);
VERIFY(node_id >= 0, "[HDF5] Internal error: Failed to open HDF5 group during path deletion");
path += static_cast< HDF5FilePosition* >(writable->abstractFilePosition.get())->location;
herr_t status = H5Ldelete(node_id,
path.c_str(),
H5P_DEFAULT);
VERIFY(status == 0, "[HDF5] Internal error: Failed to delete HDF5 group");
status = H5Gclose(node_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 group during path deletion");
writable->written = false;
writable->abstractFilePosition.reset();
m_fileIDs.erase(writable);
}
}
void
HDF5IOHandlerImpl::deleteDataset(Writable* writable,
Parameter< Operation::DELETE_DATASET > const& parameters)
{
if( m_handler->accessTypeBackend == AccessType::READ_ONLY )
throw std::runtime_error("[HDF5] Deleting a path in a file opened as read only is not possible.");
if( writable->written )
{
/* Sanitize name */
std::string name = parameters.name;
if( auxiliary::starts_with(name, '/') )
name = auxiliary::replace_first(name, "/", "");
if( !auxiliary::ends_with(name, '/') )
name += '/';
/* Open H5Object to delete in
* Ugly hack: H5Ldelete can't delete "."
* Work around this by deleting from the parent
*/
auto res = m_fileIDs.find(writable);
if( res == m_fileIDs.end() )
res = m_fileIDs.find(writable->parent);
hid_t node_id = H5Gopen(res->second,
concrete_h5_file_position(writable->parent).c_str(),
H5P_DEFAULT);
VERIFY(node_id >= 0, "[HDF5] Internal error: Failed to open HDF5 group during dataset deletion");
name += static_cast< HDF5FilePosition* >(writable->abstractFilePosition.get())->location;
herr_t status = H5Ldelete(node_id,
name.c_str(),
H5P_DEFAULT);
VERIFY(status == 0, "[HDF5] Internal error: Failed to delete HDF5 group");
status = H5Gclose(node_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 group during dataset deletion");
writable->written = false;
writable->abstractFilePosition.reset();
m_fileIDs.erase(writable);
}
}
void
HDF5IOHandlerImpl::deleteAttribute(Writable* writable,
Parameter< Operation::DELETE_ATT > const& parameters)
{
if( m_handler->accessTypeBackend == AccessType::READ_ONLY )
throw std::runtime_error("[HDF5] Deleting an attribute in a file opened as read only is not possible.");
if( writable->written )
{
std::string name = parameters.name;
/* Open H5Object to delete in */
auto res = m_fileIDs.find(writable);
if( res == m_fileIDs.end() )
res = m_fileIDs.find(writable->parent);
hid_t node_id = H5Oopen(res->second,
concrete_h5_file_position(writable).c_str(),
H5P_DEFAULT);
VERIFY(node_id >= 0, "[HDF5] Internal error: Failed to open HDF5 group during attribute deletion");
herr_t status = H5Adelete(node_id,
name.c_str());
VERIFY(status == 0, "[HDF5] Internal error: Failed to delete HDF5 attribute");
status = H5Oclose(node_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 group during attribute deletion");
}
}
void
HDF5IOHandlerImpl::writeDataset(Writable* writable,
Parameter< Operation::WRITE_DATASET > const& parameters)
{
if( m_handler->accessTypeBackend == AccessType::READ_ONLY )
throw std::runtime_error("[HDF5] Writing into a dataset in a file opened as read only is not possible.");
auto res = m_fileIDs.find(writable);
if( res == m_fileIDs.end() )
res = m_fileIDs.find(writable->parent);
hid_t dataset_id, filespace, memspace;
herr_t status;
dataset_id = H5Dopen(res->second,
concrete_h5_file_position(writable).c_str(),
H5P_DEFAULT);
VERIFY(dataset_id >= 0, "[HDF5] Internal error: Failed to open HDF5 dataset during dataset write");
std::vector< hsize_t > start;
for( auto const& val : parameters.offset )
start.push_back(static_cast< hsize_t >(val));
std::vector< hsize_t > stride(start.size(), 1); /* contiguous region */
std::vector< hsize_t > count(start.size(), 1); /* single region */
std::vector< hsize_t > block;
for( auto const& val : parameters.extent )
block.push_back(static_cast< hsize_t >(val));
memspace = H5Screate_simple(static_cast< int >(block.size()), block.data(), nullptr);
filespace = H5Dget_space(dataset_id);
status = H5Sselect_hyperslab(filespace,
H5S_SELECT_SET,
start.data(),
stride.data(),
count.data(),
block.data());
VERIFY(status == 0, "[HDF5] Internal error: Failed to select hyperslab during dataset write");
std::shared_ptr< void const > data = parameters.data;
//TODO Check if parameter dtype and dataset dtype match
Attribute a(0);
a.dtype = parameters.dtype;
hid_t dataType = getH5DataType(a);
VERIFY(dataType >= 0, "[HDF5] Internal error: Failed to get HDF5 datatype during dataset write");
switch( a.dtype )
{
using DT = Datatype;
case DT::DOUBLE:
case DT::FLOAT:
case DT::SHORT:
case DT::INT:
case DT::LONG:
case DT::LONGLONG:
case DT::USHORT:
case DT::UINT:
case DT::ULONG:
case DT::ULONGLONG:
case DT::CHAR:
case DT::UCHAR:
case DT::BOOL:
status = H5Dwrite(dataset_id,
dataType,
memspace,
filespace,
m_datasetTransferProperty,
data.get());
VERIFY(status == 0, "[HDF5] Internal error: Failed to write dataset " + concrete_h5_file_position(writable));
break;
case DT::UNDEFINED:
throw std::runtime_error("[HDF5] Undefined Attribute datatype");
case DT::DATATYPE:
throw std::runtime_error("[HDF5] Meta-Datatype leaked into IO");
default:
throw std::runtime_error("[HDF5] Datatype not implemented in HDF5 IO");
}
status = H5Tclose(dataType);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close dataset datatype during dataset write");
status = H5Sclose(filespace);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close dataset file space during dataset write");
status = H5Sclose(memspace);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close dataset memory space during dataset write");
status = H5Dclose(dataset_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close dataset " + concrete_h5_file_position(writable) + " during dataset write");
m_fileIDs[writable] = res->second;
}
void
HDF5IOHandlerImpl::writeAttribute(Writable* writable,
Parameter< Operation::WRITE_ATT > const& parameters)
{
if( m_handler->accessTypeBackend == AccessType::READ_ONLY )
throw std::runtime_error("[HDF5] Writing an attribute in a file opened as read only is not possible.");
auto res = m_fileIDs.find(writable);
if( res == m_fileIDs.end() )
res = m_fileIDs.find(writable->parent);
hid_t node_id, attribute_id;
node_id = H5Oopen(res->second,
concrete_h5_file_position(writable).c_str(),
H5P_DEFAULT);
VERIFY(node_id >= 0, "[HDF5] Internal error: Failed to open HDF5 object during attribute write");
Attribute const att(parameters.resource);
Datatype dtype = parameters.dtype;
herr_t status;
hid_t dataType;
if( dtype == Datatype::BOOL )
dataType = m_H5T_BOOL_ENUM;
else
dataType = getH5DataType(att);
VERIFY(dataType >= 0, "[HDF5] Internal error: Failed to get HDF5 datatype during attribute write");
std::string name = parameters.name;
if( H5Aexists(node_id, name.c_str()) == 0 )
{
hid_t dataspace = getH5DataSpace(att);
VERIFY(dataspace >= 0, "[HDF5] Internal error: Failed to get HDF5 dataspace during attribute write");
attribute_id = H5Acreate(node_id,
name.c_str(),
dataType,
dataspace,
H5P_DEFAULT,
H5P_DEFAULT);
VERIFY(node_id >= 0, "[HDF5] Internal error: Failed to create HDF5 attribute during attribute write");
status = H5Sclose(dataspace);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 dataspace during attribute write");
} else
{
attribute_id = H5Aopen(node_id,
name.c_str(),
H5P_DEFAULT);
VERIFY(node_id >= 0, "[HDF5] Internal error: Failed to open HDF5 attribute during attribute write");
}
using DT = Datatype;
switch( dtype )
{
case DT::CHAR:
{
char c = att.get< char >();
status = H5Awrite(attribute_id, dataType, &c);
break;
}
case DT::UCHAR:
{
unsigned char u = att.get< unsigned char >();
status = H5Awrite(attribute_id, dataType, &u);
break;
}
case DT::SHORT:
{
short i = att.get< short >();
status = H5Awrite(attribute_id, dataType, &i);
break;
}
case DT::INT:
{
int i = att.get< int >();
status = H5Awrite(attribute_id, dataType, &i);
break;
}
case DT::LONG:
{
long i = att.get< long >();
status = H5Awrite(attribute_id, dataType, &i);
break;
}
case DT::LONGLONG:
{
long long i = att.get< long long >();
status = H5Awrite(attribute_id, dataType, &i);
break;
}
case DT::USHORT:
{
unsigned short u = att.get< unsigned short >();
status = H5Awrite(attribute_id, dataType, &u);
break;
}
case DT::UINT:
{
unsigned int u = att.get< unsigned int >();
status = H5Awrite(attribute_id, dataType, &u);
break;
}
case DT::ULONG:
{
unsigned long u = att.get< unsigned long >();
status = H5Awrite(attribute_id, dataType, &u);
break;
}
case DT::ULONGLONG:
{
unsigned long long u = att.get< unsigned long long >();
status = H5Awrite(attribute_id, dataType, &u);
break;
}
case DT::FLOAT:
{
float f = att.get< float >();
status = H5Awrite(attribute_id, dataType, &f);
break;
}
case DT::DOUBLE:
{
double d = att.get< double >();
status = H5Awrite(attribute_id, dataType, &d);
break;
}
case DT::LONG_DOUBLE:
{
long double d = att.get< long double >();
status = H5Awrite(attribute_id, dataType, &d);
break;
}
case DT::STRING:
status = H5Awrite(attribute_id,
dataType,
att.get< std::string >().c_str());
break;
case DT::VEC_CHAR:
status = H5Awrite(attribute_id,
dataType,
att.get< std::vector< char > >().data());
break;
case DT::VEC_SHORT:
status = H5Awrite(attribute_id,
dataType,
att.get< std::vector< short > >().data());
break;
case DT::VEC_INT:
status = H5Awrite(attribute_id,
dataType,
att.get< std::vector< int > >().data());
break;
case DT::VEC_LONG:
status = H5Awrite(attribute_id,
dataType,
att.get< std::vector< long > >().data());
break;
case DT::VEC_LONGLONG:
status = H5Awrite(attribute_id,
dataType,
att.get< std::vector< long long > >().data());
break;
case DT::VEC_UCHAR:
status = H5Awrite(attribute_id,
dataType,
att.get< std::vector< unsigned char > >().data());
break;
case DT::VEC_USHORT:
status = H5Awrite(attribute_id,
dataType,
att.get< std::vector< unsigned short > >().data());
break;
case DT::VEC_UINT:
status = H5Awrite(attribute_id,
dataType,
att.get< std::vector< unsigned int > >().data());
break;
case DT::VEC_ULONG:
status = H5Awrite(attribute_id,
dataType,
att.get< std::vector< unsigned long > >().data());
break;
case DT::VEC_ULONGLONG:
status = H5Awrite(attribute_id,
dataType,
att.get< std::vector< unsigned long long > >().data());
break;
case DT::VEC_FLOAT:
status = H5Awrite(attribute_id,
dataType,
att.get< std::vector< float > >().data());
break;
case DT::VEC_DOUBLE:
status = H5Awrite(attribute_id,
dataType,
att.get< std::vector< double > >().data());
break;
case DT::VEC_LONG_DOUBLE:
status = H5Awrite(attribute_id,
dataType,
att.get< std::vector< long double > >().data());
break;
case DT::VEC_STRING:
{
auto vs = att.get< std::vector< std::string > >();
size_t max_len = 0;
for( std::string const& s : vs )
max_len = std::max(max_len, s.size());
std::unique_ptr< char[] > c_str(new char[max_len * vs.size()]);
for( size_t i = 0; i < vs.size(); ++i )
strncpy(c_str.get() + i*max_len, vs[i].c_str(), max_len);
status = H5Awrite(attribute_id, dataType, c_str.get());
break;
}
case DT::ARR_DBL_7:
status = H5Awrite(attribute_id,
dataType,
att.get< std::array< double, 7 > >().data());
break;
case DT::BOOL:
{
bool b = att.get< bool >();
status = H5Awrite(attribute_id, dataType, &b);
break;
}
case DT::UNDEFINED:
case DT::DATATYPE:
throw std::runtime_error("[HDF5] Unknown Attribute datatype (HDF5 Attribute write)");
default:
throw std::runtime_error("[HDF5] Datatype not implemented in HDF5 IO");
}
VERIFY(status == 0, "[HDF5] Internal error: Failed to write attribute " + name + " at " + concrete_h5_file_position(writable));
if( dataType != m_H5T_BOOL_ENUM )
{
status = H5Tclose(dataType);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close HDF5 datatype during Attribute write");
}
status = H5Aclose(attribute_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close attribute " + name + " at " + concrete_h5_file_position(writable) + " during attribute write");
status = H5Oclose(node_id);
VERIFY(status == 0, "[HDF5] Internal error: Failed to close " + concrete_h5_file_position(writable) + " during attribute write");
m_fileIDs[writable] = res->second;
}
void
HDF5IOHandlerImpl::readDataset(Writable* writable,
Parameter< Operation::READ_DATASET > & parameters)
{
auto res = m_fileIDs.find(writable);
if( res == m_fileIDs.end() )
res = m_fileIDs.find(writable->parent);
hid_t dataset_id, memspace, filespace;
herr_t status;
dataset_id = H5Dopen(res->second,
concrete_h5_file_position(writable).c_str(),
H5P_DEFAULT);
VERIFY(dataset_id >= 0, "[HDF5] Internal error: Failed to open HDF5 dataset during dataset read");
std::vector< hsize_t > start;
for( auto const& val : parameters.offset )