1 // Copyright (C) 2004-2006 The Trustees of Indiana University.
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Authors: Douglas Gregor
10 #include <boost/assert.hpp>
11 #include <boost/property_map/parallel/distributed_property_map.hpp>
12 #include <boost/graph/parallel/detail/untracked_pair.hpp>
13 #include <boost/type_traits/is_base_and_derived.hpp>
14 #include <boost/bind.hpp>
15 #include <boost/graph/parallel/simple_trigger.hpp>
17 #ifndef BOOST_GRAPH_USE_MPI
18 #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
21 namespace boost { namespace parallel {
23 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
24 template<typename Reduce>
26 ::distributed_property_map(const ProcessGroup& pg, const GlobalMap& global,
27 const StorageMap& pm, const Reduce& reduce)
28 : data(new data_t(pg, global, pm, reduce, Reduce::non_default_resolver))
30 typedef handle_message<Reduce> Handler;
32 data->ghost_cells.reset(new ghost_cells_type());
33 data->reset = &data_t::template do_reset<Reduce>;
34 data->process_group.replace_handler(Handler(data, reduce));
35 data->process_group.template get_receiver<Handler>()
36 ->setup_triggers(data->process_group);
39 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
40 PBGL_DISTRIB_PMAP::~distributed_property_map() { }
42 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
43 template<typename Reduce>
45 PBGL_DISTRIB_PMAP::set_reduce(const Reduce& reduce)
47 typedef handle_message<Reduce> Handler;
48 data->process_group.replace_handler(Handler(data, reduce));
49 Handler* handler = data->process_group.template get_receiver<Handler>();
50 BOOST_ASSERT(handler);
51 handler->setup_triggers(data->process_group);
52 data->get_default_value = reduce;
53 data->has_default_resolver = Reduce::non_default_resolver;
54 int model = data->model;
55 data->reset = &data_t::template do_reset<Reduce>;
56 set_consistency_model(model);
59 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
60 void PBGL_DISTRIB_PMAP::prune_ghost_cells() const
62 if (data->max_ghost_cells == 0)
65 while (data->ghost_cells->size() > data->max_ghost_cells) {
66 // Evict the last ghost cell
68 if (data->model & cm_flush) {
69 // We need to flush values when we evict them.
70 boost::parallel::detail::untracked_pair<key_type, value_type> const& victim
71 = data->ghost_cells->back();
72 send(data->process_group, get(data->global, victim.first).first,
73 property_map_put, victim);
76 // Actually remove the ghost cell
77 data->ghost_cells->pop_back();
81 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
82 typename PBGL_DISTRIB_PMAP::value_type&
83 PBGL_DISTRIB_PMAP::cell(const key_type& key, bool request_if_missing) const
86 ghost_cells_key_index_type const& key_index
87 = data->ghost_cells->template get<1>();
89 // Search for the ghost cell by key, and project back to the sequence
91 = data->ghost_cells->template project<0>(key_index.find(key));
92 if (ghost_cell == data->ghost_cells->end()) {
94 if (data->has_default_resolver)
95 // Since we have a default resolver, use it to create a default
96 // value for this ghost cell.
97 value = data->get_default_value(key);
98 else if (request_if_missing)
99 // Request the actual value of this key from its owner
100 send_oob_with_reply(data->process_group, get(data->global, key).first,
101 property_map_get, key, value);
103 value = value_type();
105 // Create a ghost cell containing the new value
107 = data->ghost_cells->push_front(std::make_pair(key, value)).first;
109 // If we need to, prune the ghost cells
110 if (data->max_ghost_cells > 0)
112 } else if (data->max_ghost_cells > 0)
113 // Put this cell at the beginning of the MRU list
114 data->ghost_cells->relocate(data->ghost_cells->begin(), ghost_cell);
116 return const_cast<value_type&>(ghost_cell->second);
119 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
120 template<typename Reduce>
123 ::handle_message<Reduce>::operator()(process_id_type source, int tag)
128 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
129 template<typename Reduce>
131 PBGL_DISTRIB_PMAP::handle_message<Reduce>::
132 handle_put(int /*source*/, int /*tag*/,
133 const boost::parallel::detail::untracked_pair<key_type, value_type>& req, trigger_receive_context)
137 shared_ptr<data_t> data(data_ptr);
139 owner_local_pair p = get(data->global, req.first);
140 BOOST_ASSERT(p.first == process_id(data->process_group));
142 detail::maybe_put(data->storage, p.second,
144 get(data->storage, p.second),
148 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
149 template<typename Reduce>
150 typename PBGL_DISTRIB_PMAP::value_type
151 PBGL_DISTRIB_PMAP::handle_message<Reduce>::
152 handle_get(int source, int /*tag*/, const key_type& key,
153 trigger_receive_context)
157 shared_ptr<data_t> data(data_ptr);
160 owner_local_pair p = get(data->global, key);
161 return get(data->storage, p.second);
164 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
165 template<typename Reduce>
167 PBGL_DISTRIB_PMAP::handle_message<Reduce>::
168 handle_multiget(int source, int tag, const std::vector<key_type>& keys,
169 trigger_receive_context)
171 shared_ptr<data_t> data(data_ptr);
174 typedef boost::parallel::detail::untracked_pair<key_type, value_type> key_value;
175 std::vector<key_value> results;
176 std::size_t n = keys.size();
181 for (std::size_t i = 0; i < n; ++i) {
182 local_key_type local_key = get(data->global, keys[i]).second;
183 results.push_back(key_value(keys[i], get(data->storage, local_key)));
185 send(data->process_group, source, property_map_multiget_reply, results);
188 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
189 template<typename Reduce>
191 PBGL_DISTRIB_PMAP::handle_message<Reduce>::
192 handle_multiget_reply
193 (int source, int tag,
194 const std::vector<boost::parallel::detail::untracked_pair<key_type, value_type> >& msg,
195 trigger_receive_context)
197 shared_ptr<data_t> data(data_ptr);
201 ghost_cells_key_index_type const& key_index
202 = data->ghost_cells->template get<1>();
204 std::size_t n = msg.size();
205 for (std::size_t i = 0; i < n; ++i) {
206 // Search for the ghost cell by key, and project back to the sequence
208 = data->ghost_cells->template project<0>(key_index.find(msg[i].first));
210 if (position != data->ghost_cells->end())
211 const_cast<value_type&>(position->second) = msg[i].second;
215 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
216 template<typename Reduce>
218 PBGL_DISTRIB_PMAP::handle_message<Reduce>::
220 (int source, int tag,
221 const std::vector<unsafe_pair<local_key_type, value_type> >& values,
222 trigger_receive_context)
226 shared_ptr<data_t> data(data_ptr);
229 std::size_t n = values.size();
230 for (std::size_t i = 0; i < n; ++i) {
231 local_key_type local_key = values[i].first;
232 value_type local_value = get(data->storage, local_key);
233 detail::maybe_put(data->storage, values[i].first,
234 reduce(values[i].first,
240 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
241 template<typename Reduce>
243 PBGL_DISTRIB_PMAP::handle_message<Reduce>::
244 setup_triggers(process_group_type& pg)
246 using boost::graph::parallel::simple_trigger;
248 simple_trigger(pg, property_map_put, this, &handle_message::handle_put);
249 simple_trigger(pg, property_map_get, this, &handle_message::handle_get);
250 simple_trigger(pg, property_map_multiget, this,
251 &handle_message::handle_multiget);
252 simple_trigger(pg, property_map_multiget_reply, this,
253 &handle_message::handle_multiget_reply);
254 simple_trigger(pg, property_map_multiput, this,
255 &handle_message::handle_multiput);
258 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
261 ::on_synchronize::operator()()
263 int stage=0; // we only get called at the start now
264 shared_ptr<data_t> data(data_ptr);
267 // Determine in which stage backward consistency messages should be sent.
268 int backward_stage = -1;
269 if (data->model & cm_backward) {
270 if (data->model & cm_flush) backward_stage = 1;
271 else backward_stage = 0;
274 // Flush results in first stage
275 if (stage == 0 && data->model & cm_flush)
278 // Backward consistency
279 if (stage == backward_stage && !(data->model & (cm_clear | cm_reset)))
280 data->refresh_ghost_cells();
282 // Optionally clear results
283 if (data->model & cm_clear)
286 // Optionally reset results
287 if (data->model & cm_reset) {
288 if (data->reset) ((*data).*data->reset)();
293 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
295 PBGL_DISTRIB_PMAP::set_consistency_model(int model)
300 bool need_on_synchronize = (model != cm_forward);
302 // Backward consistency is a two-stage process.
303 if (model & cm_backward) {
304 if (model & cm_flush) stages = 3;
307 // For backward consistency to work, we absolutely cannot throw
308 // away any ghost cells.
309 data->max_ghost_cells = 0;
312 // attach the on_synchronize handler.
313 if (need_on_synchronize)
314 data->process_group.replace_on_synchronize_handler(on_synchronize(data));
317 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
319 PBGL_DISTRIB_PMAP::set_max_ghost_cells(std::size_t max_ghost_cells)
321 if ((data->model & cm_backward) && max_ghost_cells > 0)
322 boost::throw_exception(std::runtime_error("distributed_property_map::set_max_ghost_cells: "
323 "cannot limit ghost-cell usage with a backward "
324 "consistency model"));
326 if (max_ghost_cells == 1)
327 // It is not safe to have only 1 ghost cell; the cell() method
331 data->max_ghost_cells = max_ghost_cells;
335 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
336 void PBGL_DISTRIB_PMAP::clear()
341 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
342 void PBGL_DISTRIB_PMAP::data_t::clear()
344 ghost_cells->clear();
347 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
348 void PBGL_DISTRIB_PMAP::reset()
350 if (data->reset) ((*data).*data->reset)();
353 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
354 void PBGL_DISTRIB_PMAP::flush()
359 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
360 void PBGL_DISTRIB_PMAP::data_t::refresh_ghost_cells()
364 std::vector<std::vector<key_type> > keys;
365 keys.resize(num_processes(process_group));
367 // Collect the set of keys for which we will request values
368 for (iterator i = ghost_cells->begin(); i != ghost_cells->end(); ++i)
369 keys[get(global, i->first).first].push_back(i->first);
371 // Send multiget requests to each of the other processors
372 typedef typename ProcessGroup::process_size_type process_size_type;
373 process_size_type n = num_processes(process_group);
374 process_id_type id = process_id(process_group);
375 for (process_size_type p = (id + 1) % n ; p != id ; p = (p + 1) % n) {
376 if (!keys[p].empty())
377 send(process_group, p, property_map_multiget, keys[p]);
381 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
382 void PBGL_DISTRIB_PMAP::data_t::flush()
386 int n = num_processes(process_group);
387 std::vector<std::vector<unsafe_pair<local_key_type, value_type> > > values;
390 // Collect all of the flushed values
391 for (iterator i = ghost_cells->begin(); i != ghost_cells->end(); ++i) {
392 std::pair<int, local_key_type> g = get(global, i->first);
393 values[g.first].push_back(std::make_pair(g.second, i->second));
396 // Transmit flushed values
397 for (int p = 0; p < n; ++p) {
398 if (!values[p].empty())
399 send(process_group, p, property_map_multiput, values[p]);
403 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
404 void PBGL_DISTRIB_PMAP::do_synchronize()
406 if (data->model & cm_backward) {
407 synchronize(data->process_group);
411 // Request refreshes of the values of our ghost cells
412 data->refresh_ghost_cells();
414 // Allows all of the multigets to get to their destinations
415 synchronize(data->process_group);
417 // Allows all of the multiget responses to get to their destinations
418 synchronize(data->process_group);
421 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
422 template<typename Resolver>
423 void PBGL_DISTRIB_PMAP::data_t::do_reset()
425 Resolver* resolver = get_default_value.template target<Resolver>();
426 BOOST_ASSERT(resolver);
428 for (iterator i = ghost_cells->begin(); i != ghost_cells->end(); ++i)
429 const_cast<value_type&>(i->second) = (*resolver)(i->first);
432 } } // end namespace boost::parallel