Skip to content

Commit

Permalink
Fix: Use explicit ::close() call in WriteIterations
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Feb 20, 2023
1 parent 54c462f commit b9ce527
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
15 changes: 13 additions & 2 deletions include/openPMD/WriteIterations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,16 @@ class Series;
* not possible once it has been closed.
*
*/

namespace internal
{
class SeriesData;
}

class WriteIterations
{
friend class Series;
friend class internal::SeriesData;

private:
using IterationsContainer_t =
Expand All @@ -62,6 +69,7 @@ class WriteIterations
struct SharedResources
{
IterationsContainer_t iterations;
//! Index of the last opened iteration
std::optional<Iteration::IterationIndex_t> currentlyOpen;

SharedResources(IterationsContainer_t);
Expand All @@ -70,8 +78,11 @@ class WriteIterations

WriteIterations(IterationsContainer_t);
explicit WriteIterations() = default;
//! Index of the last opened iteration
std::shared_ptr<SharedResources> shared;
// std::optional so that a single instance is able to close this without
// needing to wait for all instances to deallocate
std::shared_ptr<std::optional<SharedResources>> shared;

void close();

public:
mapped_type &operator[](key_type const &key);
Expand Down
9 changes: 4 additions & 5 deletions src/Series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2235,7 +2235,10 @@ namespace internal
void SeriesData::close()
{
// WriteIterations gets the first shot at flushing
this->m_writeIterations = std::optional<WriteIterations>();
if (this->m_writeIterations.has_value())
{
this->m_writeIterations.value().close();
}
/*
* Scenario: A user calls `Series::flush()` but does not check for
* thrown exceptions. The exception will propagate further up,
Expand All @@ -2251,10 +2254,6 @@ namespace internal
impl.flush();
impl.flushStep(/* doFlush = */ true);
}
if (m_writeIterations.has_value())
{
m_writeIterations = std::optional<WriteIterations>();
}
// Not strictly necessary, but clear the map of iterations
// This releases the openPMD hierarchy
iterations.container().clear();
Expand Down
25 changes: 19 additions & 6 deletions src/WriteIterations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include "openPMD/WriteIterations.hpp"
#include "openPMD/Error.hpp"

#include "openPMD/Series.hpp"

Expand All @@ -45,9 +46,15 @@ WriteIterations::SharedResources::~SharedResources()
}

WriteIterations::WriteIterations(IterationsContainer_t iterations)
: shared{std::make_shared<SharedResources>(std::move(iterations))}
: shared{std::make_shared<std::optional<SharedResources>>(
std::move(iterations))}
{}

void WriteIterations::close()
{
*shared = std::nullopt;
}

WriteIterations::mapped_type &WriteIterations::operator[](key_type const &key)
{
// make a copy
Expand All @@ -56,17 +63,23 @@ WriteIterations::mapped_type &WriteIterations::operator[](key_type const &key)
}
WriteIterations::mapped_type &WriteIterations::operator[](key_type &&key)
{
if (shared->currentlyOpen.has_value())
if (!shared || !shared->has_value())
{
throw error::WrongAPIUsage(
"[WriteIterations] Trying to access after closing Series.");
}
auto &s = shared->value();
if (s.currentlyOpen.has_value())
{
auto lastIterationIndex = shared->currentlyOpen.value();
auto &lastIteration = shared->iterations.at(lastIterationIndex);
auto lastIterationIndex = s.currentlyOpen.value();
auto &lastIteration = s.iterations.at(lastIterationIndex);
if (lastIterationIndex != key && !lastIteration.closed())
{
lastIteration.close();
}
}
shared->currentlyOpen = key;
auto &res = shared->iterations[std::move(key)];
s.currentlyOpen = key;
auto &res = s.iterations[std::move(key)];
if (res.getStepStatus() == StepStatus::NoStep)
{
res.beginStep(/* reread = */ false);
Expand Down

0 comments on commit b9ce527

Please sign in to comment.