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