2020 */
2121
2222#include " openPMD/config.hpp"
23+ #include < algorithm>
2324#if openPMD_HAVE_ADIOS2
2425
2526#include " openPMD/IO/ADIOS/ADIOS2PreloadAttributes.hpp"
@@ -73,61 +74,50 @@ namespace
7374 template <typename T>
7475 static void call (
7576 adios2::IO &IO,
76- adios2::Engine &engine,
7777 std::string const &name,
7878 char *buffer,
7979 PreloadAdiosAttributes::AttributeLocation &location)
8080 {
81- adios2::Variable <T> var = IO.InquireVariable <T>(name);
82- if (!var )
81+ adios2::Attribute <T> attr = IO.InquireAttribute <T>(name);
82+ if (!attr )
8383 {
8484 throw std::runtime_error (
8585 " [ADIOS2] Variable not found: " + name);
8686 }
87- adios2::Dims const &shape = location.shape ;
88- adios2::Dims offset (shape.size (), 0 );
89- if (shape.size () > 0 )
90- {
91- var.SetSelection ({offset, shape});
92- }
93- T *dest = reinterpret_cast <T *>(buffer);
94- size_t numItems = 1 ;
95- for (auto extent : shape)
96- {
97- numItems *= extent;
98- }
9987 /*
10088 * MSVC does not like placement new of arrays, so we do it
10189 * in a loop instead.
10290 * https://developercommunity.visualstudio.com/t/c-placement-new-is-incorrectly-compiled/206439
10391 */
104- for (size_t i = 0 ; i < numItems; ++i)
92+ T *dest = reinterpret_cast <T *>(buffer);
93+ for (size_t i = 0 ; i < location.len ; ++i)
10594 {
10695 new (dest + i) T ();
10796 }
10897 location.destroy = buffer;
109- engine.Get (var, dest, adios2::Mode::Deferred);
98+ auto data = attr.Data ();
99+ std::copy_n (data.begin (), data.size (), dest);
110100 }
111101
112102 static constexpr char const *errorMsg = " ADIOS2" ;
113103 };
114104
115- struct VariableShape
105+ struct AttributeLen
116106 {
117107 template <typename T>
118- static adios2::Dims call (adios2::IO &IO, std::string const &name)
108+ static size_t call (adios2::IO &IO, std::string const &name)
119109 {
120- auto var = IO.InquireVariable <T>(name);
121- if (!var )
110+ auto attr = IO.InquireAttribute <T>(name);
111+ if (!attr )
122112 {
123113 throw std::runtime_error (
124114 " [ADIOS2] Variable not found: " + name);
125115 }
126- return var. Shape ();
116+ return attr. Data (). size ();
127117 }
128118
129119 template <unsigned long n, typename ... Args>
130- static adios2::Dims call (Args &&...)
120+ static size_t call (Args &&...)
131121 {
132122 return {};
133123 }
@@ -154,25 +144,22 @@ namespace
154144using AttributeLocation = PreloadAdiosAttributes::AttributeLocation;
155145
156146AttributeLocation::AttributeLocation (
157- adios2::Dims shape_in , size_t offset_in, Datatype dt_in)
158- : shape(std::move(shape_in) ), offset(offset_in), dt(dt_in)
147+ size_t len_in , size_t offset_in, Datatype dt_in)
148+ : len(len_in ), offset(offset_in), dt(dt_in)
159149{}
160150
161151AttributeLocation::AttributeLocation (AttributeLocation &&other)
162- : shape{std::move (other.shape )}
163- , offset{std::move (other.offset )}
164- , dt{std::move (other.dt )}
165- , destroy{std::move (other.destroy )}
152+ : len{other.len }, offset{other.offset }, dt{other.dt }, destroy{other.destroy }
166153{
167154 other.destroy = nullptr ;
168155}
169156
170157AttributeLocation &AttributeLocation::operator =(AttributeLocation &&other)
171158{
172- this ->shape = std::move ( other.shape ) ;
173- this ->offset = std::move ( other.offset ) ;
174- this ->dt = std::move ( other.dt ) ;
175- this ->destroy = std::move ( other.destroy ) ;
159+ this ->len = other.len ;
160+ this ->offset = other.offset ;
161+ this ->dt = other.dt ;
162+ this ->destroy = other.destroy ;
176163 other.destroy = nullptr ;
177164 return *this ;
178165}
@@ -185,18 +172,11 @@ PreloadAdiosAttributes::AttributeLocation::~AttributeLocation()
185172 */
186173 if (destroy)
187174 {
188- size_t length = 1 ;
189- for (auto ext : shape)
190- {
191- length *= ext;
192- }
193- switchAdios2AttributeType<AttributeLocationDestroy>(
194- dt, destroy, length);
175+ switchAdios2AttributeType<AttributeLocationDestroy>(dt, destroy, len);
195176 }
196177}
197178
198- void PreloadAdiosAttributes::preloadAttributes (
199- adios2::IO &IO, adios2::Engine &engine)
179+ void PreloadAdiosAttributes::preloadAttributes (adios2::IO &IO)
200180{
201181 m_offsets.clear ();
202182 std::map<Datatype, std::vector<std::string> > attributesByType;
@@ -212,15 +192,11 @@ void PreloadAdiosAttributes::preloadAttributes(
212192 it->second .push_back (std::move (name));
213193 };
214194 // PHASE 1: collect names of available attributes by ADIOS datatype
215- for (auto &variable : IO.AvailableVariables ())
195+ for (auto &attribute : IO.AvailableAttributes ())
216196 {
217- if (auxiliary::ends_with (variable.first , " /__data__" ))
218- {
219- continue ;
220- }
221197 // this will give us basic types only, no fancy vectors or similar
222- Datatype dt = fromADIOS2Type (IO.VariableType (variable .first ));
223- addAttribute (dt, std::move (variable .first ) );
198+ Datatype dt = fromADIOS2Type (IO.AttributeType (attribute .first ));
199+ addAttribute (dt, attribute .first );
224200 }
225201
226202 // PHASE 2: get offsets for attributes in buffer
@@ -238,18 +214,13 @@ void PreloadAdiosAttributes::preloadAttributes(
238214 }
239215 for (std::string &name : pair.second )
240216 {
241- adios2::Dims shape =
242- switchAdios2AttributeType<VariableShape>(pair.first , IO, name);
243- size_t elements = 1 ;
244- for (auto extent : shape)
245- {
246- elements *= extent;
247- }
217+ size_t elements =
218+ switchAdios2AttributeType<AttributeLen>(pair.first , IO, name);
219+
248220 m_offsets.emplace (
249221 std::piecewise_construct,
250222 std::forward_as_tuple (std::move (name)),
251- std::forward_as_tuple (
252- std::move (shape), currentOffset, pair.first ));
223+ std::forward_as_tuple (elements, currentOffset, pair.first ));
253224 currentOffset += elements * size;
254225 }
255226 }
@@ -261,7 +232,6 @@ void PreloadAdiosAttributes::preloadAttributes(
261232 switchAdios2AttributeType<ScheduleLoad>(
262233 pair.second .dt ,
263234 IO,
264- engine,
265235 pair.first ,
266236 &m_rawBuffer[pair.second .offset ],
267237 pair.second );
@@ -289,7 +259,7 @@ PreloadAdiosAttributes::getAttribute(std::string const &name) const
289259 throw std::runtime_error (errorMsg.str ());
290260 }
291261 AttributeWithShape<T> res;
292- res.shape = location.shape ;
262+ res.len = location.len ;
293263 res.data = reinterpret_cast <T const *>(&m_rawBuffer[location.offset ]);
294264 return res;
295265}
0 commit comments