Skip to content

Commit

Permalink
Reading changes: Use snapshot attribute
Browse files Browse the repository at this point in the history
This means that the snapshot attribute, if present, is used for
accessing iterations inside `series.readIterations()`. Fallback to the
old behavior (linear progression through iterations) if the attribute is
not found.
  • Loading branch information
franzpoeschel committed Jan 4, 2022
1 parent c645f8a commit d2672ae
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 65 deletions.
45 changes: 41 additions & 4 deletions include/openPMD/Iteration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "openPMD/ParticleSpecies.hpp"
#include "openPMD/Streaming.hpp"

#include <deque>
#include <tuple>

namespace openPMD
{
Expand Down Expand Up @@ -271,23 +273,58 @@ class Iteration : public Attributable
void read_impl( std::string const & groupPath );


/**
* Status after beginning an IO step. Currently includes:
* * The advance status (OK, OVER, RANDOMACCESS)
* * The opened iterations, in case the snapshot attribute is found
*/
struct BeginStepStatus
{
using AvailableIterations_t =
auxiliary::Option< std::deque< uint64_t > >;

AdvanceStatus stepStatus{};
/*
* If the iteration attribute `snapshot` is present, the value of that
* attribute. Otherwise empty.
*/
AvailableIterations_t iterationsInOpenedStep;

/*
* Most of the time, the AdvanceStatus part of this struct is what we
* need, so let's make it easy to access.
*/
inline operator AdvanceStatus() const
{
return stepStatus;
}

/*
* Support for std::tie()
*/
inline
operator std::tuple< AdvanceStatus &, AvailableIterations_t & >()
{
return std::tuple< AdvanceStatus &, AvailableIterations_t & >{
stepStatus, iterationsInOpenedStep };
}
};

/**
* @brief Begin an IO step on the IO file (or file-like object)
* containing this iteration. In case of group-based iteration
* layout, this will be the complete Series.
*
* @return AdvanceStatus
* @return BeginStepStatus
*/
AdvanceStatus
beginStep();
BeginStepStatus beginStep();

/**
* @brief End an IO step on the IO file (or file-like object)
* containing this iteration. In case of group-based iteration
* layout, this will be the complete Series.
*
* @return AdvanceStatus
* @return void
*/
void
endStep();
Expand Down
21 changes: 20 additions & 1 deletion include/openPMD/ReadIterations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#include "openPMD/Iteration.hpp"
#include "openPMD/Series.hpp"

#include <deque>
#include <iostream>

namespace openPMD
{
/**
Expand Down Expand Up @@ -54,7 +57,8 @@ class SeriesIterator
using maybe_series_t = auxiliary::Option< Series >;

maybe_series_t m_series;
iteration_index_t m_currentIteration = 0;
std::deque< iteration_index_t > m_iterationsInCurrentStep;
uint64_t m_currentIteration{};

public:
//! construct the end() iterator
Expand All @@ -71,6 +75,21 @@ class SeriesIterator
bool operator!=( SeriesIterator const & other ) const;

static SeriesIterator end();

private:
inline bool setCurrentIteration()
{
if( m_iterationsInCurrentStep.empty() )
{
std::cerr << "[ReadIterations] Encountered a step without "
"iterations. Closing the Series."
<< std::endl;
*this = end();
return false;
}
m_currentIteration = *m_iterationsInCurrentStep.begin();
return true;
}
};

/**
Expand Down
6 changes: 5 additions & 1 deletion include/openPMD/Series.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
# include <mpi.h>
#endif

#include <deque>
#include <map>
#include <set>
#include <string>
Expand Down Expand Up @@ -510,8 +511,11 @@ class Series : public Attributable
* Note on re-parsing of a Series:
* If init == false, the parsing process will seek for new
* Iterations/Records/Record Components etc.
* If series.iterations contains the attribute `snapshot`, returns its
* value.
*/
void readGorVBased( bool init = true );
auxiliary::Option< std::deque< uint64_t > >
readGorVBased( bool init = true );
void readBase();
std::string iterationFilename( uint64_t i );

Expand Down
20 changes: 14 additions & 6 deletions src/Iteration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,9 @@ void Iteration::read_impl( std::string const & groupPath )
readAttributes( ReadMode::FullyReread );
}

AdvanceStatus
Iteration::beginStep()
auto Iteration::beginStep() -> BeginStepStatus
{
BeginStepStatus res;
using IE = IterationEncoding;
auto series = retrieveSeries();
// Initialize file with this to quiet warnings
Expand All @@ -556,11 +556,17 @@ Iteration::beginStep()
file = &series.get();
break;
}

AdvanceStatus status = series.advance(
AdvanceMode::BEGINSTEP, *file, series.indexOf( *this ), *this );
if( status != AdvanceStatus::OK )
switch( status )
{
return status;
case AdvanceStatus::OVER:
res.stepStatus = status;
return res;
case AdvanceStatus::OK:
case AdvanceStatus::RANDOMACCESS:
break;
}

// re-read -> new datasets might be available
Expand All @@ -575,12 +581,13 @@ Iteration::beginStep()
auto newType =
const_cast< Access * >( &this->IOHandler()->m_frontendAccess );
*newType = Access::READ_WRITE;
series.readGorVBased( false );
res.iterationsInOpenedStep = series.readGorVBased( false );
*newType = oldType;
series.iterations.written() = previous;
}

return status;
res.stepStatus = status;
return res;
}

void
Expand All @@ -604,6 +611,7 @@ Iteration::endStep()
// @todo filebased check
series.advance(
AdvanceMode::ENDSTEP, *file, series.indexOf( *this ), *this );
series.get().m_currentlyActiveIterations.clear();
}

StepStatus
Expand Down
Loading

0 comments on commit d2672ae

Please sign in to comment.