Skip to content

Commit

Permalink
Cache the Series per thread
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Jul 23, 2024
1 parent 74fdc47 commit 57be2d4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
35 changes: 32 additions & 3 deletions include/openPMD/binding/python/Pickle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,38 @@ add_pickle(pybind11::class_<T_Args...> &cl, T_SeriesAccessor &&seriesAccessor)
std::vector<std::string> const group =
t[1].cast<std::vector<std::string> >();

openPMD::Series series(
filename, Access::READ_ONLY, "defer_iteration_parsing = true");
return seriesAccessor(std::move(series), group);
/*
* Cache the Series per thread.
*/
thread_local std::optional<openPMD::Series> series;
bool re_initialize = [&]() {
try
{
return !series.has_value() || !series->operator bool() ||
series->myPath().filePath() != filename;
}
/*
* Better safe than sorry, if anything goes wrong because the
* Series is in a weird state, just reinitialize it.
*/
catch (...)
{
return true;
}
}();
if (re_initialize)
{
/*
* Do NOT close the old Series, it might still be active in
* terms of handed-out handles.
*/
series = std::make_optional<Series>(
filename,
Access::READ_ONLY,
"defer_iteration_parsing = true");
}

return seriesAccessor(*series, group);
}));
}
} // namespace openPMD
4 changes: 4 additions & 0 deletions test/SerialIOTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ void write_and_read_many_iterations(

{
Series write(filename, Access::CREATE);
REQUIRE(write.myPath().filePath() == filename);
for (unsigned int i = 0; i < nIterations; ++i)
{
// std::cout << "Putting iteration " << i << std::endl;
Expand Down Expand Up @@ -1850,6 +1851,9 @@ inline void fileBased_write_test(const std::string &backend)
"../samples/subdir/serial_fileBased_write%03T." + backend,
Access::CREATE,
jsonCfg);
REQUIRE(
o.myPath().filePath() ==
("../samples/subdir/serial_fileBased_write%03T." + backend));

ParticleSpecies &e_1 = o.iterations[1].particles["e"];

Expand Down

0 comments on commit 57be2d4

Please sign in to comment.