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