openPMD-api
RecordComponent.hpp
1 /* Copyright 2017-2021 Fabian Koller, Axel Huebl and Franz Poeschel
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/Dataset.hpp"
24 #include "openPMD/auxiliary/ShareRaw.hpp"
25 #include "openPMD/auxiliary/TypeTraits.hpp"
26 #include "openPMD/auxiliary/UniquePtr.hpp"
27 #include "openPMD/backend/BaseRecordComponent.hpp"
28 
29 #include <array>
30 #include <cmath>
31 #include <limits>
32 #include <memory>
33 #include <queue>
34 #include <sstream>
35 #include <stdexcept>
36 #include <string>
37 #include <type_traits>
38 #include <vector>
39 
40 // expose private and protected members for invasive testing
41 #ifndef OPENPMD_protected
42 #define OPENPMD_protected protected:
43 #endif
44 
45 namespace openPMD
46 {
47 
48 template <typename T>
50 
51 class RecordComponent;
52 
53 namespace internal
54 {
56  {
57  public:
59 
60  RecordComponentData(RecordComponentData const &) = delete;
62 
63  RecordComponentData &operator=(RecordComponentData const &) = delete;
64  RecordComponentData &operator=(RecordComponentData &&) = delete;
65 
69  std::queue<IOTask> m_chunks;
84  std::string m_name;
91  bool m_isEmpty = false;
96  bool m_hasBeenExtended = false;
97  };
98 } // namespace internal
99 
101 {
102  template <typename T, typename T_key, typename T_container>
103  friend class Container;
104  friend class Iteration;
105  friend class ParticleSpecies;
106  template <typename T_elem>
107  friend class BaseRecord;
108  template <typename T_elem>
109  friend class BaseRecordInterface;
110  friend class Record;
111  friend class Mesh;
112  template <typename>
113  friend class DynamicMemoryView;
114  friend class internal::RecordComponentData;
115  friend class MeshRecordComponent;
116 
117 public:
118  enum class Allocation
119  {
120  USER,
121  API,
122  AUTO
123  }; // Allocation
124 
125  RecordComponent &setUnitSI(double);
126 
146  RecordComponent &resetDataset(Dataset);
147 
148  uint8_t getDimensionality() const;
149  Extent getExtent() const;
150 
159  template <typename T>
160  RecordComponent &makeConstant(T);
161 
170  template <typename T>
171  RecordComponent &makeEmpty(uint8_t dimensions);
172 
181  RecordComponent &makeEmpty(Datatype dt, uint8_t dimensions);
182 
190  bool empty() const;
191 
199  template <typename T>
200  std::shared_ptr<T> loadChunk(Offset = {0u}, Extent = {-1u});
201 
223  template <typename T>
224  void loadChunk(std::shared_ptr<T> data, Offset offset, Extent extent);
225 
242  template <typename T>
243  void loadChunk(std::shared_ptr<T[]> data, Offset offset, Extent extent);
244 
260  template <typename T>
261  void loadChunkRaw(T *data, Offset offset, Extent extent);
262 
281  template <typename T>
282  void storeChunk(std::shared_ptr<T> data, Offset offset, Extent extent);
283 
293  template <typename T>
294  void storeChunk(std::shared_ptr<T[]> data, Offset offset, Extent extent);
295 
308  template <typename T>
309  void storeChunk(UniquePtrWithLambda<T> data, Offset offset, Extent extent);
310 
323  template <typename T, typename Del>
324  void storeChunk(std::unique_ptr<T, Del> data, Offset offset, Extent extent);
325 
338  template <typename T>
339  void storeChunkRaw(T *data, Offset offset, Extent extent);
340 
356  template <typename T_ContiguousContainer>
357  typename std::enable_if_t<
358  auxiliary::IsContiguousContainer_v<T_ContiguousContainer>>
359  storeChunk(
360  T_ContiguousContainer &data,
361  Offset offset = {0u},
362  Extent extent = {-1u});
363 
397  template <typename T, typename F>
399  storeChunk(Offset offset, Extent extent, F &&createBuffer);
400 
405  template <typename T>
406  DynamicMemoryView<T> storeChunk(Offset, Extent);
407 
408  static constexpr char const *const SCALAR = "\vScalar";
409 
410 private:
411  void flush(std::string const &, internal::FlushParams const &);
412  virtual void read();
413 
420  RecordComponent &makeEmpty(Dataset d);
421 
422  void storeChunk(
423  auxiliary::WriteBuffer buffer, Datatype datatype, Offset o, Extent e);
424 
433  bool dirtyRecursive() const;
434 
435  std::shared_ptr<internal::RecordComponentData> m_recordComponentData{
437 
438  RecordComponent();
439 
440  // clang-format off
441 OPENPMD_protected
442  // clang-format on
443 
444  RecordComponent(std::shared_ptr<internal::RecordComponentData>);
445 
446  inline internal::RecordComponentData const &get() const
447  {
448  return *m_recordComponentData;
449  }
450 
451  inline internal::RecordComponentData &get()
452  {
453  return *m_recordComponentData;
454  }
455 
456  inline void setData(std::shared_ptr<internal::RecordComponentData> data)
457  {
458  m_recordComponentData = std::move(data);
459  BaseRecordComponent::setData(m_recordComponentData);
460  }
461 
462  void readBase();
463 }; // RecordComponent
464 
465 } // namespace openPMD
466 
467 #include "RecordComponent.tpp"
Unique Pointer class that uses a dynamic destructor type.
Definition: UniquePtr.hpp:86
Definition: Dataset.hpp:35
bool m_isEmpty
True if this component is an empty dataset, i.e.
Definition: RecordComponent.hpp:91
Logical compilation of data from one snapshot (e.g.
Definition: Iteration.hpp:126
Attribute m_constantValue
Stores the value for constant record components.
Definition: RecordComponent.hpp:74
Definition: Memory.hpp:174
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:45
Definition: RecordComponent.hpp:55
Varidic datatype supporting at least all formats for attributes specified in the openPMD standard...
Definition: Attribute.hpp:54
Definition: RecordComponent.hpp:100
std::string m_name
The same std::string that the parent class would pass as parameter to RecordComponent::flush().
Definition: RecordComponent.hpp:84
Public definitions of openPMD-api.
bool m_hasBeenExtended
User has extended the dataset, but the EXTEND task must yet be flushed to the backend.
Definition: RecordComponent.hpp:96
Definition: Record.hpp:32
Definition: BaseRecord.hpp:58
Parameters recursively passed through the openPMD hierarchy when flushing.
Definition: AbstractIOHandler.hpp:84
std::queue< IOTask > m_chunks
Chunk reading/writing requests on the contained dataset.
Definition: RecordComponent.hpp:69
A view into a buffer that might be reallocated at some points and thus has changing base pointers ove...
Definition: RecordComponent.hpp:49
Definition: ParticleSpecies.hpp:33
Container for N-dimensional, homogeneous Records.
Definition: Mesh.hpp:40
Map-like container that enforces openPMD requirements and handles IO.
Definition: Container.hpp:131
Definition: BaseRecordComponent.hpp:36
Definition: MeshRecordComponent.hpp:29
Definition: BaseRecordComponent.hpp:62