Skip to content

Commit 72be3cb

Browse files
committed
Use AdiosAttributes struct
1 parent 49a078e commit 72be3cb

5 files changed

Lines changed: 58 additions & 53 deletions

File tree

include/openPMD/IO/ADIOS/ADIOS2File.hpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#pragma once
2222

2323
#include "openPMD/IO/ADIOS/ADIOS2Auxiliary.hpp"
24+
#include "openPMD/IO/ADIOS/ADIOS2PreloadAttributes.hpp"
2425
#include "openPMD/IO/AbstractIOHandler.hpp"
2526
#include "openPMD/IO/IOTask.hpp"
2627
#include "openPMD/IO/InvalidatableFile.hpp"
@@ -325,16 +326,9 @@ class ADIOS2File
325326
*/
326327
void drop();
327328

328-
AttributeMap_t const &availableAttributes();
329-
330329
std::vector<std::string>
331330
availableAttributesPrefixed(std::string const &prefix);
332331

333-
/*
334-
* See description below.
335-
*/
336-
void invalidateAttributesMap();
337-
338332
AttributeMap_t const &availableVariables();
339333

340334
std::vector<std::string>
@@ -442,7 +436,7 @@ class ADIOS2File
442436
* the map that would be returned by a call to
443437
* IO::Available(Attributes|Variables).
444438
*/
445-
std::optional<AttributeMap_t> m_availableAttributes;
439+
AdiosAttributes m_attributes;
446440
std::optional<AttributeMap_t> m_availableVariables;
447441

448442
std::set<Writable *> m_pathsMarkedAsActive;

include/openPMD/IO/ADIOS/ADIOS2PreloadAttributes.hpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
*/
2121
#pragma once
2222

23+
#include "openPMD/auxiliary/Variant.hpp"
2324
#include "openPMD/config.hpp"
25+
#include <variant>
2426
#if openPMD_HAVE_ADIOS2
2527

2628
#include <adios2.h>
@@ -133,6 +135,12 @@ class PreloadAdiosAttributes
133135
AttributeWithShape<T> getAttribute(std::string const &name) const;
134136

135137
Datatype attributeType(std::string const &name) const;
138+
139+
std::map<std::string, AttributeLocation> const &
140+
availableAttributes() const &
141+
{
142+
return m_offsets;
143+
}
136144
};
137145

138146
struct AdiosAttributes
@@ -144,7 +152,32 @@ struct AdiosAttributes
144152
std::optional<std::map<std::string, adios2::Params>> m_attributes;
145153
};
146154

147-
std::variant<RandomAccess_t, StreamAccess_t> m_data;
155+
std::variant<RandomAccess_t, StreamAccess_t> m_data = StreamAccess_t{};
156+
157+
template <typename Functor>
158+
auto withAvailableAttributes(size_t step, adios2::IO &IO, Functor &&f)
159+
-> decltype(std::forward<Functor>(f)(
160+
std::declval<std::map<std::string, adios2::Params> &>()))
161+
{
162+
using ret_t = decltype(std::forward<Functor>(f)(
163+
std::declval<std::map<std::string, adios2::Params> &>()));
164+
return std::visit(
165+
auxiliary::overloaded{
166+
[step, &f](RandomAccess_t &ra) -> ret_t {
167+
auto &attribute_data = ra.at(step);
168+
return std::forward<Functor>(f)(
169+
attribute_data.availableAttributes());
170+
},
171+
[step, &f, &IO](StreamAccess_t &sa) -> ret_t {
172+
if (!sa.m_attributes.has_value() ||
173+
sa.m_currentStep != step)
174+
{
175+
sa = StreamAccess_t{step, IO.AvailableAttributes()};
176+
}
177+
return std::forward<Functor>(f)(*sa.m_attributes);
178+
}},
179+
m_data);
180+
}
148181
};
149182
} // namespace openPMD::detail
150183

src/IO/ADIOS/ADIOS2File.cpp

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -759,19 +759,21 @@ void ADIOS2File::configure_IO()
759759

760760
auto ADIOS2File::detectGroupTable() -> UseGroupTable
761761
{
762-
auto const &attributes = availableAttributes();
763-
auto lower_bound =
764-
attributes.lower_bound(adios_defaults::str_activeTablePrefix);
765-
if (lower_bound != attributes.end() &&
766-
auxiliary::starts_with(
767-
lower_bound->first, adios_defaults::str_activeTablePrefix))
768-
{
769-
return UseGroupTable::Yes;
770-
}
771-
else
772-
{
773-
return UseGroupTable::No;
774-
}
762+
return m_attributes.withAvailableAttributes(
763+
currentStep(), m_IO, [&](auto const &attributes) {
764+
auto lower_bound =
765+
attributes.lower_bound(adios_defaults::str_activeTablePrefix);
766+
if (lower_bound != attributes.end() &&
767+
auxiliary::starts_with(
768+
lower_bound->first, adios_defaults::str_activeTablePrefix))
769+
{
770+
return UseGroupTable::Yes;
771+
}
772+
else
773+
{
774+
return UseGroupTable::No;
775+
}
776+
});
775777
}
776778

