Skip to content

Commit 8df6ebb

Browse files
committed
Add reviewer's suggestions
Also fix a bug in group-based mode
1 parent 9a702f2 commit 8df6ebb

11 files changed

Lines changed: 127 additions & 65 deletions

File tree

include/openPMD/Iteration.hpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ class Iteration : public Attributable
116116
* Background: Upon calling Iteration::close(), the openPMD API
117117
* will add metadata to the iteration in form of an attribute,
118118
* indicating that the iteration has indeed been closed.
119-
* Useful mainly in streaming context.
119+
* Useful mainly in streaming context when a reader inquires from
120+
* a writer that it is done writing.
120121
*
121122
* @return Whether the iteration has been explicitly closed (yet) by the
122123
* writer.
@@ -136,11 +137,17 @@ class Iteration : public Attributable
136137
void flush();
137138
void read();
138139

140+
/**
141+
* @brief Whether an iteration has been closed yet.
142+
*
143+
*/
139144
enum class CloseStatus
140145
{
141-
Open,
142-
ClosedInFrontend,
143-
ClosedInBackend
146+
Open, //!< Iteration has not been closed
147+
ClosedInFrontend, /*!< Iteration has been closed, but task has not yet
148+
been propagated to the backend */
149+
ClosedInBackend /*!< Iteration has been closed and task has been
150+
propagated to the backend */
144151
};
145152

146153
/*
@@ -152,14 +159,16 @@ class Iteration : public Attributable
152159
std::shared_ptr< CloseStatus > m_closed =
153160
std::make_shared< CloseStatus >( CloseStatus::Open );
154161

155-
/**
156-
* @brief Verify that a closed iteration has not been wrongly accessed.
162+
/*
163+
* @brief Check recursively whether this Iteration is dirty.
164+
* It is dirty if any attribute or dataset is read from or written to
165+
* the backend.
157166
*
158-
* @return true If closed iteration had no wrong accesses.
167+
* @return true If dirty.
159168
* @return false Otherwise.
160169
*/
161170
bool
162-
verifyClosed() const;
171+
dirtyRecursive() const;
163172

164173
virtual void linkHierarchy(std::shared_ptr< Writable > const& w);
165174
}; // Iteration

include/openPMD/Mesh.hpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,6 @@ class Mesh : public BaseRecord< MeshRecordComponent >
182182

183183
void flush_impl(std::string const&) override;
184184
void read() override;
185-
186-
/**
187-
* @brief Verify that a mesh in a closed iteration has not
188-
* been wrongly accessed.
189-
*
190-
* @return true If closed iteration had no wrong accesses.
191-
* @return false Otherwise.
192-
*/
193185
}; // Mesh
194186

195187
template< typename T >

include/openPMD/ParticleSpecies.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ class ParticleSpecies : public Container< Record >
4747
void flush(std::string const &) override;
4848

4949
/**
50-
* @brief Verify that a particle species in a closed iteration has not
51-
* been wrongly accessed.
50+
* @brief Check recursively whether this ParticleSpecies is dirty.
51+
* It is dirty if any attribute or dataset is read from or written to
52+
* the backend.
5253
*
53-
* @return true If closed iteration had no wrong accesses.
54+
* @return true If dirty.
5455
* @return false Otherwise.
5556
*/
5657
bool
57-
verifyClosed() const;
58+
dirtyRecursive() const;
5859
};
5960

6061
namespace traits

include/openPMD/RecordComponent.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,15 @@ class RecordComponent : public BaseRecordComponent
200200
RecordComponent& makeEmpty( Dataset d );
201201

202202
/**
203-
* @brief Verify that a record component in a closed iteration has not
204-
* been wrongly accessed.
203+
* @brief Check recursively whether this RecordComponent is dirty.
204+
* It is dirty if any attribute or dataset is read from or written to
205+
* the backend.
205206
*
206-
* @return true If closed iteration had no wrong accesses.
207+
* @return true If dirty.
207208
* @return false Otherwise.
208209
*/
209210
bool
210-
verifyClosed() const;
211+
dirtyRecursive() const;
211212
}; // RecordComponent
212213

