-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy path10_streaming_write.cpp
More file actions
105 lines (96 loc) · 3.7 KB
/
10_streaming_write.cpp
File metadata and controls
105 lines (96 loc) · 3.7 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
/* Copyright 2025 Axel Huebl, 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 "openPMD/Series.hpp"
#include "openPMD/snapshots/Snapshots.hpp"
#include <openPMD/openPMD.hpp>
#include <algorithm>
#include <iostream>
#include <memory>
#include <numeric> // std::iota
using std::cout;
using namespace openPMD;
int main()
{
#if openPMD_HAVE_ADIOS2
using position_t = double;
auto backends = openPMD::getFileExtensions();
if (std::find(backends.begin(), backends.end(), "sst") == backends.end())
{
std::cout << "SST engine not available in ADIOS2." << std::endl;
return 0;
}
// open file for writing
// use QueueFullPolicy = Discard in order to create a situation where from
// the reader's perspective steps are skipped. This tests the bug reported
// in https://github.com/openPMD/openPMD-api/issues/1747.
// Create the Series with linear write access, i.e. one Iteration after
// the other. The alternative would be random-access where multiple
// Iterations can be accessed independently from one another. This more
// restricted mode enables performance optimizations in the backends, and
// more importantly is compatible with streaming I/O.
Series series = Series("electrons.sst", Access::CREATE_LINEAR, R"(
{
"adios2": {
"engine": {
"parameters": {
"DataTransport": "WAN",
"QueueFullPolicy": "Discard"
}
}
}
})");
Datatype datatype = determineDatatype<position_t>();
constexpr unsigned long length = 10ul;
Extent global_extent = {length};
Dataset dataset = Dataset(datatype, global_extent);
std::shared_ptr<position_t> local_data(
new position_t[length], [](position_t const *ptr) { delete[] ptr; });
auto iterations = series.snapshots();
for (size_t i = 0; i < 100; ++i)
{
Iteration iteration = iterations[i];
Record electronPositions = iteration.particles["e"]["position"];
std::iota(local_data.get(), local_data.get() + length, i * length);
for (auto const &dim : {"x", "y", "z"})
{
RecordComponent pos = electronPositions[dim];
pos.resetDataset(dataset);
pos.storeChunk(local_data, Offset{0}, global_extent);
}
auto ch = iteration.customHierarchies();
ch["rabimmel"].setAttribute("rabammel", "rabumm");
iteration.close();
}
/* The files in 'series' are still open until the object is destroyed, on
* which it cleanly flushes and closes all open file handles.
* When running out of scope on return, the 'Series' destructor is called.
* Alternatively, one can call `series.close()` to the same effect as
* calling the destructor, including the release of file handles.
*/
series.close();
return 0;
#else
std::cout << "The streaming example requires that openPMD has been built "
"with ADIOS2."
<< std::endl;
return 0;
#endif
}