openPMD-api
Series.hpp
1 /* Copyright 2017-2021 Fabian Koller, Axel Huebl
2  *
3  * This file is part of openPMD-api.
4  *
5  * openPMD-api is free software: you can redistribute it and/or modify
6  * it under the terms of of either the GNU General Public License or
7  * the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * openPMD-api is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License and the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * and the GNU Lesser General Public License along with openPMD-api.
19  * If not, see <http://www.gnu.org/licenses/>.
20  */
21 #pragma once
22 
23 #include "openPMD/IO/AbstractIOHandler.hpp"
24 #include "openPMD/IO/Access.hpp"
25 #include "openPMD/IO/Format.hpp"
26 #include "openPMD/Iteration.hpp"
27 #include "openPMD/IterationEncoding.hpp"
28 #include "openPMD/Streaming.hpp"
29 #include "openPMD/WriteIterations.hpp"
30 #include "openPMD/auxiliary/Variant.hpp"
31 #include "openPMD/backend/Attributable.hpp"
32 #include "openPMD/backend/Container.hpp"
33 #include "openPMD/backend/ParsePreference.hpp"
34 #include "openPMD/config.hpp"
35 #include "openPMD/version.hpp"
36 
37 #if openPMD_HAVE_MPI
38 #include <mpi.h>
39 #endif
40 
41 #include <cstdint> // uint64_t
42 #include <deque>
43 #include <map>
44 #include <optional>
45 #include <set>
46 #include <string>
47 
48 // expose private and protected members for invasive testing
49 #ifndef OPENPMD_private
50 #define OPENPMD_private private:
51 #endif
52 
53 namespace openPMD
54 {
55 class ReadIterations;
56 class SeriesIterator;
57 class Series;
58 class Series;
59 
60 namespace internal
61 {
69  {
70  public:
71  explicit SeriesData() = default;
72 
73  virtual ~SeriesData();
74 
75  SeriesData(SeriesData const &) = delete;
76  SeriesData(SeriesData &&) = delete;
77 
78  SeriesData &operator=(SeriesData const &) = delete;
79  SeriesData &operator=(SeriesData &&) = delete;
80 
81  using IterationIndex_t = Iteration::IterationIndex_t;
83  IterationsContainer_t iterations{};
84 
91  std::optional<WriteIterations> m_writeIterations;
92 
108  std::unique_ptr<SeriesIterator> m_sharedStatefulIterator;
114  std::set<IterationIndex_t> m_currentlyActiveIterations;
124  std::optional<std::string> m_overrideFilebasedFilename;
131  std::string m_name;
136  std::string m_filenamePrefix;
140  std::string m_filenamePostfix;
145  std::string m_filenameExtension;
167  StepStatus m_stepStatus = StepStatus::NoStep;
171  bool m_parseLazily = false;
172 
181 
188  std::optional<ParsePreference> m_parsePreference;
189 
190  void close();
191  }; // SeriesData
192 
193  class SeriesInternal;
194 } // namespace internal
195 
205 class Series : public Attributable
206 {
207  friend class Attributable;
208  friend class Iteration;
209  friend class Writable;
210  friend class ReadIterations;
211  friend class SeriesIterator;
212  friend class internal::SeriesData;
213  friend class WriteIterations;
214 
215 protected:
216  // Should not be called publicly, only by implementing classes
217  Series(std::shared_ptr<internal::SeriesData>);
218 
219 public:
220  explicit Series();
221 
222 #if openPMD_HAVE_MPI
223  Series(
224  std::string const &filepath,
225  Access at,
226  MPI_Comm comm,
227  std::string const &options = "{}");
228 #endif
229 
239  Series(
240  std::string const &filepath,
241  Access at,
242  std::string const &options = "{}");
243 
244  virtual ~Series() = default;
245 
249  using IterationIndex_t = Iteration::IterationIndex_t;
254  IterationsContainer_t iterations;
255 
262  operator bool() const;
263 
269  std::string openPMD() const;
278  Series &setOpenPMD(std::string const &openPMD);
279 
285  uint32_t openPMDextension() const;
295 
300  std::string basePath() const;
308  Series &setBasePath(std::string const &basePath);
309 
315  std::string meshesPath() const;
325  Series &setMeshesPath(std::string const &meshesPath);
326 
332  std::string particlesPath() const;
342  Series &setParticlesPath(std::string const &particlesPath);
343 
349  std::string author() const;
356  Series &setAuthor(std::string const &author);
357 
363  std::string software() const;
373  std::string const &newName,
374  std::string const &newVersion = std::string("unspecified"));
375 
381  std::string softwareVersion() const;
391  [[deprecated(
392  "Set the version with the second argument of setSoftware()")]] Series &
393  setSoftwareVersion(std::string const &softwareVersion);
394 
399  std::string date() const;
405  Series &setDate(std::string const &date);
406 
412  std::string softwareDependencies() const;
420  Series &setSoftwareDependencies(std::string const &newSoftwareDependencies);
421 
427  std::string machine() const;
434  Series &setMachine(std::string const &newMachine);
435 
458 
464  std::string iterationFormat() const;
476  Series &setIterationFormat(std::string const &iterationFormat);
477 
481  std::string name() const;
482 
489  Series &setName(std::string const &name);
490 
497  std::string backend() const;
498 
506  void flush(std::string backendConfig = "{}");
507 
524 
539  void parseBase();
540 
559 
570  void close();
571 
572  // clang-format off
573 OPENPMD_private
574  // clang-format on
575 
576  static constexpr char const *const BASEPATH = "/data/%T/";
577 
578  struct ParsedInput;
579  using iterations_t = decltype(internal::SeriesData::iterations);
580  using iterations_iterator = iterations_t::iterator;
581 
582  std::shared_ptr<internal::SeriesData> m_series = nullptr;
583 
584  inline internal::SeriesData &get()
585  {
586  if (m_series)
587  {
588  return *m_series;
589  }
590  else
591  {
592  throw std::runtime_error(
593  "[Series] Cannot use default-constructed Series.");
594  }
595  }
596 
597  inline internal::SeriesData const &get() const
598  {
599  if (m_series)
600  {
601  return *m_series;
602  }
603  else
604  {
605  throw std::runtime_error(
606  "[Series] Cannot use default-constructed Series.");
607  }
608  }
609 
610  std::unique_ptr<ParsedInput> parseInput(std::string);
620  template <typename TracingJSON>
621  void parseJsonOptions(TracingJSON &options, ParsedInput &);
622  bool hasExpansionPattern(std::string filenameWithExtension);
623  bool reparseExpansionPattern(std::string filenameWithExtension);
624  void init(std::unique_ptr<AbstractIOHandler>, std::unique_ptr<ParsedInput>);
625  void initDefaults(IterationEncoding, bool initAll = false);
637  std::future<void> flush_impl(
638  iterations_iterator begin,
639  iterations_iterator end,
640  internal::FlushParams flushParams,
641  bool flushIOHandler = true);
642  void flushFileBased(
643  iterations_iterator begin,
644  iterations_iterator end,
645  internal::FlushParams flushParams,
646  bool flushIOHandler = true);
647  /*
648  * Group-based and variable-based iteration layouts share a lot of logic
649  * (realistically, the variable-based iteration layout only throws out
650  * one layer in the hierarchy).
651  * As a convention, methods that deal with both layouts are called
652  * .*GorVBased, short for .*GroupOrVariableBased
653  */
654  void flushGorVBased(
655  iterations_iterator begin,
656  iterations_iterator end,
657  internal::FlushParams flushParams,
658  bool flushIOHandler = true);
659  void flushMeshesPath();
660  void flushParticlesPath();
661  void readFileBased();
662  void readOneIterationFileBased(std::string const &filePath);
676  std::optional<std::deque<IterationIndex_t> > readGorVBased(
677  bool do_always_throw_errors,
678  bool init,
679  std::set<IterationIndex_t> const &ignoreIterations = {});
680  void readBase();
681  std::string iterationFilename(IterationIndex_t i);
682 
683  enum class IterationOpened : bool
684  {
685  HasBeenOpened,
686  RemainsClosed
687  };
688  /*
689  * For use by flushFileBased, flushGorVBased
690  * Open an iteration, but only if necessary.
691  * Only open if the iteration is dirty and if it is not in deferred
692  * parse state.
693  */
694  IterationOpened
695  openIterationIfDirty(IterationIndex_t index, Iteration iteration);
696  /*
697  * Open an iteration. Ensures that the iteration's m_closed status
698  * is set properly and that any files pertaining to the iteration
699  * is opened.
700  * Does not create files when called in CREATE mode.
701  */
702  void openIteration(IterationIndex_t index, Iteration iteration);
703 
708  iterations_iterator indexOf(Iteration const &);
709 
726  AdvanceMode mode,
727  internal::AttributableData &file,
728  iterations_iterator it,
729  Iteration &iteration);
730 
732 
740  void flushStep(bool doFlush);
741 
742  /*
743  * Returns the current content of the /data/snapshot attribute.
744  * (We could also add this to the public API some time)
745  */
746  std::optional<std::vector<IterationIndex_t> > currentSnapshot() const;
747 }; // Series
748 } // namespace openPMD
749 
750 // Make sure that this one is always included if Series.hpp is included,
751 // otherwise Series::readIterations() cannot be used
752 #include "openPMD/ReadIterations.hpp"
Layer to manage storage of attributes associated with file objects.
Definition: Attributable.hpp:94
Logical compilation of data from one snapshot (e.g.
Definition: Iteration.hpp:127
Reading side of the streaming API.
Definition: ReadIterations.hpp:181
Implementation for the root level of the openPMD hierarchy.
Definition: Series.hpp:206
Series & setName(std::string const &name)
Set the pattern for file names.
Definition: Series.cpp:359
std::string softwareVersion() const
Definition: Series.cpp:234
std::optional< std::deque< IterationIndex_t > > readGorVBased(bool do_always_throw_errors, bool init, std::set< IterationIndex_t > const &ignoreIterations={})
Note on re-parsing of a Series: If init == false, the parsing process will seek for new Iterations/Re...
Definition: Series.cpp:1306
Series & setMeshesPath(std::string const &meshesPath)
Set the path to mesh records, relative(!) to basePath.
Definition: Series.cpp:163
std::string iterationFormat() const
Definition: Series.cpp:324
void flush(std::string backendConfig="{}")
Execute all required remaining IO operations to write or read data.
Definition: Series.cpp:398
void parseBase()
Parse the Series.
Definition: Series.cpp:2374
std::string basePath() const
Definition: Series.cpp:142
Series & setSoftwareDependencies(std::string const &newSoftwareDependencies)
Indicate dependencies of software that were used to create the file.
Definition: Series.cpp:262
iterations_iterator indexOf(Iteration const &)
Find the given iteration in Series::iterations and return an iterator into Series::iterations at that...
Definition: Series.cpp:1744
Iteration::IterationIndex_t IterationIndex_t
An unsigned integer type, used to identify Iterations in a Series.
Definition: Series.hpp:249
Series & setParticlesPath(std::string const &particlesPath)
Set the path to groups for each particle species, relative(!) to basePath.
Definition: Series.cpp:189
std::string machine() const
Definition: Series.cpp:268
std::string date() const
Definition: Series.cpp:245
WriteIterations writeIterations()
Entry point to the writing end of the streaming API.
Definition: Series.cpp:2379
std::string backend() const
The currently used backend.
Definition: Series.cpp:393
void close()
Close the Series and release the data storage/transport backends.
Definition: Series.cpp:2389
Series & setOpenPMD(std::string const &openPMD)
Set the version of the enforced openPMD standard.
Definition: Series.cpp:125
void flushStep(bool doFlush)
Called at the end of an IO step to store the iterations defined in the IO step to the snapshot attrib...
Definition: Series.cpp:1956
std::string openPMD() const
Definition: Series.cpp:120
Series & setAuthor(std::string const &author)
Indicate the author and contact for the information in the file.
Definition: Series.cpp:215
Series & setSoftwareVersion(std::string const &softwareVersion)
Indicate the version of the software/code/simulation that created the file.
Definition: Series.cpp:239
std::string author() const
Definition: Series.cpp:210
std::future< void > flush_impl(iterations_iterator begin, iterations_iterator end, internal::FlushParams flushParams, bool flushIOHandler=true)
Internal call for flushing a Series.
Definition: Series.cpp:701
std::string meshesPath() const
Definition: Series.cpp:158
Series & setMachine(std::string const &newMachine)
Indicate the machine or relevant hardware that created the file.
Definition: Series.cpp:273
std::string software() const
Definition: Series.cpp:221
uint32_t openPMDextension() const
Definition: Series.cpp:131
Series & setBasePath(std::string const &basePath)
Set the common prefix for all data sets and sub-groups of a specific iteration.
Definition: Series.cpp:147
void parseJsonOptions(TracingJSON &options, ParsedInput &)
Parse non-backend-specific configuration in JSON config.
Definition: Series.cpp:2161
std::string particlesPath() const
Definition: Series.cpp:184
AdvanceStatus advance(AdvanceMode mode, internal::AttributableData &file, iterations_iterator it, Iteration &iteration)
In step-based IO mode, begin or end an IO step for the given iteration.
Definition: Series.cpp:1759
Series & setDate(std::string const &date)
Indicate the date of creation.
Definition: Series.cpp:250
std::string name() const
Definition: Series.cpp:354
Series & setSoftware(std::string const &newName, std::string const &newVersion=std::string("unspecified"))
Indicate the software/code/simulation that created the file.
Definition: Series.cpp:227
std::string softwareDependencies() const
Definition: Series.cpp:256
ReadIterations readIterations()
Entry point to the reading end of the streaming API.
Definition: Series.cpp:2366
Series & setOpenPMDextension(uint32_t openPMDextension)
Set a 32-bit mask of applied extensions to the openPMD standard.
Definition: Series.cpp:136
Series & setIterationEncoding(IterationEncoding iterationEncoding)
Set the encoding style for multiple iterations in this series.
Definition: Series.cpp:284
IterationEncoding iterationEncoding() const
Definition: Series.cpp:279
Series & setIterationFormat(std::string const &iterationFormat)
Set a pattern describing how to access single iterations in the raw file.
Definition: Series.cpp:329
Definition: ReadIterations.hpp:55
Layer to mirror structure of logical data and persistent data in file.
Definition: Writable.hpp:65
Writing side of the streaming API.
Definition: WriteIterations.hpp:67
Definition: Attributable.hpp:57
Data members for Series.
Definition: Series.hpp:69
std::string m_filenamePrefix
Filename leading up to the expansion pattern.
Definition: Series.hpp:136
std::set< IterationIndex_t > m_currentlyActiveIterations
For writing: Remember which iterations have been written in the currently active output step.
Definition: Series.hpp:114
std::unique_ptr< SeriesIterator > m_sharedStatefulIterator
Series::readIterations() returns an iterator type that modifies the state of the Series (by proceedin...
Definition: Series.hpp:108
std::optional< std::string > m_overrideFilebasedFilename
Needed if reading a single iteration of a file-based series.
Definition: Series.hpp:124
StepStatus m_stepStatus
Whether a step is currently active for this iteration.
Definition: Series.hpp:167
std::optional< WriteIterations > m_writeIterations
For each instance of Series, there is only one instance of WriteIterations, stored in this Option.
Definition: Series.hpp:91
std::string m_name
Name of the iteration without filename suffix.
Definition: Series.hpp:131
std::optional< ParsePreference > m_parsePreference
Remember the preference that the backend specified for parsing.
Definition: Series.hpp:188
std::string m_filenamePostfix
Filename after the expansion pattern without filename extension.
Definition: Series.hpp:140
int m_filenamePadding
The padding in file-based iteration encoding.
Definition: Series.hpp:151
bool m_parseLazily
True if a user opts into lazy parsing.
Definition: Series.hpp:171
bool m_wroteAtLeastOneIOStep
In variable-based encoding, all backends except ADIOS2 can only write one single iteration.
Definition: Series.hpp:180
std::string m_filenameExtension
Filename extension as specified by the user.
Definition: Series.hpp:145
IterationEncoding m_iterationEncoding
The iteration encoding used in this series.
Definition: Series.hpp:155
Format m_format
Detected IO format (backend).
Definition: Series.hpp:159
Public definitions of openPMD-api.
Definition: Date.cpp:29
Access
File access mode to use during IO.
Definition: Access.hpp:30
AdvanceMode
In step-based mode (i.e.
Definition: Streaming.hpp:35
StepStatus
Used in step-based mode (i.e.
Definition: Streaming.hpp:46
AdvanceStatus
In step-based mode (i.e.
Definition: Streaming.hpp:21
Format
File format to use during IO.
Definition: Format.hpp:30
IterationEncoding
Encoding scheme of an Iterations Series'.
Definition: IterationEncoding.hpp:33
Definition: Series.cpp:109