openPMD-api
IOTask.hpp
1 /* Copyright 2017-2021 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/auxiliary/Export.hpp"
24 #include "openPMD/auxiliary/Variant.hpp"
25 #include "openPMD/backend/Attribute.hpp"
26 #include "openPMD/ChunkInfo.hpp"
27 #include "openPMD/Dataset.hpp"
28 #include "openPMD/IterationEncoding.hpp"
29 #include "openPMD/Streaming.hpp"
30 
31 #include <memory>
32 #include <map>
33 #include <string>
34 #include <utility>
35 #include <vector>
36 
37 
38 namespace openPMD
39 {
40 class AttributableInterface;
41 class Writable;
42 
43 Writable*
44 getWritable(AttributableInterface*);
45 
49 {
50  CREATE_FILE,
51  OPEN_FILE,
52  CLOSE_FILE,
53  DELETE_FILE,
54 
55  CREATE_PATH,
56  CLOSE_PATH,
57  OPEN_PATH,
58  DELETE_PATH,
59  LIST_PATHS,
60 
61  CREATE_DATASET,
62  EXTEND_DATASET,
63  OPEN_DATASET,
64  DELETE_DATASET,
65  WRITE_DATASET,
66  READ_DATASET,
67  LIST_DATASETS,
68  GET_BUFFER_VIEW,
69 
70  DELETE_ATT,
71  WRITE_ATT,
72  READ_ATT,
73  LIST_ATTS,
74 
75  ADVANCE,
76  AVAILABLE_CHUNKS
77 }; // note: if you change the enum members here, please update docs/source/dev/design.rst
78 
79 struct OPENPMDAPI_EXPORT AbstractParameter
80 {
81  virtual ~AbstractParameter() = default;
82  AbstractParameter() = default;
83  //AbstractParameter(AbstractParameter&&) = default;
84 
85  // avoid object slicing
86  AbstractParameter(const AbstractParameter&) = delete;
87  AbstractParameter& operator=(const AbstractParameter&) = delete;
88  virtual std::unique_ptr< AbstractParameter > clone() const = 0;
89 };
90 
99 template< Operation >
100 struct OPENPMDAPI_EXPORT Parameter : public AbstractParameter
101 {
102  Parameter() = delete;
103  Parameter(Parameter const &) = delete;
104  Parameter(Parameter &&) = delete;
105 };
106 
107 template<>
108 struct OPENPMDAPI_EXPORT Parameter< Operation::CREATE_FILE > : public AbstractParameter
109 {
110  Parameter() = default;
111  Parameter(Parameter const & p) :
112  AbstractParameter(), name(p.name), encoding(p.encoding) {}
113 
114  std::unique_ptr< AbstractParameter >
115  clone() const override
116  {
117  return std::unique_ptr< AbstractParameter >(
119  }
120 
121  std::string name = "";
122  IterationEncoding encoding = IterationEncoding::groupBased;
123 };
124 
125 template<>
126 struct OPENPMDAPI_EXPORT Parameter< Operation::OPEN_FILE > : public AbstractParameter
127 {
128  Parameter() = default;
129  Parameter(Parameter const & p) :
130  AbstractParameter(), name(p.name), encoding(p.encoding) {}
131 
132  std::unique_ptr< AbstractParameter >
133  clone() const override
134  {
135  return std::unique_ptr< AbstractParameter >(
137  }
138 
139  std::string name = "";
140  /*
141  * The backends might need to ensure availability of certain features
142  * for some iteration encodings, e.g. availability of ADIOS steps for
143  * variableBased encoding.
144  */
145  IterationEncoding encoding = IterationEncoding::groupBased;
146 };
147 
148 template<>
149 struct OPENPMDAPI_EXPORT Parameter< Operation::CLOSE_FILE > : public AbstractParameter
150 {
151  Parameter() = default;
152  Parameter( Parameter const & ) : AbstractParameter() {}
153 
154  std::unique_ptr< AbstractParameter >
155  clone() const override
156  {
157  return std::unique_ptr< AbstractParameter >(
158  new Parameter< Operation::CLOSE_FILE >( *this ) );
159  }
160 };
161 
162 template<>
163 struct OPENPMDAPI_EXPORT Parameter< Operation::DELETE_FILE > : public AbstractParameter
164 {
165  Parameter() = default;
166  Parameter(Parameter const & p) : AbstractParameter(), name(p.name) {}
167 
168  std::unique_ptr< AbstractParameter >
169  clone() const override
170  {
171  return std::unique_ptr< AbstractParameter >(
173  }
174 
175  std::string name = "";
176 };
177 
178 template<>
179 struct OPENPMDAPI_EXPORT Parameter< Operation::CREATE_PATH > : public AbstractParameter
180 {
181  Parameter() = default;
182  Parameter(Parameter const & p) : AbstractParameter(), path(p.path) {}
183 
184  std::unique_ptr< AbstractParameter >
185  clone() const override
186  {
187  return std::unique_ptr< AbstractParameter >(
189  }
190 
191  std::string path = "";
192 };
193 
194 template<>
195 struct OPENPMDAPI_EXPORT Parameter< Operation::CLOSE_PATH > : public AbstractParameter
196 {
197  Parameter() = default;
198  Parameter( Parameter const & ) : AbstractParameter()
199  {
200  }
201 
202  Parameter &
203  operator=( Parameter const & )
204  {
205  return *this;
206  }
207 
208  std::unique_ptr< AbstractParameter >
209  clone() const override
210  {
211  return std::unique_ptr< AbstractParameter >(
212  new Parameter< Operation::CLOSE_PATH >( *this ) );
213  }
214 };
215 
216 template<>
217 struct OPENPMDAPI_EXPORT Parameter< Operation::OPEN_PATH > : public AbstractParameter
218 {
219  Parameter() = default;
220  Parameter(Parameter const & p) : AbstractParameter(), path(p.path) {}
221 
222  std::unique_ptr< AbstractParameter >
223  clone() const override
224  {
225  return std::unique_ptr< AbstractParameter >(
227  }
228 
229  std::string path = "";
230 };
231 
232 template<>
233 struct OPENPMDAPI_EXPORT Parameter< Operation::DELETE_PATH > : public AbstractParameter
234 {
235  Parameter() = default;
236  Parameter(Parameter const & p) : AbstractParameter(), path(p.path) {}
237 
238  std::unique_ptr< AbstractParameter >
239  clone() const override
240  {
241  return std::unique_ptr< AbstractParameter >(
243  }
244 
245  std::string path = "";
246 };
247 
248 template<>
249 struct OPENPMDAPI_EXPORT Parameter< Operation::LIST_PATHS > : public AbstractParameter
250 {
251  Parameter() = default;
252  Parameter(Parameter const & p) : AbstractParameter(), paths(p.paths) {}
253 
254  std::unique_ptr< AbstractParameter >
255  clone() const override
256  {
257  return std::unique_ptr< AbstractParameter >(
259  }
260 
261  std::shared_ptr< std::vector< std::string > > paths
262  = std::make_shared< std::vector< std::string > >();
263 };
264 
265 template<>
266 struct OPENPMDAPI_EXPORT Parameter< Operation::CREATE_DATASET > : public AbstractParameter
267 {
268  Parameter() = default;
269  Parameter(Parameter const & p) : AbstractParameter(),
270  name(p.name), extent(p.extent), dtype(p.dtype),
271  chunkSize(p.chunkSize), compression(p.compression),
272  transform(p.transform), options(p.options) {}
273 
274  std::unique_ptr< AbstractParameter >
275  clone() const override
276  {
277  return std::unique_ptr< AbstractParameter >(
279  }
280 
281  std::string name = "";
282  Extent extent = {};
283  Datatype dtype = Datatype::UNDEFINED;
284  Extent chunkSize = {};
285  std::string compression = "";
286  std::string transform = "";
287  std::string options = "{}";
288 };
289 
290 template<>
291 struct OPENPMDAPI_EXPORT Parameter< Operation::EXTEND_DATASET > : public AbstractParameter
292 {
293  Parameter() = default;
294  Parameter(Parameter const & p) : AbstractParameter(), extent(p.extent) {}
295 
296  std::unique_ptr< AbstractParameter >
297  clone() const override
298  {
299  return std::unique_ptr< AbstractParameter >(
301  }
302 
303  Extent extent = {};
304 };
305 
306 template<>
307 struct OPENPMDAPI_EXPORT Parameter< Operation::OPEN_DATASET > : public AbstractParameter
308 {
309  Parameter() = default;
310  Parameter(Parameter const & p) : AbstractParameter(),
311  name(p.name), dtype(p.dtype), extent(p.extent) {}
312 
313  std::unique_ptr< AbstractParameter >
314  clone() const override
315  {
316  return std::unique_ptr< AbstractParameter >(
318  }
319 
320  std::string name = "";
321  std::shared_ptr< Datatype > dtype
322  = std::make_shared< Datatype >();
323  std::shared_ptr< Extent > extent
324  = std::make_shared< Extent >();
325 };
326 
327 template<>
328 struct OPENPMDAPI_EXPORT Parameter< Operation::DELETE_DATASET > : public AbstractParameter
329 {
330  Parameter() = default;
331  Parameter(Parameter const & p) : AbstractParameter(), name(p.name) {}
332 
333  std::unique_ptr< AbstractParameter >
334  clone() const override
335  {
336  return std::unique_ptr< AbstractParameter >(
338  }
339 
340  std::string name = "";
341 };
342 
343 template<>
344 struct OPENPMDAPI_EXPORT Parameter< Operation::WRITE_DATASET > : public AbstractParameter
345 {
346  Parameter() = default;
348  extent(p.extent), offset(p.offset), dtype(p.dtype),
349  data(p.data) {}
350 
351  Parameter& operator=(const Parameter& p) {
352  this->extent = p.extent;
353  this->offset = p.offset;
354  this->dtype = p.dtype;
355  this->data = p.data;
356  return *this;
357  }
358 
359  std::unique_ptr< AbstractParameter >
360  clone() const override
361  {
362  return std::unique_ptr< AbstractParameter >(
364  }
365 
366  Extent extent = {};
367  Offset offset = {};
368  Datatype dtype = Datatype::UNDEFINED;
369  std::shared_ptr< void const > data = nullptr;
370 };
371 
372 template<>
373 struct OPENPMDAPI_EXPORT Parameter< Operation::READ_DATASET > : public AbstractParameter
374 {
375  Parameter() = default;
377  extent(p.extent), offset(p.offset), dtype(p.dtype),
378  data(p.data) {}
379 
380  Parameter& operator=(const Parameter &p) {
381  this->extent = p.extent;
382  this->offset = p.offset;
383  this->dtype = p.dtype;
384  this->data = p.data;
385  return *this;
386  }
387 
388  std::unique_ptr< AbstractParameter >
389  clone() const override
390  {
391  return std::unique_ptr< AbstractParameter >(
393  }
394 
395  Extent extent = {};
396  Offset offset = {};
397  Datatype dtype = Datatype::UNDEFINED;
398  std::shared_ptr< void > data = nullptr;
399 };
400 
401 template<>
402 struct OPENPMDAPI_EXPORT Parameter< Operation::LIST_DATASETS > : public AbstractParameter
403 {
404  Parameter() = default;
405  Parameter(Parameter const & p) : AbstractParameter(),
406  datasets(p.datasets) {}
407 
408  std::unique_ptr< AbstractParameter >
409  clone() const override
410  {
411  return std::unique_ptr< AbstractParameter >(
413  }
414 
415  std::shared_ptr< std::vector< std::string > > datasets
416  = std::make_shared< std::vector< std::string > >();
417 };
418 
419 template<>
420 struct OPENPMDAPI_EXPORT Parameter< Operation::GET_BUFFER_VIEW > : public AbstractParameter
421 {
422  Parameter() = default;
423  Parameter(Parameter const & p) : AbstractParameter(),
424  offset(p.offset), extent(p.extent), dtype(p.dtype), update(p.update),
425  out(p.out)
426  {}
427 
428  std::unique_ptr< AbstractParameter >
429  clone() const override
430  {
431  return std::unique_ptr< AbstractParameter >(
433  }
434 
435  // in parameters
436  Offset offset;
437  Extent extent;
438  Datatype dtype = Datatype::UNDEFINED;
439  bool update = false;
440  // out parameters
441  struct OutParameters
442  {
443  bool backendManagedBuffer = false;
444  unsigned viewIndex = 0;
445  void *ptr = nullptr;
446  };
447  std::shared_ptr< OutParameters > out = std::make_shared< OutParameters >();
448 };
449 
450 template<>
451 struct OPENPMDAPI_EXPORT Parameter< Operation::DELETE_ATT > : public AbstractParameter
452 {
453  Parameter() = default;
454  Parameter(Parameter const & p) : AbstractParameter(), name(p.name) {}
455 
456  std::unique_ptr< AbstractParameter >
457  clone() const override
458  {
459  return std::unique_ptr< AbstractParameter >(
461  }
462 
463  std::string name = "";
464 };
465 
466 template<>
467 struct OPENPMDAPI_EXPORT Parameter< Operation::WRITE_ATT > : public AbstractParameter
468 {
469  Parameter() = default;
470  Parameter(Parameter const & p) : AbstractParameter(),
471  name(p.name), dtype(p.dtype), resource(p.resource) {}
472 
473  std::unique_ptr< AbstractParameter >
474  clone() const override
475  {
476  return std::unique_ptr< AbstractParameter >(
478  }
479 
480  std::string name = "";
481  Datatype dtype = Datatype::UNDEFINED;
482  Attribute::resource resource;
483 };
484 
485 template<>
486 struct OPENPMDAPI_EXPORT Parameter< Operation::READ_ATT > : public AbstractParameter
487 {
488  Parameter() = default;
489  Parameter(Parameter const & p) : AbstractParameter(),
490  name(p.name), dtype(p.dtype), resource(p.resource) {}
491 
492  Parameter& operator=(const Parameter &p) {
493  this->name = p.name;
494  this->dtype = p.dtype;
495  this->resource = p.resource;
496  return *this;
497  }
498 
499  std::unique_ptr< AbstractParameter >
500  clone() const override
501  {
502  return std::unique_ptr< AbstractParameter >(
504  }
505 
506  std::string name = "";
507  std::shared_ptr< Datatype > dtype
508  = std::make_shared< Datatype >();
509  std::shared_ptr< Attribute::resource > resource
510  = std::make_shared< Attribute::resource >();
511 };
512 
513 template<>
514 struct OPENPMDAPI_EXPORT Parameter< Operation::LIST_ATTS > : public AbstractParameter
515 {
516  Parameter() = default;
517  Parameter(Parameter const & p) : AbstractParameter(),
518  attributes(p.attributes) {}
519 
520  std::unique_ptr< AbstractParameter >
521  clone() const override
522  {
523  return std::unique_ptr< AbstractParameter >(
525  }
526 
527  std::shared_ptr< std::vector< std::string > > attributes
528  = std::make_shared< std::vector< std::string > >();
529 };
530 
531 template<>
532 struct OPENPMDAPI_EXPORT Parameter< Operation::ADVANCE > : public AbstractParameter
533 {
534  Parameter() = default;
535  Parameter( Parameter const & p )
536  : AbstractParameter(), mode( p.mode ), status( p.status )
537  {
538  }
539 
540  std::unique_ptr< AbstractParameter >
541  clone() const override
542  {
543  return std::unique_ptr< AbstractParameter >(
544  new Parameter< Operation::ADVANCE >( *this ) );
545  }
546 
550  std::shared_ptr< AdvanceStatus > status =
551  std::make_shared< AdvanceStatus >( AdvanceStatus::OK );
552 };
553 
554 template<>
555 struct OPENPMDAPI_EXPORT Parameter< Operation::AVAILABLE_CHUNKS >
556  : public AbstractParameter
557 {
558  Parameter() = default;
559  Parameter( Parameter const & p ) : AbstractParameter(), chunks( p.chunks )
560  {
561  }
562 
563  Parameter &
564  operator=( Parameter const & p )
565  {
566  chunks = p.chunks;
567  return *this;
568  }
569 
570  std::unique_ptr< AbstractParameter >
571  clone() const override
572  {
573  return std::unique_ptr< AbstractParameter >(
575  }
576 
577  // output parameter
578  std::shared_ptr< ChunkTable > chunks = std::make_shared< ChunkTable >();
579 };
580 
589 class OPENPMDAPI_EXPORT IOTask
590 {
591 public:
598  template< Operation op >
599  explicit IOTask(Writable* w,
600  Parameter< op > const & p)
601  : writable{w},
602  operation{op},
603  parameter{p.clone()}
604  { }
605 
606  template< Operation op >
607  explicit IOTask(AttributableInterface* a,
608  Parameter< op > const & p)
609  : writable{getWritable(a)},
610  operation{op},
611  parameter{p.clone()}
612  { }
613 
614  explicit IOTask(IOTask const & other) :
615  writable{other.writable},
616  operation{other.operation},
617  parameter{other.parameter}
618  {}
619 
620  IOTask& operator=(IOTask const & other)
621  {
622  writable = other.writable;
623  operation = other.operation;
624  parameter = other.parameter;
625  return *this;
626  }
627 
628  Writable* writable;
629  Operation operation;
630  std::shared_ptr< AbstractParameter > parameter;
631 }; // IOTask
632 } // namespace openPMD
openPMD::Parameter< Operation::CLOSE_FILE >
Definition: IOTask.hpp:149
openPMD::Datatype
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:45
openPMD::Parameter< Operation::LIST_ATTS >
Definition: IOTask.hpp:514
openPMD::Writable
Layer to mirror structure of logical data and persistent data in file.
Definition: Writable.hpp:64
openPMD::Parameter< Operation::GET_BUFFER_VIEW >
Definition: IOTask.hpp:420
openPMD::Parameter< Operation::ADVANCE >
Definition: IOTask.hpp:532
openPMD::Parameter< Operation::READ_DATASET >
Definition: IOTask.hpp:373
openPMD::Parameter< Operation::DELETE_FILE >
Definition: IOTask.hpp:163
openPMD::Parameter< Operation::WRITE_ATT >
Definition: IOTask.hpp:467
openPMD::OPENPMDAPI_EXPORT_ENUM_CLASS
OPENPMDAPI_EXPORT_ENUM_CLASS(Operation)
Type of IO operation between logical and persistent data.
Definition: IOTask.hpp:48
openPMD::Parameter< Operation::AVAILABLE_CHUNKS >
Definition: IOTask.hpp:555
openPMD::Parameter< Operation::CREATE_FILE >
Definition: IOTask.hpp:108
openPMD::IterationEncoding
IterationEncoding
Encoding scheme of an Iterations Series'.
Definition: IterationEncoding.hpp:32
openPMD::Parameter< Operation::DELETE_PATH >
Definition: IOTask.hpp:233
openPMD::Parameter< Operation::WRITE_DATASET >
Definition: IOTask.hpp:344
openPMD::Parameter< Operation::LIST_PATHS >
Definition: IOTask.hpp:249
openPMD::Parameter< Operation::LIST_DATASETS >
Definition: IOTask.hpp:402
openPMD
Public definitions of openPMD-api.
Definition: Date.cpp:29
openPMD::Parameter< Operation::EXTEND_DATASET >
Definition: IOTask.hpp:291
openPMD::Parameter< Operation::DELETE_ATT >
Definition: IOTask.hpp:451
openPMD::Parameter< Operation::OPEN_DATASET >
Definition: IOTask.hpp:307
openPMD::Parameter< Operation::DELETE_DATASET >
Definition: IOTask.hpp:328
openPMD::Parameter< Operation::CREATE_DATASET >
Definition: IOTask.hpp:266
openPMD::Parameter< Operation::CREATE_PATH >
Definition: IOTask.hpp:179
openPMD::IOTask
Self-contained description of a single IO operation.
Definition: IOTask.hpp:589
openPMD::AdvanceMode
AdvanceMode
In step-based mode (i.e.
Definition: Streaming.hpp:33
openPMD::AbstractParameter
Definition: IOTask.hpp:79
openPMD::Parameter
Typesafe description of all required arguments for a specified Operation.
Definition: IOTask.hpp:100
openPMD::Parameter< Operation::OPEN_PATH >
Definition: IOTask.hpp:217
openPMD::Parameter< Operation::CLOSE_PATH >
Definition: IOTask.hpp:195
openPMD::Parameter< Operation::OPEN_FILE >
Definition: IOTask.hpp:126
openPMD::Parameter< Operation::ADVANCE >::mode
AdvanceMode mode
input parameter
Definition: IOTask.hpp:548
openPMD::IOTask::IOTask
IOTask(Writable *w, Parameter< op > const &p)
Constructor for self-contained description of single IO operation.
Definition: IOTask.hpp:599
openPMD::Parameter< Operation::READ_ATT >
Definition: IOTask.hpp:486