Skip to content

Commit

Permalink
ADIOS2: Optionally write attributes only from given ranks
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Nov 16, 2023
1 parent 9ad33b0 commit 3d35d8d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
6 changes: 5 additions & 1 deletion include/openPMD/IO/ADIOS/ADIOS2IOHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ class ADIOS2IOHandlerImpl
return m_useGroupTable.value();
}

bool m_writeAttributesFromThisRank = true;

struct ParameterizedOperator
{
adios2::Operator op;
Expand All @@ -285,7 +287,9 @@ class ADIOS2IOHandlerImpl
json::TracingJSON m_config;
static json::TracingJSON nullvalue;

void init(json::TracingJSON config);
template <typename Callback>
void
init(json::TracingJSON config, Callback &&callbackWriteAttributesFromRank);

template <typename Key>
json::TracingJSON config(Key &&key, json::TracingJSON &cfg)
Expand Down
68 changes: 61 additions & 7 deletions src/IO/ADIOS/ADIOS2IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,43 @@ ADIOS2IOHandlerImpl::ADIOS2IOHandlerImpl(
, m_engineType(std::move(engineType))
, m_userSpecifiedExtension{std::move(specifiedExtension)}
{
init(std::move(cfg));
init(
std::move(cfg),
/* callbackWriteAttributesFromRank = */
[communicator, this](nlohmann::json const &attribute_writing_ranks) {
int rank = 0;
MPI_Comm_rank(communicator, &rank);
auto throw_error = []() {
throw error::BackendConfigSchema(
{"adios2", "attribute_writing_ranks"},
"Type must be either an integer or an array of integers.");
};
if (attribute_writing_ranks.is_array())
{
m_writeAttributesFromThisRank = false;
for (auto const &val : attribute_writing_ranks)
{
if (!val.is_number())
{
throw_error();
}
if (val.get<int>() == rank)
{
m_writeAttributesFromThisRank = true;
break;
}
}
}
else if (attribute_writing_ranks.is_number())
{
m_writeAttributesFromThisRank =
attribute_writing_ranks.get<int>() == rank;
}
else
{
throw_error();
}
});
}

#endif // openPMD_HAVE_MPI
Expand All @@ -94,7 +130,7 @@ ADIOS2IOHandlerImpl::ADIOS2IOHandlerImpl(
, m_engineType(std::move(engineType))
, m_userSpecifiedExtension(std::move(specifiedExtension))
{
init(std::move(cfg));
init(std::move(cfg), [](auto const &...) {});
}

ADIOS2IOHandlerImpl::~ADIOS2IOHandlerImpl()
Expand Down Expand Up @@ -135,7 +171,9 @@ ADIOS2IOHandlerImpl::~ADIOS2IOHandlerImpl()
}
}

void ADIOS2IOHandlerImpl::init(json::TracingJSON cfg)
template <typename Callback>
void ADIOS2IOHandlerImpl::init(
json::TracingJSON cfg, Callback &&callbackWriteAttributesFromRank)
{
// allow overriding through environment variable
m_engineType =
Expand Down Expand Up @@ -181,6 +219,12 @@ void ADIOS2IOHandlerImpl::init(json::TracingJSON cfg)
: ModifiableAttributes::No;
}

if (m_config.json().contains("attribute_writing_ranks"))
{
callbackWriteAttributesFromRank(
m_config["attribute_writing_ranks"].json());
}

auto engineConfig = config(ADIOS2Defaults::str_engine);
if (!engineConfig.json().is_null())
{
Expand Down Expand Up @@ -915,6 +959,10 @@ void ADIOS2IOHandlerImpl::writeDataset(
void ADIOS2IOHandlerImpl::writeAttribute(
Writable *writable, const Parameter<Operation::WRITE_ATT> &parameters)
{
if (!m_writeAttributesFromThisRank)
{
return;
}
#if openPMD_HAS_ADIOS_2_9
switch (useGroupTable())
{
Expand Down Expand Up @@ -3033,7 +3081,11 @@ namespace detail
if (!initializedDefaults)
{
// Currently only schema 0 supported
m_IO.DefineAttribute<uint64_t>(ADIOS2Defaults::str_adios2Schema, 0);
if (m_impl->m_writeAttributesFromThisRank)
{
m_IO.DefineAttribute<uint64_t>(
ADIOS2Defaults::str_adios2Schema, 0);
}
initializedDefaults = true;
}

Expand Down Expand Up @@ -3168,7 +3220,8 @@ namespace detail
{
if (writeOnly(m_mode) &&
!m_IO.InquireAttribute<bool_representation>(
ADIOS2Defaults::str_usesstepsAttribute))
ADIOS2Defaults::str_usesstepsAttribute) &&
m_impl->m_writeAttributesFromThisRank)
{
m_IO.DefineAttribute<bool_representation>(
ADIOS2Defaults::str_usesstepsAttribute, 0);
Expand All @@ -3189,7 +3242,8 @@ namespace detail
*/
if (calledExplicitly && writeOnly(m_mode) &&
!m_IO.InquireAttribute<bool_representation>(
ADIOS2Defaults::str_usesstepsAttribute))
ADIOS2Defaults::str_usesstepsAttribute) &&
m_impl->m_writeAttributesFromThisRank)
{
m_IO.DefineAttribute<bool_representation>(
ADIOS2Defaults::str_usesstepsAttribute, 1);
Expand Down Expand Up @@ -3356,7 +3410,7 @@ namespace detail
case UseGroupTable::Yes:
#if openPMD_HAS_ADIOS_2_9
{
if (writeOnly(m_mode))
if (writeOnly(m_mode) && m_impl->m_writeAttributesFromThisRank)
{
requireActiveStep();
auto currentStepBuffered = currentStep();
Expand Down
2 changes: 2 additions & 0 deletions test/ParallelIOTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,8 @@ doshuffle = "BLOSC_BITSHUFFLE"
std::string writeConfigBP4 = R"END(
[adios2]
unused = "parameter"
attribute_writing_ranks = 0
use_group_table = true
[adios2.engine]
type = "bp4"
Expand Down

0 comments on commit 3d35d8d

Please sign in to comment.