13#include <dolfinx/common/log.h>
21namespace dolfinx::io::hdf5
28 if constexpr (std::is_same_v<T, float>)
29 return H5T_NATIVE_FLOAT;
30 else if constexpr (std::is_same_v<T, double>)
31 return H5T_NATIVE_DOUBLE;
32 else if constexpr (std::is_same_v<T, std::int32_t>)
33 return H5T_NATIVE_INT32;
34 else if constexpr (std::is_same_v<T, std::uint32_t>)
35 return H5T_NATIVE_UINT32;
36 else if constexpr (std::is_same_v<T, std::int64_t>)
37 return H5T_NATIVE_INT64;
38 else if constexpr (std::is_same_v<T, std::uint64_t>)
39 return H5T_NATIVE_UINT64;
40 else if constexpr (std::is_same_v<T, std::uint8_t>)
41 return H5T_NATIVE_UINT8;
42 else if constexpr (std::is_same_v<T, std::size_t>)
44 throw std::runtime_error(
45 "Cannot determine size of std::size_t. std::size_t is not the same "
46 "size as long or int.");
50 throw std::runtime_error(
"Cannot get HDF5 primitive data type. No "
51 "specialised function for this data type.");
60hid_t open_file(MPI_Comm comm,
const std::filesystem::path& filename,
61 const std::string& mode,
bool use_mpi_io);
65void close_file(hid_t handle);
69void flush_file(hid_t handle);
74std::filesystem::path get_filename(hid_t handle);
80bool has_dataset(hid_t handle,
const std::string& dataset_path);
86hid_t open_dataset(hid_t handle,
const std::string& path);
92std::vector<std::int64_t> get_dataset_shape(hid_t handle,
93 const std::string& dataset_path);
101void set_mpi_atomicity(hid_t handle,
bool atomic);
107bool get_mpi_atomicity(hid_t handle);
112void add_group(hid_t handle,
const std::string& dataset_path);
125void write_dataset(hid_t file_handle,
const std::string& dataset_path,
126 const T* data, std::array<std::int64_t, 2> range,
127 const std::vector<int64_t>& global_size,
bool use_mpi_io,
131 const int rank = global_size.size();
135 throw std::runtime_error(
"Cannot write dataset to HDF5 file"
136 "Only rank 1 and rank 2 dataset are supported");
140 const hid_t h5type = hdf5::hdf5_type<T>();
143 std::vector<hsize_t> count(global_size.begin(), global_size.end());
144 count[0] = range[1] - range[0];
147 std::vector<hsize_t> offset(rank, 0);
148 offset[0] = range[0];
151 const std::vector<hsize_t> dimsf(global_size.begin(), global_size.end());
154 const hid_t filespace0 = H5Screate_simple(rank, dimsf.data(),
nullptr);
155 if (filespace0 == H5I_INVALID_HID)
156 throw std::runtime_error(
"Failed to create HDF5 data space");
159 hid_t chunking_properties;
163 hsize_t chunk_size = dimsf[0] / 2;
164 if (chunk_size > 1048576)
165 chunk_size = 1048576;
166 if (chunk_size < 1024)
169 hsize_t chunk_dims[2] = {chunk_size, dimsf[1]};
170 chunking_properties = H5Pcreate(H5P_DATASET_CREATE);
171 H5Pset_chunk(chunking_properties, rank, chunk_dims);
174 chunking_properties = H5P_DEFAULT;
177 const std::string group_name(dataset_path, 0, dataset_path.rfind(
'/'));
178 add_group(file_handle, group_name);
182 = H5Dcreate2(file_handle, dataset_path.c_str(), h5type, filespace0,
183 H5P_DEFAULT, chunking_properties, H5P_DEFAULT);
184 if (dset_id == H5I_INVALID_HID)
185 throw std::runtime_error(
"Failed to create HDF5 global dataset.");
188 if (herr_t status = H5Sclose(filespace0); status < 0)
189 throw std::runtime_error(
"Failed to close HDF5 global data space.");
192 const hid_t memspace = H5Screate_simple(rank, count.data(),
nullptr);
193 if (memspace == H5I_INVALID_HID)
194 throw std::runtime_error(
"Failed to create HDF5 local data space.");
197 const hid_t filespace1 = H5Dget_space(dset_id);
198 herr_t status = H5Sselect_hyperslab(filespace1, H5S_SELECT_SET, offset.data(),
199 nullptr, count.data(),
nullptr);
201 throw std::runtime_error(
"Failed to create HDF5 dataspace.");
204 const hid_t plist_id = H5Pcreate(H5P_DATASET_XFER);
207 if (herr_t status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
210 throw std::runtime_error(
211 "Failed to set HDF5 data transfer property list.");
216 if (H5Dwrite(dset_id, h5type, memspace, filespace1, plist_id, data) < 0)
218 throw std::runtime_error(
219 "Failed to write HDF5 local dataset into hyperslab.");
225 if (H5Pclose(chunking_properties) < 0)
226 throw std::runtime_error(
"Failed to close HDF5 chunking properties.");
230 if (H5Dclose(dset_id) < 0)
231 throw std::runtime_error(
"Failed to close HDF5 dataset.");
234 if (H5Sclose(filespace1) < 0)
235 throw std::runtime_error(
"Failed to close HDF5 hyperslab.");
238 if (H5Sclose(memspace) < 0)
239 throw std::runtime_error(
"Failed to close local HDF5 dataset.");
242 if (H5Pclose(plist_id) < 0)
243 throw std::runtime_error(
"Failed to release HDF5 file-access template.");
256std::vector<T> read_dataset(hid_t dset_id, std::array<std::int64_t, 2> range,
259 auto timer_start = std::chrono::system_clock::now();
265 hid_t dtype = H5Dget_type(dset_id);
266 if (dtype == H5I_INVALID_HID)
267 throw std::runtime_error(
"Failed to get HDF5 data type.");
268 if (htri_t eq = H5Tequal(dtype, hdf5::hdf5_type<T>()); eq < 0)
269 throw std::runtime_error(
"HDF5 datatype equality test failed.");
273 throw std::runtime_error(
"Wrong type for reading from HDF5. Use \"h5ls "
274 "-v\" to inspect the types in the HDF5 file.");
279 hid_t dataspace = H5Dget_space(dset_id);
280 if (dataspace == H5I_INVALID_HID)
281 throw std::runtime_error(
"Failed to open HDF5 data space.");
284 int rank = H5Sget_simple_extent_ndims(dataspace);
286 throw std::runtime_error(
"Failed to get rank of data space.");
288 spdlog::warn(
"io::hdf5::read_dataset untested for rank > 2.");
291 std::vector<hsize_t> shape(rank);
294 if (
int ndims = H5Sget_simple_extent_dims(dataspace, shape.data(),
nullptr);
297 throw std::runtime_error(
"Failed to get dimensionality of dataspace.");
301 std::vector<hsize_t> offset(rank, 0);
302 std::vector<hsize_t> count = shape;
303 if (range[0] != -1 and range[1] != -1)
305 offset[0] = range[0];
306 count[0] = range[1] - range[0];
314 = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset.data(),
nullptr,
315 count.data(),
nullptr);
318 throw std::runtime_error(
"Failed to select HDF5 hyperslab.");
322 hid_t memspace = H5Screate_simple(rank, count.data(),
nullptr);
323 if (memspace == H5I_INVALID_HID)
324 throw std::runtime_error(
"Failed to create HDF5 dataspace.");
328 std::reduce(count.begin(), count.end(), 1, std::multiplies{}));
331 hid_t h5type = hdf5::hdf5_type<T>();
333 = H5Dread(dset_id, h5type, memspace, dataspace, H5P_DEFAULT, data.data());
336 throw std::runtime_error(
"Failed to read HDF5 data.");
340 if (herr_t status = H5Sclose(dataspace); status < 0)
341 throw std::runtime_error(
"Failed to close HDF5 dataspace.");
344 if (herr_t status = H5Sclose(memspace); status < 0)
345 throw std::runtime_error(
"Failed to close HDF5 memory space.");
347 auto timer_end = std::chrono::system_clock::now();
348 std::chrono::duration<double> dt = (timer_end - timer_start);
349 double data_rate = data.size() *
sizeof(T) / (1e6 * dt.count());
350 spdlog::info(
"HDF5 Read data rate: {} MB/s", data_rate);
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition MPI.cpp:64