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