213214

include/openPMD/backend/BaseRecord.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,15 @@ class BaseRecord : public Container< T_elem >
9999
virtual void read() = 0;
100100

101101
/**
102-
* @brief Verify that a base record in a closed iteration has not
103-
* been wrongly accessed.
102+
* @brief Check recursively whether this BaseRecord is dirty.
103+
* It is dirty if any attribute or dataset is read from or written to
104+
* the backend.
104105
*
105-
* @return true If closed iteration had no wrong accesses.
106+
* @return true If dirty.
106107
* @return false Otherwise.
107108
*/
108109
bool
109-
verifyClosed() const;
110+
dirtyRecursive() const;
110111
}; // BaseRecord
111112

112113

@@ -308,19 +309,19 @@ BaseRecord< T_elem >::flush(std::string const& name)
308309

309310
template< typename T_elem >
310311
inline bool
311-
BaseRecord< T_elem >::verifyClosed() const
312+
BaseRecord< T_elem >::dirtyRecursive() const
312313
{
313314
if( Attributable::dirty )
314315
{
315-
return false;
316+
return true;
316317
}
317318
for( auto const & pair : *this )
318319
{
319-
if( !pair.second.verifyClosed() )
320+
if( pair.second.dirtyRecursive() )
320321
{
321-
return false;
322+
return true;
322323
}
323324
}
324-
return true;
325+
return false;
325326
}
326327
} // namespace openPMD

src/IO/HDF5/HDF5IOHandler.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,17 @@ HDF5IOHandlerImpl::closeFile(
406406
auto fileID_it = m_fileIDs.find( writable );
407407
if( fileID_it == m_fileIDs.end() )
408408
{
409+
throw std::runtime_error(
410+
"[HDF5] Trying to close a file that is not "
411+
"present in the backend" );
409412
return;
410413
}
411414
hid_t fileID = fileID_it->second;
412415
H5Fclose( fileID );
413416
m_openFileIDs.erase( fileID );
414417
m_fileIDs.erase( fileID_it );
415418

416-
/*
417-
* std::unordered_map::erase:
419+
/* std::unordered_map::erase:
418420
* References and iterators to the erased elements are invalidated. Other
419421
* iterators and references are not invalidated.
420422
*/

src/Iteration.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
*/
2121
#include "openPMD/Iteration.hpp"
2222

23-
#include <tuple>
24-
2523
#include "openPMD/Dataset.hpp"
2624
#include "openPMD/Datatype.hpp"
2725
#include "openPMD/Series.hpp"
26+
#include "openPMD/auxiliary/DerefDynamicCast.hpp"
2827
#include "openPMD/auxiliary/StringManip.hpp"
2928
#include "openPMD/backend/Writable.hpp"
3029

30+
#include <tuple>
31+
3132

3233
namespace openPMD
3334
{
@@ -113,7 +114,7 @@ Iteration::close( bool _flush )
113114
*m_closed = CloseStatus::ClosedInFrontend;
114115
if( _flush )
115116
{
116-
Series * s = dynamic_cast< Series * >(
117+
Series * s = &auxiliary::deref_dynamic_cast< Series >(
117118
parent->attributable->parent->attributable );
118119
// figure out my iteration number
119120
uint64_t index;
@@ -431,27 +432,27 @@ Iteration::read()
431432
}
432433

433434
bool
434-
Iteration::verifyClosed() const
435+
Iteration::dirtyRecursive() const
435436
{
436437
if( dirty )
437438
{
438-
return false;
439+
return true;
439440
}
440441
for( auto const & pair : particles )
441442
{
442-
if( !pair.second.verifyClosed() )
443+
if( pair.second.dirtyRecursive() )
443444
{
444-
return false;
445+
return true;
445446
}
446447
}
447448
for( auto const & pair : meshes )
448449
{
449-
if( !pair.second.verifyClosed() )
450+
if( pair.second.dirtyRecursive() )
450451
{
451-
return false;
452+
return true;
452453
}
453454
}
454-
return true;
455+
return false;
455456
}
456457

457458
void

src/ParticleSpecies.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,19 @@ ParticleSpecies::flush(std::string const& path)
145145
}
146146

147147
bool
148-
ParticleSpecies::verifyClosed() const
148+
ParticleSpecies::dirtyRecursive() const
149149
{
150150
if( dirty )
151151
{
152-
return false;
152+
return true;
153153
}
154154
for( auto const & pair : *this )
155155
{
156-
if( !pair.second.verifyClosed() )
156+
if( pair.second.dirtyRecursive() )
157157
{
158-
return false;
158+
return true;
159159
}
160160
}
161-
return true;
161+
return false;
162162
}
163163
} // namespace openPMD

src/RecordComponent.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,12 @@ RecordComponent::readBase()
267267
}
268268

