Skip to content

Commit da3a45f

Browse files
committed
Read dataset-specific configuration also in ADIOS2::openDataset
1 parent 956b6b2 commit da3a45f

File tree

5 files changed

+116
-50
lines changed

5 files changed

+116
-50
lines changed

include/openPMD/IO/ADIOS/ADIOS2IOHandler.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ class ADIOS2IOHandlerImpl
338338
// use m_config
339339
std::optional<std::vector<ParameterizedOperator>> getOperators();
340340

341+
template <typename Parameter>
342+
std::vector<ParameterizedOperator> getDatasetOperators(
343+
Parameter const &, Writable *, std::string const &varName);
344+
341345
std::string fileSuffix(bool verbose = true) const;
342346

343347
/*
@@ -560,7 +564,9 @@ namespace detail
560564
ADIOS2IOHandlerImpl *impl,
561565
InvalidatableFile const &,
562566
std::string const &varName,
563-
Parameter<Operation::OPEN_DATASET> &parameters);
567+
Parameter<Operation::OPEN_DATASET> &parameters,
568+
std::vector<ADIOS2IOHandlerImpl::ParameterizedOperator> const
569+
&operators);
564570

565571
static constexpr char const *errorMsg = "ADIOS2: openDataset()";
566572
};

include/openPMD/IO/IOTask.hpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ struct OPENPMDAPI_EXPORT AbstractParameter
102102

103103
virtual std::unique_ptr<AbstractParameter> to_heap() && = 0;
104104

105+
/** Warn about unused JSON paramters
106+
*
107+
* Template parameter so we don't have to include the JSON lib here.
108+
* This function is useful for the createDataset() methods in,
109+
* IOHandlerImpl's, so putting that here is the simplest way to make it
110+
* available for them. */
111+
template <typename TracingJSON>
112+
static void warnUnusedParameters(
113+
TracingJSON &,
114+
std::string const &currentBackendName,
115+
std::string const &warningMessage);
116+
105117
protected:
106118
// avoid object slicing
107119
// by allow only child classes to use these things for defining their own
@@ -363,18 +375,6 @@ struct OPENPMDAPI_EXPORT Parameter<Operation::CREATE_DATASET>
363375
std::string options = "{}";
364376
std::optional<size_t> joinedDimension;
365377

366-
/** Warn about unused JSON paramters
367-
*
368-
* Template parameter so we don't have to include the JSON lib here.
369-
* This function is useful for the createDataset() methods in,
370-
* IOHandlerImpl's, so putting that here is the simplest way to make it
371-
* available for them. */
372-
template <typename TracingJSON>
373-
static void warnUnusedParameters(
374-
TracingJSON &,
375-
std::string const &currentBackendName,
376-
std::string const &warningMessage);
377-
378378
template <typename TracingJSON>
379379
TracingJSON compileJSONConfig(
380380
Writable const *writable,
@@ -417,6 +417,12 @@ struct OPENPMDAPI_EXPORT Parameter<Operation::OPEN_DATASET>
417417
new Parameter<Operation::OPEN_DATASET>(std::move(*this)));
418418
}
419419

420+
template <typename TracingJSON>
421+
static TracingJSON compileJSONConfig(
422+
Writable const *writable,
423+
json::JsonMatcher &,
424+
std::string const &backendName);
425+
420426
std::string name = "";
421427
std::shared_ptr<Datatype> dtype = std::make_shared<Datatype>();
422428
std::shared_ptr<Extent> extent = std::make_shared<Extent>();

include/openPMD/backend/Writable.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class Writable final
104104
friend class Span;
105105
friend void debug::printDirty(Series const &);
106106
friend struct Parameter<Operation::CREATE_DATASET>;
107+
friend struct Parameter<Operation::OPEN_DATASET>;
107108

108109
private:
109110
Writable(internal::AttributableData *);

src/IO/ADIOS/ADIOS2IOHandler.cpp

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,35 @@ ADIOS2IOHandlerImpl::getOperators()
371371
return getOperators(m_config);
372372
}
373373

