Skip to content

Commit 5d950f5

Browse files
committed
Basic implementation
1 parent 08ec9ff commit 5d950f5

File tree

3 files changed

+77
-11
lines changed

3 files changed

+77
-11
lines changed

include/openPMD/auxiliary/JSON.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121

2222
#pragma once
2323

24+
#include "openPMD/config.hpp"
25+
26+
#if openPMD_HAVE_MPI
27+
#include <mpi.h>
28+
#endif
29+
2430
#include <string>
2531

2632
namespace openPMD
@@ -61,5 +67,12 @@ namespace json
6167
*/
6268
std::string
6369
merge(std::string const &defaultValue, std::string const &overwrite);
70+
71+
#if openPMD_HAVE_MPI
72+
std::string merge(
73+
std::string const &defaultValue,
74+
std::string const &overwrite,
75+
MPI_Comm);
76+
#endif
6477
} // namespace json
6578
} // namespace openPMD

src/auxiliary/JSON.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,22 @@ merge(nlohmann::json &defaultVal, nlohmann::json const &overwrite)
597597
return defaultVal;
598598
}
599599

600-
std::string merge(std::string const &defaultValue, std::string const &overwrite)
600+
template <typename... MPI_Comm_t>
601+
std::string merge_impl(
602+
std::string const &defaultValue,
603+
std::string const &overwrite,
604+
MPI_Comm_t &&...comm)
601605
{
602-
auto [res, returnFormat] =
603-
parseOptions(defaultValue, /* considerFiles = */ false);
604-
merge(res, parseOptions(overwrite, /* considerFiles = */ false).config);
606+
auto res = parseOptions(
607+
defaultValue,
608+
std::forward<MPI_Comm_t>(comm)...,
609+
/* considerFiles = */ true)
610+
.config;
611+
auto [second, returnFormat] = parseOptions(
612+
overwrite,
613+
std::forward<MPI_Comm_t>(comm)...,
614+
/* considerFiles = */ true);
615+
merge(res, second);
605616
switch (returnFormat)
606617
{
607618
case SupportedLanguages::JSON:
@@ -617,6 +628,21 @@ std::string merge(std::string const &defaultValue, std::string const &overwrite)
617628
throw std::runtime_error("Unreachable!");
618629
}
619630

631+
std::string merge(std::string const &defaultValue, std::string const &overwrite)
632+
{
633+
return merge_impl(defaultValue, overwrite);
634+
}
635+
636+
#if openPMD_HAVE_MPI
637+
std::string merge(
638+
std::string const &defaultValue,
639+
std::string const &overwrite,
640+
MPI_Comm comm)
641+
{
642+
return merge_impl(defaultValue, overwrite, comm);
643+
}
644+
#endif
645+
620646
nlohmann::json &
621647
filterByTemplate(nlohmann::json &defaultVal, nlohmann::json const &positiveMask)
622648
{

src/binding/python/Series.cpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,7 @@ Look for the WriteIterations class for further documentation.
408408
return series;
409409
});
410410

411-
m.def(
412-
"merge_json",
413-
&json::merge,
414-
py::arg("default_value") = "{}",
415-
py::arg("overwrite") = "{}",
416-
R"END(
411+
constexpr char const *docs_merge_json = &R"END(
417412
Merge two JSON/TOML datasets into one.
418413
419414
Merging rules:
@@ -445,5 +440,37 @@ users to overwrite default options, while keeping any other ones.
445440
* returns: The merged dataset, according to the above rules.
446441
If `defaultValue` was a JSON dataset, then as a JSON string,
447442
otherwise as a TOML string.
448-
)END");
443+
)END"[1];
444+
445+
m.def(
446+
"merge_json",
447+
py::overload_cast<std::string const &, std::string const &>(
448+
&json::merge),
449+
py::arg("default_value") = "{}",
450+
py::arg("overwrite") = "{}",
451+
docs_merge_json)
452+
#if openPMD_HAVE_MPI
453+
.def(
454+
"merge_json",
455+
[](std::string const &default_value,
456+
std::string const &overwrite,
457+
py::object &comm) {
458+
auto variant = pythonObjectAsMpiComm(comm);
459+
if (auto errorMsg = std::get_if<std::string>(&variant))
460+
{
461+
throw std::runtime_error("[merge_json] " + *errorMsg);
462+
}
463+
else
464+
{
465+
py::gil_scoped_release release;
466+
return json::merge(
467+
default_value, overwrite, std::get<MPI_Comm>(variant));
468+
}
469+
},
470+
py::arg("default_value") = "{}",
471+
py::arg("overwrite") = "{}",
472+
py::arg("comm"),
473+
docs_merge_json)
474+
#endif
475+
;
449476
}

0 commit comments

Comments
 (0)