269269
bool
270-
RecordComponent::verifyClosed() const
270+
RecordComponent::dirtyRecursive() const
271271
{
272272
if( Attributable::dirty )
273273
{
274-
return false;
274+
return true;
275275
}
276-
return m_chunks->empty();
276+
return !m_chunks->empty();
277277
}
278278
} // namespace openPMD

src/Series.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ Series::flushFileBased( IterationsContainer && iterationsToFlush )
566566
// file corresponding with the iteration has previously been
567567
// closed and fully flushed
568568
// verify that there have been no further accesses
569-
if( !i.second.verifyClosed() )
569+
if( i.second.dirtyRecursive() )
570570
{
571571
throw std::runtime_error(
572572
"[Series] Detected illegal access to iteration that "
@@ -599,7 +599,7 @@ Series::flushFileBased( IterationsContainer && iterationsToFlush )
599599
"[Series] Closed iteration has not been written. This "
600600
"is an internal error.");
601601
}
602-
if( !i.second.verifyClosed() )
602+
if( i.second.dirtyRecursive() )
603603
{
604604
throw std::runtime_error(
605605
"[Series] Detected illegal access to iteration that "
@@ -657,7 +657,7 @@ Series::flushGroupBased( IterationsContainer && iterationsToFlush )
657657
// file corresponding with the iteration has previously been
658658
// closed and fully flushed
659659
// verify that there have been no further accesses
660-
if( !i.second.verifyClosed() )
660+
if( i.second.dirtyRecursive() )
661661
{
662662
throw std::runtime_error(
663663
"[Series] Illegal access to iteration " +
@@ -667,6 +667,12 @@ Series::flushGroupBased( IterationsContainer && iterationsToFlush )
667667
continue;
668668
}
669669
i.second.flush();
670+
if( *i.second.m_closed == Iteration::CloseStatus::ClosedInFrontend )
671+
{
672+
// the iteration has no dedicated file in group-based mode
673+
*i.second.m_closed = Iteration::CloseStatus::ClosedInBackend;
674+
}
675+
IOHandler->flush();
670676
}
671677
else
672678
{
@@ -692,7 +698,7 @@ Series::flushGroupBased( IterationsContainer && iterationsToFlush )
692698
"[Series] Closed iteration has not been written. This "
693699
"is an internal error.");
694700
}
695-
if( !i.second.verifyClosed() )
701+
if( i.second.dirtyRecursive() )
696702
{
697703
throw std::runtime_error(
698704
"[Series] Illegal access to iteration " +
@@ -707,9 +713,15 @@ Series::flushGroupBased( IterationsContainer && iterationsToFlush )
707713
i.second.parent = getWritable(&iterations);
708714
}
709715
i.second.flushGroupBased(i.first);
716+
if( *i.second.m_closed == Iteration::CloseStatus::ClosedInFrontend )
717+
{
718+
// the iteration has no dedicated file in group-based mode
719+
*i.second.m_closed = Iteration::CloseStatus::ClosedInBackend;
720+
}
710721
}
711722

712723
flushAttributes();
724+
IOHandler->flush();
713725
}
714726
}
715727

0 commit comments

Comments
 (0)