|
| 1 | +/* Copyright 2020-2021 Franz Poeschel |
| 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/config.hpp" |
| 24 | +#if openPMD_HAVE_ADIOS2 |
| 25 | + |
| 26 | +#include <adios2.h> |
| 27 | +#include <functional> |
| 28 | +#include <map> |
| 29 | +#include <sstream> |
| 30 | +#include <stddef.h> |
| 31 | +#include <type_traits> |
| 32 | + |
| 33 | +#include "openPMD/Datatype.hpp" |
| 34 | +#include "openPMD/IO/ADIOS/ADIOS2Auxiliary.hpp" |
| 35 | + |
| 36 | +namespace openPMD::detail |
| 37 | +{ |
| 38 | +/** |
| 39 | + * @brief Pointer to an attribute's data along with its shape. |
| 40 | + * |
| 41 | + * @tparam T Underlying attribute data type. |
| 42 | + */ |
| 43 | +template <typename T> |
| 44 | +struct AttributeWithShape |
| 45 | +{ |
| 46 | + adios2::Dims shape; |
| 47 | + T const *data; |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | + * Class that is responsible for scheduling and buffering openPMD attribute |
| 52 | + * loads from ADIOS2, if using ADIOS variables to store openPMD attributes. |
| 53 | + * |
| 54 | + * Reasoning: ADIOS variables can be of any shape and size, and ADIOS cannot |
| 55 | + * know which variables to buffer. While it will preload and buffer scalar |
| 56 | + * variables, openPMD also stores vector-type attributes which are not |
| 57 | + * preloaded. Since in Streaming setups, every variable load requires full |
| 58 | + * communication back to the writer, this can quickly become very expensive. |
| 59 | + * Hence, do this manually. |
| 60 | + * |
| 61 | + */ |
| 62 | +class PreloadAdiosAttributes |
| 63 | +{ |
| 64 | +public: |
| 65 | + /** |
| 66 | + * Internally used struct to store meta information on a buffered |
| 67 | + * attribute. Public for simplicity (helper struct in the |
| 68 | + * implementation uses it). |
| 69 | + */ |
| 70 | + struct AttributeLocation |
| 71 | + { |
| 72 | + adios2::Dims shape; |
| 73 | + size_t offset; |
| 74 | + Datatype dt; |
| 75 | + char *destroy = nullptr; |
| 76 | + |
| 77 | + AttributeLocation() = delete; |
| 78 | + AttributeLocation(adios2::Dims shape, size_t offset, Datatype dt); |
| 79 | + |
| 80 | + AttributeLocation(AttributeLocation const &other) = delete; |
| 81 | + AttributeLocation &operator=(AttributeLocation const &other) = delete; |
| 82 | + |
| 83 | + AttributeLocation(AttributeLocation &&other); |
| 84 | + AttributeLocation &operator=(AttributeLocation &&other); |
| 85 | + |
| 86 | + ~AttributeLocation(); |
| 87 | + }; |
| 88 | + |
| 89 | +private: |
| 90 | + /* |
| 91 | + * Allocate one large buffer instead of hundreds of single heap |
| 92 | + * allocations. |
| 93 | + * This will comply with alignment requirements, since |
| 94 | + * std::allocator<char>::allocate() will call the untyped new operator |
| 95 | + * ::operator new(std::size_t) |
| 96 | + * https://en.cppreference.com/w/cpp/memory/allocator/allocate |
| 97 | + */ |
| 98 | + std::vector<char> m_rawBuffer; |
| 99 | + std::map<std::string, AttributeLocation> m_offsets; |
| 100 | + |
| 101 | +public: |
| 102 | + explicit PreloadAdiosAttributes() = default; |
| 103 | + PreloadAdiosAttributes(PreloadAdiosAttributes const &other) = delete; |
| 104 | + PreloadAdiosAttributes & |
| 105 | + operator=(PreloadAdiosAttributes const &other) = delete; |
| 106 | + |
| 107 | + PreloadAdiosAttributes(PreloadAdiosAttributes &&other) = default; |
| 108 | + PreloadAdiosAttributes &operator=(PreloadAdiosAttributes &&other) = default; |
| 109 | + |
| 110 | + /** |
| 111 | + * @brief Schedule attributes for preloading. |
| 112 | + * |
| 113 | + * This will invalidate all previously buffered attributes. |
| 114 | + * This will *not* flush the scheduled loads. This way, attributes can |
| 115 | + * be loaded along with the next adios2::Engine flush. |
| 116 | + * |
| 117 | + * @param IO |
| 118 | + * @param engine |
| 119 | + */ |
| 120 | + void preloadAttributes(adios2::IO &IO, adios2::Engine &engine); |
| 121 | + |
| 122 | + /** |
| 123 | + * @brief Get an attribute that has been buffered previously. |
| 124 | + * |
| 125 | + * @tparam T The underlying primitive datatype of the attribute. |
| 126 | + * Will fail if the type found in ADIOS does not match. |
| 127 | + * @param name The full name of the attribute. |
| 128 | + * @return Pointer to the buffered attribute along with information on |
| 129 | + * the attribute's shape. Valid only until any non-const member |
| 130 | + * of PreloadAdiosAttributes is called. |
| 131 | + */ |
| 132 | + template <typename T> |
| 133 | + AttributeWithShape<T> getAttribute(std::string const &name) const; |
| 134 | + |
| 135 | + Datatype attributeType(std::string const &name) const; |
| 136 | +}; |
| 137 | +} // namespace openPMD::detail |
| 138 | + |
| 139 | +#endif // openPMD_HAVE_ADIOS2 |
0 commit comments