-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhdf5_readers.h
More file actions
360 lines (320 loc) · 14.2 KB
/
Copy pathhdf5_readers.h
File metadata and controls
360 lines (320 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#pragma once
#include <cstdint>
#include <cstring>
#include <fstream>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "hdf5_types.h"
#include "hdf5_io.h"
namespace hdf5_reader {
// ============================================================================
// Internal helpers
// ============================================================================
namespace detail {
inline void open_file(std::ifstream& f, const std::string& filepath) {
f.open(filepath, std::ios::binary);
if (!f.is_open()) {
throw std::runtime_error("hdf5_reader: cannot open: " + filepath);
}
}
template <typename T>
inline void validate_dims(const DatasetInfo& info, const char* fn_name) {
if (info.num_cols == 0) {
throw std::runtime_error(std::string(fn_name) + ": 1-D dataset not supported");
}
}
template <typename T>
inline void read_flat(std::ifstream& f, const DatasetInfo& info, std::vector<T>& out, size_t count, size_t dims, size_t start_row = 0, size_t num_rows = 0) {
if (start_row >= count) {
out.clear();
return;
}
size_t read_count = num_rows;
if (read_count == 0 || start_row + read_count > count) {
read_count = count - start_row;
}
const size_t bytes_per_row = dims * sizeof(T);
uint64_t target_offset = info.file_offset + (uint64_t)start_row * bytes_per_row;
detail::fseek(f, target_offset);
out.resize(read_count * dims);
for (size_t i = 0; i < read_count; ++i) {
f.read(reinterpret_cast<char*>(out.data() + i * dims),
static_cast<std::streamsize>(bytes_per_row));
if (!f.good()) {
throw std::runtime_error("hdf5_reader: read error at row " + std::to_string(start_row + i));
}
}
}
template <typename T>
inline void read_matrix(std::ifstream& f, const DatasetInfo& info, std::vector<std::vector<T>>& out, size_t count, size_t dims, size_t start_row = 0, size_t num_rows = 0) {
if (start_row >= count) {
out.clear();
return;
}
size_t read_count = num_rows;
if (read_count == 0 || start_row + read_count > count) {
read_count = count - start_row;
}
const size_t bytes_per_row = dims * sizeof(T);
uint64_t target_offset = info.file_offset + (uint64_t)start_row * bytes_per_row;
detail::fseek(f, target_offset);
out.resize(read_count);
for (size_t i = 0; i < read_count; ++i) {
out[i].resize(dims);
f.read(reinterpret_cast<char*>(out[i].data()),
static_cast<std::streamsize>(bytes_per_row));
if (!f.good()) {
throw std::runtime_error("hdf5_reader: read error at row " + std::to_string(start_row + i));
}
}
}
} // namespace detail
// ============================================================================
// Flat readers — return std::vector<T> (row-major, count × dims)
// ============================================================================
inline std::vector<std::byte> read_flat_bytes(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<std::byte>(info, "read_flat_bytes");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<std::byte> out;
detail::read_flat<std::byte>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<uint8_t> read_flat_uint8(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<uint8_t>(info, "read_flat_uint8");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<uint8_t> out;
detail::read_flat<uint8_t>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<int8_t> read_flat_int8(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<int8_t>(info, "read_flat_int8");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<int8_t> out;
detail::read_flat<int8_t>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<uint16_t> read_flat_uint16(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<uint16_t>(info, "read_flat_uint16");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<uint16_t> out;
detail::read_flat<uint16_t>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<int16_t> read_flat_int16(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<int16_t>(info, "read_flat_int16");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<int16_t> out;
detail::read_flat<int16_t>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<uint32_t> read_flat_uint32(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<uint32_t>(info, "read_flat_uint32");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<uint32_t> out;
detail::read_flat<uint32_t>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<int32_t> read_flat_int32(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<int32_t>(info, "read_flat_int32");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<int32_t> out;
detail::read_flat<int32_t>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<float> read_flat_fp32(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<float>(info, "read_flat_fp32");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<float> out;
detail::read_flat<float>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<int64_t> read_flat_int64(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<int64_t>(info, "read_flat_int64");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<int64_t> out;
detail::read_flat<int64_t>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<double> read_flat_fp64(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<double>(info, "read_flat_fp64");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<double> out;
detail::read_flat<double>(f, info, out, count, dims, start_row, num_rows);
return out;
}
// ============================================================================
// Matrix readers — return std::vector<std::vector<T>> (one row per inner vector)
// ============================================================================
inline std::vector<std::vector<std::byte>> read_matrix_bytes(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
if (info.num_cols == 0) {
throw std::runtime_error("read_matrix_bytes: 1-D dataset not supported");
}
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
if (start_row >= count) {
return {};
}
size_t read_count = num_rows;
if (read_count == 0 || start_row + read_count > count) {
read_count = count - start_row;
}
size_t dims = (size_t)info.num_cols;
const size_t bytes_per_row = dims * info.element_size;
uint64_t target_offset = info.file_offset + (uint64_t)start_row * bytes_per_row;
detail::fseek(f, target_offset);
std::vector<std::vector<std::byte>> out(read_count);
for (size_t i = 0; i < read_count; ++i) {
out[i].resize(bytes_per_row);
f.read(reinterpret_cast<char*>(out[i].data()), static_cast<std::streamsize>(bytes_per_row));
if (!f.good()) {
throw std::runtime_error("hdf5_reader: read error at row " + std::to_string(start_row + i));
}
}
return out;
}
inline std::vector<std::vector<uint8_t>> read_matrix_uint8(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<uint8_t>(info, "read_matrix_uint8");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<std::vector<uint8_t>> out;
detail::read_matrix<uint8_t>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<std::vector<int8_t>> read_matrix_int8(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<int8_t>(info, "read_matrix_int8");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<std::vector<int8_t>> out;
detail::read_matrix<int8_t>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<std::vector<uint16_t>> read_matrix_uint16(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<uint16_t>(info, "read_matrix_uint16");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<std::vector<uint16_t>> out;
detail::read_matrix<uint16_t>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<std::vector<int16_t>> read_matrix_int16(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<int16_t>(info, "read_matrix_int16");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<std::vector<int16_t>> out;
detail::read_matrix<int16_t>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<std::vector<uint32_t>> read_matrix_uint32(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<uint32_t>(info, "read_matrix_uint32");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<std::vector<uint32_t>> out;
detail::read_matrix<uint32_t>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<std::vector<int32_t>> read_matrix_int32(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<int32_t>(info, "read_matrix_int32");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<std::vector<int32_t>> out;
detail::read_matrix<int32_t>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<std::vector<float>> read_matrix_fp32(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<float>(info, "read_matrix_fp32");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<std::vector<float>> out;
detail::read_matrix<float>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<std::vector<int64_t>> read_matrix_int64(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<int64_t>(info, "read_matrix_int64");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<std::vector<int64_t>> out;
detail::read_matrix<int64_t>(f, info, out, count, dims, start_row, num_rows);
return out;
}
inline std::vector<std::vector<double>> read_matrix_fp64(const std::string& filepath, const DatasetInfo& info, size_t start_row = 0, size_t num_rows = 0) {
detail::validate_dims<double>(info, "read_matrix_fp64");
std::ifstream f;
detail::open_file(f, filepath);
size_t count = (size_t)info.num_rows;
size_t dims = (size_t)info.num_cols;
std::vector<std::vector<double>> out;
detail::read_matrix<double>(f, info, out, count, dims, start_row, num_rows);
return out;
}
// ============================================================================
// Find dataset by name
// ============================================================================
inline const DatasetInfo& find_dataset(
const std::map<std::string, DatasetInfo>& datasets,
const std::string& name)
{
auto it = datasets.find(name);
if (it == datasets.end()) {
throw std::runtime_error("hdf5_reader: dataset '" + name + "' not found in file. "
"Available datasets: " +
[datasets]() {
std::string s;
for (const auto& [k, _] : datasets) {
if (!s.empty()) s += ", ";
s += k;
}
return s;
}());
}
return it->second;
}
} // namespace hdf5_reader