Skip to content

Commit

Permalink
Fix Attributable::containingIteration()
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Dec 18, 2024
1 parent 957282c commit 41d950a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
10 changes: 6 additions & 4 deletions include/openPMD/backend/Attributable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#pragma once

#include "openPMD/IO/AbstractIOHandler.hpp"
#include "openPMD/IterationEncoding.hpp"
#include "openPMD/ThrowError.hpp"
#include "openPMD/auxiliary/OutOfRangeMsg.hpp"
#include "openPMD/backend/Attribute.hpp"
Expand Down Expand Up @@ -398,10 +399,11 @@ OPENPMD_protected
* Throws an error otherwise, e.g., for Series objects.
* @{
*/
[[nodiscard]] auto containingIteration() const -> std::pair<
std::optional<internal::IterationData const *>,
internal::SeriesData const *>;
auto containingIteration() -> std::
[[nodiscard]] auto containingIteration(IterationEncoding ie) const
-> std::pair<
std::optional<internal::IterationData const *>,
internal::SeriesData const *>;
auto containingIteration(IterationEncoding ie) -> std::
pair<std::optional<internal::IterationData *>, internal::SeriesData *>;
/** @} */

Expand Down
25 changes: 16 additions & 9 deletions src/backend/Attributable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "openPMD/backend/Attributable.hpp"
#include "openPMD/IO/AbstractIOHandler.hpp"
#include "openPMD/Iteration.hpp"
#include "openPMD/IterationEncoding.hpp"
#include "openPMD/ParticleSpecies.hpp"
#include "openPMD/RecordComponent.hpp"
#include "openPMD/Series.hpp"
Expand Down Expand Up @@ -150,19 +151,19 @@ Series Attributable::retrieveSeries() const
return findSeries->attributable->asInternalCopyOf<Series>();
}

auto Attributable::containingIteration() const -> std::pair<
auto Attributable::containingIteration(IterationEncoding ie) const -> std::pair<
std::optional<internal::IterationData const *>,
internal::SeriesData const *>
{
constexpr size_t search_queue_size = 3;
Writable const *search_queue[search_queue_size]{nullptr};
internal::AttributableData const *search_queue[search_queue_size]{nullptr};
size_t search_queue_idx = 0;
Writable const *findSeries = &writable();
internal::AttributableData const *findSeries = m_attri.get();
while (true)
{
search_queue[search_queue_idx] = findSeries;
// we don't need to push the last Writable since it's the Series anyway
findSeries = findSeries->parent;
findSeries = findSeries->frontend_parent;
if (!findSeries)
{
break;
Expand All @@ -174,15 +175,21 @@ auto Attributable::containingIteration() const -> std::pair<
}
// End of the queue:
// Iteration -> Series.iterations -> Series
// in variable-based encoding, Iteration and Series.iterations is the same
// thing, hence:
// Iteration -> Series
size_t distance_to_iteration =
ie == IterationEncoding::variableBased ? 1 : 2;
auto *series = &auxiliary::deref_dynamic_cast<internal::SeriesData const>(
search_queue[search_queue_idx]->attributable);
search_queue[search_queue_idx]);
auto maybe_iteration = search_queue
[(search_queue_idx + (search_queue_size - 2)) % search_queue_size];
[(search_queue_idx + (search_queue_size - distance_to_iteration)) %
search_queue_size];
if (maybe_iteration)
{
auto *iteration =
&auxiliary::deref_dynamic_cast<internal::IterationData const>(
maybe_iteration->attributable);
maybe_iteration);
return std::make_pair(std::make_optional(iteration), series);
}
else
Expand All @@ -191,11 +198,11 @@ auto Attributable::containingIteration() const -> std::pair<
}
}

auto Attributable::containingIteration() -> std::
auto Attributable::containingIteration(IterationEncoding ie) -> std::
pair<std::optional<internal::IterationData *>, internal::SeriesData *>
{
auto const_res =
static_cast<Attributable const *>(this)->containingIteration();
static_cast<Attributable const *>(this)->containingIteration(ie);
return std::make_pair(
const_res.first.has_value()
? std::make_optional(
Expand Down
3 changes: 2 additions & 1 deletion src/backend/BaseRecordComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ ChunkTable BaseRecordComponent::availableChunks()
Offset offset(rc.m_dataset.value().extent.size(), 0);
return ChunkTable{{std::move(offset), rc.m_dataset.value().extent}};
}
if (auto iteration_data = containingIteration().first;
if (auto iteration_data =
containingIteration(IOHandler()->m_encoding).first;
iteration_data.has_value())
{
(*iteration_data)->asInternalCopyOf<Iteration>().open();
Expand Down
3 changes: 2 additions & 1 deletion src/backend/Writable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ void Writable::seriesFlush(internal::FlushParams const &flushParams)
{
Attributable impl;
impl.setData({attributable, [](auto const *) {}});
auto [iteration_internal, series_internal] = impl.containingIteration();
auto [iteration_internal, series_internal] =
impl.containingIteration(impl.IOHandler()->m_encoding);
if (iteration_internal)
{
(*iteration_internal)->asInternalCopyOf<Iteration>().touch();
Expand Down

0 comments on commit 41d950a

Please sign in to comment.