Loading...
Searching...
No Matches
H5Dump.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifdef WITH_HDF5
6
7#include "H5Dump.h"
8
9#include <stdexcept>
10#include <vector>
11
12namespace gpr {
13namespace io {
14
15namespace {
16
17// Split "/a/b/c" into {"a", "b", "c"}; the trailing component is the
18// dataset name, everything before is group path. Leading slash optional.
19std::vector<std::string> split_path(const std::string& path)
20{
21 std::vector<std::string> out;
22 std::string cur;
23 for (char c: path) {
24 if (c == '/') {
25 if (!cur.empty()) {
26 out.push_back(std::move(cur));
27 cur.clear();
28 }
29 } else {
30 cur.push_back(c);
31 }
32 }
33 if (!cur.empty()) out.push_back(std::move(cur));
34 return out;
35}
36
37// Open group at `relative_path` under parent, creating intermediate
38// groups on the way if they do not exist. Returns a new handle that the
39// caller must H5Gclose. Empty path returns parent itself (open new handle).
40hid_t open_or_create_groups(hid_t parent,
41 const std::vector<std::string>& groups)
42{
43 hid_t cur = parent;
44 bool owned = false;
45 for (const auto& g: groups) {
46 hid_t next;
47 if (H5Lexists(cur, g.c_str(), H5P_DEFAULT) > 0) {
48 next = H5Gopen2(cur, g.c_str(), H5P_DEFAULT);
49 } else {
50 next = H5Gcreate2(cur, g.c_str(), H5P_DEFAULT, H5P_DEFAULT,
51 H5P_DEFAULT);
52 }
53 if (next < 0) {
54 if (owned) H5Gclose(cur);
55 throw std::runtime_error("H5Dump: failed to open/create group '" +
56 g + "'");
57 }
58 if (owned) H5Gclose(cur);
59 cur = next;
60 owned = true;
61 }
62 return cur; // caller owns if groups was non-empty, else parent (not owned)
63}
64
65} // namespace
66
67H5Dumper::H5Dumper(const std::string& path, Mode mode) : file_(-1)
68{
69 if (mode == Mode::Overwrite) {
70 file_ =
71 H5Fcreate(path.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
72 } else {
73 // OpenOrCreate: open an accessible file, otherwise create a fresh
74 // trace.
75 if (H5Fis_accessible(path.c_str(), H5P_DEFAULT) > 0) {
76 file_ = H5Fopen(path.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
77 } else {
78 file_ = H5Fcreate(path.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT,
79 H5P_DEFAULT);
80 }
81 }
82 if (file_ < 0) {
83 throw std::runtime_error("H5Dump: failed to open/create file '" + path +
84 "'");
85 }
86}
87
88H5Dumper::~H5Dumper()
89{
90 if (file_ >= 0) H5Fclose(file_);
91}
92
93void H5Dumper::dump(const std::string& dataset_path, const double* data,
94 std::size_t n)
95{
96 const hsize_t dims[1] = {static_cast<hsize_t>(n)};
97 write(dataset_path, H5T_NATIVE_DOUBLE, 1, dims, data);
98}
99
100void H5Dumper::dump2d(const std::string& dataset_path, const double* data,
101 std::size_t rows, std::size_t cols)
102{
103 const hsize_t dims[2] = {static_cast<hsize_t>(rows),
104 static_cast<hsize_t>(cols)};
105 write(dataset_path, H5T_NATIVE_DOUBLE, 2, dims, data);
106}
107
108void H5Dumper::dumpInt32(const std::string& dataset_path,
109 const std::int32_t* data, std::size_t n)
110{
111 const hsize_t dims[1] = {static_cast<hsize_t>(n)};
112 write(dataset_path, H5T_NATIVE_INT32, 1, dims, data);
113}
114
115void H5Dumper::rootAttribute(const std::string& name, const std::string& value)
116{
117 if (H5Aexists(file_, name.c_str()) > 0) H5Adelete(file_, name.c_str());
118 const hid_t type = H5Tcopy(H5T_C_S1);
119 H5Tset_size(type, H5T_VARIABLE);
120 H5Tset_cset(type, H5T_CSET_UTF8);
121 const hid_t space = H5Screate(H5S_SCALAR);
122 const hid_t attr =
123 H5Acreate2(file_, name.c_str(), type, space, H5P_DEFAULT, H5P_DEFAULT);
124 herr_t status = -1;
125 if (attr >= 0) {
126 const char* text = value.c_str();
127 status = H5Awrite(attr, type, &text);
128 H5Aclose(attr);
129 }
130 H5Sclose(space);
131 H5Tclose(type);
132 if (status < 0) {
133 throw std::runtime_error("H5Dump: failed to write attribute '" + name +
134 "'");
135 }
136}
137
138void H5Dumper::write(const std::string& dataset_path, hid_t type, int rank,
139 const hsize_t* dims, const void* data)
140{
141 auto parts = split_path(dataset_path);
142 if (parts.empty()) {
143 throw std::runtime_error("H5Dump: empty dataset path");
144 }
145 std::string dset_name = parts.back();
146 parts.pop_back();
147
148 hid_t parent = open_or_create_groups(file_, parts);
149 bool parent_owned = !parts.empty();
150
151 // Delete existing dataset of same name (emulates H5Easy::dump overwrite).
152 if (H5Lexists(parent, dset_name.c_str(), H5P_DEFAULT) > 0) {
153 H5Ldelete(parent, dset_name.c_str(), H5P_DEFAULT);
154 }
155
156 hid_t space = H5Screate_simple(rank, dims, nullptr);
157 hid_t dset = H5Dcreate2(parent, dset_name.c_str(), type, space, H5P_DEFAULT,
158 H5P_DEFAULT, H5P_DEFAULT);
159 if (dset < 0) {
160 H5Sclose(space);
161 if (parent_owned) H5Gclose(parent);
162 throw std::runtime_error("H5Dump: H5Dcreate2 failed for '" +
163 dataset_path + "'");
164 }
165
166 herr_t status = H5Dwrite(dset, type, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
167 H5Dclose(dset);
168 H5Sclose(space);
169 if (parent_owned) H5Gclose(parent);
170
171 if (status < 0) {
172 throw std::runtime_error("H5Dump: H5Dwrite failed for '" +
173 dataset_path + "'");
174 }
175}
176
177} // namespace io
178} // namespace gpr
179
180#endif // WITH_HDF5
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
int status
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16