DIY  3.0
data-parallel out-of-core C++ library
 All Classes Namespaces Functions Typedefs Groups Pages
proxy.hpp
1 #ifndef DIY_PROXY_HPP
2 #define DIY_PROXY_HPP
3 
4 
5 namespace diy
6 {
8  struct Master::Proxy
9  {
10  template <class T>
12 
13  Proxy(Master* master, int gid):
14  gid_(gid),
15  master_(master),
16  incoming_(&master->incoming(gid)),
17  outgoing_(&master->outgoing(gid)),
18  collectives_(&master->collectives(gid)) {}
19 
20  int gid() const { return gid_; }
21 
23  template<class T>
24  void enqueue(const BlockID& to,
25  const T& x,
26  void (*save)(BinaryBuffer&, const T&) = &::diy::save<T>
27  ) const
28  { OutgoingQueues& out = *outgoing_; save(out[to], x); }
29 
31  template<class T>
32  void enqueue(const BlockID& to,
33  const T* x,
34  size_t n,
35  void (*save)(BinaryBuffer&, const T&) = &::diy::save<T>
36  ) const;
37 
41  template<class T>
42  void dequeue(int from,
43  T& x,
44  void (*load)(BinaryBuffer&, T&) = &::diy::load<T>
45  ) const
46  { IncomingQueues& in = *incoming_; load(in[from], x); }
47 
50  template<class T>
51  void dequeue(int from,
52  T* x,
53  size_t n,
54  void (*load)(BinaryBuffer&, T&) = &::diy::load<T>
55  ) const;
56 
57  template<class T>
58  EnqueueIterator<T> enqueuer(const T& x,
59  void (*save)(BinaryBuffer&, const T&) = &::diy::save<T>) const
60  { return EnqueueIterator<T>(this, x, save); }
61 
62  IncomingQueues* incoming() const { return incoming_; }
63  MemoryBuffer& incoming(int from) const { return (*incoming_)[from]; }
64  inline void incoming(std::vector<int>& v) const; // fill v with every gid from which we have a message
65 
66  OutgoingQueues* outgoing() const { return outgoing_; }
67  MemoryBuffer& outgoing(const BlockID& to) const { return (*outgoing_)[to]; }
68 
76  template<class T, class Op>
77  inline void all_reduce(const T& in,
78  Op op
79  ) const;
84  template<class T>
85  inline T read() const;
90  template<class T>
91  inline T get() const;
92 
93  template<class T>
94  inline void scratch(const T& in) const;
95 
100  CollectivesList* collectives() const { return collectives_; }
101 
102  Master* master() const { return master_; }
103 
104  private:
105  int gid_;
106  Master* master_;
107  IncomingQueues* incoming_;
108  OutgoingQueues* outgoing_;
109  CollectivesList* collectives_;
110  };
111 
112  template<class T>
113  struct Master::Proxy::EnqueueIterator:
114  public std::iterator<std::output_iterator_tag, void, void, void, void>
115  {
116  typedef void (*SaveT)(BinaryBuffer&, const T&);
117 
118  EnqueueIterator(const Proxy* proxy, const T& x,
119  SaveT save = &::diy::save<T>):
120  proxy_(proxy), x_(x), save_(save) {}
121 
122  EnqueueIterator& operator=(const BlockID& to) { proxy_->enqueue(to, x_, save_); return *this; }
123  EnqueueIterator& operator*() { return *this; }
124  EnqueueIterator& operator++() { return *this; }
125  EnqueueIterator& operator++(int) { return *this; }
126 
127  private:
128  const Proxy* proxy_;
129  const T& x_;
130  SaveT save_;
131 
132  };
133 
135  {
136  ProxyWithLink(const Proxy& proxy,
137  void* block,
138  Link* link):
139  Proxy(proxy),
140  block_(block),
141  link_(link) {}
142 
143  Link* link() const { return link_; }
144  void* block() const { return block_; }
145 
146  private:
147  void* block_;
148  Link* link_;
149  };
150 }
151 
152 
153 void
154 diy::Master::Proxy::
155 incoming(std::vector<int>& v) const
156 {
157  for (IncomingQueues::const_iterator it = incoming_->begin(); it != incoming_->end(); ++it)
158  v.push_back(it->first);
159 }
160 
161 template<class T, class Op>
162 void
164 all_reduce(const T& in, Op op) const
165 {
166  collectives_->push_back(Collective(new detail::AllReduceOp<T,Op>(in, op)));
167 }
168 
169 template<class T>
170 T
172 read() const
173 {
174  T res;
175  collectives_->front().result_out(&res);
176  return res;
177 }
178 
179 template<class T>
180 T
182 get() const
183 {
184  T res = read<T>();
185  collectives_->pop_front();
186  return res;
187 }
188 
189 template<class T>
190 void
191 diy::Master::Proxy::
192 scratch(const T& in) const
193 {
194  collectives_->push_back(Collective(new detail::Scratch<T>(in)));
195 }
196 
197 template<class T>
198 void
200 enqueue(const BlockID& to, const T* x, size_t n,
201  void (*save)(BinaryBuffer&, const T&)) const
202 {
203  OutgoingQueues& out = *outgoing_;
204  BinaryBuffer& bb = out[to];
205  if (save == (void (*)(BinaryBuffer&, const T&)) &::diy::save<T>)
206  diy::save(bb, x, n); // optimized for unspecialized types
207  else
208  for (size_t i = 0; i < n; ++i)
209  save(bb, x[i]);
210 }
211 
212 template<class T>
213 void
215 dequeue(int from, T* x, size_t n,
216  void (*load)(BinaryBuffer&, T&)) const
217 {
218  IncomingQueues& in = *incoming_;
219  BinaryBuffer& bb = in[from];
220  if (load == (void (*)(BinaryBuffer&, T&)) &::diy::load<T>)
221  diy::load(bb, x, n); // optimized for unspecialized types
222  else
223  for (size_t i = 0; i < n; ++i)
224  load(bb, x[i]);
225 }
226 
227 
228 #endif
void load(BinaryBuffer &bb, T &x)
Loads x from bb by calling diy::Serialization<T>::load(bb,x).
Definition: serialization.hpp:106
void dequeue(int from, T &x, void(*load)(BinaryBuffer &, T &)=&::diy::load< T >) const
Dequeue data whose size can be determined automatically (e.g., STL vector) and that was previously en...
Definition: proxy.hpp:42
Definition: master.hpp:356
T get() const
Return the result of a proxy collective; result is popped off the collectives list.
Definition: proxy.hpp:182
Definition: types.hpp:10
void all_reduce(const T &in, Op op) const
Post an all-reduce collective using an existing communication proxy. Available operators are: maximum...
Definition: proxy.hpp:164
void save(BinaryBuffer &bb, const T &x)
Saves x to bb by calling diy::Serialization<T>::save(bb,x).
Definition: serialization.hpp:102
void in(const RegularLink< Bounds > &link, const Point &p, OutIter out, const Bounds &domain)
Finds the neighbor(s) containing the target point.
Definition: pick.hpp:102
Definition: master.hpp:35
Definition: proxy.hpp:11
Communication proxy, used for enqueueing and dequeueing items for future exchange.
Definition: proxy.hpp:8
T read() const
Return the result of a proxy collective without popping it off the collectives list (same result woul...
Definition: proxy.hpp:172
A serialization buffer.
Definition: serialization.hpp:19
CollectivesList * collectives() const
Return the list of proxy collectives (values and operations)
Definition: proxy.hpp:100
void enqueue(const BlockID &to, const T &x, void(*save)(BinaryBuffer &, const T &)=&::diy::save< T >) const
Enqueue data whose size can be determined automatically, e.g., an STL vector.
Definition: proxy.hpp:24