View on GitHub

Vistle

Distributed Data-parallel Scientific Visualization in VR

archives.h
Go to the documentation of this file.
1#ifndef VISTLE_ARCHIVES_H
2#define VISTLE_ARCHIVES_H
3
4#include "export.h"
5#include "archives_config.h"
6
7#include "serialize.h"
8
9#include <functional>
10#include <vector>
11#include <memory>
12#include <iostream>
14#include <vistle/util/buffer.h>
15#include "message.h"
16
17#ifdef USE_BOOST_ARCHIVE
18#include <boost/archive/binary_oarchive.hpp>
19#include <boost/archive/binary_iarchive.hpp>
20
21#include <boost/archive/detail/oserializer.hpp>
22#include <boost/archive/detail/iserializer.hpp>
23
24#include <boost/archive/basic_binary_oprimitive.hpp>
25#include <boost/archive/basic_binary_iprimitive.hpp>
26
27#include <boost/serialization/nvp.hpp>
28#include <boost/serialization/array.hpp>
29#include <boost/serialization/string.hpp>
30#include <boost/serialization/vector.hpp>
31#include <boost/serialization/access.hpp>
32#include <boost/serialization/version.hpp>
33#if BOOST_VERSION >= 106400
34#include <boost/serialization/array_wrapper.hpp>
35#endif
36#endif
37
38#ifdef USE_YAS
39#include <yas/mem_streams.hpp>
40#endif
41
42#include <boost/mpl/vector.hpp>
43
45//#include "findobjectreferenceoarchive.h"
46#include "shm_array.h"
47#include "object.h"
48
49namespace vistle {
50
52 FieldCompressionMode m_compress = Uncompressed;
53 double m_zfpRate = 8.;
55 double m_zfpAccuracy = 1e-20;
56};
57
58template<class T>
59class shm_obj_ref;
60} // namespace vistle
61
62#ifdef USE_BOOST_ARCHIVE
63namespace boost {
64namespace archive {
65extern template class basic_binary_oprimitive<vistle::boost_oarchive, std::ostream::char_type,
66 std::ostream::traits_type>;
67extern template class basic_binary_iprimitive<vistle::boost_iarchive, std::istream::char_type,
68 std::istream::traits_type>;
69extern template class detail::common_oarchive<vistle::boost_oarchive>;
70} // namespace archive
71} // namespace boost
72#endif
73#endif // VISTLE_ARCHIVES_H
74
75#ifndef ARCHIVES_IMPL_H
76#define ARCHIVES_IMPL_H
77
78#include "shm.h"
79
80namespace vistle {
81
82// repeated from core/object.h
83class Object;
84struct ObjectData;
85typedef std::shared_ptr<Object> obj_ptr;
86typedef std::shared_ptr<const Object> obj_const_ptr;
87typedef std::function<void(const std::string &name)> ArrayCompletionHandler;
88typedef std::function<void(Object::const_ptr)> ObjectCompletionHandler;
89
91 std::string name;
92 bool is_array = false;
93 size_t size = 0, compressedSize = 0;
94 char *data = nullptr;
95 std::unique_ptr<buffer> storage;
96 message::CompressionMode compression = message::CompressionNone;
97
98 SubArchiveDirectoryEntry(): is_array(false), size(0), data(nullptr) {}
99 SubArchiveDirectoryEntry(const std::string &name, bool is_array, size_t size, char *data)
100 : name(name), is_array(is_array), size(size), data(data)
101 {}
102
104 template<class Archive>
105 void serialize(Archive &ar)
106 {
107 ar &name;
108 ar &is_array;
109 ar &size;
110 }
111#if 0
112 template<class Archive>
113 void serialize(Archive &ar, unsigned version) {
114 serialize(ar);
115 }
116#endif
117};
118typedef std::vector<SubArchiveDirectoryEntry> SubArchiveDirectory;
119
121public:
122 virtual ~Saver();
123 virtual void saveArray(const std::string &name, int type, const void *array) = 0;
124 virtual void saveObject(const std::string &name, obj_const_ptr obj) = 0;
125};
126
127
128#ifdef USE_BOOST_ARCHIVE
129class V_COREEXPORT boost_oarchive
130: public boost::archive::binary_oarchive_impl<boost_oarchive, std::ostream::char_type, std::ostream::traits_type> {
131 typedef boost::archive::binary_oarchive_impl<boost_oarchive, std::ostream::char_type, std::ostream::traits_type>
132 Base;
133
134public:
135 boost_oarchive(std::streambuf &bsb, unsigned int flags = 0);
136 ~boost_oarchive();
137
138 void setCompressionMode(vistle::FieldCompressionMode mode);
139 void setCompressionSettings(const CompressionSettings &other);
140
141 void setSaver(std::shared_ptr<Saver> saver);
142
143 template<class T>
144 void saveArray(const vistle::ShmVector<T> &t)
145 {
146 if (m_saver)
147 m_saver->saveArray(t.name(), t->type(), &t);
148 }
149
150 template<class T>
151 void saveObject(const vistle::shm_obj_ref<T> &t)
152 {
153 if (m_saver)
154 m_saver->saveObject(t.name(), t.getObject());
155 }
156
157 std::shared_ptr<Saver> m_saver;
158};
159
160#endif
161
162#ifdef USE_YAS
163template<class Archive, typename OS, std::size_t F = detail::yas_flags>
164struct yas_binary_oarchive: yas::detail::binary_ostream<OS, F>, yas::detail::oarchive_header<F> {
165 YAS_NONCOPYABLE(yas_binary_oarchive)
166 YAS_MOVABLE(yas_binary_oarchive)
167
168 using stream_type = OS;
169 using this_type = Archive;
170
171 yas_binary_oarchive(OS &os): yas::detail::binary_ostream<OS, F>(os), yas::detail::oarchive_header<F>(os) {}
172
173 template<typename T>
174 this_type &operator&(const T &v)
175 {
176 using namespace yas::detail;
177 return serializer<type_properties<T>::value, serialization_method<T, this_type>::value, F, T>::save(
178 *static_cast<this_type *>(this), v);
179 }
180
181 this_type &serialize() { return *static_cast<this_type *>(this); }
182
183 template<typename Head, typename... Tail>
184 this_type &serialize(const Head &head, const Tail &...tail)
185 {
186 return operator&(head).serialize(tail...);
187 }
188
189 template<typename... Args>
190 this_type &operator()(const Args &...args)
191 {
192 return serialize(args...);
193 }
194};
195
196class V_COREEXPORT yas_oarchive: public yas_binary_oarchive<yas_oarchive, vecostreambuf<vistle::buffer>>,
197 public CompressionSettings {
200 FieldCompressionMode m_compress = Uncompressed;
201 double m_zfpRate = 8.;
202 int m_zfpPrecision = 8;
203 double m_zfpAccuracy = 1e-20;
204 std::shared_ptr<Saver> m_saver;
205 Stream &m_os;
206
207public:
208 void setCompressionMode(vistle::FieldCompressionMode mode);
209 FieldCompressionMode compressionMode() const;
210
211 void setZfpRate(double rate);
212 double zfpRate() const;
213 void setZfpAccuracy(double rate);
214 double zfpAccuracy() const;
215 void setZfpPrecision(int precision);
216 int zfpPrecision() const;
217
218 void setCompressionSettings(const CompressionSettings &other);
219
220 yas_oarchive(Stream &mo, unsigned int flags = 0);
221
222 void setSaver(std::shared_ptr<Saver> saver);
223
224 template<class T>
226 {
227 if (m_saver) {
228 std::size_t sz = m_os.get_vector().capacity();
229 m_os.get_vector().reserve(sz + sizeof(T) * t->size() + 10000);
230 m_saver->saveArray(t.name(), t->type(), &t);
231 }
232 }
233
234 template<class T>
236 {
237 if (m_saver)
238 m_saver->saveObject(t.name(), t.getObject());
239 }
240};
241#endif
242
243
245public:
246 virtual ~Fetcher();
247 virtual void requestArray(const std::string &name, int type, const ArrayCompletionHandler &completeCallback) = 0;
248 virtual void requestObject(const std::string &name, const ObjectCompletionHandler &completeCallback) = 0;
249
250 virtual bool renameObjects() const;
251 virtual std::string translateObjectName(const std::string &name) const;
252 virtual std::string translateArrayName(const std::string &name) const;
253 virtual void registerObjectNameTranslation(const std::string &arname, const std::string &name);
254 virtual void registerArrayNameTranslation(const std::string &arname, const std::string &name);
255};
256
257
258#ifdef USE_BOOST_ARCHIVE
259class V_COREEXPORT boost_iarchive
260: public boost::archive::binary_iarchive_impl<boost_iarchive, std::istream::char_type, std::istream::traits_type> {
261 typedef boost::archive::binary_iarchive_impl<boost_iarchive, std::istream::char_type, std::istream::traits_type>
262 Base;
263
264public:
265 boost_iarchive(std::streambuf &bsb, unsigned int flags = 0);
266 ~boost_iarchive();
267
268 std::string translateObjectName(const std::string &name) const;
269 std::string translateArrayName(const std::string &name) const;
270 void registerObjectNameTranslation(const std::string &arname, const std::string &name) const;
271 void registerArrayNameTranslation(const std::string &arname, const std::string &name) const;
272
273 void setFetcher(std::shared_ptr<Fetcher> fetcher);
274 void setCurrentObject(ObjectData *data);
275 ObjectData *currentObject() const;
276 std::shared_ptr<Fetcher> fetcher() const;
277
278 template<typename T>
279 void fetchArray(const std::string &arname, const ArrayCompletionHandler &completeCallback) const
280 {
281 std::string name = translateArrayName(arname);
282 if (!name.empty()) {
283 if (auto arr = Shm::the().getArrayFromName<T>(name)) {
284 if (completeCallback)
285 completeCallback(name);
286 return;
287 }
288 }
289 assert(m_fetcher);
290 m_fetcher->requestArray(arname, shm<T>::array::typeId(), completeCallback);
291 }
292
293 obj_const_ptr getObject(const std::string &name,
294 const std::function<void(Object::const_ptr)> &completeCallback) const;
295 void setObjectCompletionHandler(const std::function<void()> &completer);
296 const std::function<void()> &objectCompletionHandler() const;
297
298private:
299 bool m_rename = false;
300 std::shared_ptr<Fetcher> m_fetcher;
301 ObjectData *m_currentObject = nullptr;
302 std::function<void()> m_completer;
303 std::map<std::string, std::string> m_transObject, m_transArray;
304};
305#endif
306
307#ifdef USE_YAS
308template<class Archive, typename IS, std::size_t F = detail::yas_flags>
309struct yas_binary_iarchive: yas::detail::binary_istream<IS, F>, yas::detail::iarchive_header<F> {
310 YAS_NONCOPYABLE(yas_binary_iarchive)
311 YAS_MOVABLE(yas_binary_iarchive)
312
313 using stream_type = IS;
314 using this_type = Archive;
315
316 yas_binary_iarchive(IS &is): yas::detail::binary_istream<IS, F>(is), yas::detail::iarchive_header<F>(is) {}
317
318 template<typename T>
320 {
321 using namespace yas::detail;
322 using real_type = typename std::remove_reference<typename std::remove_const<T>::type>::type;
323 return serializer<type_properties<real_type>::value, serialization_method<real_type, this_type>::value, F,
324 real_type>::load(*static_cast<this_type *>(this), v);
325 }
326
327 this_type &serialize() { return *static_cast<this_type *>(this); }
328
329 template<typename Head, typename... Tail>
330 this_type &serialize(Head &&head, Tail &&...tail)
331 {
332 return operator&(std::forward<Head>(head)).serialize(std::forward<Tail>(tail)...);
333 }
334
335 template<typename... Args>
336 this_type &operator()(Args &&...args)
337 {
338 return serialize(std::forward<Args>(args)...);
339 }
340};
341
342class V_COREEXPORT yas_iarchive: public yas_binary_iarchive<yas_iarchive, vecistreambuf<vistle::buffer>> {
345
346public:
347 yas_iarchive(Stream &mi, unsigned int flags = 0);
349
350 std::string translateObjectName(const std::string &name) const;
351 std::string translateArrayName(const std::string &name) const;
352 void registerObjectNameTranslation(const std::string &arname, const std::string &name) const;
353 void registerArrayNameTranslation(const std::string &arname, const std::string &name) const;
354
355 void setFetcher(std::shared_ptr<Fetcher> fetcher);
356 void setCurrentObject(ObjectData *data);
357 ObjectData *currentObject() const;
358 std::shared_ptr<Fetcher> fetcher() const;
359
360 template<typename T>
361 void fetchArray(const std::string &arname, const ArrayCompletionHandler &completeCallback) const
362 {
363 std::string name = translateArrayName(arname);
364 if (!name.empty()) {
365 if (auto arr = Shm::the().getArrayFromName<T>(name)) {
366 if (completeCallback)
367 completeCallback(name);
368 return;
369 }
370 }
371 assert(m_fetcher);
372 m_fetcher->requestArray(arname, shm<T>::array::typeId(), completeCallback);
373 }
374
375 obj_const_ptr getObject(const std::string &name, const ObjectCompletionHandler &completeCallback) const;
376 void setObjectCompletionHandler(const std::function<void()> &completer);
377 const std::function<void()> &objectCompletionHandler() const;
378
379private:
380 std::shared_ptr<Fetcher> m_fetcher;
381 ObjectData *m_currentObject = nullptr;
382 std::function<void()> m_completer;
383 std::map<std::string, std::string> m_transObject, m_transArray;
384};
385#endif
386
387typedef boost::mpl::vector<
388#ifdef USE_BOOST_ARCHIVE
389 boost_iarchive
390#ifdef USE_YAS
391 ,
392 yas_iarchive
393#endif
394#else
395#ifdef USE_YAS
396 yas_iarchive
397#endif
398#endif
399 >
401
402typedef boost::mpl::vector<
403#ifdef USE_BOOST_ARCHIVE
404 boost_oarchive
405#ifdef USE_YAS
406 ,
408#endif
409#else
410#ifdef USE_YAS
412#endif
413#endif
414 >
416
417} // namespace vistle
418
419
420#ifdef USE_BOOST_ARCHIVE
421BOOST_SERIALIZATION_REGISTER_ARCHIVE(vistle::boost_oarchive)
422BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(vistle::boost_oarchive)
423BOOST_SERIALIZATION_REGISTER_ARCHIVE(vistle::boost_iarchive)
424BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(vistle::boost_iarchive)
425#endif
426
427#endif // ARCHIVES_IMPL_H
#define ARCHIVE_ACCESS
Definition: archives_config.h:465
Definition: archives.h:244
virtual void requestObject(const std::string &name, const ObjectCompletionHandler &completeCallback)=0
virtual void requestArray(const std::string &name, int type, const ArrayCompletionHandler &completeCallback)=0
std::shared_ptr< const Object > const_ptr
Definition: object.h:68
Definition: archives.h:120
virtual void saveArray(const std::string &name, int type, const void *array)=0
virtual void saveObject(const std::string &name, obj_const_ptr obj)=0
Definition: shm_reference.h:15
const shm_name_t & name() const
Definition: shm_reference_impl.h:129
Definition: shm_obj_ref.h:15
ObjType::const_ptr getObject() const
Definition: shm_obj_ref_impl.h:110
const shm_name_t & name() const
Definition: shm_obj_ref_impl.h:128
Definition: vecstreambuf.h:52
Definition: vecstreambuf.h:12
const Vector & get_vector() const
Definition: vecstreambuf.h:42
Definition: archives.h:342
void fetchArray(const std::string &arname, const ArrayCompletionHandler &completeCallback) const
Definition: archives.h:361
Definition: archives.h:197
void saveArray(const vistle::ShmVector< T > &t)
Definition: archives.h:225
void saveObject(const vistle::shm_obj_ref< T > &t)
Definition: archives.h:235
#define V_COREEXPORT
Definition: export.h:9
void serialize(Archive &ar, vistle::Vector1 &v, const unsigned int version)
Definition: vector.h:105
Definition: serialize.h:35
Definition: allobjects.cpp:30
std::function< void(Object::const_ptr)> ObjectCompletionHandler
Definition: archives.h:88
boost::mpl::vector< yas_oarchive > OutputArchives
Definition: archives.h:415
std::vector< SubArchiveDirectoryEntry > SubArchiveDirectory
Definition: archives.h:118
std::shared_ptr< const Object > obj_const_ptr
Definition: archives.h:86
std::function< void(const std::string &name)> ArrayCompletionHandler
Definition: archives.h:87
boost::mpl::vector< yas_iarchive > InputArchives
Definition: archives.h:400
std::shared_ptr< Object > obj_ptr
Definition: archives.h:84
Definition: archives.h:51
double m_zfpAccuracy
Definition: archives.h:55
int m_zfpPrecision
Definition: archives.h:54
FieldCompressionMode m_compress
Definition: archives.h:52
double m_zfpRate
Definition: archives.h:53
Definition: object.h:233
Definition: archives.h:90
std::unique_ptr< buffer > storage
Definition: archives.h:95
SubArchiveDirectoryEntry(const std::string &name, bool is_array, size_t size, char *data)
Definition: archives.h:99
SubArchiveDirectoryEntry()
Definition: archives.h:98
std::string name
Definition: archives.h:91
ARCHIVE_ACCESS void serialize(Archive &ar)
Definition: archives.h:105
Definition: shm.h:94
Definition: archives.h:309
this_type & operator&(T &&v)
Definition: archives.h:319
this_type & serialize()
Definition: archives.h:327
this_type & operator()(Args &&...args)
Definition: archives.h:336
Archive this_type
Definition: archives.h:314
this_type & serialize(Head &&head, Tail &&...tail)
Definition: archives.h:330
IS stream_type
Definition: archives.h:313
Definition: archives.h:164
this_type & operator()(const Args &...args)
Definition: archives.h:190
this_type & operator&(const T &v)
Definition: archives.h:174
Archive this_type
Definition: archives.h:169
OS stream_type
Definition: archives.h:168
this_type & serialize(const Head &head, const Tail &...tail)
Definition: archives.h:184
this_type & serialize()
Definition: archives.h:181