Skip to content

Commit

Permalink
Fully replace WriteIterations class with the new one
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Jan 31, 2024
1 parent a905c13 commit 67842ca
Show file tree
Hide file tree
Showing 17 changed files with 189 additions and 307 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ set(CORE_SOURCE
src/RecordComponent.cpp
src/Series.cpp
src/version.cpp
src/WriteIterations.cpp
src/auxiliary/Date.cpp
src/auxiliary/Filesystem.cpp
src/auxiliary/JSON.cpp
Expand Down
4 changes: 1 addition & 3 deletions include/openPMD/Iteration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ class Iteration : public Attributable
template <typename T, typename T_key, typename T_container>
friend class Container;
friend class Series;
friend class WriteIterations;
friend class StatefulIterator;
friend class StatefulSnapshotsContainer;

Expand Down Expand Up @@ -439,18 +438,17 @@ inline T Iteration::dt() const
class IndexedIteration : public Iteration
{
friend class StatefulIterator;
friend class WriteIterations;
friend class LegacyIteratorAdaptor;

public:
using index_t = Iteration::IterationIndex_t;
index_t const iterationIndex;

private:
inline IndexedIteration(std::pair<index_t const, Iteration> pair)
: Iteration(std::move(pair.second)), iterationIndex(pair.first)
{}

private:
template <typename Iteration_t>
IndexedIteration(Iteration_t &&it, index_t index)
: Iteration(std::forward<Iteration_t>(it)), iterationIndex(index)
Expand Down
10 changes: 0 additions & 10 deletions include/openPMD/Series.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "openPMD/Iteration.hpp"
#include "openPMD/IterationEncoding.hpp"
#include "openPMD/Streaming.hpp"
#include "openPMD/WriteIterations.hpp"
#include "openPMD/auxiliary/Variant.hpp"
#include "openPMD/backend/Attributable.hpp"
#include "openPMD/backend/Container.hpp"
Expand Down Expand Up @@ -88,14 +87,6 @@ namespace internal
using IterationsContainer_t = Container<Iteration, IterationIndex_t>;
IterationsContainer_t iterations{};

/**
* For each instance of Series, there is only one instance
* of WriteIterations, stored in this Option.
* This ensures that Series::writeIteration() always returns
* the same instance.
*/
std::optional<WriteIterations> m_writeIterations;

/**
* Series::readIterations() returns an iterator type that modifies the
* state of the Series (by proceeding through IO steps).
Expand Down Expand Up @@ -216,7 +207,6 @@ class Series : public Attributable
friend class ReadIterations;
friend class StatefulIterator;
friend class internal::SeriesData;
friend class WriteIterations;
friend class StatefulSnapshotsContainer;

public:
Expand Down
109 changes: 0 additions & 109 deletions include/openPMD/WriteIterations.hpp

This file was deleted.

1 change: 0 additions & 1 deletion include/openPMD/backend/Attributable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ class Attributable
friend class Iteration;
friend class Series;
friend class Writable;
friend class WriteIterations;
friend class StatefulSnapshotsContainer;

protected:
Expand Down
1 change: 0 additions & 1 deletion include/openPMD/openPMD.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ namespace openPMD
#include "openPMD/RecordComponent.hpp"
#include "openPMD/Series.hpp"
#include "openPMD/UnitDimension.hpp"
#include "openPMD/WriteIterations.hpp"
#include "openPMD/snapshots/StatefulIterator.hpp"

#include "openPMD/backend/Attributable.hpp"
Expand Down
4 changes: 4 additions & 0 deletions include/openPMD/snapshots/ContainerImpls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "openPMD/snapshots/ContainerTraits.hpp"
#include "openPMD/snapshots/RandomAccessIterator.hpp"
#include "openPMD/snapshots/StatefulIterator.hpp"
#include <optional>

namespace openPMD
{
Expand All @@ -19,6 +20,9 @@ class StatefulSnapshotsContainer : public AbstractSnapshotsContainer
auto get() const -> StatefulIterator const *;

public:
using AbstractSnapshotsContainer::currentIteration;
auto currentIteration() const -> std::optional<value_type const *> override;

auto begin() -> iterator override;
auto end() -> iterator override;
auto begin() const -> const_iterator override;
Expand Down
3 changes: 3 additions & 0 deletions include/openPMD/snapshots/ContainerTraits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class AbstractSnapshotsContainer
using reverse_iterator = OpaqueSeriesIterator<value_type>;
using const_reverse_iterator = OpaqueSeriesIterator<value_type const>;

virtual auto currentIteration() -> std::optional<value_type *>;
virtual auto currentIteration() const -> std::optional<value_type const *>;

virtual auto begin() -> iterator = 0;
virtual auto begin() const -> const_iterator = 0;
virtual auto end() -> iterator = 0;
Expand Down
9 changes: 9 additions & 0 deletions include/openPMD/snapshots/Snapshots.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class Snapshots

Snapshots(std::shared_ptr<AbstractSnapshotsContainer> snapshots);

inline auto get() const -> AbstractSnapshotsContainer const &;
inline auto get() -> AbstractSnapshotsContainer &;

public:
using key_type = AbstractSnapshotsContainer::key_type;
using value_type = AbstractSnapshotsContainer::value_type;
Expand All @@ -50,6 +53,9 @@ class Snapshots
using const_reverse_iterator =
AbstractSnapshotsContainer::const_reverse_iterator;

auto currentIteration() -> std::optional<value_type *>;
auto currentIteration() const -> std::optional<value_type const *>;

iterator begin();
iterator end();
const_iterator begin() const;
Expand All @@ -66,4 +72,7 @@ class Snapshots

mapped_type &operator[](key_type const &key);
};

// backwards compatibility
using WriteIterations = Snapshots;
} // namespace openPMD
12 changes: 11 additions & 1 deletion include/openPMD/snapshots/StatefulIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@

namespace openPMD
{
namespace internal
{
class SeriesData;
}
class StatefulIterator
: public AbstractSeriesIterator<
StatefulIterator,
Container<Iteration, Iteration::IterationIndex_t>::value_type>
{
friend class StatefulSnapshotsContainer;
friend class Series;
friend class internal::SeriesData;

using iteration_index_t = IndexedIteration::index_t;

Expand All @@ -52,6 +58,8 @@ class StatefulIterator
SharedData &operator=(SharedData const &) = delete;
SharedData &operator=(SharedData &&) = delete;

~SharedData();

Series series;
std::deque<iteration_index_t> iterationsInCurrentStep;
// nullopt <-> currently out of step
Expand Down Expand Up @@ -141,7 +149,9 @@ class StatefulIterator

auto setCurrentIteration() -> bool;
auto peekCurrentIteration() -> std::optional<uint64_t>;
auto peekCurrentlyOpenIteration() -> std::optional<IndexedIteration>;
auto peekCurrentlyOpenIteration() const
-> std::optional<value_type const *>;
auto peekCurrentlyOpenIteration() -> std::optional<value_type *>;
};

class LegacyIteratorAdaptor
Expand Down
74 changes: 39 additions & 35 deletions src/Series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2282,9 +2282,9 @@ namespace internal
void SeriesData::close()
{
// WriteIterations gets the first shot at flushing
if (this->m_writeIterations.has_value())
if (this->m_sharedReadIterations)
{
this->m_writeIterations.value().close();
this->m_sharedReadIterations->close();
}
/*
* Scenario: A user calls `Series::flush()` but does not check for
Expand Down Expand Up @@ -2395,6 +2395,38 @@ namespace
};
}

namespace
{
auto make_writing_stateful_iterator(
Series copied_series, internal::SeriesData &series)
-> std::function<StatefulIterator *()>
{
if (!series.m_sharedReadIterations)
{
series.m_sharedReadIterations = std::make_unique<StatefulIterator>(
StatefulIterator::tag_write, std::move(copied_series));
}
return [ptr = series.m_sharedReadIterations.get()]() { return ptr; };
}
auto make_reading_stateful_iterator(
Series copied_series, internal::SeriesData &series)
-> std::function<StatefulIterator *()>
{
return [s = std::move(copied_series), &series]() mutable {
if (!series.m_sharedReadIterations)
{
auto parse_preference = series.m_parsePreference;
series.m_sharedReadIterations =
std::make_unique<StatefulIterator>(
StatefulIterator::tag_read,
std::move(s),
parse_preference);
}
return series.m_sharedReadIterations.get();
};
}
} // namespace

Snapshots Series::snapshots()
{
auto &series = get();
Expand Down Expand Up @@ -2447,43 +2479,17 @@ Snapshots Series::snapshots()
new RandomAccessIteratorContainer(series.iterations)});
}
case IteratorKind::Stateful: {
// Use private constructor instead of copy constructor to avoid
// object slicing
Series copied_series;
copied_series.setData(
std::dynamic_pointer_cast<internal::SeriesData>(this->m_attri));
std::function<StatefulIterator *()> begin;

// @todo: distinguish read/write access
if (access::write(IOHandler()->m_frontendAccess))
{
if (!series.m_sharedReadIterations)
{
series.m_sharedReadIterations =
std::make_unique<StatefulIterator>(
StatefulIterator::tag_write, std::move(copied_series));
}
begin = [ptr = series.m_sharedReadIterations.get()]() {
return ptr;
};
begin = make_writing_stateful_iterator(*this, series);
}
else
{
begin = [s = std::move(copied_series)]() mutable {
auto &series_data = s.get();
if (!series_data.m_sharedReadIterations)
{
auto parse_preference = series_data.m_parsePreference;
series_data.m_sharedReadIterations =
std::make_unique<StatefulIterator>(
StatefulIterator::tag_read,
std::move(s),
parse_preference);
}
return series_data.m_sharedReadIterations.get();
};
begin = make_reading_stateful_iterator(*this, series);
}

return Snapshots(std::shared_ptr<StatefulSnapshotsContainer>(
new StatefulSnapshotsContainer(std::move(begin))));
}
Expand All @@ -2499,11 +2505,9 @@ void Series::parseBase()
WriteIterations Series::writeIterations()
{
auto &series = get();
if (!series.m_writeIterations.has_value())
{
series.m_writeIterations = WriteIterations(this->iterations);
}
return series.m_writeIterations.value();
auto begin = make_writing_stateful_iterator(*this, series);
return Snapshots(std::shared_ptr<StatefulSnapshotsContainer>(
new StatefulSnapshotsContainer(std::move(begin))));
}

void Series::close()
Expand Down
Loading

0 comments on commit 67842ca

Please sign in to comment.