forked from openPMD/openPMD-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAttributable.cpp
More file actions
602 lines (558 loc) · 18.8 KB
/
Attributable.cpp
File metadata and controls
602 lines (558 loc) · 18.8 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/* Copyright 2017-2025 Fabian Koller, Axel Huebl, Franz Poeschel, Luca Fedeli
*
* 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/backend/Attributable.hpp"
#include "openPMD/IO/AbstractIOHandler.hpp"
#include "openPMD/Iteration.hpp"
#include "openPMD/ParticleSpecies.hpp"
#include "openPMD/RecordComponent.hpp"
#include "openPMD/Series.hpp"
#include "openPMD/auxiliary/DerefDynamicCast.hpp"
#include "openPMD/auxiliary/StringManip.hpp"
#include "openPMD/backend/Attribute.hpp"
#include <algorithm>
#include <complex>
#include <iostream>
#include <set>
#include <sstream>
#include <stdexcept>
namespace openPMD
{
namespace internal
{
SharedAttributableData::SharedAttributableData(AttributableData *attr)
: m_writable{attr}
{}
AttributableData::AttributableData()
: SharedData_t(std::make_shared<SharedAttributableData>(this))
{}
AttributableData::AttributableData(SharedAttributableData *raw_ptr)
: SharedData_t({raw_ptr, [](auto const *) {}})
{}
void AttributableData::cloneFrom(AttributableData const &other)
{
using parent_t = std::shared_ptr<SharedAttributableData>;
static_cast<parent_t &>(*this) = static_cast<parent_t const &>(other);
}
} // namespace internal
Attributable::Attributable()
{
// Might already be initialized by inheriting classes due to virtual
// inheritance
if (!m_attri)
{
m_attri = std::make_shared<Data_t>();
}
}
Attributable::Attributable(NoInit) noexcept
{}
Attribute Attributable::getAttribute(std::string const &key) const
{
auto &attri = get();
auto it = attri.m_attributes.find(key);
if (it != attri.m_attributes.cend())
return it->second;
throw no_such_attribute_error(key);
}
bool Attributable::deleteAttribute(std::string const &key)
{
auto &attri = get();
if (Access::READ_ONLY == IOHandler()->m_frontendAccess)
throw std::runtime_error(
"Can not delete an Attribute in a read-only Series.");
auto it = attri.m_attributes.find(key);
if (it != attri.m_attributes.end())
{
Parameter<Operation::DELETE_ATT> aDelete;
aDelete.name = key;
IOHandler()->enqueue(IOTask(this, aDelete));
IOHandler()->flush(internal::defaultFlushParams);
attri.m_attributes.erase(it);
return true;
}
return false;
}
std::vector<std::string> Attributable::attributes() const
{
auto &attri = get();
std::vector<std::string> ret;
ret.reserve(attri.m_attributes.size());
for (auto const &entry : attri.m_attributes)
ret.emplace_back(entry.first);
return ret;
}
size_t Attributable::numAttributes() const
{
return get().m_attributes.size();
}
bool Attributable::containsAttribute(std::string const &key) const
{
auto &attri = get();
return attri.m_attributes.find(key) != attri.m_attributes.end();
}
std::string Attributable::comment() const
{
return getAttribute("comment").get<std::string>();
}
Attributable &Attributable::setComment(std::string const &c)
{
setAttribute("comment", c);
return *this;
}
void Attributable::seriesFlush(std::string backendConfig)
{
writable().seriesFlush</* flush_entire_series = */ true>(
std::move(backendConfig));
}
void Attributable::iterationFlush(std::string backendConfig)
{
writable().seriesFlush</* flush_entire_series = */ false>(
std::move(backendConfig));
}
Series Attributable::retrieveSeries() const
{
Writable const *findSeries = &writable();
while (findSeries->parent)
{
findSeries = findSeries->parent;
}
return findSeries->attributable->asInternalCopyOf<Series>();
}
auto Attributable::containingIteration() const -> std::pair<
std::optional<internal::IterationData const *>,
internal::SeriesData const *>
{
constexpr size_t search_queue_size = 3;
Writable const *search_queue[search_queue_size]{nullptr};
size_t search_queue_idx = 0;
Writable const *findSeries = &writable();
while (true)
{
search_queue[search_queue_idx] = findSeries;
// we don't need to push the last Writable since it's the Series anyway
findSeries = findSeries->parent;
if (!findSeries)
{
break;
}
else
{
search_queue_idx = (search_queue_idx + 1) % search_queue_size;
}
}
// End of the queue:
// Iteration -> Series.iterations -> Series
auto *series = &auxiliary::deref_dynamic_cast<internal::SeriesData const>(
search_queue[search_queue_idx]->attributable);
auto maybe_iteration = search_queue
[(search_queue_idx + (search_queue_size - 2)) % search_queue_size];
if (maybe_iteration)
{
auto *iteration =
&auxiliary::deref_dynamic_cast<internal::IterationData const>(
maybe_iteration->attributable);
return std::make_pair(std::make_optional(iteration), series);
}
else
{
return std::make_pair(std::nullopt, series);
}
}
auto Attributable::containingIteration() -> std::
pair<std::optional<internal::IterationData *>, internal::SeriesData *>
{
auto const_res =
static_cast<Attributable const *>(this)->containingIteration();
return std::make_pair(
const_res.first.has_value()
? std::make_optional(
const_cast<internal::IterationData *>(*const_res.first))
: std::nullopt,
const_cast<internal::SeriesData *>(const_res.second));
}
std::string Attributable::MyPath::filePath() const
{
return directory + seriesName + seriesExtension;
}
std::string Attributable::MyPath::openPMDPath() const
{
if (group.empty())
{
return std::string("/");
}
else
{
std::stringstream res;
for (auto const &element : group)
{
res << '/' << element;
}
return res.str();
}
}
auto Attributable::myPath() const -> MyPath
{
MyPath res;
Writable const *findSeries = &writable();
while (findSeries->parent)
{
// we don't need to push_back the ownKeyWithinParent of the Series class
// so it's alright that this loop doesn't ask the key of the last found
// Writable
res.group.push_back(findSeries->ownKeyWithinParent);
findSeries = findSeries->parent;
}
std::reverse(res.group.begin(), res.group.end());
auto &seriesData = auxiliary::deref_dynamic_cast<internal::SeriesData>(
findSeries->attributable);
Series series;
series.setData(
std::shared_ptr<internal::SeriesData>{
&seriesData, [](auto const *) {}});
res.seriesName = series.name();
res.seriesExtension = suffix(seriesData.m_format);
res.directory = IOHandler()->directory;
res.access = IOHandler()->m_backendAccess;
return res;
}
void Attributable::touch()
{
setDirtyRecursive(true);
}
OpenpmdStandard Attributable::openPMDStandard() const
{
return IOHandler()->m_standard;
}
template <bool flush_entire_series>
void Attributable::seriesFlush_impl(internal::FlushParams const &flushParams)
{
writable().seriesFlush<flush_entire_series>(flushParams);
}
template void
Attributable::seriesFlush_impl<true>(internal::FlushParams const &flushParams);
template void
Attributable::seriesFlush_impl<false>(internal::FlushParams const &flushParams);
void Attributable::flushAttributes(internal::FlushParams const &flushParams)
{
switch (flushParams.flushLevel)
{
case FlushLevel::SkeletonOnly:
case FlushLevel::CreateOrOpenFiles:
return;
case FlushLevel::InternalFlush:
case FlushLevel::UserFlush:
// pass
break;
}
if (dirty())
{
Parameter<Operation::WRITE_ATT> aWrite;
for (std::string const &att_name : attributes())
{
aWrite.name = att_name;
aWrite.m_resource = getAttribute(att_name).getAny();
aWrite.dtype = getAttribute(att_name).dtype;
IOHandler()->enqueue(IOTask(this, aWrite));
}
}
// Do this outside the if branch to also setDirty to dirtyRecursive
if (flushParams.flushLevel != FlushLevel::SkeletonOnly)
{
setDirty(false);
}
}
void Attributable::readAttributes(ReadMode mode)
{
auto &attri = get();
Parameter<Operation::LIST_ATTS> aList;
IOHandler()->enqueue(IOTask(this, aList));
IOHandler()->flush(internal::defaultFlushParams);
std::vector<std::string> written_attributes = attributes();
/* std::set_difference requires sorted ranges */
std::sort(aList.attributes->begin(), aList.attributes->end());
std::sort(written_attributes.begin(), written_attributes.end());
std::set<std::string> tmpAttributes;
switch (mode)
{
case ReadMode::IgnoreExisting:
// reread: aList - written_attributes
std::set_difference(
aList.attributes->begin(),
aList.attributes->end(),
written_attributes.begin(),
written_attributes.end(),
std::inserter(tmpAttributes, tmpAttributes.begin()));
break;
case ReadMode::OverrideExisting:
tmpAttributes = std::set<std::string>(
aList.attributes->begin(), aList.attributes->end());
break;
case ReadMode::FullyReread:
attri.m_attributes.clear();
tmpAttributes = std::set<std::string>(
aList.attributes->begin(), aList.attributes->end());
break;
}
using DT = Datatype;
Parameter<Operation::READ_ATT> aRead;
for (auto const &att_name : tmpAttributes)
{
aRead.name = att_name;
std::string att = auxiliary::strip(att_name, {'\0'});
IOHandler()->enqueue(IOTask(this, aRead));
try
{
IOHandler()->flush(internal::defaultFlushParams);
}
catch (unsupported_data_error const &e)
{
std::cerr << "Skipping non-standard attribute " << att << " ("
<< e.what() << ")\n";
continue;
}
Attribute a(Attribute::from_any, *aRead.m_resource);
auto guardUnitDimension = [this](std::string const &key, auto vector) {
if (key == "unitDimension")
{
// Some backends may report the wrong type when reading
if (vector.size() != 7)
{
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"[Attributable] Unexpected datatype for unitDimension "
"(supplied vector has " +
std::to_string(vector.size()) +
" entries, but 7 are expected).");
}
std::array<double, 7> arr;
std::copy_n(vector.begin(), 7, arr.begin());
setAttribute(key, arr);
}
else
{
setAttribute(key, std::move(vector));
}
};
switch (*aRead.dtype)
{
case DT::CHAR:
setAttribute(att, a.get<char>());
break;
case DT::UCHAR:
setAttribute(att, a.get<unsigned char>());
break;
case DT::SCHAR:
setAttribute(att, a.get<signed char>());
break;
case DT::SHORT:
setAttribute(att, a.get<short>());
break;
case DT::INT:
setAttribute(att, a.get<int>());
break;
case DT::LONG:
setAttribute(att, a.get<long>());
break;
case DT::LONGLONG:
setAttribute(att, a.get<long long>());
break;
case DT::USHORT:
setAttribute(att, a.get<unsigned short>());
break;
case DT::UINT:
setAttribute(att, a.get<unsigned int>());
break;
case DT::ULONG:
setAttribute(att, a.get<unsigned long>());
break;
case DT::ULONGLONG:
setAttribute(att, a.get<unsigned long long>());
break;
case DT::FLOAT:
setAttribute(att, a.get<float>());
break;
case DT::DOUBLE:
setAttribute(att, a.get<double>());
break;
case DT::LONG_DOUBLE:
setAttribute(att, a.get<long double>());
break;
case DT::CFLOAT:
setAttribute(att, a.get<std::complex<float> >());
break;
case DT::CDOUBLE:
setAttribute(att, a.get<std::complex<double> >());
break;
case DT::CLONG_DOUBLE:
setAttribute(att, a.get<std::complex<long double> >());
break;
case DT::STRING:
setAttribute(att, a.get<std::string>());
break;
case DT::VEC_CHAR:
setAttribute(att, a.get<std::vector<char> >());
break;
case DT::VEC_SHORT:
setAttribute(att, a.get<std::vector<short> >());
break;
case DT::VEC_INT:
setAttribute(att, a.get<std::vector<int> >());
break;
case DT::VEC_LONG:
setAttribute(att, a.get<std::vector<long> >());
break;
case DT::VEC_LONGLONG:
setAttribute(att, a.get<std::vector<long long> >());
break;
case DT::VEC_UCHAR:
setAttribute(att, a.get<std::vector<unsigned char> >());
break;
case DT::VEC_USHORT:
setAttribute(att, a.get<std::vector<unsigned short> >());
break;
case DT::VEC_UINT:
setAttribute(att, a.get<std::vector<unsigned int> >());
break;
case DT::VEC_ULONG:
setAttribute(att, a.get<std::vector<unsigned long> >());
break;
case DT::VEC_ULONGLONG:
setAttribute(att, a.get<std::vector<unsigned long long> >());
break;
case DT::VEC_FLOAT:
guardUnitDimension(att, a.get<std::vector<float> >());
break;
case DT::VEC_DOUBLE:
guardUnitDimension(att, a.get<std::vector<double> >());
break;
case DT::VEC_LONG_DOUBLE:
guardUnitDimension(att, a.get<std::vector<long double> >());
break;
case DT::VEC_CFLOAT:
setAttribute(att, a.get<std::vector<std::complex<float> > >());
break;
case DT::VEC_CDOUBLE:
setAttribute(att, a.get<std::vector<std::complex<double> > >());
break;
case DT::VEC_CLONG_DOUBLE:
setAttribute(
att, a.get<std::vector<std::complex<long double> > >());
break;
case DT::VEC_SCHAR:
setAttribute(att, a.get<std::vector<signed char> >());
break;
case DT::VEC_STRING:
setAttribute(att, a.get<std::vector<std::string> >());
break;
case DT::ARR_DBL_7:
setAttribute(att, a.get<std::array<double, 7> >());
break;
case DT::BOOL:
setAttribute(att, a.get<bool>());
break;
case DT::UNDEFINED:
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"Undefined Attribute datatype during read");
}
}
setDirty(false);
}
void Attributable::setWritten(bool val, EnqueueAsynchronously ea)
{
switch (ea)
{
case EnqueueAsynchronously::OnlyAsync: {
Parameter<Operation::SET_WRITTEN> param;
param.target_status = val;
IOHandler()->enqueue(IOTask(this, param));
return;
}
case EnqueueAsynchronously::Both: {
Parameter<Operation::SET_WRITTEN> param;
param.target_status = val;
IOHandler()->enqueue(IOTask(this, param));
break;
}
case EnqueueAsynchronously::No:
break;
}
writable().written = val;
}
void Attributable::linkHierarchy(Writable &w)
{
auto handler = w.IOHandler;
writable().IOHandler = handler;
writable().parent = &w;
setDirty(true);
}
namespace internal
{
template <typename T>
T &makeOwning(T &self, Series s)
{
/*
* `self` is a handle object such as RecordComponent or Mesh (see
* instantiations below).
* These objects don't normally keep alive the Series, i.e. as soon as
* the Series is destroyed, the handle becomes invalid.
* This function modifies the handle such that it actually keeps the
* Series alive and behaves otherwise identically.
* First, get the internal shared pointer of the handle.
*/
std::shared_ptr<typename T::Data_t> data_ptr = self.T::getShared();
auto raw_ptr = data_ptr.get();
/*
* Now, create a new shared pointer pointing to the same address as the
* actual pointer and replace the old internal shared pointer by the new
* one.
*/
self.setData(
std::shared_ptr<typename T::Data_t>{
raw_ptr,
/*
* Here comes the main trick.
* The new shared pointer stores (and thus keeps alive) two
* items via lambda capture in its destructor:
* 1. The old shared pointer.
* 2. The Series.
* It's important to notice that these two items are only stored
* within the newly created handle, and not internally within
* the actual openPMD object model. This means that no reference
* cycles can occur.
*/
[s_lambda = std::move(s),
data_ptr_lambda = std::move(data_ptr)](auto const *) {
/* no-op, the lambda captures simply go out of scope */
}});
return self;
}
template RecordComponent &makeOwning(RecordComponent &, Series);
template MeshRecordComponent &makeOwning(MeshRecordComponent &, Series);
template Mesh &makeOwning(Mesh &, Series);
template Record &makeOwning(Record &, Series);
template ParticleSpecies &makeOwning(ParticleSpecies &, Series);
template Iteration &makeOwning(Iteration &, Series);
} // namespace internal
} // namespace openPMD