forked from openPMD/openPMD-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathADIOS2PreloadAttributes.cpp
More file actions
344 lines (315 loc) · 10.4 KB
/
ADIOS2PreloadAttributes.cpp
File metadata and controls
344 lines (315 loc) · 10.4 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
/* Copyright 2020-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 "openPMD/config.hpp"
#if openPMD_HAVE_ADIOS2
#include "openPMD/IO/ADIOS/ADIOS2PreloadAttributes.hpp"
#include "openPMD/Datatype.hpp"
#include "openPMD/IO/ADIOS/ADIOS2Auxiliary.hpp"
#include <algorithm>
#include <cstddef>
#include <cstdlib>
#include <optional>
namespace openPMD::detail
{
namespace
{
struct GetAlignment
{
template <typename T>
static constexpr size_t call()
{
return alignof(T);
}
template <unsigned long, typename... Args>
static constexpr size_t call(Args &&...)
{
return alignof(std::max_align_t);
}
};
struct GetSize
{
template <typename T>
static constexpr size_t call()
{
return sizeof(T);
}
template <unsigned long, typename... Args>
static constexpr size_t call(Args &&...)
{
return 0;
}
};
struct ScheduleLoad
{
template <typename T>
static void call(
adios2::IO &IO,
std::string const &name,
char *buffer,
PreloadAdiosAttributes::AttributeLocation &location)
{
adios2::Attribute<T> attr = IO.InquireAttribute<T>(name);
if (!attr)
{
throw std::runtime_error(
"[ADIOS2] Variable not found: " + name);
}
/*
* MSVC does not like placement new of arrays, so we do it
* in a loop instead.
* https://developercommunity.visualstudio.com/t/c-placement-new-is-incorrectly-compiled/206439
*/
T *dest = reinterpret_cast<T *>(buffer);
for (size_t i = 0; i < location.len; ++i)
{
new (dest + i) T();
}
location.destroy = buffer;
auto data = attr.Data();
std::copy_n(data.begin(), data.size(), dest);
}
static constexpr char const *errorMsg = "ADIOS2";
};
struct AttributeLen
{
template <typename T>
static size_t call(adios2::IO &IO, std::string const &name)
{
auto attr = IO.InquireAttribute<T>(name);
if (!attr)
{
throw std::runtime_error(
"[ADIOS2] Variable not found: " + name);
}
return attr.Data().size();
}
template <unsigned long n, typename... Args>
static size_t call(Args &&...)
{
return {};
}
};
struct AttributeLocationDestroy
{
template <typename T>
static void call(char *ptr, size_t numItems)
{
T *destroy = reinterpret_cast<T *>(ptr);
for (size_t i = 0; i < numItems; ++i)
{
destroy[i].~T();
}
}
template <unsigned long n, typename... Args>
static void call(Args &&...)
{}
};
} // namespace
using AttributeLocation = PreloadAdiosAttributes::AttributeLocation;
AttributeLocation::AttributeLocation(
size_t len_in, size_t offset_in, Datatype dt_in)
: len(len_in), offset(offset_in), dt(dt_in)
{}
AttributeLocation::AttributeLocation(AttributeLocation &&other) noexcept
: len{other.len}, offset{other.offset}, dt{other.dt}, destroy{other.destroy}
{
other.destroy = nullptr;
}
AttributeLocation &
AttributeLocation::operator=(AttributeLocation &&other) noexcept
{
this->len = other.len;
this->offset = other.offset;
this->dt = other.dt;
this->destroy = other.destroy;
other.destroy = nullptr;
return *this;
}
PreloadAdiosAttributes::AttributeLocation::~AttributeLocation()
{
/*
* If the object has been moved from, this may be empty.
* Or else, if no custom destructor has been emplaced.
*/
if (destroy)
{
switchAdios2AttributeType<AttributeLocationDestroy>(dt, destroy, len);
}
}
void PreloadAdiosAttributes::preloadAttributes(adios2::IO &IO)
{
m_offsets.clear();
std::map<Datatype, std::vector<std::string>> attributesByType;
auto addAttribute = [&attributesByType](Datatype dt, std::string name) {
constexpr size_t reserve = 10;
auto it = attributesByType.find(dt);
if (it == attributesByType.end())
{
it = attributesByType.emplace_hint(
it, dt, std::vector<std::string>());
it->second.reserve(reserve);
}
it->second.push_back(std::move(name));
};
// PHASE 1: collect names of available attributes by ADIOS datatype
for (auto &attribute : IO.AvailableAttributes())
{
// this will give us basic types only, no fancy vectors or similar
Datatype dt = fromADIOS2Type(IO.AttributeType(attribute.first));
addAttribute(dt, attribute.first);
}
// PHASE 2: get offsets for attributes in buffer
std::map<Datatype, size_t> offsets;
size_t currentOffset = 0;
for (auto &pair : attributesByType)
{
size_t alignment = switchAdios2AttributeType<GetAlignment>(pair.first);
size_t size = switchAdios2AttributeType<GetSize>(pair.first);
// go to next offset with valid alignment
size_t modulus = currentOffset % alignment;
if (modulus > 0)
{
currentOffset += alignment - modulus;
}
for (std::string &name : pair.second)
{
size_t elements =
switchAdios2AttributeType<AttributeLen>(pair.first, IO, name);
m_offsets.emplace(
std::piecewise_construct,
std::forward_as_tuple(std::move(name)),
std::forward_as_tuple(elements, currentOffset, pair.first));
currentOffset += elements * size;
}
}
// now, currentOffset is the number of bytes that we need to allocate
// PHASE 3: allocate new buffer and schedule loads
m_rawBuffer.resize(currentOffset);
for (auto &pair : m_offsets)
{
switchAdios2AttributeType<ScheduleLoad>(
pair.second.dt,
IO,
pair.first,
&m_rawBuffer[pair.second.offset],
pair.second);
}
}
template <typename T>
AttributeWithShape<T>
PreloadAdiosAttributes::getAttribute(std::string const &name) const
{
auto it = m_offsets.find(name);
if (it == m_offsets.end())
{
throw std::runtime_error(
"[ADIOS2] Requested attribute not found: " + name);
}
AttributeLocation const &location = it->second;
Datatype determinedDatatype = determineDatatype<T>();
if (!isSame(location.dt, determinedDatatype))
{
std::stringstream errorMsg;
errorMsg << "[ADIOS2] Wrong datatype for attribute: " << name
<< "(location.dt=" << location.dt
<< ", T=" << determineDatatype<T>() << ")";
throw std::runtime_error(errorMsg.str());
}
AttributeWithShape<T> res;
res.len = location.len;
res.data = reinterpret_cast<T const *>(&m_rawBuffer[location.offset]);
return res;
}
Datatype PreloadAdiosAttributes::attributeType(std::string const &name) const
{
auto it = m_offsets.find(name);
if (it == m_offsets.end())
{
return Datatype::UNDEFINED;
}
return it->second.dt;
}
std::map<std::string, AttributeLocation> const &
PreloadAdiosAttributes::availableAttributes() const
{
return m_offsets;
}
template <typename T>
auto AdiosAttributes::getAttribute(
size_t step, adios2::IO &IO, std::string const &name) const
-> AttributeWithShapeAndResource<T>
{
return std::visit(
auxiliary::overloaded{
[step, &name](
RandomAccess_t const &ra) -> AttributeWithShapeAndResource<T> {
auto &attribute_data = ra.at(step);
return attribute_data.getAttribute<T>(name);
},
[&name,
&IO](StreamAccess_t const &) -> AttributeWithShapeAndResource<T> {
auto attr = IO.InquireAttribute<T>(name);
return {std::move(attr)};
}},
m_data);
}
template <typename T>
AttributeWithShapeAndResource<T>::AttributeWithShapeAndResource(
AttributeWithShape<T> parent)
: AttributeWithShape<T>(std::move(parent))
{}
template <typename T>
AttributeWithShapeAndResource<T>::AttributeWithShapeAndResource(
size_t len_in, T const *data_in, std::optional<std::vector<T>> resource_in)
: AttributeWithShape<T>{len_in, data_in}, resource{std::move(resource_in)}
{}
template <typename T>
AttributeWithShapeAndResource<T>::AttributeWithShapeAndResource(
adios2::Attribute<T> attr)
{
if (!attr)
{
this->data = nullptr;
this->len = 0;
return;
}
auto vec = attr.Data();
this->len = vec.size();
this->data = vec.data();
this->resource = std::move(vec);
}
template <typename T>
AttributeWithShapeAndResource<T>::operator bool() const
{
return this->data;
}
#define OPENPMD_INSTANTIATE_GETATTRIBUTE(type) \
template struct AttributeWithShape<type>; \
template struct AttributeWithShapeAndResource<type>; \
template AttributeWithShape<type> PreloadAdiosAttributes::getAttribute( \
std::string const &name) const; \
template auto AdiosAttributes::getAttribute( \
size_t step, adios2::IO &IO, std::string const &name) const \
-> AttributeWithShapeAndResource<type>;
ADIOS2_FOREACH_TYPE_1ARG(OPENPMD_INSTANTIATE_GETATTRIBUTE)
#undef OPENPMD_INSTANTIATE_GETATTRIBUTE
} // namespace openPMD::detail
#endif // openPMD_HAVE_ADIOS2