777779
adios2::Engine &ADIOS2File::getEngine()
@@ -1264,7 +1266,6 @@ AdvanceStatus ADIOS2File::advance(AdvanceMode mode)
12641266
case adios2::StepStatus::OtherError:
12651267
throw std::runtime_error("[ADIOS2] Unexpected step status.");
12661268
}
1267-
invalidateAttributesMap();
12681269
invalidateVariablesMap();
12691270
m_pathsMarkedAsActive.clear();
12701271
return res;
@@ -1280,13 +1281,11 @@ void ADIOS2File::drop()
12801281
assert(m_buffer.empty());
12811282
}
12821283

1284+
template <typename AttributesMap>
12831285
static std::vector<std::string> availableAttributesOrVariablesPrefixed(
1284-
std::string const &prefix,
1285-
ADIOS2File::AttributeMap_t const &(ADIOS2File::*getBasicMap)(),
1286-
ADIOS2File &ba)
1286+
std::string const &prefix, AttributesMap const &attributes)
12871287
{
12881288
std::string var = auxiliary::ends_with(prefix, '/') ? prefix : prefix + '/';
1289-
ADIOS2File::AttributeMap_t const &attributes = (ba.*getBasicMap)();
12901289
std::vector<std::string> ret;
12911290
for (auto it = attributes.lower_bound(prefix); it != attributes.end(); ++it)
12921291
{
@@ -1305,33 +1304,17 @@ static std::vector<std::string> availableAttributesOrVariablesPrefixed(
13051304
std::vector<std::string>
13061305
ADIOS2File::availableAttributesPrefixed(std::string const &prefix)
13071306
{
1308-
return availableAttributesOrVariablesPrefixed(
1309-
prefix, &ADIOS2File::availableAttributes, *this);
1307+
return m_attributes.withAvailableAttributes(
1308+
currentStep(), m_IO, [&](auto const &attributes) {
1309+
return availableAttributesOrVariablesPrefixed(prefix, attributes);
1310+
});
13101311
}
13111312

13121313
std::vector<std::string>
13131314
ADIOS2File::availableVariablesPrefixed(std::string const &prefix)
13141315
{
13151316
return availableAttributesOrVariablesPrefixed(
1316-
prefix, &ADIOS2File::availableVariables, *this);
1317-
}
1318-
1319-
void ADIOS2File::invalidateAttributesMap()
1320-
{
1321-
m_availableAttributes = std::optional<AttributeMap_t>();
1322-
}
1323-
1324-
ADIOS2File::AttributeMap_t const &ADIOS2File::availableAttributes()
1325-
{
1326-
if (m_availableAttributes)
1327-
{
1328-
return m_availableAttributes.value();
1329-
}
1330-
else
1331-
{
1332-
m_availableAttributes = std::make_optional(m_IO.AvailableAttributes());
1333-
return m_availableAttributes.value();
1334-
}
1317+
prefix, ADIOS2File::availableVariables());
13351318
}
13361319

13371320
void ADIOS2File::invalidateVariablesMap()

src/IO/ADIOS/ADIOS2IOHandler.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2148,7 +2148,6 @@ namespace detail
21482148

21492149
auto &filedata = impl->getFileData(
21502150
file, ADIOS2IOHandlerImpl::IfFileNotOpen::ThrowError);
2151-
filedata.invalidateAttributesMap();
21522151
adios2::IO IO = filedata.m_IO;
21532152
impl->m_dirty.emplace(std::move(file));
21542153

src/IO/ADIOS/ADIOS2PreloadAttributes.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,9 @@
2727

2828
#include "openPMD/Datatype.hpp"
2929
#include "openPMD/IO/ADIOS/ADIOS2Auxiliary.hpp"
30-
#include "openPMD/auxiliary/StringManip.hpp"
3130

3231
#include <cstddef>
3332
#include <cstdlib>
34-
#include <iostream>
35-
#include <numeric>
36-
#include <type_traits>
3733

3834
namespace openPMD::detail
3935
{

0 commit comments

Comments
 (0)