forked from openPMD/openPMD-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclose_and_reopen_test.cpp
More file actions
426 lines (404 loc) · 14.3 KB
/
close_and_reopen_test.cpp
File metadata and controls
426 lines (404 loc) · 14.3 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* Copyright 2025 Franz Poeschel
*
* This file is part of openPMD-api.
*
* openPMD-api is free software: you can redistribute it and/or modify
* it under the terms of of either the GNU General Public License or
* the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* openPMD-api is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License and the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with openPMD-api.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "SerialIOTests.hpp"
#include "openPMD/IO/ADIOS/macros.hpp"
#include "openPMD/IO/Access.hpp"
#include "openPMD/Series.hpp"
#include "openPMD/auxiliary/Filesystem.hpp"
#include <catch2/catch_test_macros.hpp>
namespace close_and_reopen_test
{
using namespace openPMD;
#if openPMD_HAVE_ADIOS2
constexpr char const *write_cfg =
R"(adios2.use_group_table = true
adios2.modifiable_attributes = true
adios2.engine.parameters.FlattenSteps = "ON")";
template <typename WriteIterations>
auto run_test_filebased(
Access writeAccess,
WriteIterations &&writeIterations,
std::string const &ext,
bool synchronous)
{
std::string filename =
"../samples/close_iteration_reopen/filebased_%T." + ext;
auxiliary::remove_directory("../samples/close_iteration_reopen");
Series series(filename, writeAccess, write_cfg);
{
auto it = writeIterations(series)[0];
auto E_x = it.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {5}});
std::vector<int> data{0, 1, 2, 3, 4};
E_x.storeChunk(data, {0}, {5});
it.close();
it.open();
auto B_y = it.meshes["B"]["y"];
B_y.resetDataset({Datatype::INT, {5}});
B_y.storeChunk(data, {0}, {5});
it.close();
// This also verifies that operator[] and at() can be used to access the
// Iteration after closing
REQUIRE(series.iterations.at(0).closed());
REQUIRE(writeIterations(series)[0].closed() == !synchronous);
REQUIRE(writeIterations(series).at(0).closed() == !synchronous);
}
{
auto it = writeIterations(series)[1];
auto E_x = it.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {5}});
std::vector<int> data{0, 1, 2, 3, 4};
E_x.storeChunk(data, {0}, {5});
it.close();
it.open();
auto e_position_x = it.particles["e"]["position"]["x"];
e_position_x.resetDataset({Datatype::INT, {5}});
e_position_x.storeChunk(data, {0}, {5});
it.close();
REQUIRE(series.iterations.at(1).closed());
REQUIRE(writeIterations(series).at(1).closed() == !synchronous);
REQUIRE(writeIterations(series)[1].closed() == !synchronous);
// We are in file-based iteration encoding, so the old iteration should
// remain accessible
// Note: this will create a particlespath at iteration 0, which will
// lead to parsing warnings in HDF5.
writeIterations(series).at(0);
}
{
auto it = writeIterations(series)[2];
auto E_x = it.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {5}});
std::vector<int> data{0, 1, 2, 3, 4};
E_x.storeChunk(data, {0}, {5});
it.close();
if (series.backend() != "ADIOS2")
{
it.open();
it.setTimeUnitSI(2.0);
it.close();
}
}
series.close();
series = Series(filename, Access::READ_WRITE, write_cfg);
{
auto it = series.snapshots()[0].open();
std::vector<int> data(5);
it.meshes["E"]["x"].loadChunkRaw(data.data(), {0}, {5});
it.close();
REQUIRE((data == std::vector<int>{0, 1, 2, 3, 4}));
}
{
auto it = series.snapshots()[2].open();
std::vector<int> data(5);
it.meshes["E"]["x"].loadChunkRaw(data.data(), {0}, {5});
it.close();
REQUIRE((data == std::vector<int>{0, 1, 2, 3, 4}));
// no guarantee which attribute version we get
REQUIRE((it.timeUnitSI() == 2.0 || it.timeUnitSI() == 1.0));
}
{
auto it = series.snapshots()[3].open();
auto E_x = it.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {5}});
std::vector<int> data{0, 1, 2, 3, 4};
E_x.storeChunk(data, {0}, {5});
it.close();
it.open();
auto e_position_x = it.particles["e"]["position"]["x"];
e_position_x.resetDataset({Datatype::INT, {5}});
e_position_x.storeChunk(data, {0}, {5});
it.close();
}
series.close();
for (auto mode : {Access::READ_RANDOM_ACCESS, Access::READ_LINEAR})
{
Series read(filename, mode);
{
auto it = read.snapshots()[0];
std::vector<int> data(5);
it.meshes["E"]["x"].loadChunkRaw(data.data(), {0}, {5});
it.close();
REQUIRE((data == std::vector<int>{0, 1, 2, 3, 4}));
}
REQUIRE(read.iterations.size() == 4);
{
auto it = read.snapshots()[1];
std::vector<int> data(5);
it.meshes["E"]["x"].loadChunkRaw(data.data(), {0}, {5});
it.close();
REQUIRE((data == std::vector<int>{0, 1, 2, 3, 4}));
}
{
auto it = read.snapshots()[3];
std::vector<int> data(5);
it.meshes["E"]["x"].loadChunkRaw(data.data(), {0}, {5});
it.close();
REQUIRE((data == std::vector<int>{0, 1, 2, 3, 4}));
}
{
auto it = read.snapshots()[2];
std::vector<int> data(5);
it.meshes["E"]["x"].loadChunkRaw(data.data(), {0}, {5});
it.close();
REQUIRE((data == std::vector<int>{0, 1, 2, 3, 4}));
// no guarantee which attribute version we get
REQUIRE((it.timeUnitSI() == 2.0 || it.timeUnitSI() == 1.0));
}
{
auto it = read.snapshots()[0].open();
std::vector<int> data(5);
it.meshes["B"]["y"].loadChunkRaw(data.data(), {0}, {5});
it.close();
REQUIRE((data == std::vector<int>{0, 1, 2, 3, 4}));
}
{
auto it = read.snapshots()[1].open();
std::vector<int> data(5);
it.particles["e"]["position"]["x"].loadChunkRaw(
data.data(), {0}, {5});
it.close();
REQUIRE((data == std::vector<int>{0, 1, 2, 3, 4}));
}
{
auto it = read.snapshots()[3].open();
std::vector<int> data(5);
it.particles["e"]["position"]["x"].loadChunkRaw(
data.data(), {0}, {5});
it.close();
REQUIRE((data == std::vector<int>{0, 1, 2, 3, 4}));
}
}
}
template <typename WriteIterations>
auto run_test_groupbased(
Access writeAccess,
WriteIterations &&writeIterations,
std::string const &ext,
std::vector<Access> const &readModes,
bool synchronous)
{
std::string filename =
"../samples/close_iteration_reopen/groupbased." + ext;
/*
* Need to enforce group-based encoding for this test. Since this closes and
* reopens the same Iteration for writing, the output will go to multiple
* ADIOS2 steps. Opening this in group-based encoding with
* READ_RANDOM_ACCESS will show all data from all steps. However, in
* variable-based encoding, Iterations are selected by specifying an ADIOS2
* step selection, hence only a single step's data will be shown for each
* Iteration.
*/
Series series(
filename,
writeAccess,
json::merge(write_cfg, R"({"iteration_encoding": "group_based"})"));
{
auto it = writeIterations(series)[0];
auto E_x = it.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {5}});
std::vector<int> data{0, 1, 2, 3, 4};
E_x.storeChunk(data, {0}, {5});
it.close();
it.open();
auto B_y = it.meshes["B"]["y"];
B_y.resetDataset({Datatype::INT, {5}});
B_y.storeChunk(data, {0}, {5});
it.close();
// This also verifies that operator[] and at() can be used to access the
// Iteration after closing
REQUIRE(series.iterations.at(0).closed());
REQUIRE(writeIterations(series)[0].closed() == !synchronous);
REQUIRE(writeIterations(series).at(0).closed() == !synchronous);
if (synchronous)
{
// we opened a new step, need to do something in it now,
// otherwise we get a corrupted file
B_y.storeChunk(data, {0}, {5});
it.close();
}
}
{
auto it = writeIterations(series)[1];
auto E_x = it.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {5}});
std::vector<int> data{0, 1, 2, 3, 4};
E_x.storeChunk(data, {0}, {5});
it.close();
it.open();
auto E_y = it.meshes["E"]["y"];
E_y.resetDataset({Datatype::INT, {5}});
E_y.storeChunk(data, {0}, {5});
it.close();
if (!synchronous || series.backend() != "ADIOS2")
{
writeIterations(series).at(0);
}
else
{
// Cannot go back to an old IO step
// Since the other backends do not use IO steps,
// going back to an old Iteration should remain possible even
// in synchronous modes
REQUIRE_THROWS(writeIterations(series).at(0));
}
}
{
auto it = writeIterations(series)[2];
auto E_x = it.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {5}});
std::vector<int> data{0, 1, 2, 3, 4};
E_x.storeChunk(data, {0}, {5});
it.close();
it.open();
it.setTimeUnitSI(2.0);
it.close();
}
series.close();
for (auto mode : readModes)
{
Series read(filename, mode);
{
auto it = read.snapshots()[0];
std::vector<int> data(5);
it.meshes["E"]["x"].loadChunkRaw(data.data(), {0}, {5});
it.close();
REQUIRE((data == std::vector<int>{0, 1, 2, 3, 4}));
}
REQUIRE(read.iterations.size() == 3);
{
auto it = read.snapshots()[1];
std::vector<int> data(5);
it.meshes["E"]["x"].loadChunkRaw(data.data(), {0}, {5});
it.close();
REQUIRE((data == std::vector<int>{0, 1, 2, 3, 4}));
}
{
auto it = read.snapshots()[2];
std::vector<int> data(5);
it.meshes["E"]["x"].loadChunkRaw(data.data(), {0}, {5});
it.close();
REQUIRE((data == std::vector<int>{0, 1, 2, 3, 4}));
// no guarantee which attribute version we get
REQUIRE((it.timeUnitSI() == 2.0 || it.timeUnitSI() == 1.0));
}
{
auto it = read.snapshots()[0].open();
std::vector<int> data(5);
it.meshes["B"]["y"].loadChunkRaw(data.data(), {0}, {5});
it.close();
REQUIRE((data == std::vector<int>{0, 1, 2, 3, 4}));
}
{
auto it = read.snapshots()[1].open();
std::vector<int> data(5);
it.meshes["E"]["y"].loadChunkRaw(data.data(), {0}, {5});
it.close();
REQUIRE((data == std::vector<int>{0, 1, 2, 3, 4}));
}
}
}
auto close_and_reopen_test() -> void
{
for (auto writeAccess :
{Access::CREATE_RANDOM_ACCESS, Access::CREATE_LINEAR})
{
bool synchronous = writeAccess == Access::CREATE_LINEAR;
run_test_filebased(
writeAccess, [](Series &s) { return s.iterations; }, "bp", false);
run_test_filebased(
writeAccess,
[](Series &s) { return s.writeIterations(); },
"bp",
true);
run_test_filebased(
writeAccess,
[](Series &s) { return s.snapshots(); },
"bp",
synchronous);
run_test_filebased(
writeAccess,
[](Series &s) { return s.snapshots(); },
"bp",
synchronous);
run_test_filebased(
writeAccess,
[](Series &s) { return s.snapshots(); },
"json",
synchronous);
#if openPMD_HAVE_HDF5
run_test_filebased(
writeAccess,
[](Series &s) { return s.snapshots(); },
"h5",
synchronous);
#endif
/*
* This test writes the same attribute with different values over steps,
* triggering a bug in ADIOS2 v2.7.
*/
run_test_groupbased(
writeAccess,
[](Series &s) { return s.iterations; },
"bp4",
{Access::READ_ONLY, Access::READ_LINEAR},
false);
// since these write data in a way that distributes one iteration's data
// over multiple steps, only random access read mode makes sense
run_test_groupbased(
writeAccess,
[](Series &s) { return s.writeIterations(); },
"bp4",
{Access::READ_RANDOM_ACCESS},
true);
run_test_groupbased(
writeAccess,
[](Series &s) { return s.snapshots(); },
"bp4",
{Access::READ_RANDOM_ACCESS},
synchronous);
// that doesnt matter for json tho
run_test_groupbased(
writeAccess,
[](Series &s) { return s.snapshots(); },
"json",
{Access::READ_RANDOM_ACCESS, Access::READ_LINEAR},
synchronous);
#if openPMD_HAVE_HDF5
run_test_groupbased(
writeAccess,
[](Series &s) { return s.snapshots(); },
"h5",
{Access::READ_RANDOM_ACCESS, Access::READ_LINEAR},
synchronous);
#endif
run_test_groupbased(
writeAccess,
[](Series &s) { return s.snapshots(); },
"json",
{Access::READ_RANDOM_ACCESS, Access::READ_LINEAR},
synchronous);
}
}
#else
auto close_and_reopen_test() -> void
{}
#endif
} // namespace close_and_reopen_test