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 
109  PreloadAdiosAttributes( PreloadAdiosAttributes && other ) = default;
111  operator=( PreloadAdiosAttributes && other ) = default;
112 
123  void
124  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( std::is_same< T, signed char >::value )
155  {
156  // workaround: we use Datatype::CHAR to represent ADIOS2 signed char
157  // (ADIOS2 does not have chars with unspecified signed-ness
158  // anyway)
159  determinedDatatype = Datatype::CHAR;
160  }
161  if( location.dt != determinedDatatype )
162  {
163  std::stringstream errorMsg;
164  errorMsg << "[ADIOS2] Wrong datatype for attribute: " << name
165  << "(location.dt=" << location.dt
166  << ", T=" << determineDatatype< T >() << ")";
167  throw std::runtime_error( errorMsg.str() );
168  }
170  res.shape = location.shape;
171  res.data =
172  reinterpret_cast< T const * >( &m_rawBuffer[ location.offset ] );
173  return res;
174  }
175 } // namespace detail
176 } // namespace openPMD
177 
178 #endif // openPMD_HAVE_ADIOS2
openPMD::Datatype
Datatype
Concrete datatype of an object available at runtime.
Definition: Datatype.hpp:45
openPMD::detail::PreloadAdiosAttributes::getAttribute
AttributeWithShape< T > getAttribute(std::string const &name) const
Get an attribute that has been buffered previously.
Definition: ADIOS2PreloadAttributes.hpp:144
openPMD::detail::PreloadAdiosAttributes
Class that is responsible for scheduling and buffering openPMD attribute loads from ADIOS2,...
Definition: ADIOS2PreloadAttributes.hpp:63
openPMD::UnitDimension::T
@ T
time
openPMD
Public definitions of openPMD-api.
Definition: Date.cpp:29
openPMD::detail::AttributeWithShape
Pointer to an attribute's data along with its shape.
Definition: ADIOS2PreloadAttributes.hpp:45
openPMD::detail::PreloadAdiosAttributes::AttributeLocation
Internally used struct to store meta information on a buffered attribute.
Definition: ADIOS2PreloadAttributes.hpp:71
openPMD::detail::PreloadAdiosAttributes::preloadAttributes
void preloadAttributes(adios2::IO &IO, adios2::Engine &engine)
Schedule attributes for preloading.
Definition: ADIOS2PreloadAttributes.cpp:211