9#ifndef GRIDFORMAT_VTK_HDF_COMMON_HPP_
10#define GRIDFORMAT_VTK_HDF_COMMON_HPP_
17#include <gridformat/common/hdf5.hpp>
18#include <gridformat/common/lazy_field.hpp>
19#include <gridformat/common/string_conversion.hpp>
40 const bool is_parallel;
41 const std::vector<std::size_t> rank_cells;
42 const std::vector<std::size_t> rank_points;
43 const std::size_t num_cells_total;
44 const std::size_t num_points_total;
45 const std::size_t my_cell_offset;
46 const std::size_t my_point_offset;
50 std::vector<std::size_t> _rank_cells,
51 std::vector<std::size_t> _rank_points)
53 , num_ranks{_num_ranks}
54 , is_parallel{_num_ranks > 1}
55 , rank_cells{std::move(_rank_cells)}
56 , rank_points{std::move(_rank_points)}
57 , num_cells_total{_accumulate(rank_cells)}
58 , num_points_total{_accumulate(rank_points)}
59 , my_cell_offset{_accumulate_rank_offset(rank_cells)}
60 , my_point_offset{_accumulate_rank_offset(rank_points)} {
61 if (my_rank >= num_ranks)
62 throw ValueError(as_error(
"Given rank is not within communicator size"));
63 if (num_ranks !=
static_cast<int>(rank_cells.size()))
64 throw ValueError(as_error(
"Cells vector does not match communicator size"));
65 if (num_ranks !=
static_cast<int>(rank_points.size()))
66 throw ValueError(as_error(
"Points vector does not match communicator size"));
69 template<Concepts::Gr
id Gr
id, Concepts::Communicator Communicator>
70 static IOContext from(
const Grid& grid,
const Communicator& comm,
int root_rank = 0) {
71 const int size = Parallel::size(comm);
72 const int rank = Parallel::rank(comm);
73 const std::size_t num_points = number_of_points(grid);
74 const std::size_t num_cells = number_of_cells(grid);
76 return IOContext{rank, size, std::vector{num_cells}, std::vector{num_points}};
78 const auto all_num_points = Parallel::gather(comm, num_points, root_rank);
79 const auto all_num_cells = Parallel::gather(comm, num_cells, root_rank);
80 const auto my_all_num_points = Parallel::broadcast(comm, all_num_points, root_rank);
81 const auto my_all_num_cells = Parallel::broadcast(comm, all_num_cells, root_rank);
82 return IOContext{rank, size, my_all_num_cells, my_all_num_points};
86 std::size_t _accumulate(
const std::vector<std::size_t>& in)
const {
87 return std::accumulate(in.begin(), in.end(), std::size_t{0});
90 std::size_t _accumulate_rank_offset(
const std::vector<std::size_t>& in)
const {
91 if (in.size() <=
static_cast<std::size_t
>(my_rank))
92 throw ValueError(
"Rank-vector length must be equal to number of ranks");
93 return std::accumulate(in.begin(), std::next(in.begin(), my_rank), std::size_t{0});
97#if GRIDFORMAT_HAVE_HIGH_FIVE
105 using ParentType = LazyField<const HDF5::File<C>&>;
108 using ParentType::ParentType;
110 explicit DataSetField(
const HDF5::File<C>& file, std::string path)
113 MDLayout{file.get_dimensions(path).value()},
114 DynamicPrecision{file.get_precision(path).value()},
115 [_p=std::move(path)] (
const HDF5::File<C>& file) {
116 return file.visit_dataset(_p, [&] <
typename F> (F&& field) {
117 return field.serialized();
126template<
typename C,
typename CB>
132 if (!file.exists(
"/VTKHDF"))
133 throw IOError(
"Given file is not a VTK-HDF file");
134 if (!file.has_attribute_at(
"/VTKHDF/Type"))
135 throw IOError(
"VTKHDF-Type attribute missing");
136 return file.template read_attribute_to<std::string>(
"/VTKHDF/Type");
142 if (file.has_attribute_at(
"/VTKHDF/Version"))
143 file.visit_attribute(
"/VTKHDF/Version", [&] (
auto&& field) {
144 const auto version = field.template export_to<std::vector<std::size_t>>();
145 if ((version.size() > 0 && version.at(0) > supported[0]) ||
146 (version.size() > 1 && version.at(0) == supported[0] && version.at(1) > supported[1]))
148 "File version is higher than supported by the reader (" + as_string(supported,
".") +
")"
void check_version_compatibility(const HDF5::File< C > &file, const std::array< std::size_t, 2 > &supported)
Check that the version stated in the file is supported.
Definition: hdf_common.hpp:141
std::string get_file_type(const HDF5::File< C > &file)
Read the vtk-hdf file type from an hdf5 file.
Definition: hdf_common.hpp:131
Helper class to store processor offsets in parallel writes.
Definition: hdf_common.hpp:37