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 {
39 {
40  template<
41  typename T,
42  typename T_key,
43  typename T_container
44  >
45  friend
46  class Container;
47 
48  friend class ParticlePatches;
49  friend class PatchRecord;
50 
51 public:
52  PatchRecordComponent& setUnitSI(double);
53 
54  PatchRecordComponent& resetDataset(Dataset);
55 
56  uint8_t getDimensionality() const;
57  Extent getExtent() const;
58 
59  template< typename T >
60  std::shared_ptr< T > load();
61  template< typename T >
62  void load(std::shared_ptr< T >);
63  template< typename T >
64  void store(uint64_t idx, T);
66 
67 OPENPMD_private:
69 
70  void flush(std::string const&);
71  void read();
72 
73  std::shared_ptr< std::queue< IOTask > > m_chunks;
74 }; // PatchRecordComponent
75 
76 template< typename T >
77 inline std::shared_ptr< T >
78 PatchRecordComponent::load()
79 {
80  uint64_t numPoints = getExtent()[0];
81  auto newData = std::shared_ptr< T >(new T[numPoints], []( T *p ){ delete [] p; });
82  load(newData);
83  return newData;
84 }
85 
86 template< typename T >
87 inline void
88 PatchRecordComponent::load(std::shared_ptr< T > data)
89 {
90  Datatype dtype = determineDatatype< T >();
91  if( dtype != getDatatype() )
92  throw std::runtime_error("Type conversion during particle patch loading not yet implemented");
93 
94  if( !data )
95  throw std::runtime_error("Unallocated pointer passed during ParticlePatch loading.");
96 
97  uint64_t numPoints = getExtent()[0];
98 
100 
102  dRead.offset = {0};
103  dRead.extent = {numPoints};
104  dRead.dtype = getDatatype();
105  dRead.data = std::static_pointer_cast< void >(data);
106  m_chunks->push(IOTask(this, dRead));
107 }
108 
109 template< typename T >
110 inline void
111 PatchRecordComponent::store(uint64_t idx, T data)
112 {
113  Datatype dtype = determineDatatype< T >();
114  if( dtype != getDatatype() )
115  {
116  std::ostringstream oss;
117  oss << "Datatypes of patch data ("
118  << dtype
119  << ") and dataset ("
120  << getDatatype()
121  << ") do not match.";
122  throw std::runtime_error(oss.str());
123  }
124 
125  Extent dse = getExtent();
126  if( dse[0] - 1u < idx )
127  throw std::runtime_error("Index does not reside inside patch (no. patches: " + std::to_string(dse[0])
128  + " - index: " + std::to_string(idx) + ")");
129 
131  dWrite.offset = {idx};
132  dWrite.extent = {1};
133  dWrite.dtype = dtype;
134  dWrite.data = std::make_shared< T >(data);
135  m_chunks->push(IOTask(this, dWrite));
136 }
137 } // namespace openPMD
Definition: ParticlePatches.hpp:32
Definition: Dataset.hpp:36
Self-contained description of a single IO operation.
Definition: IOTask.hpp:468
Definition: PatchRecord.hpp:32
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:38
Public definitions of openPMD-api.
Definition: Date.cpp:28
Definition: PatchRecordComponent.hpp:38
Map-like container that enforces openPMD requirements and handles IO.
Definition: Container.hpp:70
Definition: BaseRecordComponent.hpp:34