OGS
HdfData.cpp
Go to the documentation of this file.
1
10#include "HdfData.h"
11
12#include <hdf5.h>
13
14#include <map>
15
16#include "BaseLib/Error.h"
17#include "BaseLib/Logging.h"
18#include "partition.h"
19
20namespace MeshLib::IO
21{
22static hid_t meshPropertyType2HdfType(MeshPropertyDataType const ogs_data_type)
23{
24 std::map<MeshPropertyDataType const, hid_t> ogs_to_hdf_type = {
25 {MeshPropertyDataType::float64, H5T_NATIVE_DOUBLE},
26 {MeshPropertyDataType::float32, H5T_NATIVE_FLOAT},
27 {MeshPropertyDataType::int32, H5T_NATIVE_INT32},
28 {MeshPropertyDataType::int64, H5T_NATIVE_INT64},
29 {MeshPropertyDataType::uint32, H5T_NATIVE_UINT32},
30 {MeshPropertyDataType::uint64, H5T_NATIVE_UINT64},
31 {MeshPropertyDataType::int8, H5T_NATIVE_INT8},
32 {MeshPropertyDataType::uint8, H5T_NATIVE_UINT8},
33 {MeshPropertyDataType::char_native, H5T_NATIVE_CHAR},
34 {MeshPropertyDataType::uchar, H5T_NATIVE_UCHAR}};
35 try
36 {
37 return ogs_to_hdf_type.at(ogs_data_type);
38 }
39 catch (std::exception const& e)
40 {
41 OGS_FATAL("No known HDF5 type for OGS type. {:s}", e.what());
42 }
43}
44
45HdfData::HdfData(void const* data_start, std::size_t const size_partitioned_dim,
46 std::size_t const size_tuple, std::string const& name,
47 MeshPropertyDataType const mesh_property_data_type,
48 unsigned int const n_files,
49 unsigned int const chunk_size_bytes)
50 : data_start(data_start), name(name)
51{
52 data_type = meshPropertyType2HdfType(mesh_property_data_type);
53
54 auto const& partition_info =
55 getPartitionInfo(size_partitioned_dim, n_files);
56 auto const& offset_partitioned_dim = partition_info.local_offset;
57 offsets = {offset_partitioned_dim, 0};
58
59 std::size_t const unified_length = partition_info.local_length;
60 int const type_size = H5Tget_size(data_type);
61 std::size_t const space =
62 (chunk_size_bytes > 0)
63 ? std::min(std::size_t(std::lround(float(chunk_size_bytes) /
64 (size_tuple * type_size) +
65 0.5)),
66 partition_info.global_length)
67 : partition_info.longest_local_length;
68
69 if (chunk_size_bytes > 0 && space == partition_info.global_length)
70 {
71 INFO("HDF5: Using a single chunk for dataset {:s} .", name);
72 }
73
74 chunk_space = (size_tuple > 1) ? std::vector<Hdf5DimType>{space, size_tuple}
75 : std::vector<Hdf5DimType>{space};
76
77 data_space = (size_tuple > 1)
78 ? std::vector<Hdf5DimType>{unified_length, size_tuple}
79 : std::vector<Hdf5DimType>{unified_length};
81 (size_tuple > 1)
82 ? std::vector<Hdf5DimType>{partition_info.global_length, size_tuple}
83 : std::vector<Hdf5DimType>{partition_info.global_length};
84
85 DBUG(
86 "HDF: dataset name: {:s}, offset: {:d}, data_space: {:d}, chunk_space "
87 "{:d}, file_space: {:d}, tuples: {:d}",
88 name, partition_info.local_offset, data_space[0], chunk_space[0],
89 file_space[0], size_tuple);
90}
91} // namespace MeshLib::IO
#define OGS_FATAL(...)
Definition Error.h:26
Collects and holds all metadata for writing HDF5 file.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
MeshPropertyDataType
static hid_t meshPropertyType2HdfType(MeshPropertyDataType const ogs_data_type)
Definition HdfData.cpp:22
PartitionInfo getPartitionInfo(std::size_t const size, unsigned int const n_files)
Definition partition.cpp:33
Dispatches functions specific to execution platform (w/o MPI)
std::vector< Hdf5DimType > file_space
Definition HdfData.h:35
std::vector< Hdf5DimType > offsets
Definition HdfData.h:34
std::vector< Hdf5DimType > chunk_space
Definition HdfData.h:36
std::string name
Definition HdfData.h:37
HdfData(void const *data_start, std::size_t size_partitioned_dim, std::size_t size_tuple, std::string const &name, MeshPropertyDataType mesh_property_data_type, unsigned int n_files, unsigned int chunk_size_bytes)
Definition HdfData.cpp:45
std::vector< Hdf5DimType > data_space
Definition HdfData.h:33