Skip to content

Commit

Permalink
Basic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Jul 19, 2024
1 parent 7cfa859 commit f7b78d8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
13 changes: 13 additions & 0 deletions include/openPMD/auxiliary/JSON.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

#pragma once

#include "openPMD/config.hpp"

#if openPMD_HAVE_MPI
#include <mpi.h>
#endif

#include <string>

namespace openPMD
Expand Down Expand Up @@ -61,5 +67,12 @@ namespace json
*/
std::string
merge(std::string const &defaultValue, std::string const &overwrite);

#if openPMD_HAVE_MPI
std::string merge(
std::string const &defaultValue,
std::string const &overwrite,
MPI_Comm);
#endif
} // namespace json
} // namespace openPMD
34 changes: 30 additions & 4 deletions src/auxiliary/JSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,22 @@ merge(nlohmann::json &defaultVal, nlohmann::json const &overwrite)
return defaultVal;
}

std::string merge(std::string const &defaultValue, std::string const &overwrite)
template <typename... MPI_Comm_t>
std::string merge_impl(
std::string const &defaultValue,
std::string const &overwrite,
MPI_Comm_t &&...comm)
{
auto [res, returnFormat] =
parseOptions(defaultValue, /* considerFiles = */ false);
merge(res, parseOptions(overwrite, /* considerFiles = */ false).config);
auto res = parseOptions(
defaultValue,
std::forward<MPI_Comm_t>(comm)...,
/* considerFiles = */ true)
.config;
auto [second, returnFormat] = parseOptions(
overwrite,
std::forward<MPI_Comm_t>(comm)...,
/* considerFiles = */ true);
merge(res, second);
switch (returnFormat)
{
case SupportedLanguages::JSON:
Expand All @@ -620,6 +631,21 @@ std::string merge(std::string const &defaultValue, std::string const &overwrite)
throw std::runtime_error("Unreachable!");
}

std::string merge(std::string const &defaultValue, std::string const &overwrite)
{
return merge_impl(defaultValue, overwrite);
}

#if openPMD_HAVE_MPI
std::string merge(
std::string const &defaultValue,
std::string const &overwrite,
MPI_Comm comm)
{
return merge_impl(defaultValue, overwrite, comm);
}
#endif

nlohmann::json &
filterByTemplate(nlohmann::json &defaultVal, nlohmann::json const &positiveMask)
{
Expand Down
41 changes: 34 additions & 7 deletions src/binding/python/Series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,7 @@ this method twice.
Look for the WriteIterations class for further documentation.
)END");

m.def(
"merge_json",
&json::merge,
py::arg("default_value") = "{}",
py::arg("overwrite") = "{}",
R"END(
constexpr char const *docs_merge_json = &R"END(
Merge two JSON/TOML datasets into one.
Merging rules:
Expand Down Expand Up @@ -431,5 +426,37 @@ users to overwrite default options, while keeping any other ones.
* returns: The merged dataset, according to the above rules.
If `defaultValue` was a JSON dataset, then as a JSON string,
otherwise as a TOML string.
)END");
)END"[1];

m.def(
"merge_json",
py::overload_cast<std::string const &, std::string const &>(
&json::merge),
py::arg("default_value") = "{}",
py::arg("overwrite") = "{}",
docs_merge_json)
#if openPMD_HAVE_MPI
.def(
"merge_json",
[](std::string const &default_value,
std::string const &overwrite,
py::object &comm) {
auto variant = pythonObjectAsMpiComm(comm);
if (auto errorMsg = std::get_if<std::string>(&variant))
{
throw std::runtime_error("[merge_json] " + *errorMsg);
}
else
{
py::gil_scoped_release release;
return json::merge(
default_value, overwrite, std::get<MPI_Comm>(variant));
}
},
py::arg("default_value") = "{}",
py::arg("overwrite") = "{}",
py::arg("comm"),
docs_merge_json)
#endif
;
}

0 comments on commit f7b78d8

Please sign in to comment.