OGS
HdfWriter.cpp File Reference

Detailed Description

Definition in file HdfWriter.cpp.

#include "HdfWriter.h"
#include <hdf5.h>
#include <string>
#include <utility>
#include <vector>
#include "BaseLib/Error.h"
#include "BaseLib/Logging.h"
#include "fileIO.h"
Include dependency graph for HdfWriter.cpp:

Go to the source code of this file.

Classes

struct  MeshLib::IO::HdfWriter::HdfMesh
 

Namespaces

namespace  MeshLib
 
namespace  MeshLib::IO
 

Functions

template<typename... Args>
void checkHdfStatus (const hid_t status, fmt::format_string< Args... > formatting, Args &&... args)
 
static bool checkCompression ()
 
static std::vector< Hdf5DimTypeprependDimension (Hdf5DimType const prepend_value, std::vector< Hdf5DimType > const &dimensions)
 
static hid_t createDataSet (hid_t const data_type, std::vector< Hdf5DimType > const &data_dims, std::vector< Hdf5DimType > const &max_dims, std::vector< Hdf5DimType > const &chunk_dims, bool const use_compression, hid_t const section, std::string const &dataset_name)
 
static void writeDataSet (void const *nodes_data, hid_t const data_type, std::vector< Hdf5DimType > const &data_dims, std::vector< Hdf5DimType > const &offset_dims, std::vector< Hdf5DimType > const &max_dims, std::vector< Hdf5DimType > const &chunk_dims, std::string const &dataset_name, Hdf5DimType const step, hid_t const dataset)
 Assumes a dataset is already opened by createDatasetFunction.
 
static void writeTimeSeries (hid_t const file, std::vector< double > const &step_times, bool const is_file_manager)
 Write vector with time values into open hdf file.
 

Variables

static unsigned short int const default_compression_factor = 1
 

Function Documentation

◆ checkCompression()

static bool checkCompression ( )
static

Definition at line 35 of file HdfWriter.cpp.

36{
37 // Check if gzip compression is available and can be used for both
38 // compression and decompression.
39 if (htri_t avail = H5Zfilter_avail(H5Z_FILTER_DEFLATE); !avail)
40 {
41 WARN("gzip filter not available.\n");
42 return false;
43 }
44 unsigned int filter_info;
45 H5Zget_filter_info(H5Z_FILTER_DEFLATE, &filter_info);
46 if (!(filter_info & H5Z_FILTER_CONFIG_ENCODE_ENABLED) ||
47 !(filter_info & H5Z_FILTER_CONFIG_DECODE_ENABLED))
48 {
49 WARN("gzip filter not available for encoding and decoding.\n");
50 return false;
51 }
52 return true;
53}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40

References WARN().

◆ checkHdfStatus()

template<typename... Args>
void checkHdfStatus ( const hid_t status,
fmt::format_string< Args... > formatting,
Args &&... args )

Definition at line 22 of file HdfWriter.cpp.

24{
25 if (status < 0)
26 {
27 OGS_FATAL(formatting, std::forward<Args>(args)...);
28 }
29}
#define OGS_FATAL(...)
Definition Error.h:26

References OGS_FATAL.

Referenced by MeshLib::IO::HdfWriter::HdfWriter().

◆ createDataSet()

static hid_t createDataSet ( hid_t const data_type,
std::vector< Hdf5DimType > const & data_dims,
std::vector< Hdf5DimType > const & max_dims,
std::vector< Hdf5DimType > const & chunk_dims,
bool const use_compression,
hid_t const section,
std::string const & dataset_name )
static

Definition at line 63 of file HdfWriter.cpp.

69{
70 int const time_dim_local_size = data_dims.size() + 1;
71
72 std::vector<Hdf5DimType> const time_max_dims =
73 prependDimension(H5S_UNLIMITED, max_dims);
74 std::vector<Hdf5DimType> const time_data_global_dims =
75 prependDimension(1, max_dims);
76
77 std::vector<Hdf5DimType> const time_data_chunk_dims =
78 prependDimension(1, chunk_dims);
79
80 hid_t const fspace =
81 H5Screate_simple(time_dim_local_size, time_data_global_dims.data(),
82 time_max_dims.data());
83 assert(fspace >= 0);
84
85 hid_t const dcpl = H5Pcreate(H5P_DATASET_CREATE);
86 assert(dcpl >= 0);
87
88 hid_t const status =
89 H5Pset_chunk(dcpl, chunk_dims.size() + 1, time_data_chunk_dims.data());
90 if (status < 0)
91 {
92 OGS_FATAL("H5Pset_layout failed for data set: {:s}.", dataset_name);
93 }
94
95 if (use_compression)
96 {
97 H5Pset_deflate(dcpl, default_compression_factor);
98 }
99
100 hid_t const dataset = H5Dcreate2(section, dataset_name.c_str(), data_type,
101 fspace, H5P_DEFAULT, dcpl, H5P_DEFAULT);
102
103 assert(dataset >= 0);
104 H5Pclose(dcpl);
105 assert(H5Sclose(fspace) >= 0);
106
107 return dataset;
108}
static unsigned short int const default_compression_factor
Definition HdfWriter.cpp:31
static std::vector< Hdf5DimType > prependDimension(Hdf5DimType const prepend_value, std::vector< Hdf5DimType > const &dimensions)
Definition HdfWriter.cpp:55

