23 #include "openPMD/Error.hpp"
24 #include "openPMD/IO/ADIOS/ADIOS2Auxiliary.hpp"
25 #include "openPMD/IO/ADIOS/ADIOS2FilePosition.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/ThrowError.hpp"
34 #include "openPMD/auxiliary/JSON_internal.hpp"
35 #include "openPMD/backend/Writable.hpp"
36 #include "openPMD/config.hpp"
39 #if openPMD_HAVE_ADIOS2
45 #include <nlohmann/json.hpp>
55 #include <unordered_map>
61 #if openPMD_HAVE_ADIOS2
63 std::optional<size_t> joinedDimension(adios2::Dims
const &dims);
65 class ADIOS2IOHandler;
69 template <
typename,
typename>
87 struct BufferedAttributeRead;
88 struct BufferedAttributeWrite;
95 template <
typename,
typename>
111 friend struct detail::BufferedAttributeRead;
114 using UseGroupTable = adios_defs::UseGroupTable;
115 using FlushTarget = adios_defs::FlushTarget;
124 std::string engineType,
125 std::string specifiedExtension);
132 std::string engineType,
133 std::string specifiedExtension);
145 bool checkFile(std::string fullFilePath)
const;
215 FlushTarget m_flushTarget = FlushTarget::Disk;
218 adios2::ADIOS m_ADIOS;
220 std::optional<MPI_Comm> m_communicator;
225 std::string m_engineType;
229 std::string m_userSpecifiedExtension;
235 std::optional<UseGroupTable> m_useGroupTable;
237 enum class UseSpan : char
244 UseSpan m_useSpanBasedPutByDefault = UseSpan::Auto;
246 enum class ModifiableAttributes : char
253 ModifiableAttributes m_modifiableAttributes =
254 ModifiableAttributes::Unspecified;
256 inline UseGroupTable useGroupTable()
const
258 if (!m_useGroupTable.has_value())
260 return UseGroupTable::No;
262 return m_useGroupTable.value();
265 bool m_writeAttributesFromThisRank =
true;
267 struct ParameterizedOperator
270 adios2::Params params;
273 std::vector<ParameterizedOperator> defaultOperators;
278 template <
typename Callback>
282 template <
typename Key>
285 if (cfg.
json().is_object() && cfg.
json().contains(key))
295 template <
typename Key>
298 return config<Key>(std::forward<Key>(key), m_config);
308 std::optional<std::vector<ParameterizedOperator>>
312 std::optional<std::vector<ParameterizedOperator>> getOperators();
314 std::string fileSuffix(
bool verbose =
true)
const;
336 std::unordered_map<InvalidatableFile, std::unique_ptr<detail::ADIOS2File>>
339 std::map<std::string, adios2::Operator> m_operators;
344 filePositionToString(std::shared_ptr<ADIOS2FilePosition>)
override;
346 std::shared_ptr<ADIOS2FilePosition> extendFilePosition(
347 std::shared_ptr<ADIOS2FilePosition>
const &pos,
348 std::string extend)
override;
352 std::optional<adios2::Operator>
353 getCompressionOperator(std::string
const &compression);
359 std::string nameOfVariable(
Writable *writable);
370 std::string nameOfAttribute(
Writable *writable, std::string attribute);
376 GroupOrDataset groupOrDataset(
Writable *);
378 enum class IfFileNotOpen : bool
397 template <
typename T>
398 adios2::Variable<T> verifyDataset(
399 Offset
const &offset,
400 Extent
const &extent,
402 std::string
const &varName)
405 auto requiredType = adios2::GetType<T>();
406 auto actualType = IO.VariableType(varName);
408 if (requiredType != actualType)
410 std::stringstream errorMessage;
411 errorMessage <<
"Trying to access a dataset with wrong type "
412 "(trying to access dataset with type '"
413 << requiredType <<
"', but has type '"
414 << actualType <<
"')";
416 error::AffectedObject::Dataset,
417 error::Reason::UnexpectedContent,
422 adios2::Variable<T> var = IO.InquireVariable<
T>(varName);
423 if (!var.operator
bool())
426 throw std::runtime_error(
427 "[ADIOS2] Internal error: Failed opening ADIOS2 variable.");
430 adios2::Dims shape = var.Shape();
431 auto actualDim = shape.size();
433 auto requiredDim = extent.size();
434 if (requiredDim != actualDim)
437 error::AffectedObject::Dataset,
438 error::Reason::UnexpectedContent,
440 "Trying to access a dataset with wrong dimensionality "
441 "(trying to access dataset with dimensionality " +
442 std::to_string(requiredDim) +
443 ", but has dimensionality " +
444 std::to_string(actualDim) +
")");
447 auto joinedDim = joinedDimension(shape);
448 if (joinedDim.has_value())
452 throw std::runtime_error(
453 "[ADIOS2] Offset must be an empty vector in case of joined "
456 for (
unsigned int i = 0; i < actualDim; i++)
458 if (*joinedDim != i && extent[i] != shape[i])
460 throw std::runtime_error(
461 "[ADIOS2] store_chunk extent of non-joined dimensions "
462 "must be equivalent to the total extent.");
468 for (
unsigned int i = 0; i < actualDim; i++)
470 if (!(joinedDim.has_value() && *joinedDim == i) &&
471 offset[i] + extent[i] > shape[i])
473 throw std::runtime_error(
474 "[ADIOS2] Dataset access out of bounds.");
480 {adios2::Dims(offset.begin(), offset.end()),
481 adios2::Dims(extent.begin(), extent.end())});
487 bool noGroupBased =
false;
488 bool blosc2bp5 =
false;
489 } printedWarningsAlready;
496 template <
typename T>
497 inline constexpr
bool IsUnsupportedComplex_v =
498 std::is_same_v<T, std::complex<long double>> ||
499 std::is_same_v<T, std::vector<std::complex<long double>>>;
503 template <
typename T>
508 Attribute::resource &resource);
510 template <
int n,
typename... Params>
516 template <
typename T>
522 template <
int n,
typename... Params>
523 static void call(Params &&...);
528 template <
typename T>
532 std::string
const &varName,
535 static constexpr
char const *errorMsg =
"ADIOS2: openDataset()";
555 template <
typename T>
558 std::string
const &name,
559 std::vector<ADIOS2IOHandlerImpl::ParameterizedOperator>
const
561 adios2::Dims
const &shape = adios2::Dims(),
562 adios2::Dims
const &start = adios2::Dims(),
563 adios2::Dims
const &count = adios2::Dims(),
564 bool const constantDims =
false);
566 static constexpr
char const *errorMsg =
"ADIOS2: defineVariable()";
571 template <
typename T>
575 adios2::Engine &engine,
576 std::string
const &varName,
579 template <
int n,
typename... Params>
580 static void call(Params &&...);
591 template <
typename T>
600 auto attr = IO.InquireAttribute<
T>(name);
605 std::vector<T> data = attr.Data();
606 if (data.size() != 1)
610 return data[0] == val;
620 throw std::runtime_error(
621 "[ADIOS2] Internal error: no support for long double complex "
630 adios2::IO &, std::string, std::vector<std::complex<long double>>)
632 throw std::runtime_error(
633 "[ADIOS2] Internal error: no support for long double complex "
634 "vector attribute types");
638 template <
typename T>
644 auto attr = IO.InquireAttribute<
T>(name);
649 std::vector<T> data = attr.Data();
650 if (data.size() != val.size())
654 for (
size_t i = 0; i < val.size(); ++i)
656 if (data[i] != val[i])
669 adios2::IO &IO, std::string name, std::vector<std::string> val)
671 auto attr = IO.InquireAttribute<std::string>(name);
676 std::vector<std::string> data = attr.Data();
677 if (data.size() != val.size())
681 for (
size_t i = 0; i < val.size(); ++i)
683 if (data[i] != val[i])
692 template <
typename T,
size_t n>
696 adios2::IO &IO, std::string name, std::array<T, n> val)
698 auto attr = IO.InquireAttribute<
T>(name);
703 std::vector<T> data = attr.Data();
704 if (data.size() != n)
708 for (
size_t i = 0; i < n; ++i)
710 if (data[i] != val[i])
721 using rep = detail::bool_representation;
723 static constexpr rep toRep(
bool b)
728 static constexpr
bool fromRep(rep r)
737 using rep = detail::bool_representation;
739 static constexpr rep toRep(
bool b)
744 static constexpr
bool fromRep(rep r)
752 auto attr = IO.InquireAttribute<rep>(name);
757 std::vector<rep> data = attr.Data();
758 if (data.size() != 1)
762 return data[0] == toRep(val);
770 #if openPMD_HAVE_ADIOS2
783 auto params = internal::defaultParsedFlushParams;
786 catch (std::exception
const &ex)
788 std::cerr <<
"[~ADIOS2IOHandler] An error occurred: " << ex.what()
793 std::cerr <<
"[~ADIOS2IOHandler] An error occurred." << std::endl;
808 std::string engineType,
809 std::string specifiedExtension);
817 std::string engineType,
818 std::string specifiedExtension);
Definition: ADIOS2IOHandler.hpp:769
std::future< void > flush(internal::ParsedFlushParams &) override
Process operations in queue according to FIFO.
Definition: ADIOS2IOHandler.cpp:2097
std::string backendName() const override
The currently used backend.
Definition: ADIOS2IOHandler.hpp:820
Definition: ADIOS2IOHandler.hpp:94
void readDataset(Writable *, Parameter< Operation::READ_DATASET > &) override
Read a chunk of data from an existing dataset.
Definition: ADIOS2IOHandler.cpp:1052
void openFile(Writable *, Parameter< Operation::OPEN_FILE > &) override
Open an existing file assuming it conforms to openPMD.
Definition: ADIOS2IOHandler.cpp:873
void writeAttribute(Writable *, Parameter< Operation::WRITE_ATT > const &) override
Create a single attribute and fill the value, possibly overwriting an existing attribute.
Definition: ADIOS2IOHandler.cpp:1015
void createFile(Writable *, Parameter< Operation::CREATE_FILE > const &) override
Create a new file in physical storage, possibly overriding an existing file.
Definition: ADIOS2IOHandler.cpp:577
void deletePath(Writable *, Parameter< Operation::DELETE_PATH > const &) override
Delete all objects within an existing path.
Definition: ADIOS2IOHandler.cpp:978
void deleteDataset(Writable *, Parameter< Operation::DELETE_DATASET > const &) override
Delete an existing dataset.
Definition: ADIOS2IOHandler.cpp:984
void listDatasets(Writable *, Parameter< Operation::LIST_DATASETS > &) override
List all datasets inside a group, non-recursively.
Definition: ADIOS2IOHandler.cpp:1348
void closePath(Writable *, Parameter< Operation::CLOSE_PATH > const &) override
Close an openPMD group.
Definition: ADIOS2IOHandler.cpp:1427
void advance(Writable *, Parameter< Operation::ADVANCE > &) override
Advance the file/stream that this writable belongs to.
Definition: ADIOS2IOHandler.cpp:1419
void deleteFile(Writable *, Parameter< Operation::DELETE_FILE > const &) override
Delete an existing file from physical storage.
Definition: ADIOS2IOHandler.cpp:972
void listAttributes(Writable *, Parameter< Operation::LIST_ATTS > ¶meters) override
List all attributes associated with an object.
Definition: ADIOS2IOHandler.cpp:1390
void openDataset(Writable *, Parameter< Operation::OPEN_DATASET > &) override
Open an existing dataset and determine its datatype and extent.
Definition: ADIOS2IOHandler.cpp:955
void extendDataset(Writable *, Parameter< Operation::EXTEND_DATASET > const &) override
Increase the extent of an existing dataset.
Definition: ADIOS2IOHandler.cpp:858
void availableChunks(Writable *, Parameter< Operation::AVAILABLE_CHUNKS > &) override
Report chunks that are available for loading from the dataset represented by this writable.
Definition: ADIOS2IOHandler.cpp:1459
void closeFile(Writable *, Parameter< Operation::CLOSE_FILE > const &) override
Close the file corresponding with the writable and release file handles.
Definition: ADIOS2IOHandler.cpp:900
adios2::Mode adios2AccessMode(std::string const &fullPath)
The ADIOS2 access type to chose for Engines opened within this instance.
Definition: ADIOS2IOHandler.cpp:1485
void createDataset(Writable *, Parameter< Operation::CREATE_DATASET > const &) override
Create a new dataset of given type, extent and storage properties.
Definition: ADIOS2IOHandler.cpp:730
void checkFile(Writable *, Parameter< Operation::CHECK_FILE > &) override
Check if the file specified by the parameter is already present on disk.
Definition: ADIOS2IOHandler.cpp:642
void writeDataset(Writable *, Parameter< Operation::WRITE_DATASET > &) override
Write a chunk of data into an existing dataset.
Definition: ADIOS2IOHandler.cpp:998
void listPaths(Writable *, Parameter< Operation::LIST_PATHS > &) override
List all paths/sub-groups inside a group, non-recursively.
Definition: ADIOS2IOHandler.cpp:1219
void createPath(Writable *, Parameter< Operation::CREATE_PATH > const &) override
Create all necessary groups for a path, possibly recursively.
Definition: ADIOS2IOHandler.cpp:696
void getBufferView(Writable *, Parameter< Operation::GET_BUFFER_VIEW > &) override
Get a view into a dataset buffer that can be filled by a user.
Definition: ADIOS2IOHandler.cpp:1131
void readAttribute(Writable *, Parameter< Operation::READ_ATT > &) override
Read the value of an existing attribute.
Definition: ADIOS2IOHandler.cpp:1196
void openPath(Writable *, Parameter< Operation::OPEN_PATH > const &) override
Open all contained groups in a path, possibly recursively.
Definition: ADIOS2IOHandler.cpp:927
void deregister(Writable *, Parameter< Operation::DEREGISTER > const &) override
Notify the backend that the Writable has been / will be deallocated.
Definition: ADIOS2IOHandler.cpp:1479
void deleteAttribute(Writable *, Parameter< Operation::DELETE_ATT > const &) override
Delete an existing attribute.
Definition: ADIOS2IOHandler.cpp:991
Interface for communicating between logical and physically persistent data.
Definition: AbstractIOHandler.hpp:186
Definition: AbstractIOHandlerImplCommon.hpp:38
std::string fullPath(InvalidatableFile)
Definition: AbstractIOHandlerImplCommon.hpp:177
Layer to mirror structure of logical data and persistent data in file.
Definition: Writable.hpp:75
Definition: ADIOS2File.hpp:137
Extend nlohmann::json with tracing of which keys have been accessed by operator[]().
Definition: JSON_internal.hpp:68
nlohmann::json & json()
Access the underlying JSON value.
Definition: JSON_internal.hpp:79
Public definitions of openPMD-api.
Definition: Date.cpp:29
Access
File access mode to use during IO.
Definition: Access.hpp:30
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:51
Wrapper around a shared pointer to:
Definition: InvalidatableFile.hpp:44
Definition: IOTask.hpp:597
Definition: IOTask.hpp:621
Definition: IOTask.hpp:133
Definition: IOTask.hpp:182
Definition: IOTask.hpp:237
Definition: IOTask.hpp:312
Definition: IOTask.hpp:114
Definition: IOTask.hpp:218
Definition: IOTask.hpp:502
Definition: IOTask.hpp:387
Definition: IOTask.hpp:199
Definition: IOTask.hpp:273
Definition: IOTask.hpp:641
Definition: IOTask.hpp:347
Definition: IOTask.hpp:471
Definition: IOTask.hpp:577
Definition: IOTask.hpp:451
Definition: IOTask.hpp:292
Definition: IOTask.hpp:366
Definition: IOTask.hpp:160
Definition: IOTask.hpp:254
Definition: IOTask.hpp:555
Definition: IOTask.hpp:429
Definition: IOTask.hpp:521
Definition: IOTask.hpp:406
Definition: ADIOS2IOHandler.hpp:502
Definition: ADIOS2IOHandler.hpp:593
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:598
Definition: ADIOS2IOHandler.hpp:515
Definition: ADIOS2File.hpp:67
Definition: ADIOS2File.hpp:88
Definition: ADIOS2IOHandler.hpp:70
Definition: ADIOS2IOHandler.hpp:527
Definition: ADIOS2File.hpp:75
Definition: ADIOS2IOHandler.hpp:82
Definition: ADIOS2IOHandler.cpp:1068
Definition: ADIOS2IOHandler.hpp:570
Definition: ADIOS2File.cpp:140
Definition: ADIOS2IOHandler.hpp:539
static void call(adios2::IO &IO, std::string const &name, std::vector< ADIOS2IOHandlerImpl::ParameterizedOperator > const &compressions, adios2::Dims const &shape=adios2::Dims(), adios2::Dims const &start=adios2::Dims(), adios2::Dims const &count=adios2::Dims(), bool const constantDims=false)
Define a Variable of type T within the passed IO.
Definition: ADIOS2IOHandler.cpp:1959
Definition: ADIOS2File.hpp:96
Definition: FlushParametersInternal.hpp:32