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/backend/BaseRecordComponent.hpp"
24 #include "openPMD/auxiliary/ShareRaw.hpp"
25 #include "openPMD/Dataset.hpp"
26 
27 #include <cmath>
28 #include <memory>
29 #include <limits>
30 #include <queue>
31 #include <string>
32 #include <sstream>
33 #include <stdexcept>
34 #include <type_traits>
35 #include <vector>
36 #include <array>
37 
38 // expose private and protected members for invasive testing
39 #ifndef OPENPMD_protected
40 # define OPENPMD_protected protected
41 #endif
42 
43 
44 namespace openPMD
45 {
46 namespace traits
47 {
56 template< typename T >
58 {
59  static constexpr bool value = false;
60 };
61 
62 template< typename T_Value >
63 struct IsContiguousContainer< std::vector< T_Value > >
64 {
65  static constexpr bool value = true;
66 };
67 
68 template<
69  typename T_Value,
70  std::size_t N
71 >
72 struct IsContiguousContainer< std::array< T_Value, N > >
73 {
74  static constexpr bool value = true;
75 };
76 } // namespace traits
77 
78 template< typename T >
80 
82 {
83  template<
84  typename T,
85  typename T_key,
86  typename T_container
87  >
88  friend class Container;
89  friend class Iteration;
90  friend class ParticleSpecies;
91  template< typename T_elem >
92  friend class BaseRecord;
93  friend class Record;
94  friend class Mesh;
95  template< typename >
96  friend class DynamicMemoryView;
97 
98 public:
99  enum class Allocation
100  {
101  USER,
102  API,
103  AUTO
104  }; // Allocation
105 
106  RecordComponent& setUnitSI(double);
107 
128 
129  uint8_t getDimensionality() const;
130  Extent getExtent() const;
131 
140  template< typename T >
142 
151  template< typename T >
152  RecordComponent& makeEmpty( uint8_t dimensions );
153 
162  RecordComponent& makeEmpty( Datatype dt, uint8_t dimensions );
163 
171  bool empty() const;
172 
180  template< typename T >
181  std::shared_ptr< T > loadChunk(
182  Offset = { 0u },
183  Extent = { -1u } );
184 
194  template< typename T >
195  void loadChunk(
196  std::shared_ptr< T >,
197  Offset,
198  Extent );
199 
200  template< typename T >
201  void storeChunk(std::shared_ptr< T >, Offset, Extent);
202 
203  template< typename T_ContiguousContainer >
204  typename std::enable_if<
206  >::type
207  storeChunk(T_ContiguousContainer &, Offset = {0u}, Extent = {-1u});
208 
239  template< typename T, typename F >
240  DynamicMemoryView< T > storeChunk( Offset, Extent, F && createBuffer );
241 
246  template< typename T >
247  DynamicMemoryView< T > storeChunk( Offset, Extent );
248 
249  static constexpr char const * const SCALAR = "\vScalar";
250 
251  virtual ~RecordComponent() = default;
252 
253 OPENPMD_protected:
254  RecordComponent();
255 
256  void readBase();
257 
258  std::shared_ptr< std::queue< IOTask > > m_chunks;
259  std::shared_ptr< Attribute > m_constantValue;
260  std::shared_ptr< bool > m_isEmpty = std::make_shared< bool >( false );
261  // User has extended the dataset, but the EXTEND task must yet be flushed
262  // to the backend
263  std::shared_ptr< bool > m_hasBeenExtended =
264  std::make_shared< bool >( false );
265 
266 private:
267  void flush(std::string const&);
268  virtual void read();
269 
277 
286  bool dirtyRecursive() const;
287 
288 protected:
289 
298  std::shared_ptr< std::string > m_name = std::make_shared< std::string >();
299 
300 }; // RecordComponent
301 } // namespace openPMD
302 
303 #include "RecordComponent.tpp"
openPMD::ParticleSpecies
Definition: ParticleSpecies.hpp:34
openPMD::Datatype
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:45
openPMD::RecordComponent::empty
bool empty() const
Returns true if this is an empty record component.
Definition: RecordComponent.cpp:176
openPMD::BaseRecord
Definition: BaseRecord.hpp:36
openPMD::Mesh
Container for N-dimensional, homogeneous Records.
Definition: Mesh.hpp:40
openPMD::UnitDimension::T
@ T
time
openPMD::RecordComponent::m_name
std::shared_ptr< std::string > m_name
The same std::string that the parent class would pass as parameter to RecordComponent::flush().
Definition: RecordComponent.hpp:298
openPMD::Iteration
Logical compilation of data from one snapshot (e.g.
Definition: Iteration.hpp:39
openPMD::UnitDimension::N
@ N
amount of substance
openPMD
Public definitions of openPMD-api.
Definition: Date.cpp:29
openPMD::Record
Definition: Record.hpp:33
openPMD::RecordComponent::makeEmpty
RecordComponent & makeEmpty(uint8_t dimensions)
Create a dataset with zero extent in each dimension.
openPMD::Container
Map-like container that enforces openPMD requirements and handles IO.
Definition: Container.hpp:106
openPMD::traits::IsContiguousContainer
Emulate in the C++17 concept ContiguousContainer.
Definition: RecordComponent.hpp:57
openPMD::RecordComponent::resetDataset
RecordComponent & resetDataset(Dataset)
Declare the dataset's type and extent.
Definition: RecordComponent.cpp:58
openPMD::BaseRecordComponent
Definition: BaseRecordComponent.hpp:34
openPMD::DynamicMemoryView
A view into a buffer that might be reallocated at some points and thus has changing base pointers ove...
Definition: RecordComponent.hpp:79
openPMD::RecordComponent
Definition: RecordComponent.hpp:81
openPMD::Dataset
Definition: Dataset.hpp:36
openPMD::RecordComponent::loadChunk
std::shared_ptr< T > loadChunk(Offset={ 0u }, Extent={ -1u })
Load and allocate a chunk of data.
openPMD::RecordComponent::makeConstant
RecordComponent & makeConstant(T)
Create a dataset with regular extent and constant value.