View on GitHub

Vistle

Distributed Data-parallel Scientific Visualization in VR

archive_loader.h
Go to the documentation of this file.
1#ifndef VISTLE_ARCHIVE_LOADER_H
2#define VISTLE_ARCHIVE_LOADER_H
3
4#include "archives.h"
5#include "shmvector.h"
6
8
9#include <boost/mpl/for_each.hpp>
10#include <cassert>
11#include <set>
12#include <map>
13#include <string>
14#include <memory>
15#include <iostream>
16
17namespace vistle {
18
20 struct ArrayOwner {
21 virtual ~ArrayOwner() {}
22 };
23
24 template<typename T>
25 struct Unreffer: public ArrayOwner {
26 explicit Unreffer(ShmVector<T> &ref): m_ref(ref) {}
28 };
29
30 ArrayLoader(const std::string &name, int type, const vistle::iarchive &ar);
31 ArrayLoader() = delete;
32 ArrayLoader(const ArrayLoader &other) = delete;
33
34 std::shared_ptr<ArrayOwner> m_unreffer;
35
36 template<typename T>
37 void operator()(T)
38 {
39 if (shm_array<T, typename shm<T>::allocator>::typeId() == m_type) {
40 if (m_ok) {
41 m_ok = false;
42 std::cerr << "ArrayLoader: multiple type matches for data array " << m_name << std::endl;
43 return;
44 }
45 ShmVector<T> arr;
46 if (!m_name.empty())
47 arr = Shm::the().getArrayFromName<T>(m_name);
48 if (arr) {
49 std::cerr << "ArrayLoader: already have data array with name " << m_name << std::endl;
50 m_unreffer.reset(new Unreffer<T>(arr));
51 return;
52 }
53 auto &ar = const_cast<vistle::iarchive &>(m_ar);
54 std::string arname;
55 ar &arname;
56 assert(arname == m_arname);
57 m_name = ar.translateArrayName(arname);
58 arr = ShmVector<T>((shm_name_t)m_name);
59 if (!arr.valid())
60 arr.construct();
61 //std::cerr << "ArrayLoader: loading " << arname << " as " << m_name << ": arr=" << arr << std::endl;
62 m_name = arr.name().str();
63 ar.registerArrayNameTranslation(arname, arr.name());
64 //std::cerr << "ArrayLoader: constructed " << arname << " as " << arr.name() << std::endl;
65 ar &*arr;
66 m_unreffer.reset(new Unreffer<T>(arr));
67 m_ok = true;
68 }
69 }
70
71 bool load();
72 const std::string &name() const;
73 std::shared_ptr<ArrayOwner> owner() const;
74
75 bool m_ok;
76 std::string m_arname; //<! name in archive
77 std::string m_name; //<! name in shmem
78 int m_type;
80};
81
82class V_COREEXPORT DeepArchiveFetcher: public Fetcher, public std::enable_shared_from_this<DeepArchiveFetcher> {
83public:
84 DeepArchiveFetcher(const std::map<std::string, buffer> &objects, const std::map<std::string, buffer> &arrays,
85 const std::map<std::string, message::CompressionMode> &compressions,
86 const std::map<std::string, size_t> &sizes);
87
88 void requestArray(const std::string &name, int type, const ArrayCompletionHandler &completeCallback) override;
89 void requestObject(const std::string &name, const ObjectCompletionHandler &completeCallback) override;
90
91 bool renameObjects() const override;
92 std::string translateObjectName(const std::string &name) const override;
93 std::string translateArrayName(const std::string &name) const override;
94 void registerObjectNameTranslation(const std::string &arname, const std::string &name) override;
95 void registerArrayNameTranslation(const std::string &arname, const std::string &name) override;
96
97 void setRenameObjects(bool rename);
98 std::map<std::string, std::string> objectTranslations() const;
99 std::map<std::string, std::string> arrayTranslations() const;
100 void setObjectTranslations(const std::map<std::string, std::string> &objs);
101 void setArrayTranslations(const std::map<std::string, std::string> &arrs);
102
103 void releaseArrays();
104
105private:
106 bool m_rename = false;
107 std::map<std::string, std::string> m_transObject, m_transArray;
108
109 const std::map<std::string, buffer> &m_objects;
110 const std::map<std::string, buffer> &m_arrays;
111 const std::map<std::string, message::CompressionMode> &m_compression;
112 const std::map<std::string, size_t> &m_rawSize;
113
114 std::set<std::shared_ptr<ArrayLoader::ArrayOwner>> m_ownedArrays;
115};
116
117
118} // namespace vistle
119#endif
Definition: archive_loader.h:82
Definition: archives.h:244
static Shm & the()
Definition: shm.cpp:315
const ShmVector< T > getArrayFromName(const std::string &name) const
Definition: shm_impl.h:32
Definition: shm_reference.h:15
bool valid() const
Definition: shm_reference.h:110
const shm_name_t & name() const
Definition: shm_reference_impl.h:129
void construct(const Args &...args)
Definition: shm_reference.h:86
void reset()
Definition: shm_reference.h:55
Definition: shm_array.h:19
Definition: archives.h:342
#define V_COREEXPORT
Definition: export.h:9
Definition: allobjects.cpp:30
std::function< void(Object::const_ptr)> ObjectCompletionHandler
Definition: archives.h:88
std::function< void(const std::string &name)> ArrayCompletionHandler
Definition: archives.h:87
Definition: archive_loader.h:20
virtual ~ArrayOwner()
Definition: archive_loader.h:21
Definition: archive_loader.h:25
Unreffer(ShmVector< T > &ref)
Definition: archive_loader.h:26
ShmVector< T > m_ref
Definition: archive_loader.h:27
Definition: archive_loader.h:19
const vistle::iarchive & m_ar
Definition: archive_loader.h:79
std::shared_ptr< ArrayOwner > m_unreffer
Definition: archive_loader.h:34
ArrayLoader(const ArrayLoader &other)=delete
int m_type
Definition: archive_loader.h:78
std::string m_arname
Definition: archive_loader.h:76
std::string m_name
Definition: archive_loader.h:77
void operator()(T)
Definition: archive_loader.h:37
bool m_ok
Definition: archive_loader.h:75
Definition: shmname.h:11
std::string str() const
Definition: shmname.cpp:18
boost::interprocess::allocator< T, managed_shm::segment_manager > allocator
Definition: shm.h:111