Skip to content

Commit

Permalink
First attempt: Dataset-specific configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Jul 10, 2024
1 parent b04f8d3 commit 1713314
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 21 deletions.
21 changes: 9 additions & 12 deletions examples/13_write_dynamic_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,11 @@ int main()
# be passed by adding an at-sign `@` in front of the path
# The format will then be recognized by filename extension, i.e. .json or .toml
backend = "adios2"
backend = "hdf5"
iteration_encoding = "group_based"
# The following is only relevant in read mode
defer_iteration_parsing = true
[adios1.dataset]
transform = "blosc:compressor=zlib,shuffle=bit,lvl=5;nometa"
[adios2.engine]
type = "bp4"
Expand All @@ -60,13 +57,18 @@ parameters.clevel = 5
# type = "some other parameter"
# # ...
[hdf5.dataset]
chunks = "auto"
[[hdf5.dataset]]
cfg.chunks = "auto"
[[hdf5.dataset]]
select = "particles/e/.*"
cfg.chunks = [10]
cfg.chornks = []
)END";

// open file for writing
Series series =
Series("../samples/dynamicConfig.bp", Access::CREATE, defaults);
Series("../samples/dynamicConfig.h5", Access::CREATE, defaults);

Datatype datatype = determineDatatype<position_t>();
constexpr unsigned long length = 10ul;
Expand Down Expand Up @@ -103,11 +105,6 @@ chunks = "auto"
std::string const differentCompressionSettings = R"END(
{
"resizable": true,
"adios1": {
"dataset": {
"transform": "blosc:compressor=zlib,shuffle=bit,lvl=1;nometa"
}
},
"adios2": {
"dataset": {
"operators": [
Expand Down
8 changes: 8 additions & 0 deletions include/openPMD/IO/IOTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ namespace openPMD
{
class Attributable;
class Writable;
namespace json
{
class JsonMatcher;
}

Writable *getWritable(Attributable *);

Expand Down Expand Up @@ -355,6 +359,10 @@ struct OPENPMDAPI_EXPORT Parameter<Operation::CREATE_DATASET>
TracingJSON &,
std::string const &currentBackendName,
std::string const &warningMessage);

template <typename TracingJSON>
TracingJSON
compileJSONConfig(Writable const *writable, json::JsonMatcher &) const;
};

template <>
Expand Down
7 changes: 1 addition & 6 deletions include/openPMD/auxiliary/JSONMatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ struct Pattern
std::regex pattern;
nlohmann::json config;

Pattern(std::string const &pattern_in, nlohmann::json config_in)
// we construct the patterns once and use them often, so let's ask for
// some optimization
: pattern{pattern_in, std::regex_constants::egrep | std::regex_constants::optimize}
, config{std::move(config_in)}
{}
Pattern(std::string const &pattern_in, nlohmann::json config_in);
};

/**
Expand Down
1 change: 1 addition & 0 deletions include/openPMD/backend/Attributable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class Attributable
friend class WriteIterations;
friend class internal::RecordComponentData;
friend void debug::printDirty(Series const &);
friend class internal::AttributableData;

protected:
// tag for internal constructor
Expand Down
1 change: 1 addition & 0 deletions include/openPMD/backend/Writable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class Writable final
template <typename>
friend class Span;
friend void debug::printDirty(Series const &);
friend struct Parameter<Operation::CREATE_DATASET>;

private:
Writable(internal::AttributableData *);
Expand Down
4 changes: 3 additions & 1 deletion src/IO/ADIOS/ADIOS2IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "openPMD/auxiliary/Environment.hpp"
#include "openPMD/auxiliary/Filesystem.hpp"
#include "openPMD/auxiliary/JSONMatcher.hpp"
#include "openPMD/auxiliary/JSON_internal.hpp"
#include "openPMD/auxiliary/Mpi.hpp"
#include "openPMD/auxiliary/StringManip.hpp"
#include "openPMD/auxiliary/TypeTraits.hpp"
Expand Down Expand Up @@ -755,7 +756,8 @@ void ADIOS2IOHandlerImpl::createDataset(

std::vector<ParameterizedOperator> operators;
json::TracingJSON options =
json::parseOptions(parameters.options, /* considerFiles = */ false);
parameters.compileJSONConfig<json::ParsedConfig>(
writable, *m_handler->jsonMatcher);
if (options.json().contains("adios2"))
{
json::TracingJSON datasetConfig(options["adios2"]);
Expand Down
5 changes: 3 additions & 2 deletions src/IO/HDF5/HDF5IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,9 @@ void HDF5IOHandlerImpl::createDataset(
nlohmann::json::parse(mask_for_global_conf));
}
auto const &buffered_config = *m_buffered_dataset_config;
auto parsed_config = json::parseOptions(
parameters.options, /* considerFiles = */ false);
auto parsed_config =
parameters.compileJSONConfig<json::ParsedConfig>(
writable, *m_handler->jsonMatcher);
if (auto hdf5_config_it = parsed_config.config.find("hdf5");
hdf5_config_it != parsed_config.config.end())
{
Expand Down
18 changes: 18 additions & 0 deletions src/IO/IOTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "openPMD/IO/IOTask.hpp"
#include "openPMD/auxiliary/JSONMatcher.hpp"
#include "openPMD/auxiliary/JSON_internal.hpp"
#include "openPMD/backend/Attributable.hpp"

Expand Down Expand Up @@ -73,6 +74,23 @@ void Parameter<Operation::CREATE_DATASET>::warnUnusedParameters<
}
}

template <>
json::ParsedConfig Parameter<Operation::CREATE_DATASET>::compileJSONConfig(
Writable const *writable, json::JsonMatcher &jsonMatcher) const
{
auto attri = writable->attributable->asInternalCopyOf<Attributable>();
auto path = attri.myPath().openPMDPath();
auto base_config = jsonMatcher.get(path);
auto manual_config =
json::parseOptions(options, /* considerFiles = */ false);
json::merge(base_config.config, manual_config.config);
return json::ParsedConfig{
std::move(base_config.config),
(options.empty() || options == "{}")
? manual_config.originallySpecifiedAs
: base_config.originallySpecifiedAs};
}

namespace internal
{
std::string operationAsString(Operation op)
Expand Down
15 changes: 15 additions & 0 deletions src/auxiliary/JSONMatcher.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "openPMD/auxiliary/JSONMatcher.hpp"
#include "openPMD/Error.hpp"
#include "openPMD/auxiliary/JSON_internal.hpp"

#include <nlohmann/json.hpp>
#include <sstream>

namespace openPMD::json
{
Expand All @@ -27,6 +29,19 @@ namespace
nlohmann::json object) -> void;
} // namespace

Pattern::Pattern(std::string const &pattern_in, nlohmann::json config_in)
: config(std::move(config_in))
{
// transform the regex such that the path to the Iteration is optional
std::stringstream build_pattern;
build_pattern << "(/data/[0-9]+/)?(" << pattern_in << ")";
// we construct the patterns once and use them often, so let's ask for
// some optimization
pattern = std::regex(
build_pattern.str(),
std::regex_constants::egrep | std::regex_constants::optimize);
}

void MatcherPerBackend::init(TracingJSON tracing_config)
{
auto &config = tracing_config.json();
Expand Down

0 comments on commit 1713314

Please sign in to comment.