374+
template <typename Parameter>
375+
auto ADIOS2IOHandlerImpl::getDatasetOperators(
376+
Parameter const &parameters, Writable *writable, std::string const &varName)
377+
-> std::vector<ParameterizedOperator>
378+
{
379+
std::vector<ParameterizedOperator> operators;
380+
json::TracingJSON options =
381+
parameters.template compileJSONConfig<json::ParsedConfig>(
382+
writable, *m_handler->jsonMatcher, "adios2");
383+
if (options.json().contains("adios2"))
384+
{
385+
json::TracingJSON datasetConfig(options["adios2"]);
386+
auto datasetOperators = getOperators(datasetConfig);
387+
388+
operators = datasetOperators ? std::move(datasetOperators.value())
389+
: defaultOperators;
390+
}
391+
else
392+
{
393+
operators = defaultOperators;
394+
}
395+
parameters.warnUnusedParameters(
396+
options,
397+
"adios2",
398+
"Warning: parts of the backend configuration for ADIOS2 dataset '" +
399+
varName + "' remain unused:\n");
400+
return operators;
401+
}
402+
374403
using AcceptedEndingsForEngine = std::map<std::string, std::string>;
375404

376405
std::string ADIOS2IOHandlerImpl::fileSuffix(bool verbose) const
@@ -793,27 +822,8 @@ void ADIOS2IOHandlerImpl::createDataset(
793822
filePos->gd = GroupOrDataset::DATASET;
794823
auto const varName = nameOfVariable(writable);
795824

796-
std::vector<ParameterizedOperator> operators;
797-
json::TracingJSON options =
798-
parameters.compileJSONConfig<json::ParsedConfig>(
799-
writable, *m_handler->jsonMatcher, "adios2");
800-
if (options.json().contains("adios2"))
801-
{
802-
json::TracingJSON datasetConfig(options["adios2"]);
803-
auto datasetOperators = getOperators(datasetConfig);
804-
805-
operators = datasetOperators ? std::move(datasetOperators.value())
806-
: defaultOperators;
807-
}
808-
else
809-
{
810-
operators = defaultOperators;
811-
}
812-
parameters.warnUnusedParameters(
813-
options,
814-
"adios2",
815-
"Warning: parts of the backend configuration for ADIOS2 dataset '" +
816-
varName + "' remain unused:\n");
825+
std::vector<ParameterizedOperator> operators =
826+
getDatasetOperators(parameters, writable, varName);
817827

818828
// cast from openPMD::Extent to adios2::Dims
819829
adios2::Dims shape(parameters.extent.begin(), parameters.extent.end());
@@ -1018,8 +1028,18 @@ void ADIOS2IOHandlerImpl::openDataset(
10181028
auto &fileData = getFileData(file, IfFileNotOpen::ThrowError);
10191029
*parameters.dtype =
10201030
detail::fromADIOS2Type(fileData.m_IO.VariableType(varName));
1031+
1032+
/*
1033+
* Technically, the only reason to set read-time operators is for specifying
1034+
* decompression threads. This needs not happen at a per-dataset level.
1035+
* However, users may apply the same JSON/TOML config for writing and
1036+
* reading, so the dataset-specific configuration should still be explored
1037+
* here.
1038+
*/
1039+
std::vector<ParameterizedOperator> operators =
1040+
getDatasetOperators(parameters, writable, varName);
10211041
switchAdios2VariableType<detail::DatasetOpener>(
1022-
*parameters.dtype, this, file, varName, parameters);
1042+
*parameters.dtype, this, file, varName, parameters, operators);
10231043
writable->written = true;
10241044
}
10251045

