Skip to content

Commit 85a560e

Browse files
committed
Basic implementation
1 parent 05d78c1 commit 85a560e

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-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: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,22 @@ merge(nlohmann::json &defaultVal, nlohmann::json const &overwrite)
600600
return defaultVal;
601601
}
602602

603-
std::string merge(std::string const &defaultValue, std::string const &overwrite)
603+
template <typename... MPI_Comm_t>
604+
std::string merge_impl(
605+
std::string const &defaultValue,
606+
std::string const &overwrite,
607+
MPI_Comm_t &&...comm)
604608
{
605-
auto [res, returnFormat] =
606-
parseOptions(defaultValue, /* considerFiles = */ false);
607-
merge(res, parseOptions(overwrite, /* considerFiles = */ false).config);
609+
auto res = parseOptions(
610+
defaultValue,
611+
std::forward<MPI_Comm_t>(comm)...,
612+
/* considerFiles = */ true)
613+
.config;
614+
auto [second, returnFormat] = parseOptions(
615+
overwrite,
616+
std::forward<MPI_Comm_t>(comm)...,
617+
/* considerFiles = */ true);
618+
merge(res, second);
608619
switch (returnFormat)
609620
{
610621
case SupportedLanguages::JSON:
@@ -620,6 +631,19 @@ std::string merge(std::string const &defaultValue, std::string const &overwrite)
620631
throw std::runtime_error("Unreachable!");
621632
}
622633

634+
std::string merge(std::string const &defaultValue, std::string const &overwrite)
635+
{
636+
return merge_impl(defaultValue, overwrite);
637+
}
638+
639+
std::string merge(
640+
std::string const &defaultValue,
641+
std::string const &overwrite,
642+
MPI_Comm comm)
643+
{
644+
return merge_impl(defaultValue, overwrite, comm);
645+
}
646+
623647
nlohmann::json &
624648
filterByTemplate(nlohmann::json &defaultVal, nlohmann::json const &positiveMask)
625649
{

src/binding/python/Series.cpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,7 @@ this method twice.
394394
Look for the WriteIterations class for further documentation.
395395
)END");
396396

397-
m.def(
398-
"merge_json",
399-
&json::merge,
400-
py::arg("default_value") = "{}",
401-
py::arg("overwrite") = "{}",
402-
R"END(
397+
constexpr char const *docs_merge_json = &R"END(
403398
Merge two JSON/TOML datasets into one.
404399
405400
Merging rules:
@@ -431,5 +426,37 @@ users to overwrite default options, while keeping any other ones.
431426
* returns: The merged dataset, according to the above rules.
432427
If `defaultValue` was a JSON dataset, then as a JSON string,
433428
otherwise as a TOML string.
434-
)END");
429+
)END"[1];
430+
431+
m.def(
432+
"merge_json",
433+
py::overload_cast<std::string const &, std::string const &>(
434+
&json::merge),
435+
py::arg("default_value") = "{}",
436+
py::arg("overwrite") = "{}",
437+
docs_merge_json)
438+
#if openPMD_HAVE_MPI
439+
.def(
440+
"merge_json",
441+
[](std::string const &default_value,
442+
std::string const &overwrite,
443+
py::object &comm) {
444+
auto variant = pythonObjectAsMpiComm(comm);
445+
if (auto errorMsg = std::get_if<std::string>(&variant))
446+
{
447+
throw std::runtime_error("[merge_json] " + *errorMsg);
448+
}
449+
else
450+
{
451+
py::gil_scoped_release release;
452+
return json::merge(
453+
default_value, overwrite, std::get<MPI_Comm>(variant));
454+
}
455+
},
456+
py::arg("default_value") = "{}",
457+
py::arg("overwrite") = "{}",
458+
py::arg("comm"),
459+
docs_merge_json)
460+
#endif
461+
;
435462
}

0 commit comments

Comments
 (0)