View on GitHub

Vistle

Distributed Data-parallel Scientific Visualization in VR

shm_reference_impl.h
Go to the documentation of this file.
1#ifndef SHM_REFERENCE_IMPL_H
2#define SHM_REFERENCE_IMPL_H
3
4#include "archives_config.h"
5#include "object.h"
6
7namespace vistle {
8
9template<class T>
10shm_array_ref<T>::shm_array_ref(const std::vector<typename T::value_type> &data)
11: m_name(Shm::the().createArrayId()), m_p(shm<T>::construct(m_name)(Shm::the().allocator()))
12{
13 ref();
14 Shm::the().addArray(m_name, &*m_p);
15 (*this)->resize(data.size());
16 std::copy(data.begin(), data.end(), (*this)->begin());
17}
18
19template<class T>
21 const std::vector<typename T::value_type, vistle::default_init_allocator<typename T::value_type>> &data)
22: m_name(Shm::the().createArrayId()), m_p(shm<T>::construct(m_name)(Shm::the().allocator()))
23{
24 ref();
25 Shm::the().addArray(m_name, &*m_p);
26 (*this)->resize(data.size());
27 std::copy(data.begin(), data.end(), (*this)->begin());
28}
29
30template<class T>
31shm_array_ref<T>::shm_array_ref(const typename T::value_type *data, size_t size)
32: m_name(Shm::the().createArrayId()), m_p(shm<T>::construct(m_name)(Shm::the().allocator()))
33{
34 ref();
35 Shm::the().addArray(m_name, &*m_p);
36 (*this)->resize(size);
37 std::copy(data, data + size, (*this)->begin());
38}
39
40template<class T>
41template<class Archive>
42void shm_array_ref<T>::save(Archive &ar) const
44 ar &V_NAME(ar, "shm_name", m_name);
45 ar.template saveArray<typename T::value_type>(*this);
46}
47
48template<class T>
49template<class Archive>
50void shm_array_ref<T>::load(Archive &ar)
51{
52 shm_name_t shmname;
53 ar &V_NAME(ar, "shm_name", shmname);
54
55 std::string arname = shmname.str();
56 std::string name = ar.translateArrayName(arname);
57 //std::cerr << "shm_array_ref: loading " << shmname << " for " << m_name << "/" << name << ", valid=" << valid() << std::endl;
58 if (name.empty() || m_name.str() != name) {
59 unref();
60 m_name.clear();
61 }
62
63 ObjectData *obj = ar.currentObject();
64 if (obj) {
65 //std::cerr << "obj " << obj->name << ": unresolved: " << name << std::endl;
66 obj->unresolvedReference();
67 }
68
69 auto handler = ar.objectCompletionHandler();
70 ar.template fetchArray<typename T::value_type>(arname, [this, obj, handler](const std::string &name) -> void {
71 auto ref = Shm::the().getArrayFromName<typename T::value_type>(name);
72 //std::cerr << "shm_array: array completion handler: " << arname << " -> " << name << "/" << m_name << ", ref=" << ref << std::endl;
73 if (!ref) {
74 std::cerr << "shm_array: NOT COMPLETE: array completion handler: " << name << ", ref=" << ref << std::endl;
75 }
76 assert(ref);
77 *this = ref;
78 if (obj) {
79 //std::cerr << "obj " << obj->name << ": RESOLVED: " << name << std::endl;
80 obj->referenceResolved(handler);
81 } else {
82 std::cerr << "shm_array RESOLVED: " << name << ", but no handler" << std::endl;
83 }
84 });
85 //std::cerr << "shm_array: first try: this=" << *this << ", ref=" << ref << std::endl;
86}
87
88template<class T>
90{
91 return *m_p;
92}
93
94template<class T>
96{
97 return *m_p;
98}
99
100template<class T>
102{
103#ifdef NO_SHMEM
104 return m_p;
105#else
106 return m_p.get();
107#endif
108}
109
110template<class T>
112{
113#ifdef NO_SHMEM
114 return m_p;
115#else
116 return m_p.get();
117#endif
119
120template<class T>
122{
123 if (m_p)
124 return m_p->refcount();
125 return -1;
126}
127
128template<class T>
130{
131 return m_name;
132}
133
134} // namespace vistle
135#endif
#define V_NAME(ar, name, obj)
Definition: archives_config.h:441
Definition: shm.h:129
void addArray(const std::string &name, const ShmData *array)
Definition: shm.cpp:510
static Shm & the()
Definition: shm.cpp:315
const ShmVector< T > getArrayFromName(const std::string &name) const
Definition: shm_impl.h:32
Definition: allocator.h:12
Definition: shm_reference.h:15
T * operator->()
Definition: shm_reference_impl.h:101
T & operator*()
Definition: shm_reference_impl.h:89
const shm_name_t & name() const
Definition: shm_reference_impl.h:129
shm_array_ref()
Definition: shm_reference.h:17
void ref()
Definition: shm_reference.h:123
int refcount() const
Definition: shm_reference_impl.h:121
Definition: allobjects.cpp:30
Definition: shmname.h:11
std::string str() const
Definition: shmname.cpp:18
Definition: shm.h:94