openPMD-api
PatchRecordComponent.hpp
1 /* Copyright 2017-2020 Fabian Koller
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 
25 #include <unordered_map>
26 #include <string>
27 #include <sstream>
28 #include <stdexcept>
29 
30 // expose private and protected members for invasive testing
31 #ifndef OPENPMD_private
32 # define OPENPMD_private private
33 #endif
34 
35 
36 namespace openPMD
37 {
38 
43 {
44  template<
45  typename T,
46  typename T_key,
47  typename T_container
48  >
49  friend
50  class Container;
51 
52  template< typename > friend class BaseRecord;
53  friend class ParticlePatches;
54  friend class PatchRecord;
55 
56 public:
57  PatchRecordComponent& setUnitSI(double);
58 
59  PatchRecordComponent& resetDataset(Dataset);
60 
61  uint8_t getDimensionality() const;
62  Extent getExtent() const;
63 
64  template< typename T >
65  std::shared_ptr< T > load();
66  template< typename T >
67  void load(std::shared_ptr< T >);
68  template< typename T >
69  void store(uint64_t idx, T);
70 
71 OPENPMD_private:
73 
74  void flush(std::string const&);
75  void read();
76 
77  std::shared_ptr< std::queue< IOTask > > m_chunks;
78  std::shared_ptr< bool > hasBeenRead = std::make_shared< bool >( false );
79 
88  bool
89  dirtyRecursive() const;
90 }; // PatchRecordComponent
91 
92 template< typename T >
93 inline std::shared_ptr< T >
94 PatchRecordComponent::load()
95 {
96  uint64_t numPoints = getExtent()[0];
97  auto newData = std::shared_ptr< T >(new T[numPoints], []( T *p ){ delete [] p; });
98  load(newData);
99  return newData;
100 }
101 
102 template< typename T >
103 inline void
104 PatchRecordComponent::load(std::shared_ptr< T > data)
105 {
106  Datatype dtype = determineDatatype< T >();
107  if( dtype != getDatatype() )
108  throw std::runtime_error("Type conversion during particle patch loading not yet implemented");
109 
110  if( !data )
111  throw std::runtime_error("Unallocated pointer passed during ParticlePatch loading.");
112 
113  uint64_t numPoints = getExtent()[0];
114 
116 
118  dRead.offset = {0};
119  dRead.extent = {numPoints};
120  dRead.dtype = getDatatype();
121  dRead.data = std::static_pointer_cast< void >(data);
122  m_chunks->push(IOTask(this, dRead));
123 }
124 
125 template< typename T >
126 inline void
127 PatchRecordComponent::store(uint64_t idx, T data)
128 {
129  Datatype dtype = determineDatatype< T >();
130  if( dtype != getDatatype() )
131  {
132  std::ostringstream oss;
133  oss << "Datatypes of patch data ("
134  << dtype
135  << ") and dataset ("
136  << getDatatype()
137  << ") do not match.";
138  throw std::runtime_error(oss.str());
139  }
140 
141  Extent dse = getExtent();
142  if( dse[0] - 1u < idx )
143  throw std::runtime_error("Index does not reside inside patch (no. patches: " + std::to_string(dse[0])
144  + " - index: " + std::to_string(idx) + ")");
145 
147  dWrite.offset = {idx};
148  dWrite.extent = {1};
149  dWrite.dtype = dtype;
150  dWrite.data = std::make_shared< T >(data);
151  m_chunks->push(IOTask(this, dWrite));
152 }
153 } // namespace openPMD
Definition: ParticlePatches.hpp:32
Definition: Dataset.hpp:36
Self-contained description of a single IO operation.
Definition: IOTask.hpp:550
Definition: PatchRecord.hpp:32
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:42
Public definitions of openPMD-api.
Definition: Date.cpp:29
Definition: BaseRecord.hpp:36
Definition: PatchRecordComponent.hpp:42
Map-like container that enforces openPMD requirements and handles IO.
Definition: Container.hpp:70
Definition: BaseRecordComponent.hpp:34