@@ -2072,7 +2092,9 @@ namespace detail
20722092
ADIOS2IOHandlerImpl *impl,
20732093
InvalidatableFile const &file,
20742094
const std::string &varName,
2075-
Parameter<Operation::OPEN_DATASET> &parameters)
2095+
Parameter<Operation::OPEN_DATASET> &parameters,
2096+
std::vector<ADIOS2IOHandlerImpl::ParameterizedOperator> const
2097+
&operators)
20762098
{
20772099
auto &fileData = impl->getFileData(
20782100
file, ADIOS2IOHandlerImpl::IfFileNotOpen::ThrowError);
@@ -2086,7 +2108,7 @@ namespace detail
20862108
}
20872109

20882110
// Operators in reading needed e.g. for setting decompression threads
2089-
for (auto const &operation : impl->defaultOperators)
2111+
for (auto const &operation : operators)
20902112
{
20912113
if (operation.op)
20922114
{

src/IO/IOTask.cpp

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "openPMD/backend/Attributable.hpp"
2525

2626
#include <iostream> // std::cerr
27+
#include <utility>
2728

2829
namespace openPMD
2930
{
@@ -33,8 +34,7 @@ Writable *getWritable(Attributable *a)
3334
}
3435

3536
template <>
36-
void Parameter<Operation::CREATE_DATASET>::warnUnusedParameters<
37-
json::TracingJSON>(
37+
void AbstractParameter::warnUnusedParameters<json::TracingJSON>(
3838
json::TracingJSON &config,
3939
std::string const &currentBackendName,
4040
std::string const &warningMessage)
@@ -75,24 +75,55 @@ void Parameter<Operation::CREATE_DATASET>::warnUnusedParameters<
7575
}
7676
}
7777

78+
namespace
79+
{
80+
template <typename Functor>
81+
json::ParsedConfig doCompileJSONConfig(
82+
Attributable const &attri,
83+
json::JsonMatcher &jsonMatcher,
84+
std::string const &backendName,
85+
Functor &&transformResult)
86+
{
87+
auto path = attri.myPath().openPMDPath();
88+
auto base_config = jsonMatcher.get(path, backendName);
89+
json::ParsedConfig res{
90+
std::move(base_config.config), base_config.originallySpecifiedAs};
91+
std::forward<Functor>(transformResult)(res);
92+
return res;
93+
}
94+
} // namespace
95+
7896
template <>
7997
json::ParsedConfig Parameter<Operation::CREATE_DATASET>::compileJSONConfig(
8098
Writable const *writable,
8199
json::JsonMatcher &jsonMatcher,
82100
std::string const &backendName) const
83101
{
84102
auto attri = writable->attributable->asInternalCopyOf<Attributable>();
85-
auto path = attri.myPath().openPMDPath();
86-
auto base_config = jsonMatcher.get(path, backendName);
87-
auto manual_config =
88-
json::parseOptions(options, /* considerFiles = */ false);
89-
json::merge_internal(
90-
base_config.config, manual_config.config, /* do_prune = */ true);
91-
return json::ParsedConfig{
92-
std::move(base_config.config),
93-
(options.empty() || options == "{}")
94-
? base_config.originallySpecifiedAs
95-
: manual_config.originallySpecifiedAs};
103+
return doCompileJSONConfig(
104+
attri, jsonMatcher, backendName, [&](json::ParsedConfig &base_config) {
105+
auto manual_config =
106+
json::parseOptions(options, /* considerFiles = */ false);
107+
json::merge_internal(
108+
base_config.config,
109+
manual_config.config,
110+
/* do_prune = */ true);
111+
base_config.originallySpecifiedAs =
112+
(options.empty() || options == "{}")
113+
? base_config.originallySpecifiedAs
114+
: manual_config.originallySpecifiedAs;
115+
});
116+
}
117+
118+
template <>
119+
json::ParsedConfig Parameter<Operation::OPEN_DATASET>::compileJSONConfig(
120+
Writable const *writable,
121+
json::JsonMatcher &jsonMatcher,
122+
std::string const &backendName)
123+
{
124+
auto attri = writable->attributable->asInternalCopyOf<Attributable>();
125+
return doCompileJSONConfig(
126+
attri, jsonMatcher, backendName, [](auto const &) {});
96127
}
97128

98129
namespace internal

0 commit comments

Comments
 (0)