References default_compression_factor, OGS_FATAL, and prependDimension().

Referenced by MeshLib::IO::HdfWriter::HdfWriter().

◆ prependDimension()

static std::vector< Hdf5DimType > prependDimension ( Hdf5DimType const prepend_value,
std::vector< Hdf5DimType > const & dimensions )
static

Definition at line 55 of file HdfWriter.cpp.

57{
58 std::vector<Hdf5DimType> dims = {prepend_value};
59 dims.insert(dims.end(), dimensions.begin(), dimensions.end());
60 return dims;
61}

Referenced by createDataSet(), and writeDataSet().

◆ writeDataSet()

static void writeDataSet ( void const * nodes_data,
hid_t const data_type,
std::vector< Hdf5DimType > const & data_dims,
std::vector< Hdf5DimType > const & offset_dims,
std::vector< Hdf5DimType > const & max_dims,
std::vector< Hdf5DimType > const & chunk_dims,
std::string const & dataset_name,
Hdf5DimType const step,
hid_t const dataset )
static

Assumes a dataset is already opened by createDatasetFunction.

Defines what (nodes_data, data_type) will be written how (data subsections: data_dims, offset_dims, max_dims, chunk_dims, time) where (dataset and dataset_name)

Definition at line 115 of file HdfWriter.cpp.

124{
125 Hdf5DimType const time_steps = step + 1;
126
127 std::vector<Hdf5DimType> const time_data_local_dims = data_dims;
128 std::vector<Hdf5DimType> const time_max_dims =
129 prependDimension(time_steps, max_dims);
130 std::vector<Hdf5DimType> const time_offsets =
131 prependDimension(step, offset_dims);
132 std::vector<hsize_t> const count =
133 prependDimension(1, time_data_local_dims);
134
135 hid_t const io_transfer_property = createHDF5TransferPolicy();
136
137 hid_t const mspace = H5Screate_simple(time_data_local_dims.size(),
138 time_data_local_dims.data(), NULL);
139 assert(H5Sselect_all(mspace) >= 0);
140
141 hid_t status = H5Dset_extent(dataset, time_max_dims.data());
142 if (status < 0)
143 {
144 OGS_FATAL("H5D set extent failed dataset '{:s}'.", dataset_name);
145 }
146 hid_t const fspace = H5Dget_space(dataset);
147
148 H5Sselect_hyperslab(fspace, H5S_SELECT_SET, time_offsets.data(), NULL,
149 count.data(), NULL);
150
151 status = H5Dwrite(dataset, data_type, mspace, fspace, io_transfer_property,
152 nodes_data);
153 if (status < 0)
154 {
155 OGS_FATAL("H5Dwrite failed in dataset '{:s}'.", dataset_name);
156 }
157
158 H5Sclose(mspace);
159 H5Pclose(io_transfer_property);
160
161 return;
162}
hsize_t Hdf5DimType
Definition HdfData.h:24
int64_t createHDF5TransferPolicy()
Definition fileIO.cpp:70

References MeshLib::IO::createHDF5TransferPolicy(), OGS_FATAL, and prependDimension().

Referenced by MeshLib::IO::HdfWriter::HdfWriter(), and MeshLib::IO::HdfWriter::writeStep().

◆ writeTimeSeries()

static void writeTimeSeries ( hid_t const file,
std::vector< double > const & step_times,
bool const is_file_manager )
static

Write vector with time values into open hdf file.

In contrast to all other hdf write methods writing is only performed by one process (is_file_manager_true). file handle is to an already opened file

Definition at line 170 of file HdfWriter.cpp.

173{
174 hsize_t const size = step_times.size();
175 hid_t const memspace = H5Screate_simple(1, &size, NULL);
176 hid_t const filespace = H5Screate_simple(1, &size, NULL);
177
178 if (is_file_manager)
179 {
180 H5Sselect_all(memspace);
181 H5Sselect_all(filespace);
182 }
183 else
184 {
185 H5Sselect_none(memspace);
186 H5Sselect_none(filespace);
187 }
188
189 hid_t const dataset =
190 H5Dcreate2(file, "/times", H5T_NATIVE_DOUBLE, filespace, H5P_DEFAULT,
191 H5P_DEFAULT, H5P_DEFAULT);
192
193 H5Dwrite(dataset, H5T_NATIVE_DOUBLE, memspace, filespace, H5P_DEFAULT,
194 step_times.data());
195
196 H5Dclose(dataset);
197 H5Sclose(memspace);
198 H5Sclose(filespace);
199};
constexpr int size(int const displacement_dim)
Vectorized tensor size for given displacement dimension.

Referenced by MeshLib::IO::HdfWriter::~HdfWriter().

Variable Documentation

◆ default_compression_factor

unsigned short int const default_compression_factor = 1
static

Definition at line 31 of file HdfWriter.cpp.

Referenced by createDataSet().