Skip to content

Commit

Permalink
Experimentally delete iterations after closing
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Nov 21, 2023
1 parent 47da1e9 commit b1b8875
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
6 changes: 6 additions & 0 deletions include/openPMD/Series.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ namespace internal
*/
bool m_wroteAtLeastOneIOStep = false;

bool m_containedAtLeastOneIteration = false;

/**
* Remember the preference that the backend specified for parsing.
* Not used in file-based iteration encoding, empty then.
Expand Down Expand Up @@ -750,6 +752,10 @@ OPENPMD_private
* (We could also add this to the public API some time)
*/
std::optional<std::vector<IterationIndex_t> > currentSnapshot() const;

Iteration resetIteration(IterationIndex_t);
std::pair<decltype(iterations)::iterator, Iteration>
resetIteration(decltype(iterations)::iterator);
}; // Series
} // namespace openPMD

Expand Down
10 changes: 8 additions & 2 deletions src/ReadIterations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,12 @@ std::optional<SeriesIterator *> SeriesIterator::loopBody()
}
}

auto oldIterationIndex = data.currentIteration;

auto guardReturn =
[&series, &iterations](
[&series, &iterations, oldIterationIndex](
auto const &option) -> std::optional<openPMD::SeriesIterator *> {
series.resetIteration(oldIterationIndex);
if (!option.has_value() || *option.value() == end())
{
return option;
Expand Down Expand Up @@ -489,13 +492,15 @@ std::optional<SeriesIterator *> SeriesIterator::loopBody()
{
// we had this iteration already, skip it
iteration.endStep();
series.resetIteration(index);
return std::nullopt; // empty, go into next iteration
}
}
else
{
// we had this iteration already, skip it
series.advance(AdvanceMode::ENDSTEP);
series.resetIteration(index);
return std::nullopt;
}
};
Expand All @@ -514,6 +519,7 @@ std::optional<SeriesIterator *> SeriesIterator::loopBody()
if (series.iterationEncoding() == IterationEncoding::fileBased)
{
// this one is handled above, stream is over once it proceeds to here
series.resetIteration(oldIterationIndex);
this->close();
return {this};
}
Expand All @@ -529,7 +535,7 @@ void SeriesIterator::deactivateDeadIteration(iteration_index_t index)
{
case IterationEncoding::fileBased: {
Parameter<Operation::CLOSE_FILE> param;
Iteration resetted = data.series->iterations[index].resetIteration();
Iteration resetted = data.series->resetIteration(index);
auto writable = &resetted.writable();
param.keep_this_data_alive = std::move(resetted);
data.series->IOHandler()->enqueue(IOTask(writable, std::move(param)));
Expand Down
21 changes: 20 additions & 1 deletion src/Series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ void Series::flushFileBased(
bool flushIOHandler)
{
auto &series = get();
if (end == begin)
if (end == begin && !series.m_containedAtLeastOneIteration)
throw std::runtime_error(
"fileBased output can not be written with no iterations.");

Expand Down Expand Up @@ -2469,6 +2469,25 @@ auto Series::currentSnapshot() const
}
}

Iteration Series::resetIteration(IterationIndex_t idx)
{
auto it = iterations.find(idx);
if (it == iterations.end())
{
return {};
}
return resetIteration(it).second;
}

auto Series::resetIteration(decltype(iterations)::iterator it)
-> std::pair<decltype(iterations)::iterator, Iteration>
{
auto res = it->second;
it = iterations.container().erase(it);
get().m_containedAtLeastOneIteration = true;
return {it, res};
}

namespace
{
CleanedFilename cleanFilename(
Expand Down
6 changes: 6 additions & 0 deletions test/SerialIOTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ void write_and_read_many_iterations(

Series read(
filename, Access::READ_ONLY, "{\"defer_iteration_parsing\": true}");
std::cout << "BEFORE: " << read.iterations.size() << " ITERATIONS"
<< std::endl;
for (auto iteration : read.iterations)
{
iteration.second.open();
Expand All @@ -206,10 +208,14 @@ void write_and_read_many_iterations(
REQUIRE(array[i] == float(i));
}
}
std::cout << "AFTER: " << read.iterations.size() << " ITERATIONS"
<< std::endl;
REQUIRE(read.iterations.size() == 10);
}

Series list(filename, Access::READ_ONLY);
helper::listSeries(list);
REQUIRE(list.iterations.size() == 0);
}

TEST_CASE("write_and_read_many_iterations", "[serial]")
Expand Down

0 comments on commit b1b8875

Please sign in to comment.