Skip to content

Commit d51f5f6

Browse files
committed
Reading changes: Use snapshot attribute
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.
1 parent 86acee4 commit d51f5f6

File tree

6 files changed

+262
-63
lines changed

6 files changed

+262
-63
lines changed

include/openPMD/Iteration.hpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "openPMD/ParticleSpecies.hpp"
3030
#include "openPMD/Streaming.hpp"
3131

32+
#include <deque>
33+
#include <tuple>
3234

3335
namespace openPMD
3436
{
@@ -272,15 +274,38 @@ class Iteration : public Attributable
272274

273275

274276

277+
struct BeginStepStatus
278+
{
279+
using AvailableIterations_t =
280+
auxiliary::Option< std::deque< uint64_t > >;
281+
282+
AdvanceStatus stepStatus{};
283+
/*
284+
* If the iteration attribute `snapshot` is present, the value of that
285+
* attribute. Otherwise empty.
286+
*/
287+
AvailableIterations_t iterationsInOpenedStep;
288+
289+
inline operator AdvanceStatus() const
290+
{
291+
return stepStatus;
292+
}
293+
inline
294+
operator std::tuple< AdvanceStatus &, AvailableIterations_t & >()
295+
{
296+
return std::tuple< AdvanceStatus &, AvailableIterations_t & >{
297+
stepStatus, iterationsInOpenedStep };
298+
}
299+
};
300+
275301
/**
276302
* @brief Begin an IO step on the IO file (or file-like object)
277303
* containing this iteration. In case of group-based iteration
278304
* layout, this will be the complete Series.
279305
*
280306
* @return AdvanceStatus
281307
*/
282-
AdvanceStatus
283-
beginStep();
308+
BeginStepStatus beginStep();
284309

285310
/**
286311
* @brief End an IO step on the IO file (or file-like object)

include/openPMD/ReadIterations.hpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include "openPMD/Iteration.hpp"
2424
#include "openPMD/Series.hpp"
2525

26+
#include <deque>
27+
#include <iostream>
28+
2629
namespace openPMD
2730
{
2831
/**
@@ -54,7 +57,8 @@ class SeriesIterator
5457
using maybe_series_t = auxiliary::Option< Series >;
5558

5659
maybe_series_t m_series;
57-
iteration_index_t m_currentIteration = 0;
60+
std::deque< iteration_index_t > m_iterationsInCurrentStep;
61+
uint64_t m_currentIteration{};
5862

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

7377
static SeriesIterator end();
78+
79+
private:
80+
inline bool setCurrentIteration()
81+
{
82+
if( m_iterationsInCurrentStep.empty() )
83+
{
84+
std::cerr << "[ReadIterations] Encountered a step without "
85+
"iterations. Closing the Series."
86+
<< std::endl;
87+
*this = end();
88+
return false;
89+
}
90+
m_currentIteration = *m_iterationsInCurrentStep.begin();
91+
return true;
92+
}
7493
};
7594

7695
/**

include/openPMD/Series.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
# include <mpi.h>
4242
#endif
4343

44+
#include <deque>
4445
#include <map>
4546
#include <set>
4647
#include <string>
@@ -510,8 +511,11 @@ class Series : public Attributable
510511
* Note on re-parsing of a Series:
511512
* If init == false, the parsing process will seek for new
512513
* Iterations/Records/Record Components etc.
514+
* If series.iterations contains the attribute `snapshot`, returns its
515+
* value (will only be true in variable-based iteration encoding).
513516
*/
514-
void readGorVBased( bool init = true );
517+
auxiliary::Option< std::deque< uint64_t > >
518+
readGorVBased( bool init = true );
515519
void readBase();
516520
std::string iterationFilename( uint64_t i );
517521

src/Iteration.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,9 @@ void Iteration::read_impl( std::string const & groupPath )
538538
readAttributes( ReadMode::FullyReread );
539539
}
540540

541-
AdvanceStatus
542-
Iteration::beginStep()
541+
auto Iteration::beginStep() -> BeginStepStatus
543542
{
543+
BeginStepStatus res;
544544
using IE = IterationEncoding;
545545
auto series = retrieveSeries();
546546
// Initialize file with this to quiet warnings
@@ -556,11 +556,17 @@ Iteration::beginStep()
556556
file = &series.get();
557557
break;
558558
}
559+
559560
AdvanceStatus status = series.advance(
560561
AdvanceMode::BEGINSTEP, *file, series.indexOf( *this ), *this );
561-
if( status != AdvanceStatus::OK )
562+
switch( status )
562563
{
563-
return status;
564+
case AdvanceStatus::OVER:
565+
res.stepStatus = status;
566+
return res;
567+
case AdvanceStatus::OK:
568+
case AdvanceStatus::RANDOMACCESS:
569+
break;
564570
}
565571

566572
// re-read -> new datasets might be available
@@ -575,12 +581,13 @@ Iteration::beginStep()
575581
auto newType =
576582
const_cast< Access * >( &this->IOHandler()->m_frontendAccess );
577583
*newType = Access::READ_WRITE;
578-
series.readGorVBased( false );
584+
res.iterationsInOpenedStep = series.readGorVBased( false );
579585
*newType = oldType;
580586
series.iterations.written() = previous;
581587
}
582588

583-
return status;
589+
res.stepStatus = status;
590+
return res;
584591
}
585592

586593
void
@@ -604,6 +611,7 @@ Iteration::endStep()
604611
// @todo filebased check
605612
series.advance(
606613
AdvanceMode::ENDSTEP, *file, series.indexOf( *this ), *this );
614+
series.get().m_currentlyActiveIterations.clear();
607615
}
608616

609617
StepStatus

0 commit comments

Comments
 (0)