openPMD-api
ADIOS2PreloadAttributes.hpp
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
37 {
38 namespace detail
39 {
45  template <typename T>
47  {
48  adios2::Dims shape;
49  T const *data;
50  };
51 
65  {
66  public:
73  {
74  adios2::Dims shape;
75  size_t offset;
76  Datatype dt;
77  char *destroy = nullptr;
78 
79  AttributeLocation() = delete;
80  AttributeLocation(adios2::Dims shape, size_t offset, Datatype dt);
81 
82  AttributeLocation(AttributeLocation const &other) = delete;
84  operator=(AttributeLocation const &other) = delete;
85 
87  AttributeLocation &operator=(AttributeLocation &&other);
88 
90  };
91 
92  private:
93  /*
94  * Allocate one large buffer instead of hundreds of single heap
95  * allocations.
96  * This will comply with alignment requirements, since
97  * std::allocator<char>::allocate() will call the untyped new operator
98  * ::operator new(std::size_t)
99  * https://en.cppreference.com/w/cpp/memory/allocator/allocate
100  */
101  std::vector<char> m_rawBuffer;
102  std::map<std::string, AttributeLocation> m_offsets;
103 
104  public:
105  explicit PreloadAdiosAttributes() = default;
106  PreloadAdiosAttributes(PreloadAdiosAttributes const &other) = delete;
108  operator=(PreloadAdiosAttributes const &other) = delete;
109 
112  operator=(PreloadAdiosAttributes &&other) = default;
113 
124  void preloadAttributes(adios2::IO &IO, adios2::Engine &engine);
125 
136  template <typename T>
137  AttributeWithShape<T> getAttribute(std::string const &name) const;
138 
139  Datatype attributeType(std::string const &name) const;
140  };
141 
142  template <typename T>
144  PreloadAdiosAttributes::getAttribute(std::string const &name) const
145  {
146  auto it = m_offsets.find(name);
147  if (it == m_offsets.end())
148  {
149  throw std::runtime_error(
150  "[ADIOS2] Requested attribute not found: " + name);
151  }
152  AttributeLocation const &location = it->second;
153  Datatype determinedDatatype = determineDatatype<T>();
154  if (location.dt != determinedDatatype)
155  {
156  std::stringstream errorMsg;
157  errorMsg << "[ADIOS2] Wrong datatype for attribute: " << name
158  << "(location.dt=" << location.dt
159  << ", T=" << determineDatatype<T>() << ")";
160  throw std::runtime_error(errorMsg.str());
161  }
163  res.shape = location.shape;
164  res.data = reinterpret_cast<T const *>(&m_rawBuffer[location.offset]);
165  return res;
166  }
167 } // namespace detail
168 } // namespace openPMD
169 
170 #endif // openPMD_HAVE_ADIOS2
Pointer to an attribute&#39;s data along with its shape.
Definition: ADIOS2PreloadAttributes.hpp:46
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:45
Definition: Container.cpp:51
Public definitions of openPMD-api.
Class that is responsible for scheduling and buffering openPMD attribute loads from ADIOS2...
Definition: ADIOS2PreloadAttributes.hpp:64
Internally used struct to store meta information on a buffered attribute.
Definition: ADIOS2PreloadAttributes.hpp:72
AttributeWithShape< T > getAttribute(std::string const &name) const
Get an attribute that has been buffered previously.
Definition: ADIOS2PreloadAttributes.hpp:144