16 #include <deal.II/base/config.h> 18 #ifdef DEAL_II_WITH_HDF5 20 # include <deal.II/base/array_view.h> 21 # include <deal.II/base/hdf5.h> 23 # include <deal.II/lac/full_matrix.h> 24 # include <deal.II/lac/vector.h> 32 DEAL_II_NAMESPACE_OPEN
47 template <
typename number>
48 std::shared_ptr<hid_t>
51 std::shared_ptr<hid_t> t_type;
52 if (std::is_same<number, float>::value)
54 return std::make_shared<hid_t>(H5T_NATIVE_FLOAT);
56 else if (std::is_same<number, double>::value)
58 return std::make_shared<hid_t>(H5T_NATIVE_DOUBLE);
60 else if (std::is_same<number, int>::value)
62 return std::make_shared<hid_t>(H5T_NATIVE_INT);
64 else if (std::is_same<number, unsigned int>::value)
66 return std::make_shared<hid_t>(H5T_NATIVE_UINT);
68 else if (std::is_same<number, std::complex<float>>::value)
70 t_type = std::shared_ptr<hid_t>(
new hid_t, [](hid_t *pointer) {
72 const herr_t ret = H5Tclose(*pointer);
77 *t_type = H5Tcreate(H5T_COMPOUND,
sizeof(std::complex<float>));
82 herr_t ret = H5Tinsert(*t_type,
"r", 0, H5T_NATIVE_FLOAT);
84 ret = H5Tinsert(*t_type,
"i",
sizeof(
float), H5T_NATIVE_FLOAT);
89 else if (std::is_same<number, std::complex<double>>::value)
91 t_type = std::shared_ptr<hid_t>(
new hid_t, [](hid_t *pointer) {
93 const herr_t ret = H5Tclose(*pointer);
98 *t_type = H5Tcreate(H5T_COMPOUND,
sizeof(std::complex<double>));
103 herr_t ret = H5Tinsert(*t_type,
"r", 0, H5T_NATIVE_DOUBLE);
105 ret = H5Tinsert(*t_type,
"i",
sizeof(
double), H5T_NATIVE_DOUBLE);
127 template <
typename number>
129 get_container_dimensions(
const std::vector<number> &data)
131 std::vector<hsize_t> dimensions = {data.size()};
137 template <
typename number>
141 std::vector<hsize_t> dimensions = {data.
size()};
147 template <
typename number>
151 std::vector<hsize_t> dimensions = {data.
m(), data.
n()};
161 template <
typename number>
163 get_container_size(
const std::vector<number> &data)
165 return static_cast<unsigned int>(data.size());
170 template <
typename number>
174 return static_cast<unsigned int>(data.
size());
179 template <
typename number>
183 return static_cast<unsigned int>(data.
m() * data.
n());
204 template <
typename Container>
205 typename std::enable_if<
206 std::is_same<Container,
207 std::vector<typename Container::value_type>>::value,
209 initialize_container(
const std::vector<hsize_t> &dimensions)
211 return Container(std::accumulate(
212 dimensions.begin(), dimensions.end(), 1, std::multiplies<int>()));
217 template <
typename Container>
218 typename std::enable_if<
219 std::is_same<Container, Vector<typename Container::value_type>>::value,
221 initialize_container(
const std::vector<hsize_t> &dimensions)
223 return Container(std::accumulate(
224 dimensions.begin(), dimensions.end(), 1, std::multiplies<int>()));
229 template <
typename Container>
230 typename std::enable_if<
231 std::is_same<Container,
234 initialize_container(
const std::vector<hsize_t> &dimensions)
240 std::vector<hsize_t> squeezed_dimensions;
242 if (dimensions.size() > 2)
244 for (
const auto &dimension : dimensions)
247 squeezed_dimensions.push_back(dimension);
252 squeezed_dimensions = dimensions;
256 return Container(squeezed_dimensions[0], squeezed_dimensions[1]);
265 set_plist(hid_t &plist,
const bool mpi)
269 # ifdef DEAL_II_WITH_MPI 270 plist = H5Pcreate(H5P_DATASET_XFER);
272 const herr_t ret = H5Pset_dxpl_mpio(plist, H5FD_MPIO_COLLECTIVE);
296 release_plist(hid_t & plist,
297 H5D_mpio_actual_io_mode_t &io_mode,
298 uint32_t & local_no_collective_cause,
299 uint32_t & global_no_collective_cause,
301 const bool query_io_mode)
305 # ifdef DEAL_II_WITH_MPI 310 ret = H5Pget_mpio_actual_io_mode(plist, &io_mode);
313 H5Pget_mpio_no_collective_cause(plist,
314 &local_no_collective_cause,
315 &global_no_collective_cause);
318 ret = H5Pclose(plist);
327 (void)local_no_collective_cause;
328 (void)global_no_collective_cause;
336 no_collective_cause_to_string(
const uint32_t no_collective_cause)
340 auto append_to_message = [&message](
const char *p) {
341 if (message.length() > 0)
356 if (no_collective_cause == 0x00)
358 append_to_message(
"H5D_MPIO_COLLECTIVE");
361 if ((no_collective_cause & 0x01) == 0x01)
363 append_to_message(
"H5D_MPIO_SET_INDEPENDENT");
366 if ((no_collective_cause & 0x02) == 0x02)
368 append_to_message(
"H5D_MPIO_DATATYPE_CONVERSION");
371 if ((no_collective_cause & 0x04) == 0x04)
373 append_to_message(
"H5D_MPIO_DATA_TRANSFORMS");
376 if ((no_collective_cause & 0x10) == 0x10)
378 append_to_message(
"H5D_MPIO_NOT_SIMPLE_OR_SCALAR_DATASPACES");
381 if ((no_collective_cause & 0x20) == 0x20)
383 append_to_message(
"H5D_MPIO_NOT_CONTIGUOUS_OR_CHUNKED_DATASET");
386 if ((no_collective_cause & 0x40) == 0x40)
388 append_to_message(
"H5D_MPIO_FILTERS");
403 template <
typename T>
407 const std::shared_ptr<hid_t> t_type = internal::get_hdf5_datatype<T>();
415 ret = H5Aread(attr, *t_type, &value);
418 ret = H5Aclose(attr);
437 ret = H5Aread(attr, H5T_NATIVE_INT, &int_value);
440 ret = H5Aclose(attr);
444 return (int_value != 0);
471 type = H5Tcopy(H5T_C_S1);
475 ret = H5Tset_cset(type, H5T_CSET_UTF8);
478 ret = H5Tset_size(type, H5T_VARIABLE);
484 ret = H5Aread(attr, type, &string_out);
487 std::string string_value(string_out);
491 ret = H5Tclose(type);
494 ret = H5Aclose(attr);
503 template <
typename T>
511 const std::shared_ptr<hid_t> t_type = internal::get_hdf5_datatype<T>();
517 aid = H5Screate(H5S_SCALAR);
530 ret = H5Awrite(attr, *t_type, &value);
535 ret = H5Aclose(attr);
546 const std::string value)
559 t_type = H5Tcopy(H5T_C_S1);
563 ret = H5Tset_cset(t_type, H5T_CSET_UTF8);
566 ret = H5Tset_size(t_type, H5T_VARIABLE);
572 aid = H5Screate(H5S_SCALAR);
575 *
hdf5_reference, attr_name.data(), t_type, aid, H5P_DEFAULT, H5P_DEFAULT);
584 const char *c_string_value = value.c_str();
585 ret = H5Awrite(attr, t_type, &c_string_value);
590 ret = H5Aclose(attr);
607 const hid_t & parent_group_id,
610 , query_io_mode(false)
611 , io_mode(H5D_MPIO_NO_COLLECTIVE)
612 , local_no_collective_cause(H5D_MPIO_SET_INDEPENDENT)
613 , global_no_collective_cause(H5D_MPIO_SET_INDEPENDENT)
615 hdf5_reference = std::shared_ptr<hid_t>(
new hid_t, [](hid_t *pointer) {
617 const herr_t ret = H5Dclose(*pointer);
622 dataspace = std::shared_ptr<hid_t>(
new hid_t, [](hid_t *pointer) {
624 const herr_t ret = H5Sclose(*pointer);
640 rank_ret = H5Sget_simple_extent_ndims(*
dataspace);
658 const hid_t & parent_group_id,
659 const std::vector<hsize_t> & dimensions,
660 const std::shared_ptr<hid_t> &t_type,
663 , rank(dimensions.size())
664 , dimensions(dimensions)
665 , query_io_mode(false)
666 , io_mode(H5D_MPIO_NO_COLLECTIVE)
667 , local_no_collective_cause(H5D_MPIO_SET_INDEPENDENT)
668 , global_no_collective_cause(H5D_MPIO_SET_INDEPENDENT)
670 hdf5_reference = std::shared_ptr<hid_t>(
new hid_t, [](hid_t *pointer) {
672 const herr_t ret = H5Dclose(*pointer);
677 dataspace = std::shared_ptr<hid_t>(
new hid_t, [](hid_t *pointer) {
679 const herr_t ret = H5Sclose(*pointer);
706 template <
typename Container>
710 const std::shared_ptr<hid_t> t_type =
711 internal::get_hdf5_datatype<typename Container::value_type>();
715 Container data = internal::initialize_container<Container>(
dimensions);
717 internal::set_plist(plist,
mpi);
724 make_array_view(data).data());
728 internal::release_plist(plist,
741 template <
typename Container>
747 "The dimension of coordinates has to be divisible by the rank"));
748 const std::shared_ptr<hid_t> t_type =
749 internal::get_hdf5_datatype<typename Container::value_type>();
751 hid_t memory_dataspace;
754 std::vector<hsize_t> data_dimensions{
755 static_cast<hsize_t
>(coordinates.size() /
rank)};
757 Container data = internal::initialize_container<Container>(data_dimensions);
759 memory_dataspace = H5Screate_simple(1, data_dimensions.data(),
nullptr);
767 internal::set_plist(plist,
mpi);
774 make_array_view(data).data());
777 internal::release_plist(plist,
784 ret = H5Sclose(memory_dataspace);
793 template <
typename Container>
796 const std::vector<hsize_t> &count)
798 const std::shared_ptr<hid_t> t_type =
799 internal::get_hdf5_datatype<typename Container::value_type>();
801 hid_t memory_dataspace;
806 std::vector<hsize_t> data_dimensions = count;
808 Container data = internal::initialize_container<Container>(data_dimensions);
811 H5Screate_simple(data_dimensions.size(), data_dimensions.data(),
nullptr);
821 internal::set_plist(plist,
mpi);
828 make_array_view(data).data());
831 internal::release_plist(plist,
838 ret = H5Sclose(memory_dataspace);
847 template <
typename Container>
850 const std::vector<hsize_t> &offset,
851 const std::vector<hsize_t> &stride,
852 const std::vector<hsize_t> &count,
853 const std::vector<hsize_t> &block)
855 const std::shared_ptr<hid_t> t_type =
856 internal::get_hdf5_datatype<typename Container::value_type>();
858 hid_t memory_dataspace;
861 Container data = internal::initialize_container<Container>(data_dimensions);
864 H5Screate_simple(data_dimensions.size(), data_dimensions.data(),
nullptr);
874 internal::set_plist(plist,
mpi);
881 make_array_view(data).data());
884 internal::release_plist(plist,
891 ret = H5Sclose(memory_dataspace);
900 template <
typename number>
904 const std::shared_ptr<hid_t> t_type = internal::get_hdf5_datatype<number>();
905 const std::vector<hsize_t> data_dimensions = {0};
907 hid_t memory_dataspace;
911 memory_dataspace = H5Screate_simple(1, data_dimensions.data(),
nullptr);
916 internal::set_plist(plist,
mpi);
925 internal::release_plist(plist,
932 ret = H5Sclose(memory_dataspace);
940 template <
typename Container>
945 const std::shared_ptr<hid_t> t_type =
946 internal::get_hdf5_datatype<typename Container::value_type>();
950 internal::set_plist(plist,
mpi);
957 make_array_view(data).data());
960 internal::release_plist(plist,
972 template <
typename Container>
975 const std::vector<hsize_t> &coordinates)
978 const std::shared_ptr<hid_t> t_type =
979 internal::get_hdf5_datatype<typename Container::value_type>();
980 const std::vector<hsize_t> data_dimensions =
981 internal::get_container_dimensions(data);
983 hid_t memory_dataspace;
988 memory_dataspace = H5Screate_simple(1, data_dimensions.data(),
nullptr);
996 internal::set_plist(plist,
mpi);
1003 make_array_view(data).data());
1006 internal::release_plist(plist,
1013 ret = H5Sclose(memory_dataspace);
1021 template <
typename Container>
1024 const std::vector<hsize_t> &offset,
1025 const std::vector<hsize_t> &count)
1030 std::multiplies<unsigned int>()),
1031 internal::get_container_size(data));
1032 const std::shared_ptr<hid_t> t_type =
1033 internal::get_hdf5_datatype<typename Container::value_type>();
1036 const std::vector<hsize_t> &data_dimensions = count;
1038 hid_t memory_dataspace;
1043 H5Screate_simple(data_dimensions.size(), data_dimensions.data(),
nullptr);
1053 internal::set_plist(plist,
mpi);
1060 make_array_view(data).data());
1063 internal::release_plist(plist,
1070 ret = H5Sclose(memory_dataspace);
1078 template <
typename Container>
1081 const std::vector<hsize_t> &data_dimensions,
1082 const std::vector<hsize_t> &offset,
1083 const std::vector<hsize_t> &stride,
1084 const std::vector<hsize_t> &count,
1085 const std::vector<hsize_t> &block)
1087 const std::shared_ptr<hid_t> t_type =
1088 internal::get_hdf5_datatype<typename Container::value_type>();
1090 hid_t memory_dataspace;
1095 H5Screate_simple(data_dimensions.size(), data_dimensions.data(),
nullptr);
1105 internal::set_plist(plist,
mpi);
1112 make_array_view(data).data());
1115 internal::release_plist(plist,
1122 ret = H5Sclose(memory_dataspace);
1130 template <
typename number>
1134 std::shared_ptr<hid_t> t_type = internal::get_hdf5_datatype<number>();
1135 std::vector<hsize_t> data_dimensions = {0};
1137 hid_t memory_dataspace;
1141 memory_dataspace = H5Screate_simple(1, data_dimensions.data(),
nullptr);
1146 internal::set_plist(plist,
mpi);
1155 internal::release_plist(plist,
1162 ret = H5Sclose(memory_dataspace);
1178 std::vector<hsize_t>
1190 ExcMessage(
"query_io_mode should be true in order to use io_mode"));
1193 case (H5D_MPIO_NO_COLLECTIVE):
1194 return std::string(
"H5D_MPIO_NO_COLLECTIVE");
1196 case (H5D_MPIO_CHUNK_INDEPENDENT):
1197 return std::string(
"H5D_MPIO_CHUNK_INDEPENDENT");
1199 case (H5D_MPIO_CHUNK_COLLECTIVE):
1200 return std::string(
"H5D_MPIO_CHUNK_COLLECTIVE");
1202 case (H5D_MPIO_CHUNK_MIXED):
1203 return std::string(
"H5D_MPIO_CHUNK_MIXED");
1205 case (H5D_MPIO_CONTIGUOUS_COLLECTIVE):
1206 return std::string(
"H5D_MPIO_CONTIGUOUS_COLLECTIVE");
1210 return std::string(
"Internal error");
1215 return std::string(
"Internal error");
1220 H5D_mpio_actual_io_mode_t
1225 "query_io_mode should be true in order to use get_io_mode"));
1237 "query_io_mode should be true in order to use get_local_no_collective_cause()"));
1249 "query_io_mode should be true in order to use get_local_no_collective_cause()"));
1261 "query_io_mode should be true in order to use get_global_no_collective_cause()"));
1273 "query_io_mode should be true in order to use get_global_no_collective_cause()"));
1303 const Group & parentGroup,
1308 hdf5_reference = std::shared_ptr<hid_t>(
new hid_t, [](hid_t *pointer) {
1310 const herr_t ret = H5Gclose(*pointer);
1368 template <
typename number>
1371 const std::vector<hsize_t> &dimensions)
const 1373 std::shared_ptr<hid_t> t_type = internal::get_hdf5_datatype<number>();
1379 template <
typename Container>
1383 std::vector<hsize_t> dimensions = internal::get_container_dimensions(data);
1385 create_dataset<typename Container::value_type>(
name, dimensions);
1386 dataset.write(data);
1395 # ifdef DEAL_II_WITH_MPI
1407 const MPI_Comm mpi_communicator)
1408 :
File(name, mode, true, mpi_communicator)
1416 const MPI_Comm mpi_communicator)
1419 hdf5_reference = std::shared_ptr<hid_t>(
new hid_t, [](hid_t *pointer) {
1421 const herr_t err = H5Fclose(*pointer);
1432 # ifdef DEAL_II_WITH_MPI 1433 # ifdef H5_HAVE_PARALLEL 1434 const MPI_Info info = MPI_INFO_NULL;
1436 plist = H5Pcreate(H5P_FILE_ACCESS);
1438 ret = H5Pset_fapl_mpio(plist, mpi_communicator, info);
1442 # endif // H5_HAVE_PARALLEL 1445 # endif // DEAL_II_WITH_MPI 1449 plist = H5P_DEFAULT;
1460 H5Fcreate(
name.data(), H5F_ACC_TRUNC, H5P_DEFAULT, plist);
1471 ret = H5Pclose(plist);
1476 (void)mpi_communicator;
1485 # include "hdf5.inst" 1493 HDF5Object::get_attribute<int>(
const std::string &attr_name)
const;
1494 template unsigned int 1495 HDF5Object::get_attribute<unsigned int>(
const std::string &attr_name)
const;
1500 HDF5Object::set_attribute<int>(
const std::string &attr_name,
int value);
1502 HDF5Object::set_attribute<unsigned int>(
const std::string &attr_name,
1503 unsigned int value);
1505 template std::vector<int>
1506 DataSet::read<std::vector<int>>();
1507 template std::vector<unsigned int>
1508 DataSet::read<std::vector<unsigned int>>();
1510 template std::vector<int>
1511 DataSet::read_selection<std::vector<int>>(
1512 const std::vector<hsize_t> &coordinates);
1513 template std::vector<unsigned int>
1514 DataSet::read_selection<std::vector<unsigned int>>(
1515 const std::vector<hsize_t> &coordinates);
1517 template std::vector<int>
1518 DataSet::read_hyperslab<std::vector<int>>(
const std::vector<hsize_t> &offset,
1519 const std::vector<hsize_t> &count);
1520 template std::vector<unsigned int>
1521 DataSet::read_hyperslab<std::vector<unsigned int>>(
1522 const std::vector<hsize_t> &offset,
1523 const std::vector<hsize_t> &count);
1525 template std::vector<int>
1526 DataSet::read_hyperslab<std::vector<int>>(
1527 const std::vector<hsize_t> &data_dimensions,
1528 const std::vector<hsize_t> &offset,
1529 const std::vector<hsize_t> &stride,
1530 const std::vector<hsize_t> &count,
1531 const std::vector<hsize_t> &block);
1532 template std::vector<unsigned int>
1533 DataSet::read_hyperslab<std::vector<unsigned int>>(
1534 const std::vector<hsize_t> &data_dimensions,
1535 const std::vector<hsize_t> &offset,
1536 const std::vector<hsize_t> &stride,
1537 const std::vector<hsize_t> &count,
1538 const std::vector<hsize_t> &block);
1541 DataSet::read_none<int>();
1543 DataSet::read_none<unsigned int>();
1546 DataSet::write<std::vector<int>>(
const std::vector<int> &data);
1548 DataSet::write<std::vector<unsigned int>>(
1549 const std::vector<unsigned int> &data);
1552 DataSet::write_selection<std::vector<int>>(
1553 const std::vector<int> & data,
1554 const std::vector<hsize_t> &coordinates);
1556 DataSet::write_selection<std::vector<unsigned int>>(
1557 const std::vector<unsigned int> &data,
1558 const std::vector<hsize_t> & coordinates);
1561 DataSet::write_hyperslab<std::vector<int>>(
const std::vector<int> & data,
1562 const std::vector<hsize_t> &offset,
1563 const std::vector<hsize_t> &count);
1565 DataSet::write_hyperslab<std::vector<unsigned int>>(
1566 const std::vector<unsigned int> &data,
1567 const std::vector<hsize_t> & offset,
1568 const std::vector<hsize_t> & count);
1571 DataSet::write_hyperslab<std::vector<int>>(
1572 const std::vector<int> & data,
1573 const std::vector<hsize_t> &data_dimensions,
1574 const std::vector<hsize_t> &offset,
1575 const std::vector<hsize_t> &stride,
1576 const std::vector<hsize_t> &count,
1577 const std::vector<hsize_t> &block);
1579 DataSet::write_hyperslab<std::vector<unsigned int>>(
1580 const std::vector<unsigned int> &data,
1581 const std::vector<hsize_t> & data_dimensions,
1582 const std::vector<hsize_t> & offset,
1583 const std::vector<hsize_t> & stride,
1584 const std::vector<hsize_t> & count,
1585 const std::vector<hsize_t> & block);
1588 DataSet::write_none<int>();
1590 DataSet::write_none<unsigned int>();
1593 Group::create_dataset<int>(
const std::string & name,
1594 const std::vector<hsize_t> &dimensions)
const;
1596 Group::create_dataset<unsigned int>(
1597 const std::string & name,
1598 const std::vector<hsize_t> &dimensions)
const;
1601 Group::write_dataset<std::vector<int>>(
const std::string & name,
1602 const std::vector<int> &data)
const;
1604 Group::write_dataset<std::vector<unsigned int>>(
1605 const std::string & name,
1606 const std::vector<unsigned int> &data)
const;
1612 DEAL_II_NAMESPACE_CLOSE
1614 #endif // DEAL_II_WITH_HDF5
#define AssertNothrow(cond, exc)
DataSet open_dataset(const std::string &name) const
#define AssertDimension(dim1, dim2)
Container read_selection(const std::vector< hsize_t > &coordinates)
std::string get_name() const
H5D_mpio_actual_io_mode_t get_io_mode_as_hdf5_type()
Group(const std::string &name, const Group &parent_group, const bool mpi, const GroupAccessMode mode)
T get_attribute(const std::string &attr_name) const
HDF5Object(const std::string &name, const bool mpi)
std::shared_ptr< hid_t > hdf5_reference
#define AssertThrow(cond, exc)
DataSet(const std::string &name, const hid_t &parent_group_id, bool mpi)
void write_selection(const Container &data, const std::vector< hsize_t > &coordinates)
void write(const Container &data)
static ::ExceptionBase & ExcMessage(std::string arg1)
Group create_group(const std::string &name) const
#define Assert(cond, exc)
void set_query_io_mode(const bool new_query_io_mode)
void set_attribute(const std::string &attr_name, const T value)
unsigned int get_rank() const
DataSet create_dataset(const std::string &name, const std::vector< hsize_t > &dimensions) const
unsigned int get_size() const
void write_hyperslab(const Container &data, const std::vector< hsize_t > &offset, const std::vector< hsize_t > &count)
std::vector< hsize_t > dimensions
uint32_t get_global_no_collective_cause_as_hdf5_type()
std::shared_ptr< hid_t > dataspace
void write_dataset(const std::string &name, const Container &data) const
std::string get_local_no_collective_cause()
uint32_t local_no_collective_cause
std::vector< hsize_t > get_dimensions() const
std::string get_global_no_collective_cause()
uint32_t global_no_collective_cause
std::string get_io_mode()
static ::ExceptionBase & ExcNotImplemented()
H5D_mpio_actual_io_mode_t io_mode
File(const std::string &name, const FileAccessMode mode)
uint32_t get_local_no_collective_cause_as_hdf5_type()
Container read_hyperslab(const std::vector< hsize_t > &offset, const std::vector< hsize_t > &count)
bool get_query_io_mode() const
static ::ExceptionBase & ExcInternalError()
Group open_group(const std::string &name) const