1 #ifndef MEDUSA_BITS_IO_HDF_HPP_
2 #define MEDUSA_BITS_IO_HDF_HPP_
17 assert_msg(H5Iis_valid(
group),
"Group id %d invalid. Did you open a group before reading?",
19 hid_t attr = H5Aopen(
group, attr_name.c_str(), H5P_DEFAULT);
20 assert_msg(attr >= 0,
"Attribute '%s' could not be accessed in group '%s' in file '%s'.",
23 hid_t type = H5Aget_type(attr);
24 assert_msg(type >= 0,
"Failed getting type of attribute '%s' in group '%s' in file '%s'.",
28 herr_t status = H5Aread(attr, type, &result);
29 assert_msg(status >= 0,
"Failed reading attribute '%s' from group '%s' in file '%s'.",
39 bool overwrite)
const {
40 assert_msg(H5Iis_valid(
group),
"Group id %d invalid. Did you open a group before writing?",
42 if (H5Aexists(
group, attr_name.c_str())) {
44 assert_msg(
false,
"Attribute '%s' in group '%s' in file '%s' already exists. To "
45 "overwrite its contents use parameter overwrite=true.",
49 herr_t status = H5Adelete(
group, attr_name.c_str());
50 assert_msg(status >= 0,
"Failed deleting existing attribute '%s' in group '%s' in "
51 "file '%s' before writing a new one.",
55 hid_t space = H5Screate(H5S_SCALAR);
56 hid_t attr = H5Acreate(
group, attr_name.c_str(), type, space, H5P_DEFAULT, H5P_DEFAULT);
57 assert_msg(attr >= 0,
"Failed creating attribute '%s' in group '%s' in file '%s'.",
59 herr_t status = H5Awrite(attr, type, &value);
60 assert_msg(status >= 0,
"Failed writing attribute '%s' to group '%s' in file '%s'.",
67 std::pair<std::vector<hsize_t>, std::vector<T>>
69 assert_msg(H5Iis_valid(
group),
"Group id %d invalid. Did you open a group before reading?",
71 hid_t dataset = H5Dopen(
group, dataset_name.c_str(), H5P_DEFAULT);
72 assert_msg(dataset >= 0,
"Dataset '%s' could not be accessed in group '%s' in file '%s'.",
75 hid_t dataspace = H5Dget_space(dataset);
76 const int ndims = H5Sget_simple_extent_ndims(dataspace);
77 std::vector<hsize_t> dims(ndims);
78 H5Sget_simple_extent_dims(dataspace, dims.data(),
nullptr);
80 for (
int d = 0; d < ndims; ++d) size *= dims[d];
82 std::vector<T> linear_value(size);
83 hid_t type = H5Dget_type(dataset);
84 herr_t status = H5Dread(dataset, type, H5S_ALL, H5S_ALL, H5P_DEFAULT,
86 assert_msg(status >= 0,
"Failed reading dataset '%s' from group '%s' in file '%s'.",
91 return {dims, linear_value};
96 std::vector<hsize_t> dims;
98 std::tie(dims, value) = readLinearArray<T>(dataset_name);
99 assert_msg(dims.size() == 1,
"This function is for one dimensional arrays only, got %d-D "
100 "array with sizes %s.", dims.size(), dims);
104 template <
typename T>
106 std::vector<hsize_t> dims;
107 std::vector<T> linear_value;
108 std::tie(dims, linear_value) = readLinearArray<T>(dataset_name);
109 assert_msg(dims.size() == 2,
"This function is for two dimensional arrays only, got %d-D "
110 "array with sizes %s.", dims.size(), dims);
111 hsize_t cols = dims[0];
112 hsize_t rows = dims[1];
114 std::vector<std::vector<T>> value(rows, std::vector<T>(cols));
115 for (hsize_t i = 0; i < rows; i++)
116 for (hsize_t j = 0; j < cols; j++)
117 value[i][j] = linear_value[j * rows + i];
122 template <
typename T>
123 std::vector<std::vector<std::vector<T>>>
HDF::read3DArray(
const std::string& dataset_name)
const {
124 std::vector<hsize_t> dims;
125 std::vector<T> linear_value;
126 std::tie(dims, linear_value) = readLinearArray<T>(dataset_name);
127 assert_msg(dims.size() == 3,
"This function is for three dimensional arrays only, got %d-D "
128 "array with sizes %s.", dims.size(), dims);
129 hsize_t tubes = dims[0];
130 hsize_t cols = dims[1];
131 hsize_t rows = dims[2];
133 std::vector<std::vector<std::vector<T>>> value(rows, std::vector<std::vector<T>>(
134 cols, std::vector<T>(tubes)));
135 for (hsize_t i = 0; i < rows; i++)
136 for (hsize_t j = 0; j < cols; j++)
137 for (hsize_t k = 0; k < tubes; k++)
138 value[i][j][k] = linear_value[k * cols * rows + j * rows + i];
143 template <
int dim,
typename T>
145 const std::array<hsize_t, dim>& sizes, hid_t type,
146 bool overwrite)
const {
147 assert_msg(H5Iis_valid(
group),
"Group id %d invalid. Did you open a group before writing?",
150 if (H5Lexists(
group, dataset_name.c_str(), H5P_DEFAULT)) {
152 assert_msg(
false,
"Dataset '%s' in group '%s' in file '%s' already exists. To "
153 "overwrite its contents use parameter overwrite=true.",
157 dataset = H5Dopen(
group, dataset_name.c_str(), H5P_DEFAULT);
158 assert_msg(dataset >= 0,
"Failed opening dataset '%s' in group '%s' in file '%s'.",
160 hid_t dataspace = H5Dget_space(dataset);
161 const int ndims = H5Sget_simple_extent_ndims(dataspace);
163 hsize_t cur_dims[
dim];
164 H5Sget_simple_extent_dims(dataspace, cur_dims,
nullptr);
165 for (
int d = 0; d <
dim; ++d) {
167 "Only data of same old_size can be overwritten, but new dataset has "
168 "old_size '%d' and existing has old_size '%d'.", sizes[d], cur_dims[d]);
173 const int rank =
dim;
174 const hsize_t* max_dims =
nullptr;
175 dataspace = H5Screate_simple(rank, &sizes[0], max_dims);
176 dataset = H5Dcreate(
group, dataset_name.c_str(), type, dataspace,
177 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
178 assert_msg(dataset >= 0,
"Failed creating dataset '%s' in group '%s' in file '%s'.",
182 int size = 1;
for (
int d = 0; d <
dim; ++d) { size *= sizes[d]; }
183 herr_t status = H5Dwrite(dataset, type, H5S_ALL, H5S_ALL, H5P_DEFAULT, value);
184 assert_msg(status >= 0,
"Failed writing dataset '%s' to group '%s' in file '%s'.",
189 template <
typename T,
typename array_t>
191 bool overwrite)
const {
192 hsize_t size = value.size();
193 std::vector<T> cast_value(size);
194 for (hsize_t j = 0; j < size; j++) { cast_value[j] =
static_cast<T
>(value[j]); }
195 writeLinearArray<1>(dataset_name, cast_value.data(), {size}, type, overwrite);
198 template <
typename T,
class array_t>
200 bool overwrite, std::false_type)
const {
201 size_t rows = value.size();
202 size_t cols = (rows == 0) ? 0 : value[0].size();
203 std::vector<T> linear_value(rows * cols);
204 for (
size_t i = 0; i < rows; i++) {
205 assert_msg(
static_cast<size_t>(value[i].size()) == cols,
206 "Not all subarrays are of the same size, subarray 0 has size %d, but subarray "
207 "%d has size %d. Maybe use pad to fix raggedness?", cols, i, value[i].size());
208 for (
size_t j = 0; j < cols; j++) {
209 linear_value[j * rows + i] =
static_cast<T
>(value[i][j]);
212 writeLinearArray<2>(dataset_name, linear_value.data(), {cols, rows}, type, overwrite);
215 template <
typename T,
class array_t>
217 bool overwrite)
const {
218 size_t rows = value.size();
219 size_t cols = (rows == 0) ? 0 : value[0].size();
220 size_t tubes = (cols == 0) ? 0 : value[0][0].size();
221 std::vector<T> linear_value(rows * cols * tubes);
222 for (
size_t i = 0; i < rows; i++) {
223 assert_msg(
static_cast<size_t>(value[i].size()) == cols,
224 "Not all subarrays are of the same size, subarray 0 has size %d, but subarray "
225 "%d has size %d. Maybe use pad to fix raggedness?", cols, i, value[i].size());
226 for (
size_t j = 0; j < cols; j++) {
227 assert_msg(
static_cast<size_t>(value[i][j].size()) == tubes,
228 "Not all subarrays are of the same size, subarray (0, 0) has size %d, "
229 "but subarray (%d, %d) has size %d. Maybe use pad to fix raggedness?",
230 tubes, i, j, value[i][j].size());
231 for (
size_t k = 0; k < tubes; ++k) {
232 linear_value[k * cols * rows + j * rows + i] =
static_cast<T
>(value[i][j][k]);
236 writeLinearArray<3>(dataset_name, linear_value.data(), {tubes, cols, rows}, type, overwrite);
239 template <
typename array_t>
241 bool overwrite)
const {
242 write1DArray<int>(dataset_name, value, H5T_NATIVE_INT, overwrite);
245 template <
typename array_t>
247 bool overwrite)
const {
248 write1DArray<double>(dataset_name, value, H5T_NATIVE_DOUBLE, overwrite);
251 template <
typename array_t>
253 bool overwrite)
const {
254 write1DArray<float>(dataset_name, value, H5T_NATIVE_FLOAT, overwrite);
257 template <
typename array_t>
259 bool overwrite)
const {
260 write2DArray<int>(dataset_name, value, H5T_NATIVE_INT, overwrite);
263 template <
typename array_t>
265 bool overwrite)
const {
266 write2DArray<double>(dataset_name, value, H5T_NATIVE_DOUBLE, overwrite);
269 template <
typename array_t>
271 bool overwrite)
const {
272 write2DArray<float>(dataset_name, value, H5T_NATIVE_FLOAT, overwrite);
275 template <
typename array_t>
277 bool overwrite)
const {
278 write3DArray<int>(dataset_name, value, H5T_NATIVE_INT, overwrite);
281 template <
typename array_t>
283 bool overwrite)
const {
284 write3DArray<double>(dataset_name, value, H5T_NATIVE_DOUBLE, overwrite);
287 template <
typename array_t>
289 bool overwrite)
const {
290 write3DArray<float>(dataset_name, value, H5T_NATIVE_FLOAT, overwrite);
293 template <
typename conf_t>
294 void HDF::writeXML(
const std::string& name,
const conf_t& conf,
bool overwrite) {
299 std::vector<std::pair<std::string, std::string>> data = conf.getAll();
300 for (
const auto& kv : data) {
302 std::string::size_type num_read;
303 double x = std::stod(kv.second, &num_read);
304 if (num_read < kv.second.size()) {
305 throw std::invalid_argument(kv.second);
308 }
catch (
const std::invalid_argument&) {
315 template <
typename SparseMatrixType>
318 std::vector<std::array<double, 3>> triplets(matrix.nonZeros());
320 for (
int k = 0; k < matrix.outerSize(); ++k) {
321 for (
typename SparseMatrixType::InnerIterator it(matrix, k); it; ++it) {
322 triplets[c][0] = one_based+it.row();
323 triplets[c][1] = one_based+it.col();
324 triplets[c][2] = it.value();
331 template <
typename domain_t>
345 template <
typename timer_t>
351 std::vector<std::string> labels = timer.labels();
352 int size = labels.size();
353 if (size == 0)
return;
354 for (
int i = 1; i < size; ++i) {
364 #endif // MEDUSA_BITS_IO_HDF_HPP_