-
Notifications
You must be signed in to change notification settings - Fork 65
/
conduit_relay_io_hdf5.cpp
4366 lines (3755 loc) · 159 KB
/
conduit_relay_io_hdf5.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 (c) Lawrence Livermore National Security, LLC and other Conduit
// Project developers. See top-level LICENSE AND COPYRIGHT files for dates and
// other details. No copyright assignment is required to contribute to Conduit.
//-----------------------------------------------------------------------------
///
/// file: conduit_relay_io_hdf5.cpp
///
//-----------------------------------------------------------------------------
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
#include "conduit_relay_mpi_io_hdf5.hpp"
#else
#include "conduit_relay_io_hdf5.hpp"
#endif
#include "conduit_fmt/conduit_fmt.h"
//-----------------------------------------------------------------------------
// standard lib includes
//-----------------------------------------------------------------------------
#include <iostream>
//-----------------------------------------------------------------------------
// external lib includes
//-----------------------------------------------------------------------------
#include <hdf5.h>
//-----------------------------------------------------------------------------
/// macro used to check if an HDF5 object id is valid
//-----------------------------------------------------------------------------
#define CONDUIT_HDF5_VALID_ID( hdf5_id ) hdf5_id >= 0
//-----------------------------------------------------------------------------
/// macro used to check if an HDF5 return status is ok
//-----------------------------------------------------------------------------
#define CONDUIT_HDF5_STATUS_OK(hdf5_id ) hdf5_id >= 0
//-----------------------------------------------------------------------------
/// The CONDUIT_HDF5_ERROR macro is used for errors with ref paths.
//-----------------------------------------------------------------------------
#define CONDUIT_HDF5_ERROR( ref_path, msg ) \
{ \
CONDUIT_ERROR( "HDF5 Error (reference path: \"" << ref_path << "\") " \
<< msg); \
}
//-----------------------------------------------------------------------------
/// The CONDUIT_HDF5_WARN macro is used for warnings with ref paths.
//-----------------------------------------------------------------------------
#define CONDUIT_HDF5_WARN( ref_path, msg ) \
{ \
CONDUIT_WARN( "HDF5 Warning (reference path: \"" << ref_path << "\") " \
<< msg); \
}
//-----------------------------------------------------------------------------
/// The CONDUIT_CHECK_HDF5_ERROR macro is used to check error codes from HDF5.
//-----------------------------------------------------------------------------
#define CONDUIT_CHECK_HDF5_ERROR_WITH_REF_PATH( hdf5_err, ref_path, msg ) \
{ \
if( hdf5_err < 0 ) \
{ \
std::ostringstream hdf5_err_oss; \
hdf5_err_oss << "HDF5 Error (error code: " \
<< hdf5_err \
<< ", reference path: \"" \
<< ref_path << "\"" \
<< ") " << msg; \
CONDUIT_ERROR( hdf5_err_oss.str()); \
} \
}
//-----------------------------------------------------------------------------
/// The CONDUIT_CHECK_HDF5_ERROR macro is used to check error codes from HDF5.
//-----------------------------------------------------------------------------
#define CONDUIT_CHECK_HDF5_ERROR_WITH_FILE_AND_REF_PATH( hdf5_err, hdf5_obj_id, ref_path, msg ) \
{ \
if( hdf5_err < 0 ) \
{ \
/* Try to find the file system path from hdf5_obj */ \
ssize_t hdf5_f_sz = H5Fget_name(hdf5_obj_id, NULL, 0 ); \
std::ostringstream hdf5_err_oss; \
hdf5_err_oss << "HDF5 Error (error code: " \
<< hdf5_err \
<< ", reference path: \""; \
if(hdf5_f_sz > 0) \
{ \
std::vector<char>hdf5_f_buff(hdf5_f_sz+1, 0); \
H5Fget_name(hdf5_obj_id, &hdf5_f_buff[0], hdf5_f_sz+1); \
hdf5_err_oss << std::string(&hdf5_f_buff[0]) << ":"; \
} \
hdf5_err_oss << ref_path << "\"" \
<< ") " << msg; \
CONDUIT_ERROR( hdf5_err_oss.str()); \
} \
}
//-----------------------------------------------------------------------------
// -- begin conduit:: --
//-----------------------------------------------------------------------------
namespace conduit
{
//-----------------------------------------------------------------------------
// -- begin conduit::relay --
//-----------------------------------------------------------------------------
namespace relay
{
#ifdef CONDUIT_RELAY_IO_MPI_ENABLED
//-----------------------------------------------------------------------------
// -- begin conduit::relay::mpi --
//-----------------------------------------------------------------------------
namespace mpi
{
#endif
//-----------------------------------------------------------------------------
// -- begin conduit::relay::<mpi>::io --
//-----------------------------------------------------------------------------
namespace io
{
static std::string conduit_hdf5_list_attr_name = "__conduit_list";
//-----------------------------------------------------------------------------
// Private class used to hold options that control hdf5 i/o params.
//
// These values are read by about(), and are set by io::hdf5_set_options()
//
//
//-----------------------------------------------------------------------------
class HDF5Options
{
public:
static bool chunking_enabled;
static int chunk_threshold;
static int chunk_size;
static bool compact_storage_enabled;
static int compact_storage_threshold;
static std::string compression_method;
static int compression_level;
static std::string libver;
static std::string messages;
public:
//------------------------------------------------------------------------
static void set(const Node &opts)
{
if(opts.has_child("libver"))
{
libver = opts["libver"].as_string();
}
if(opts.has_child("messages"))
{
messages = opts["messages"].as_string();
}
if(opts.has_child("compact_storage"))
{
const Node &compact = opts["compact_storage"];
if(compact.has_child("enabled"))
{
std::string enabled = compact["enabled"].as_string();
if(enabled == "false")
{
compact_storage_enabled = false;
}
else
{
compact_storage_enabled = true;
}
}
if(compact.has_child("threshold"))
{
compact_storage_threshold = compact["threshold"].to_value();
}
}
if(opts.has_child("chunking"))
{
const Node &chunking = opts["chunking"];
if(chunking.has_child("enabled"))
{
std::string enabled = chunking["enabled"].as_string();
if(enabled == "false")
{
chunking_enabled = false;
}
else
{
chunking_enabled = true;
}
}
if(chunking.has_child("threshold"))
{
chunk_threshold = chunking["threshold"].to_value();
}
if(chunking.has_child("chunk_size"))
{
chunk_size = chunking["chunk_size"].to_value();
}
if(chunking.has_child("compression"))
{
const Node &comp = chunking["compression"];
if(comp.has_child("method"))
{
compression_method = comp["method"].as_string();
}
if(comp.has_path("level"))
{
compression_level = comp["level"].to_value();
}
}
}
}
//------------------------------------------------------------------------
static void about(Node &opts)
{
opts.reset();
// report hdf5_library_version
unsigned int major_num=0;
unsigned int minor_num=0;
unsigned int release_num=0;
herr_t h5_status = H5get_libversion(&major_num, &minor_num,&release_num);
CONDUIT_CHECK_HDF5_ERROR(h5_status,
"Failed to fetch HDF5 library version info ");
opts["hdf5_library_version"] = conduit_fmt::format("v{0}.{1}.{2}",
major_num,
minor_num,
release_num);
opts["libver"] = libver;
if(compact_storage_enabled)
{
opts["compact_storage/enabled"] = "true";
}
else
{
opts["compact_storage/enabled"] = "false";
}
opts["compact_storage/threshold"] = compact_storage_threshold;
if(chunking_enabled)
{
opts["chunking/enabled"] = "true";
}
else
{
opts["chunking/enabled"] = "false";
}
opts["chunking/threshold"] = chunk_threshold;
opts["chunking/chunk_size"] = chunk_size;
opts["chunking/compression/method"] = compression_method;
if(compression_method == "gzip")
{
opts["chunking/compression/level"] = compression_level;
}
}
};
// default hdf5 i/o settings
bool HDF5Options::compact_storage_enabled = true;
int HDF5Options::compact_storage_threshold = 1024;
bool HDF5Options::chunking_enabled = true;
int HDF5Options::chunk_size = 1000000; // 1 mb
int HDF5Options::chunk_threshold = 2000000; // 2 mb
std::string HDF5Options::compression_method = "gzip";
int HDF5Options::compression_level = 5;
std::string HDF5Options::libver = "default";
// quiet (default) suppresses hdf5 diag warnings in outer relay API layers
std::string HDF5Options::messages = "quiet";
//-----------------------------------------------------------------------------
void
hdf5_set_options(const Node &opts)
{
HDF5Options::set(opts);
}
//-----------------------------------------------------------------------------
void
hdf5_options(Node &opts)
{
HDF5Options::about(opts);
}
//-----------------------------------------------------------------------------
// Private class used to suppress HDF5 error messages.
//
// Creating an instance of this class will disable the current HDF5 error
// callbacks. The default HDF5 callbacks print error messages when probing
// properties of the HDF5 tree. When the instance is destroyed, the prevsous
// error state is restored.
//
// Suppression is only enabled when HDF5Options::messages == "quiet"
//-----------------------------------------------------------------------------
class HDF5ErrorStackSupressor
{
public:
HDF5ErrorStackSupressor()
: herr_func(NULL),
herr_func_client_data(NULL),
active(true)
{
active = (HDF5Options::messages == "quiet");
if(active)
{
disable_hdf5_error_func();
}
}
~HDF5ErrorStackSupressor()
{
if(active)
{
restore_hdf5_error_func();
}
active = false;
}
private:
// saves current error func.
// for hdf5's default setup this disable printed error messages
// that occur when we are probing properties of the hdf5 tree
void disable_hdf5_error_func()
{
H5Eget_auto(H5E_DEFAULT,
&herr_func,
&herr_func_client_data);
H5Eset_auto(H5E_DEFAULT,
NULL,
NULL);
}
// restores saved error func
void restore_hdf5_error_func()
{
H5Eset_auto(H5E_DEFAULT,
herr_func,
herr_func_client_data);
}
// callback used for hdf5 error interface
H5E_auto2_t herr_func;
// data container for hdf5 error interface callback
void *herr_func_client_data;
// reflects if suppression is on
bool active;
};
//-----------------------------------------------------------------------------
// helper method decls
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// helpers for finding hdf5 object filename and constructing ref paths for
// errors
//-----------------------------------------------------------------------------
void hdf5_filename_from_hdf5_obj_id(hid_t hdf5_id,
std::string &result);
void hdf5_ref_path_with_filename(hid_t hdf5_id,
const std::string &ref_path,
std::string &result);
//-----------------------------------------------------------------------------
// helpers for data type conversions
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// helpers for checking if compatible
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// if incompatible, incompat_details contains human readable details
// about why
//-----------------------------------------------------------------------------
bool check_if_conduit_leaf_is_compatible_with_hdf5_obj(const DataType &dtype,
const std::string &ref_path,
hid_t hdf5_id,
const Node &opts,
std::string &incompat_details);
//-----------------------------------------------------------------------------
bool check_if_conduit_object_is_compatible_with_hdf5_tree(const Node &node,
const std::string &ref_path,
hid_t hdf5_id,
const Node &opts,
std::string &incompat_details);
//-----------------------------------------------------------------------------
bool check_if_conduit_node_is_compatible_with_hdf5_tree(const Node &node,
const std::string &ref_path,
hid_t hdf5_id,
const Node &opts,
std::string &incompat_details);
//-----------------------------------------------------------------------------
bool check_if_conduit_list_is_compatible_with_hdf5_tree(const Node &node,
const std::string &ref_path,
hid_t hdf5_id,
const Node &opts,
std::string &incompat_details);
//-----------------------------------------------------------------------------
bool check_if_hdf5_group_has_conduit_list_attribute(hid_t hdf5_group_id);
//-----------------------------------------------------------------------------
// helpers for writing
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
hid_t create_hdf5_dataset_for_conduit_leaf(const DataType &dt,
const std::string &ref_path,
hid_t hdf5_group_id,
const std::string &hdf5_dset_name,
bool extendible);
//-----------------------------------------------------------------------------
hid_t create_hdf5_group_for_conduit_node(const Node &node,
const std::string &ref_path,
hid_t hdf5_parent_group_id,
const std::string &hdf5_child_group_name);
//-----------------------------------------------------------------------------
// Note: Options may cause the dataset to be recreated,
// this is why the hdf5_dset_id is passed as a reference.
//-----------------------------------------------------------------------------
void write_conduit_leaf_to_hdf5_dataset(const Node &node,
const std::string &ref_path,
hid_t &hdf5_dset_id,
const Node &opts);
//-----------------------------------------------------------------------------
void write_conduit_leaf_to_hdf5_group(const Node &node,
const std::string &ref_path,
hid_t hdf5_group_id,
const std::string &hdf5_dset_name,
const Node &opts);
//-----------------------------------------------------------------------------
void write_conduit_node_children_to_hdf5_group(const Node &node,
const std::string &ref_path,
hid_t hdf5_group_id,
const Node &opts);
//-----------------------------------------------------------------------------
void write_conduit_hdf5_list_attribute(hid_t hdf5_group_id,
const std::string &ref_path);
//-----------------------------------------------------------------------------
void remove_conduit_hdf5_list_attribute(hid_t hdf5_group_id,
const std::string &ref_path);
//-----------------------------------------------------------------------------
// helpers for reading
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void read_hdf5_dataset_into_conduit_node(hid_t hdf5_dset_id,
const std::string &ref_path,
bool only_get_metadata,
const Node &opts,
Node &dest);
//-----------------------------------------------------------------------------
void read_hdf5_group_into_conduit_node(hid_t hdf5_group_id,
const std::string &ref_path,
bool only_get_metadata,
const Node &opts,
Node &dest);
//-----------------------------------------------------------------------------
void read_hdf5_tree_into_conduit_node(hid_t hdf5_id,
const std::string &ref_path,
bool only_get_metadata,
const Node &opts,
Node &dest);
//-----------------------------------------------------------------------------
// helper for HDF5 manipulation of ND arrays
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/// Fill options relating to reading or writing an HDF5 dataset
///
/// @param inopts Input options
/// @param dataspace_id ID of the dataspace of the dataset in question
/// @param filled_opts[out] The options, filled out with defaults
///
/// This routine uses the following children of \a inopts:
/// - sizes (or size)
/// - offsets (or offset)
/// - strides (or stride)
/// .
/// All are optional. All should be numeric and of the same length
/// (the dimensionality of the dataset \a dataspace_id).
///
/// This routine does the following:
/// 1. makes a deep copy of \a inopts into \a filled_opts
/// 2. retrieves metadata from \a dataspace_id. If it's not a dataspace,
/// throw an error.
/// 3. sets filled_opts["slabparams/rank"] as a scalar, the rank (number
/// of dimensions) of the dataset
/// 4. sets filled_opts["slabparams/dataset_sizes"] as the size of the dataset
/// 5. sets children sizes, offsets, strides of filled_opts["slabparams"]
/// based on metadata retrieved from the dataset
/// - default sizes is the size of the data set, also stored as
/// "dataset_sizes"
/// - default strides is all 1s (read or write every element)
/// - default offsets is all 0s (start at the first element)
/// 6. modifies slabparams/sizes, offsets, strides based on what
/// the user provided in inopts
/// 7. sets filled_opts["slabparams/readcount"] as a scalar, the number
/// of values to read, as specified by offset, stride, and size
void
fill_dataset_opts(const std::string & ref_path, const Node& inopts,
hid_t dataspace_id, Node& filled_opts);
hsize_t*
make_dataset_opt_copy(const Node& opts, const std::string opt_name);
//-----------------------------------------------------------------------------
// helper used to properly create a new ref_path for a child
std::string
join_ref_paths(const std::string &parent, const std::string &child)
{
if(parent.size() > 0)
{
return parent + "/" + child;
}
else
{
return child;
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Data Type Helper methods that are a part of public conduit::relay::io
//
// conduit_dtype_to_hdf5_dtype
// conduit_dtype_to_hdf5_dtype_cleanup
// hdf5_dtype_to_conduit_dtype
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
hid_t
conduit_dtype_to_hdf5_dtype(const DataType &dt,
const std::string &ref_path)
{
hid_t res = -1;
// // This code path enables writing strings in a way that is friendlier
// // to hdf5 command line tools like hd5dump and h5ls. However
// // using this path we *cannot* compress that string data, so
// // is currently disabled
//
// if(dt.is_string())
// {
//
// // modify the default hdf5 type to include string length info,
// // so hdf5 tools display the string contents in a human friendly way
//
// // create a copy of the default type
// res = H5Tcopy(H5T_C_S1);
// CONDUIT_CHECK_HDF5_ERROR_WITH_REF_PATH(res,
// ref_path,
// "Failed to copy HDF5 type for string");
//
// // set the size
// CONDUIT_CHECK_HDF5_ERROR_WITH_REF_PATH(
// H5Tset_size(res,
// // string size + null
// dt.number_of_elements()),
// ref_path,
// "Failed to set size in HDF5 string type");
//
// // set term
// CONDUIT_CHECK_HDF5_ERROR_WITH_REF_PATH(
// H5Tset_strpad(res, H5T_STR_NULLTERM),
// ref_path,
// "Failed to set strpad in HDF5 string type");
// }
// strings are special, check for them first
if( dt.is_string() )
{
res = H5T_C_S1;
}
// next check endianness
else if(dt.is_little_endian()) // we know we are little endian
{
switch(dt.id())
{
case DataType::INT8_ID: res = H5T_STD_I8LE; break;
case DataType::INT16_ID: res = H5T_STD_I16LE; break;
case DataType::INT32_ID: res = H5T_STD_I32LE; break;
case DataType::INT64_ID: res = H5T_STD_I64LE; break;
case DataType::UINT8_ID: res = H5T_STD_U8LE; break;
case DataType::UINT16_ID: res = H5T_STD_U16LE; break;
case DataType::UINT32_ID: res = H5T_STD_U32LE; break;
case DataType::UINT64_ID: res = H5T_STD_U64LE; break;
case DataType::FLOAT32_ID: res = H5T_IEEE_F32LE; break;
case DataType::FLOAT64_ID: res = H5T_IEEE_F64LE; break;
case DataType::CHAR8_STR_ID:
CONDUIT_HDF5_ERROR(ref_path,
"conduit::DataType to HDF5 Leaf DataType "
<< "Conversion:"
<< dt.to_json()
<< " needs to be handled with string logic");
break;
default:
CONDUIT_HDF5_ERROR(ref_path,
"conduit::DataType to HDF5 Leaf DataType "
<< "Conversion:"
<< dt.to_json()
<< " is not a leaf data type");
};
}
else // we know we are big endian
{
switch(dt.id())
{
case DataType::INT8_ID: res = H5T_STD_I8BE; break;
case DataType::INT16_ID: res = H5T_STD_I16BE; break;
case DataType::INT32_ID: res = H5T_STD_I32BE; break;
case DataType::INT64_ID: res = H5T_STD_I64BE; break;
case DataType::UINT8_ID: res = H5T_STD_U8BE; break;
case DataType::UINT16_ID: res = H5T_STD_U16BE; break;
case DataType::UINT32_ID: res = H5T_STD_U32BE; break;
case DataType::UINT64_ID: res = H5T_STD_U64BE; break;
case DataType::FLOAT32_ID: res = H5T_IEEE_F32BE; break;
case DataType::FLOAT64_ID: res = H5T_IEEE_F64BE; break;
case DataType::CHAR8_STR_ID:
CONDUIT_HDF5_ERROR(ref_path,
"conduit::DataType to HDF5 Leaf DataType "
<< "Conversion:"
<< dt.to_json()
<< " needs to be handled with string logic");
break;
default:
CONDUIT_HDF5_ERROR(ref_path,
"conduit::DataType to HDF5 Leaf DataType "
<< "Conversion:"
<< dt.to_json()
<< " is not a leaf data type");
};
}
return res;
}
//-----------------------------------------------------------------------------
// cleanup conduit created hdf5 dtype
// (effectively a noop, except for the string case)
// TODO: This could be a macro ... ?
//-----------------------------------------------------------------------------
void
conduit_dtype_to_hdf5_dtype_cleanup(hid_t hdf5_dtype_id,
const std::string &ref_path)
{
// NOTE: This cleanup won't be triggered when we use thee
// based H5T_C_S1 with a data space that encodes # of elements
// (Our current path, given our logic to encode string size in the
// hdf5 type is disabled )
// if this is a string using a custom type we need to cleanup
// the conduit_dtype_to_hdf5_dtype result
if( (! H5Tequal(hdf5_dtype_id, H5T_C_S1) ) &&
(H5Tget_class(hdf5_dtype_id) == H5T_STRING ) )
{
CONDUIT_CHECK_HDF5_ERROR_WITH_REF_PATH(H5Tclose(hdf5_dtype_id),
ref_path,
"Failed to close HDF5 string Type "
<< hdf5_dtype_id);
}
}
//-----------------------------------------------------------------------------
DataType
hdf5_dtype_to_conduit_dtype(hid_t hdf5_dtype_id,
index_t num_elems,
const std::string& ref_path)
{
hsize_t num_elems_array[1] = { static_cast<hsize_t>(num_elems) };
return hdf5_dtype_to_conduit_dtype(hdf5_dtype_id, num_elems_array, 1, ref_path);
}
//-----------------------------------------------------------------------------
DataType
hdf5_dtype_to_conduit_dtype(hid_t hdf5_dtype_id,
hsize_t * num_elems_array,
index_t rank,
const std::string &ref_path)
{
// TODO: there may be a more straight forward way to do this using
// hdf5's data type introspection methods
index_t num_elems = 1;
for (int d = 0; d < rank; ++d)
{
num_elems = num_elems * (index_t)num_elems_array[d];
}
DataType res;
//-----------------------------------------------
// signed ints
//-----------------------------------------------
// little endian
if(H5Tequal(hdf5_dtype_id,H5T_STD_I8LE))
{
res = DataType::int8(num_elems);
res.set_endianness(Endianness::LITTLE_ID);
}
else if(H5Tequal(hdf5_dtype_id,H5T_STD_I16LE))
{
res = DataType::int16(num_elems);
res.set_endianness(Endianness::LITTLE_ID);
}
else if(H5Tequal(hdf5_dtype_id,H5T_STD_I32LE))
{
res = DataType::int32(num_elems);
res.set_endianness(Endianness::LITTLE_ID);
}
else if(H5Tequal(hdf5_dtype_id,H5T_STD_I64LE))
{
res = DataType::int64(num_elems);
res.set_endianness(Endianness::LITTLE_ID);
}
// big endian
else if(H5Tequal(hdf5_dtype_id,H5T_STD_I8BE))
{
res = DataType::int8(num_elems);
res.set_endianness(Endianness::BIG_ID);
}
else if(H5Tequal(hdf5_dtype_id,H5T_STD_I16BE))
{
res = DataType::int16(num_elems);
res.set_endianness(Endianness::BIG_ID);
}
else if(H5Tequal(hdf5_dtype_id,H5T_STD_I32BE))
{
res = DataType::int32(num_elems);
res.set_endianness(Endianness::BIG_ID);
}
else if(H5Tequal(hdf5_dtype_id,H5T_STD_I64BE))
{
res = DataType::int64(num_elems);
res.set_endianness(Endianness::BIG_ID);
}
//-----------------------------------------------
// unsigned ints
//-----------------------------------------------
// little endian
else if(H5Tequal(hdf5_dtype_id,H5T_STD_U8LE))
{
res = DataType::uint8(num_elems);
res.set_endianness(Endianness::LITTLE_ID);
}
else if(H5Tequal(hdf5_dtype_id,H5T_STD_U16LE))
{
res = DataType::uint16(num_elems);
res.set_endianness(Endianness::LITTLE_ID);
}
else if(H5Tequal(hdf5_dtype_id,H5T_STD_U32LE))
{
res = DataType::uint32(num_elems);
res.set_endianness(Endianness::LITTLE_ID);
}
else if(H5Tequal(hdf5_dtype_id,H5T_STD_U64LE))
{
res = DataType::uint64(num_elems);
res.set_endianness(Endianness::LITTLE_ID);
}
// big endian
else if(H5Tequal(hdf5_dtype_id,H5T_STD_U8BE))
{
res = DataType::uint8(num_elems);
res.set_endianness(Endianness::BIG_ID);
}
else if(H5Tequal(hdf5_dtype_id,H5T_STD_U16BE))
{
res = DataType::uint16(num_elems);
res.set_endianness(Endianness::BIG_ID);
}
else if(H5Tequal(hdf5_dtype_id,H5T_STD_U32BE))
{
res = DataType::uint32(num_elems);
res.set_endianness(Endianness::BIG_ID);
}
else if(H5Tequal(hdf5_dtype_id,H5T_STD_U64BE))
{
res = DataType::uint64(num_elems);
res.set_endianness(Endianness::BIG_ID);
}
//-----------------------------------------------
// floating point types
//-----------------------------------------------
// little endian
else if(H5Tequal(hdf5_dtype_id,H5T_IEEE_F32LE))
{
res = DataType::float32(num_elems);
res.set_endianness(Endianness::LITTLE_ID);
}
else if(H5Tequal(hdf5_dtype_id,H5T_IEEE_F64LE))
{
res = DataType::float64(num_elems);
res.set_endianness(Endianness::LITTLE_ID);
}
// big endian
else if(H5Tequal(hdf5_dtype_id,H5T_IEEE_F32BE))
{
res = DataType::float32(num_elems);
res.set_endianness(Endianness::BIG_ID);
}
else if(H5Tequal(hdf5_dtype_id,H5T_IEEE_F64BE))
{
res = DataType::float64(num_elems);
res.set_endianness(Endianness::BIG_ID);
}
//-----------------------------------------------
// String Types
//-----------------------------------------------
else if(H5Tequal(hdf5_dtype_id,H5T_C_S1))
{
// string as array case (old way of writing)
res = DataType::char8_str(num_elems);
}
// extended string reps
else if( H5Tget_class(hdf5_dtype_id) == H5T_STRING )
{
// for strings of this type, the length
// is encoded in the hdf5 type not the hdf5 data space
index_t hdf5_strlen = H5Tget_size(hdf5_dtype_id);
// check for variable type first
if( H5Tis_variable_str(hdf5_dtype_id) )
{
res = DataType::char8_str(-1);
}
else
{
res = DataType::char8_str(hdf5_strlen);
}
}
//-----------------------------------------------
// Unsupported
//-----------------------------------------------
else
{
CONDUIT_HDF5_ERROR(ref_path,
"Error with HDF5 DataType to conduit::DataType "
<< "Leaf Conversion");
}
return res;
}
//---------------------------------------------------------------------------//
// General Helper
//---------------------------------------------------------------------------//
//---------------------------------------------------------------------------//
void
hdf5_filename_from_hdf5_obj_id(hid_t hdf5_id,
std::string &hdf5_filename)
{
hdf5_filename.clear();
ssize_t hdf5_f_sz = H5Fget_name(hdf5_id, NULL, 0 );
if(hdf5_f_sz > 0)
{
std::vector<char>hdf5_f_buff(hdf5_f_sz+1, 0);
H5Fget_name(hdf5_id, &hdf5_f_buff[0], hdf5_f_sz+1);
hdf5_filename = std::string(&hdf5_f_buff[0]);
}
}
//---------------------------------------------------------------------------//
void
hdf5_ref_path_with_filename(hid_t hdf5_id,
const std::string &ref_path,
std::string &result)
{
hdf5_filename_from_hdf5_obj_id(hdf5_id, result);
if(result.empty())
{
result = ref_path;
}
else
{
if(!ref_path.empty())
{
result += ":" + ref_path;
}
}
}
//---------------------------------------------------------------------------//
// Write Helpers
//---------------------------------------------------------------------------//
//---------------------------------------------------------------------------//
bool
check_if_conduit_leaf_is_compatible_with_hdf5_obj(const DataType &dtype,
const std::string &ref_path,
hid_t hdf5_id,
const Node& opts,
std::string &incompat_details)
{
bool res = true;
H5O_info_t h5_obj_info;
#if H5_VERSION_GE(1, 12, 0) && !defined(H5_USE_18_API)
herr_t h5_status = H5Oget_info(hdf5_id, &h5_obj_info, H5O_INFO_ALL);
#else
herr_t h5_status = H5Oget_info(hdf5_id, &h5_obj_info);
#endif
// make sure it is a dataset ...
if( CONDUIT_HDF5_STATUS_OK(h5_status) &&
( h5_obj_info.type == H5O_TYPE_DATASET ) )
{
// get the hdf5 dataspace for the passed hdf5 obj
hid_t h5_test_dspace = H5Dget_space(hdf5_id);
if( H5Sget_simple_extent_type(h5_test_dspace) == H5S_NULL )
{
// a dataset with H5S_NULL data space is only compatible with
// conduit empty
if(!dtype.is_empty())
{
std::ostringstream oss;
oss << "Conduit Node (leaf) at path '" << ref_path << "'"
<< " is not compatible with given HDF5 Dataset at path"
<< " '" << ref_path << "'"
<< "\nHDF5 dataset has a H5S_NULL Dataspace which"
<< " only compatible with an empty Conduit Node";
incompat_details = oss.str();
res = false;
}
}
else
{
// get the hdf5 datatype that matchs the conduit dtype
hid_t h5_dtype = conduit_dtype_to_hdf5_dtype(dtype,
ref_path);
// get the hdf5 datatype for the passed hdf5 obj
hid_t h5_test_dtype = H5Dget_type(hdf5_id);
// we will check the 1d-properties of the hdf5 dataspace
hssize_t h5_test_num_ele = H5Sget_simple_extent_npoints(h5_test_dspace);