23 #include "openPMD/IO/ADIOS/ADIOS2Auxiliary.hpp" 24 #include "openPMD/IO/ADIOS/ADIOS2FilePosition.hpp" 25 #include "openPMD/IO/ADIOS/ADIOS2PreloadAttributes.hpp" 26 #include "openPMD/IO/AbstractIOHandler.hpp" 27 #include "openPMD/IO/AbstractIOHandlerImpl.hpp" 28 #include "openPMD/IO/AbstractIOHandlerImplCommon.hpp" 29 #include "openPMD/IO/FlushParametersInternal.hpp" 30 #include "openPMD/IO/IOTask.hpp" 31 #include "openPMD/IO/InvalidatableFile.hpp" 32 #include "openPMD/IterationEncoding.hpp" 33 #include "openPMD/auxiliary/JSON_internal.hpp" 34 #include "openPMD/backend/Writable.hpp" 35 #include "openPMD/config.hpp" 37 #if openPMD_HAVE_ADIOS2 43 #include <nlohmann/json.hpp> 53 #include <unordered_map> 59 #if openPMD_HAVE_ADIOS2 61 class ADIOS2IOHandler;
65 template <
typename,
typename>
88 namespace ADIOS2Schema
90 using schema_t = uint64_t;
94 constexpr schema_t schema_0000_00_00 = 00000000;
98 constexpr schema_t schema_2021_02_09 = 20210209;
100 enum class SupportedSchema : char
106 using SupportedSchema = ADIOS2Schema::SupportedSchema;
111 template <
typename,
typename>
137 std::string engineType,
138 std::string specifiedExtension);
140 #endif // openPMD_HAVE_MPI 145 std::string engineType,
146 std::string specifiedExtension);
158 bool checkFile(std::string fullFilePath)
const;
190 void deleteAttribute(
216 void availableChunks(
226 adios2::Mode adios2AccessMode(std::string
const &fullPath);
228 enum class FlushTarget : unsigned char
236 FlushTarget m_flushTarget = FlushTarget::Disk;
239 adios2::ADIOS m_ADIOS;
241 std::optional<MPI_Comm> m_communicator;
251 std::string m_engineType;
255 std::string m_userSpecifiedExtension;
260 std::optional<ADIOS2Schema::schema_t> m_schema;
262 enum class UseSpan : char
269 UseSpan m_useSpanBasedPutByDefault = UseSpan::Auto;
271 enum class AttributeLayout : char
277 inline SupportedSchema schema()
const 279 if (!m_schema.has_value())
281 return SupportedSchema::s_0000_00_00;
283 switch (m_schema.value())
285 case ADIOS2Schema::schema_0000_00_00:
286 return SupportedSchema::s_0000_00_00;
287 case ADIOS2Schema::schema_2021_02_09:
288 return SupportedSchema::s_2021_02_09;
290 throw std::runtime_error(
291 "[ADIOS2] Encountered unsupported schema version: " +
292 std::to_string(m_schema.value()));
296 inline AttributeLayout attributeLayout()
const 300 case SupportedSchema::s_0000_00_00:
301 return AttributeLayout::ByAdiosAttributes;
302 case SupportedSchema::s_2021_02_09:
303 return AttributeLayout::ByAdiosVariables;
305 throw std::runtime_error(
"Unreachable!");
308 struct ParameterizedOperator
311 adios2::Params params;
314 std::vector<ParameterizedOperator> defaultOperators;
321 template <
typename Key>
324 if (cfg.
json().is_object() && cfg.
json().contains(key))
334 template <
typename Key>
337 return config<Key>(std::forward<Key>(key), m_config);
347 std::optional<std::vector<ParameterizedOperator>>
351 std::optional<std::vector<ParameterizedOperator>> getOperators();
353 std::string fileSuffix(
bool verbose =
true)
const;
377 std::unique_ptr<detail::BufferedActions>>
380 std::map<std::string, adios2::Operator> m_operators;
385 filePositionToString(std::shared_ptr<ADIOS2FilePosition>)
override;
387 std::shared_ptr<ADIOS2FilePosition> extendFilePosition(
388 std::shared_ptr<ADIOS2FilePosition>
const &pos,
389 std::string extend)
override;
393 std::optional<adios2::Operator>
394 getCompressionOperator(std::string
const &compression);
400 std::string nameOfVariable(
Writable *writable);
411 std::string nameOfAttribute(
Writable *writable, std::string attribute);
417 ADIOS2FilePosition::GD groupOrDataset(
Writable *);
419 enum class IfFileNotOpen : bool
427 void dropFileData(InvalidatableFile file);
437 template <
typename T>
438 adios2::Variable<T> verifyDataset(
439 Offset
const &offset,
440 Extent
const &extent,
442 std::string
const &var);
449 namespace ADIOS2Defaults
451 using const_str =
char const *
const;
452 constexpr const_str str_engine =
"engine";
453 constexpr const_str str_type =
"type";
454 constexpr const_str str_params =
"parameters";
455 constexpr const_str str_usesteps =
"usesteps";
456 constexpr const_str str_flushtarget =
"preferred_flush_target";
457 constexpr const_str str_usesstepsAttribute =
"__openPMD_internal/useSteps";
458 constexpr const_str str_adios2Schema =
459 "__openPMD_internal/openPMD2_adios2_schema";
460 constexpr const_str str_isBooleanOldLayout =
"__is_boolean__";
461 constexpr const_str str_isBooleanNewLayout =
462 "__openPMD_internal/is_boolean";
469 template <
typename T>
470 inline constexpr
bool IsUnsupportedComplex_v =
471 std::is_same_v<T, std::complex<long double>> ||
472 std::is_same_v<T, std::vector<std::complex<long double>>>;
476 template <
typename T>
481 adios2::Engine &engine,
482 std::string
const &fileName);
484 static constexpr
char const *errorMsg =
"ADIOS2: readDataset()";
489 template <
typename T>
493 std::shared_ptr<Attribute::resource> resource);
495 template <
int n,
typename... Params>
501 template <
typename T>
507 template <
int n,
typename... Params>
508 static void call(Params &&...);
513 template <
typename T>
518 std::shared_ptr<Attribute::resource> resource);
520 template <
int n,
typename... Params>
526 template <
typename T>
530 template <
int n,
typename... Params>
531 static void call(Params &&...);
536 template <
typename T>
540 const std::string &varName,
543 static constexpr
char const *errorMsg =
"ADIOS2: openDataset()";
548 template <
typename T>
551 template <
int n,
typename... Params>
552 static void call(Params &&...);
572 template <
typename T>
575 std::string
const &name,
576 std::vector<ADIOS2IOHandlerImpl::ParameterizedOperator>
const 578 adios2::Dims
const &shape = adios2::Dims(),
579 adios2::Dims
const &start = adios2::Dims(),
580 adios2::Dims
const &count = adios2::Dims(),
581 bool const constantDims =
false);
583 static constexpr
char const *errorMsg =
"ADIOS2: defineVariable()";
588 template <
typename T>
592 adios2::Engine &engine,
593 std::string
const &varName,
596 template <
int n,
typename... Params>
597 static void call(Params &&...);
608 template <
typename T>
611 static void createAttribute(
613 adios2::Engine &engine,
620 std::shared_ptr<Attribute::resource> resource);
628 auto attr = IO.InquireAttribute<
T>(name);
633 std::vector<T> data = attr.Data();
634 if (data.size() != 1)
638 return data[0] == val;
645 static void createAttribute(
649 std::complex<long double>)
651 throw std::runtime_error(
652 "[ADIOS2] Internal error: no support for long double complex " 659 std::shared_ptr<Attribute::resource>)
661 throw std::runtime_error(
662 "[ADIOS2] Internal error: no support for long double complex " 667 attributeUnchanged(adios2::IO &, std::string, std::complex<long double>)
669 throw std::runtime_error(
670 "[ADIOS2] Internal error: no support for long double complex " 678 static void createAttribute(
682 const std::vector<std::complex<long double>> &)
684 throw std::runtime_error(
685 "[ADIOS2] Internal error: no support for long double complex " 686 "vector attribute types");
692 std::shared_ptr<Attribute::resource>)
694 throw std::runtime_error(
695 "[ADIOS2] Internal error: no support for long double complex " 696 "vector attribute types");
699 static bool attributeUnchanged(
700 adios2::IO &, std::string, std::vector<std::complex<long double>>)
702 throw std::runtime_error(
703 "[ADIOS2] Internal error: no support for long double complex " 704 "vector attribute types");
708 template <
typename T>
711 static void createAttribute(
713 adios2::Engine &engine,
715 const std::vector<T> &value);
720 std::shared_ptr<Attribute::resource> resource);
723 attributeUnchanged(adios2::IO &IO, std::string name, std::vector<T> val)
725 auto attr = IO.InquireAttribute<
T>(name);
730 std::vector<T> data = attr.Data();
731 if (data.size() != val.size())
735 for (
size_t i = 0; i < val.size(); ++i)
737 if (data[i] != val[i])
749 static void createAttribute(
751 adios2::Engine &engine,
753 const std::vector<std::string> &vec);
758 std::shared_ptr<Attribute::resource> resource);
760 static bool attributeUnchanged(
761 adios2::IO &IO, std::string name, std::vector<std::string> val)
763 auto attr = IO.InquireAttribute<std::string>(name);
768 std::vector<std::string> data = attr.Data();
769 if (data.size() != val.size())
773 for (
size_t i = 0; i < val.size(); ++i)
775 if (data[i] != val[i])
784 template <
typename T,
size_t n>
787 static void createAttribute(
789 adios2::Engine &engine,
791 const std::array<T, n> &value);
796 std::shared_ptr<Attribute::resource> resource);
798 static bool attributeUnchanged(
799 adios2::IO &IO, std::string name, std::array<T, n> val)
801 auto attr = IO.InquireAttribute<
T>(name);
806 std::vector<T> data = attr.Data();
807 if (data.size() != n)
811 for (
size_t i = 0; i < n; ++i)
813 if (data[i] != val[i])
824 using rep = detail::bool_representation;
826 static constexpr rep toRep(
bool b)
831 static constexpr
bool fromRep(rep r)
840 using rep = detail::bool_representation;
842 static constexpr rep toRep(
bool b)
847 static constexpr
bool fromRep(rep r)
852 static void createAttribute(
854 adios2::Engine &engine,
861 std::shared_ptr<Attribute::resource> resource);
864 attributeUnchanged(adios2::IO &IO, std::string name,
bool val)
866 auto attr = IO.InquireAttribute<rep>(name);
871 std::vector<rep> data = attr.Data();
872 if (data.size() != 1)
876 return data[0] == toRep(val);
948 Attribute::resource resource;
949 std::vector<char> bufferForVecString;
956 virtual void *update() = 0;
960 template <
typename T>
963 adios2::detail::Span<T> span;
967 void *update()
override;
982 using FlushTarget = ADIOS2IOHandlerImpl::FlushTarget;
1013 adios2::ADIOS &m_ADIOS;
1051 adios2::Mode m_mode;
1068 std::set<std::string> uncommittedAttributes;
1080 bool optimizeAttributesStreaming =
false;
1082 using ParsePreference =
1084 ParsePreference parsePreference = ParsePreference::UpFront;
1086 using AttributeMap_t = std::map<std::string, adios2::Params>;
1098 adios2::Engine &getEngine();
1099 adios2::Engine &requireActiveStep();
1101 template <
typename BA>
1102 void enqueue(BA &&ba);
1104 template <
typename BA>
1105 void enqueue(BA &&ba, decltype(m_buffer) &);
1107 template <
typename... Args>
1108 void flush(Args &&...args);
1116 FlushTarget flushTarget = FlushTarget::Disk;
1122 : level(level_in), flushTarget(flushTarget_in)
1144 template <
typename F>
1147 F &&performPutsGets,
1149 bool flushUnconditionally);
1177 AttributeMap_t
const &availableAttributes();
1179 std::vector<std::string>
1180 availableAttributesPrefixed(std::string
const &prefix);
1185 void invalidateAttributesMap();
1187 AttributeMap_t
const &availableVariables();
1189 std::vector<std::string>
1190 availableVariablesPrefixed(std::string
const &prefix);
1195 void invalidateVariablesMap();
1286 StreamStatus streamStatus = StreamStatus::OutsideOfStep;
1290 std::optional<adios2::Engine> m_engine;
1294 std::string m_engineType;
1308 std::optional<AttributeMap_t> m_availableAttributes;
1309 std::optional<AttributeMap_t> m_availableVariables;
1315 bool initializedDefaults =
false;
1319 bool finalized =
false;
1321 inline SupportedSchema schema()
const 1323 return m_impl->schema();
1329 void configure_IO_Read(std::optional<bool> userSpecifiedUsesteps);
1330 void configure_IO_Write(std::optional<bool> userSpecifiedUsesteps);
1332 using AttributeLayout = ADIOS2IOHandlerImpl::AttributeLayout;
1333 inline AttributeLayout attributeLayout()
const 1335 return m_impl->attributeLayout();
1340 #endif // openPMD_HAVE_ADIOS2 1344 #if openPMD_HAVE_ADIOS2 1357 auto params = internal::defaultParsedFlushParams;
1358 this->flush(params);
1360 catch (std::exception
const &ex)
1362 std::cerr <<
"[~ADIOS2IOHandler] An error occurred: " << ex.what()
1367 std::cerr <<
"[~ADIOS2IOHandler] An error occurred." << std::endl;
1375 #if openPMD_HAVE_MPI 1382 std::string engineType,
1383 std::string specifiedExtension);
1391 std::string engineType,
1392 std::string specifiedExtension);
Definition: ADIOS2IOHandler.hpp:66
Extend nlohmann::json with tracing of which keys have been accessed by operator[]().
Definition: JSON_internal.hpp:67
Wrapper around a shared pointer to:
Definition: InvalidatableFile.hpp:43
Definition: ADIOS2IOHandler.hpp:586
Definition: IOTask.hpp:611
FlushLevel
Determine what items should be flushed upon Series::flush()
Definition: AbstractIOHandler.hpp:47
Definition: ADIOS2IOHandler.hpp:954
Definition: ADIOS2IOHandler.hpp:936
Definition: ADIOS2IOHandler.hpp:499
nlohmann::json & json()
Access the underlying JSON value.
Definition: JSON_internal.hpp:79
Definition: ADIOS2IOHandler.hpp:511
StreamStatus
Definition: ADIOS2IOHandler.hpp:1221
Access
File access mode to use during IO.
Definition: Access.hpp:29
Definition: IOTask.hpp:203
Definition: ADIOS2IOHandler.hpp:917
Definition: IOTask.hpp:673
Definition: ADIOS2IOHandler.hpp:555
Definition: IOTask.hpp:294
Definition: IOTask.hpp:407
std::string m_file
The full path to the file created on disk, including the containing directory and the file extension...
Definition: ADIOS2IOHandler.hpp:993
Definition: IOTask.hpp:148
Definition: ADIOS2IOHandler.hpp:108
Definition: ADIOS2IOHandler.hpp:975
std::vector< std::unique_ptr< BufferedAction > > m_buffer
The default queue for deferred actions.
Definition: ADIOS2IOHandler.hpp:1019
Definition: ADIOS2IOHandler.hpp:909
Definition: ADIOS2IOHandler.cpp:898
AdvanceStatus
In step-based mode (i.e.
Definition: Streaming.hpp:20
Definition: ADIOS2IOHandler.hpp:78
AdvanceMode
In step-based mode (i.e.
Definition: Streaming.hpp:34
Definition: ADIOS2IOHandler.hpp:928
Definition: ADIOS2IOHandler.hpp:901
Definition: ADIOS2IOHandler.hpp:944
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:45
Definition: IOTask.hpp:175
Definition: Container.cpp:51
std::vector< BufferedUniquePtrPut > m_uniquePtrPuts
When receiving a unique_ptr, we know that the buffer is ours and ours alone.
Definition: ADIOS2IOHandler.hpp:1043
Definition: IOTask.hpp:220
Definition: IOTask.hpp:522
Interface for communicating between logical and physically persistent data.
Definition: AbstractIOHandler.hpp:179
Definition: ADIOS2IOHandler.hpp:1110
std::map< unsigned, std::unique_ptr< I_UpdateSpan > > m_updateSpans
The base pointer of an ADIOS2 span might change after reallocations.
Definition: ADIOS2IOHandler.hpp:1060
Public definitions of openPMD-api.
Layer to mirror structure of logical data and persistent data in file.
Definition: Writable.hpp:64
Definition: IOTask.hpp:258
Definition: ADIOS2IOHandler.hpp:1342
Definition: IOTask.hpp:449
Definition: ADIOS2IOHandler.hpp:546
std::string backendName() const override
The currently used backend.
Definition: ADIOS2IOHandler.hpp:1394
Typesafe description of all required arguments for a specified Operation.
Definition: IOTask.hpp:120
Definition: IOTask.hpp:591
Class that is responsible for scheduling and buffering openPMD attribute loads from ADIOS2...
Definition: ADIOS2PreloadAttributes.hpp:64
Definition: ADIOS2IOHandler.hpp:524
std::vector< BufferedAttributeRead > m_attributeReads
Definition: ADIOS2IOHandler.hpp:1036
IterationEncoding
Encoding scheme of an Iterations Series'.
Definition: IterationEncoding.hpp:32
Definition: Writable.hpp:44
static bool attributeUnchanged(adios2::IO &IO, std::string name, T val)
Is the attribute given by parameters name and val already defined exactly in that way within the give...
Definition: ADIOS2IOHandler.hpp:626
Definition: ADIOS2IOHandler.hpp:534
std::map< std::string, BufferedAttributeWrite > m_attributeWrites
Buffer for attributes to be written in the new (variable-based) attribute layout. ...
Definition: ADIOS2IOHandler.hpp:1030
Definition: ADIOS2IOHandler.hpp:474
Definition: IOTask.hpp:128
Definition: IOTask.hpp:634
Definition: ADIOS2IOHandler.cpp:2430
Definition: IOTask.hpp:471
Definition: IOTask.hpp:386
Definition: IOTask.hpp:239
std::string m_IOName
ADIOS requires giving names to instances of adios2::IO.
Definition: ADIOS2IOHandler.hpp:1012
Definition: ADIOS2IOHandler.hpp:961
Definition: IOTask.hpp:541
Definition: IOTask.hpp:333
Definition: IOTask.hpp:491
Definition: ADIOS2IOHandler.hpp:74
Definition: ADIOS2IOHandler.hpp:887
Definition: IOTask.hpp:569
Definition: ADIOS2IOHandler.hpp:487
Definition: FlushParametersInternal.hpp:31
Definition: IOTask.hpp:367
Definition: IOTask.hpp:275
Definition: IOTask.hpp:426
std::vector< std::unique_ptr< BufferedAction > > m_alreadyEnqueued
This contains deferred actions that have already been enqueued into ADIOS2, but not yet performed in ...
Definition: ADIOS2IOHandler.hpp:1050
Definition: IOTask.hpp:313