View on GitHub

Vistle

Distributed Data-parallel Scientific Visualization in VR

archives_config.h
Go to the documentation of this file.
1#ifndef VISTLE_ARCHIVES_CONFIG_H
2#define VISTLE_ARCHIVES_CONFIG_H
3
4//#define USE_INTROSPECTION_ARCHIVE
5//#define USE_BOOST_ARCHIVE
6#define USE_BOOST_ARCHIVE_MPI
7#define USE_YAS
8
9#ifdef USE_INTROSPECTION_ARCHIVE
10#ifndef USE_BOOST_ARCHIVE
11#define USE_BOOST_ARCHIVE
12#endif
13#endif
14
15#ifdef USE_BOOST_ARCHIVE
16#ifndef USE_BOOST_ARCHIVE_MPI
17#define USE_BOOST_ARCHIVE_MPI
18#endif
19#endif
20
21#ifdef HAVE_ZFP
22#include <zfp.h>
23#endif
24
25#include <cassert>
26#include <cstdlib>
27#include <iostream>
28#include <vector>
29
30#include <vistle/util/enum.h>
31#include <vistle/util/buffer.h>
32
33#include "export.h"
34#include "index.h"
35
36namespace vistle {
37
38DEFINE_ENUM_WITH_STRING_CONVERSIONS(FieldCompressionMode, (Uncompressed)(ZfpFixedRate)(ZfpAccuracy)(ZfpPrecision))
39
40namespace detail {
41
42template<class Archive>
43struct archive_tag;
44
45template<class tag>
46struct archive_helper {
47 template<class Base>
48 class BaseClass;
49 template<class Base, class Derived>
50 static BaseClass<Base> serialize_base(Derived &d);
51
52 template<class Object>
53 class NameValuePair;
54 template<class Object>
55 static NameValuePair<Object> make_nvp(const char *name, Object &d);
56
57 template<class T>
58 class ArrayWrapper;
59 template<class T, class S>
60 static ArrayWrapper<T> wrap_array(T *t, bool exact, S nx, S ny = 1, S nz = 1);
61
62 template<class Archive>
63 class StreamType;
64};
65
66template<class Base, class Derived, class Archive>
67typename archive_helper<typename archive_tag<Archive>::type>::template BaseClass<Base> serialize_base(const Archive &,
68 Derived &d)
69{
70 return archive_helper<typename archive_tag<Archive>::type>::template serialize_base<Base>(d);
71}
72
73template<class Archive, class Object>
74typename archive_helper<typename archive_tag<Archive>::type>::template NameValuePair<Object> make_nvp(const char *name,
75 Object &d)
76{
77 return archive_helper<typename archive_tag<Archive>::type>::make_nvp(name, d);
78}
79
80template<class Archive, class T, class S>
81typename archive_helper<typename archive_tag<Archive>::type>::template ArrayWrapper<T>
82wrap_array(T *t, bool exact, S nx, S ny = 1, S nz = 1)
83{
84 return archive_helper<typename archive_tag<Archive>::type>::wrap_array(t, exact, nx, ny, nz);
85}
86
87} // namespace detail
88} // namespace vistle
89
90#ifdef USE_BOOST_ARCHIVE
91#include <boost/serialization/nvp.hpp>
92#include <boost/serialization/array.hpp>
93#if BOOST_VERSION >= 106400
94#include <boost/serialization/array_wrapper.hpp>
95#include <boost/serialization/array_optimization.hpp>
96#endif
97
98namespace vistle {
99class boost_oarchive;
100class boost_iarchive;
101
102namespace detail {
103
104struct boost_tag {};
105template<>
106struct archive_tag<boost_oarchive> {
107 typedef boost_tag type;
108};
109template<>
110struct archive_tag<boost_iarchive> {
111 typedef boost_tag type;
112};
113
114template<>
115struct archive_helper<boost_tag> {
116 template<class Base>
117 using BaseClass = Base &;
118 template<class Base, class Derived>
119 static BaseClass<Base> serialize_base(Derived &d)
120 {
121 return boost::serialization::base_object<Base>(d);
122 }
123
124 template<class Object>
125 using NameValuePair = const boost::serialization::nvp<Object>;
126 template<class Object>
127 static NameValuePair<Object> make_nvp(const char *name, Object &obj)
128 {
129 return boost::serialization::make_nvp(name, obj);
130 }
131
132#if BOOST_VERSION > 105500
133 template<class T>
134 using ArrayWrapper = const boost::serialization::array_wrapper<T>;
135#else
136 template<class T>
137 using ArrayWrapper = const boost::serialization::array<T>;
138#endif
139 template<class T, class S>
140 static ArrayWrapper<T> wrap_array(T *t, bool exact, S nx, S ny, S nz)
141 {
142 return boost::serialization::make_array(t, nx * ny * nz);
143 }
144};
145} // namespace detail
146} // namespace vistle
147#endif
148#ifdef USE_YAS
149#include <yas/binary_iarchive.hpp>
150#include <yas/binary_oarchive.hpp>
151#include <yas/serialize.hpp>
152#include <yas/boost_types.hpp>
153#include <yas/types/concepts/array.hpp>
154
155namespace vistle {
156
157class yas_oarchive;
158class yas_iarchive;
159
160namespace detail {
161const std::size_t yas_flags = yas::binary | yas::ehost;
162
163struct yas_tag {};
164template<>
165struct archive_tag<yas_oarchive> {
166 typedef yas_tag type;
167};
168template<>
169struct archive_tag<yas_iarchive> {
170 typedef yas_tag type;
171};
172
173template<class Archive>
175
176template<>
178 typedef yas::mem_istream type;
179};
180template<>
182 typedef yas::mem_ostream type;
183};
184
185#ifdef HAVE_ZFP
186struct ZfpParameters {
187 FieldCompressionMode mode = Uncompressed;
188 double rate = 8.;
189 int precision = 20;
190 double accuracy = 1e-20;
191};
192
193template<typename T>
194struct zfp_type_map {
195 static const zfp_type value = zfp_type_none;
196};
197
198template<>
199struct zfp_type_map<int32_t> {
200 static const zfp_type value = zfp_type_int32;
201};
202template<>
203struct zfp_type_map<int64_t> {
204 static const zfp_type value = zfp_type_int64;
205};
206template<>
207struct zfp_type_map<float> {
208 static const zfp_type value = zfp_type_float;
209};
210template<>
211struct zfp_type_map<double> {
212 static const zfp_type value = zfp_type_double;
213};
214
215template<zfp_type type>
216bool compressZfp(buffer &compressed, const void *src, const Index dim[3], const ZfpParameters &param);
217template<zfp_type type>
218bool decompressZfp(void *dest, const buffer &compressed, const Index dim[3]);
219#endif
220
221template<>
222struct archive_helper<yas_tag> {
223 template<class Base>
224 using BaseClass = Base &;
225 template<class Base, class Derived>
227 {
228 return yas::base_object<Base>(d);
229 }
230
231 template<class Object>
232 using NameValuePair = const Object &;
233 template<class Object>
234 static NameValuePair<Object> make_nvp(const char *name, Object &obj)
235 {
236 //return YAS_OBJECT_NVP(name, obj);
237 return obj;
238 }
239
240 template<class T>
241 struct ArrayWrapper {
242 typedef T value_type;
243 typedef T &reference;
244 T *m_begin, *m_end;
245 Index m_dim[3] = {0, 1, 1};
246 bool m_exact = true;
247
248 ArrayWrapper(T *begin, T *end): m_begin(begin), m_end(end) {}
249 T *begin() { return m_begin; }
250 const T *begin() const { return m_begin; }
251 T *end() { return m_end; }
252 const T *end() const { return m_end; }
253 bool empty() const { return m_end == m_begin; }
254 Index size() const { return (Index)(m_end - m_begin); }
255 T &operator[](size_t idx) { return *(m_begin + idx); }
256 const T &operator[](size_t idx) const { return *(m_begin + idx); }
257 void push_back(const T &) { assert("not supported" == 0); }
258 void resize(std::size_t sz)
259 {
260 if (size() != sz) {
261 std::cerr << "requesting resize from " << size() << " to " << sz << std::endl;
262 assert("not supported" == 0);
263 }
264 }
265 void setDimensions(size_t sx, size_t sy, size_t sz)
266 {
267 m_dim[0] = (Index)sx;
268 m_dim[1] = (Index)sy;
269 m_dim[2] = (Index)sz;
270 }
271 void setExact(bool exact) { m_exact = exact; }
272
273 template<class Archive>
274 void serialize(Archive &ar) const
275 {
276 save(ar);
277 }
278 template<class Archive>
279 void serialize(Archive &ar)
280 {
281 load(ar);
282 }
283 template<class Archive>
284 void load(Archive &ar)
285 {
286 bool compress = false;
287 ar &compress;
288 if (compress) {
289 ar &m_dim[0] & m_dim[1] & m_dim[2];
290 buffer compressed;
291 ar &compressed;
292#ifdef HAVE_ZFP
293 Index dim[3];
294 for (int c = 0; c < 3; ++c)
295 dim[c] = m_dim[c] == 1 ? 0 : m_dim[c];
296 decompressZfp<zfp_type_map<T>::value>(static_cast<void *>(m_begin), compressed, dim);
297#endif
298 } else {
299 yas::detail::concepts::array::load<yas_flags>(ar, *this);
300 }
301 }
302 template<class Archive>
303 void save(Archive &ar) const
304 {
305 bool compress = ar.compressionMode() != Uncompressed && !m_exact;
306 //std::cerr << "ar.compressed()=" << compress << std::endl;
307 if (compress) {
308#ifdef HAVE_ZFP
309 ZfpParameters param;
310 param.mode = ar.compressionMode();
311 param.rate = ar.zfpRate();
312 param.precision = ar.zfpPrecision();
313 param.accuracy = ar.zfpAccuracy();
314 //std::cerr << "trying to compresss " << std::endl;
315 buffer compressed;
316 Index dim[3];
317 for (int c = 0; c < 3; ++c)
318 dim[c] = m_dim[c] == 1 ? 0 : m_dim[c];
319 if (compressZfp<zfp_type_map<T>::value>(compressed, static_cast<const void *>(m_begin), dim, param)) {
320 ar &compress;
321 ar &m_dim[0] & m_dim[1] & m_dim[2];
322 ar &compressed;
323 } else {
324 std::cerr << "compression failed" << std::endl;
325 compress = false;
326 }
327#else
328 compress = false;
329#endif
330 }
331 if (!compress) {
332 ar &compress;
333 yas::detail::concepts::array::save<yas_flags>(ar, *this);
334 }
335 }
336 };
337
338 template<class T, class S>
339 static ArrayWrapper<T> wrap_array(T *t, bool exact, S nx, S ny, S nz)
340 {
341 ArrayWrapper<T> wrap(t, t + nx * ny * nz);
342 wrap.setExact(exact);
343 wrap.setDimensions(nx, ny, nz);
344 return wrap;
345 }
346
347 template<class Archive>
349};
350
351#ifdef HAVE_ZFP
352template<>
353bool V_COREEXPORT decompressZfp<zfp_type_none>(void *dest, const buffer &compressed, const Index dim[3]);
354extern template bool V_COREEXPORT decompressZfp<zfp_type_int32>(void *dest, const buffer &compressed,
355 const Index dim[3]);
356extern template bool V_COREEXPORT decompressZfp<zfp_type_int64>(void *dest, const buffer &compressed,
357 const Index dim[3]);
358extern template bool V_COREEXPORT decompressZfp<zfp_type_float>(void *dest, const buffer &compressed,
359 const Index dim[3]);
360extern template bool V_COREEXPORT decompressZfp<zfp_type_double>(void *dest, const buffer &compressed,
361 const Index dim[3]);
362
363template<>
364bool V_COREEXPORT compressZfp<zfp_type_none>(buffer &compressed, const void *src, const Index dim[3],
365 const ZfpParameters &param);
366extern template bool V_COREEXPORT compressZfp<zfp_type_int32>(buffer &compressed, const void *src, const Index dim[3],
367 const ZfpParameters &param);
368extern template bool V_COREEXPORT compressZfp<zfp_type_int64>(buffer &compressed, const void *src, const Index dim[3],
369 const ZfpParameters &param);
370extern template bool V_COREEXPORT compressZfp<zfp_type_float>(buffer &compressed, const void *src, const Index dim[3],
371 const ZfpParameters &param);
372extern template bool V_COREEXPORT compressZfp<zfp_type_double>(buffer &compressed, const void *src, const Index dim[3],
373 const ZfpParameters &param);
374#endif
375} // namespace detail
376#ifdef HAVE_ZFP
377using detail::ZfpParameters;
378using detail::compressZfp;
379using detail::decompressZfp;
380#endif
381} // namespace vistle
382#endif
383
384namespace vistle {
385#ifdef USE_INTROSPECTION_ARCHIVE
386class FindObjectReferenceOArchive;
387#endif
388
389#if defined(USE_YAS)
392#elif defined(USE_BOOST_ARCHIVE)
393typedef boost_oarchive oarchive;
394typedef boost_iarchive iarchive;
395#endif
396
397using detail::serialize_base;
398} // namespace vistle
399
400#ifdef USE_BOOST_ARCHIVE_MPI
401#include <boost/serialization/access.hpp>
402#include <boost/serialization/split_member.hpp>
403#endif
404#ifdef USE_BOOST_ARCHIVE
405#include <boost/serialization/assume_abstract.hpp>
406#include <boost/archive/archive_exception.hpp>
407
408#define ARCHIVE_ASSUME_ABSTRACT_BOOST(obj) BOOST_SERIALIZATION_ASSUME_ABSTRACT(obj)
409#else
410#define ARCHIVE_ASSUME_ABSTRACT_BOOST(obj)
411#endif
412
413#define ARCHIVE_ASSUME_ABSTRACT_YAS(obj)
414#ifdef USE_YAS
415#include <yas/serialize.hpp>
416#include <yas/object.hpp>
417#include <yas/std_types.hpp>
418#include <yas/std_traits.hpp>
419#include <yas/std_streams.hpp>
420#include <yas/boost_types.hpp>
421
422#endif
423
424//#define DEBUG_SERIALIZATION
425#ifdef DEBUG_SERIALIZATION
426template<class T>
427inline const boost::serialization::nvp<T> vistle_make_nvp(const char *name, T &t)
428{
429 std::cerr << "<" << name << ">" << std::endl;
430 return boost::serialization::make_nvp<T>(name, t);
431}
432
433#define V_NAME(name, obj) \
434 vistle_make_nvp(name, (obj)); \
435 std::cerr << "</" << name << ">" << std::endl;
436#else
437#if 0
438#define V_NAME(name, obj) boost::serialization::make_nvp(name, (obj))
439#else
440//#define V_NAME(ar, name, obj) vistle::make_nvp(ar, name, obj)
441#define V_NAME(ar, name, obj) (obj)
442#endif
443#endif
444
445#ifdef USE_BOOST_ARCHIVE_MPI
446#define ARCHIVE_ACCESS_BOOST friend class boost::serialization::access;
447#else
448#define ARCHIVE_ACCESS_BOOST
449#endif
450#if defined(USE_BOOST_ARCHIVE_MPI)
451#define ARCHIVE_FORWARD_SERIALIZE \
452 template<class Archive> \
453 void serialize(Archive &ar, const unsigned int /* version */) \
454 { \
455 serialize(ar); \
456 }
457#else
458#define ARCHIVE_FORWARD_SERIALIZE
459#endif
460#ifdef USE_YAS
461#define ARCHIVE_ACCESS_YAS public:
462#else
463#define ARCHIVE_ACCESS_YAS
464#endif
465#define ARCHIVE_ACCESS ARCHIVE_ACCESS_BOOST ARCHIVE_ACCESS_YAS ARCHIVE_FORWARD_SERIALIZE
466#define ARCHIVE_ASSUME_ABSTRACT(obj) ARCHIVE_ASSUME_ABSTRACT_BOOST(obj) ARCHIVE_ASSUME_ABSTRACT_YAS(obj)
467
468#ifdef USE_BOOST_ARCHIVE_MPI
469#define BOOST_ARCHIVE_ACCESS_SPLIT \
470 friend class boost::serialization::access; \
471 template<class Archive> \
472 void serialize(Archive &ar, const unsigned int version) \
473 { \
474 boost::serialization::split_member(ar, *this, version); \
475 } \
476 template<class Archive> \
477 void save(Archive &ar, const unsigned int version) const \
478 { \
479 save(ar); \
480 } \
481 template<class Archive> \
482 void load(Archive &ar, const unsigned int version) \
483 { \
484 load(ar); \
485 }
486#else
487#define BOOST_ARCHIVE_ACCESS_SPLIT
488#endif
489#define YAS_ARCHIVE_ACCESS_SPLIT \
490public: \
491 template<class Archive> \
492 void serialize(Archive &ar) const \
493 { \
494 save(ar); \
495 } \
496 template<class Archive> \
497 void serialize(Archive &ar) \
498 { \
499 load(ar); \
500 }
501
502#define ARCHIVE_ACCESS_SPLIT \
503public: \
504 BOOST_ARCHIVE_ACCESS_SPLIT YAS_ARCHIVE_ACCESS_SPLIT
505
506#ifdef USE_BOOST_ARCHIVE
507#define ARCHIVE_REGISTRATION_BOOST(override) \
508 virtual void saveToArchive(boost_oarchive &ar) const override; \
509 virtual void loadFromArchive(boost_iarchive &ar) override;
510#define ARCHIVE_REGISTRATION_BOOST_INLINE \
511 void saveToArchive(boost_oarchive &ar) const override { this->serialize(ar); } \
512 void loadFromArchive(boost_iarchive &ar) override { this->serialize(ar); }
513#define ARCHIVE_REGISTRATION_BOOST_IMPL(ObjType, prefix) \
514 prefix void ObjType::saveToArchive(boost_oarchive &ar) const { this->serialize(ar); } \
515 prefix void ObjType::loadFromArchive(boost_iarchive &ar) { this->serialize(ar); }
516#else
517#define ARCHIVE_REGISTRATION_BOOST(override)
518#define ARCHIVE_REGISTRATION_BOOST_INLINE
519#define ARCHIVE_REGISTRATION_BOOST_IMPL(ObjType, prefix)
520#endif
521
522#ifdef USE_YAS
523#define ARCHIVE_REGISTRATION_YAS(override) \
524 virtual void saveToArchive(yas_oarchive &ar) const override; \
525 virtual void loadFromArchive(yas_iarchive &ar) override;
526#define ARCHIVE_REGISTRATION_YAS_INLINE \
527 void saveToArchive(yas_oarchive &ar) const override { this->serialize(ar); } \
528 void loadFromArchive(yas_iarchive &ar) override { this->serialize(ar); }
529#define ARCHIVE_REGISTRATION_YAS_IMPL(ObjType, prefix) \
530 prefix void ObjType::saveToArchive(yas_oarchive &ar) const { this->serialize(ar); } \
531 prefix void ObjType::loadFromArchive(yas_iarchive &ar) { this->serialize(ar); }
532#else
533#define ARCHIVE_REGISTRATION_YAS(override)
534#define ARCHIVE_REGISTRATION_YAS_INLINE
535#define ARCHIVE_REGISTRATION_YAS_IMPL(ObjType, prefix)
536#endif
537#ifdef USE_INTROSPECTION_ARCHIVE
538#define ARCHIVE_REGISTRATION_INTROSPECT \
539 void save(FindObjectReferenceOArchive &ar) const override { const_cast<ObjType *>(this)->serialize(ar, 0); }
540#else
541#define ARCHIVE_REGISTRATION_INTROSPECT
542#endif
543#define ARCHIVE_REGISTRATION(override) \
544 ARCHIVE_REGISTRATION_BOOST(override) ARCHIVE_REGISTRATION_YAS(override) ARCHIVE_REGISTRATION_INTROSPECT
545#define ARCHIVE_REGISTRATION_INLINE \
546 ARCHIVE_REGISTRATION_BOOST_INLINE ARCHIVE_REGISTRATION_YAS_INLINE ARCHIVE_REGISTRATION_INTROSPECT
547#define ARCHIVE_REGISTRATION_IMPL(ObjType, prefix) \
548 ARCHIVE_REGISTRATION_BOOST_IMPL(ObjType, prefix) ARCHIVE_REGISTRATION_YAS_IMPL(ObjType, prefix)
549
550#endif
Definition: object.h:58
Definition: archives.h:342
Definition: archives.h:197
#define V_COREEXPORT
Definition: export.h:9
yas::mem_ostream type
Definition: archives_config.h:182
const std::size_t yas_flags
Definition: archives_config.h:161
yas_tag type
Definition: archives_config.h:166
yas::mem_istream type
Definition: archives_config.h:178
yas_tag type
Definition: archives_config.h:170
Definition: archives_config.h:174
Definition: archives_config.h:163
Definition: allobjects.cpp:30
std::vector< char, allocator< char > > buffer
Definition: buffer.h:9
yas_iarchive iarchive
Definition: archives_config.h:391
yas_oarchive oarchive
Definition: archives_config.h:390
DEFINE_ENUM_WITH_STRING_CONVERSIONS(FieldCompressionMode,(Uncompressed)(ZfpFixedRate)(ZfpAccuracy)(ZfpPrecision)) namespace detail
Definition: archives_config.h:38
uint32_t Index
Definition: index.h:13
const T & operator[](size_t idx) const
Definition: archives_config.h:256
void load(Archive &ar)
Definition: archives_config.h:284
T * m_begin
Definition: archives_config.h:244
void setExact(bool exact)
Definition: archives_config.h:271
void push_back(const T &)
Definition: archives_config.h:257
const T * begin() const
Definition: archives_config.h:250
void resize(std::size_t sz)
Definition: archives_config.h:258
bool empty() const
Definition: archives_config.h:253
T * end()
Definition: archives_config.h:251
T & operator[](size_t idx)
Definition: archives_config.h:255
Index size() const
Definition: archives_config.h:254
void serialize(Archive &ar) const
Definition: archives_config.h:274
T * begin()
Definition: archives_config.h:249
T value_type
Definition: archives_config.h:242
void setDimensions(size_t sx, size_t sy, size_t sz)
Definition: archives_config.h:265
const T * end() const
Definition: archives_config.h:252
void serialize(Archive &ar)
Definition: archives_config.h:279
T & reference
Definition: archives_config.h:243
ArrayWrapper(T *begin, T *end)
Definition: archives_config.h:248
void save(Archive &ar) const
Definition: archives_config.h:303
static BaseClass< Base > serialize_base(Derived &d)
Definition: archives_config.h:226
static NameValuePair< Object > make_nvp(const char *name, Object &obj)
Definition: archives_config.h:234
Base & BaseClass
Definition: archives_config.h:224
typename ArchiveStreamType< Archive >::type StreamType
Definition: archives_config.h:348
static ArrayWrapper< T > wrap_array(T *t, bool exact, S nx, S ny, S nz)
Definition: archives_config.h:339