annotate DEPENDENCIES/generic/include/boost/property_map/parallel/distributed_property_map.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 // Copyright (C) 2004-2008 The Trustees of Indiana University.
Chris@16 2
Chris@16 3 // Use, modification and distribution is subject to the Boost Software
Chris@16 4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 5 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 6
Chris@16 7 // Authors: Douglas Gregor
Chris@16 8 // Nick Edmonds
Chris@16 9 // Andrew Lumsdaine
Chris@16 10
Chris@16 11 // The placement of this #include probably looks very odd relative to
Chris@16 12 // the #ifndef/#define pair below. However, this placement is
Chris@16 13 // extremely important to allow the various property map headers to be
Chris@16 14 // included in any order.
Chris@16 15 #include <boost/property_map/property_map.hpp>
Chris@16 16
Chris@16 17 #ifndef BOOST_PARALLEL_DISTRIBUTED_PROPERTY_MAP_HPP
Chris@16 18 #define BOOST_PARALLEL_DISTRIBUTED_PROPERTY_MAP_HPP
Chris@16 19
Chris@16 20 #include <boost/assert.hpp>
Chris@16 21 #include <boost/type_traits/is_base_and_derived.hpp>
Chris@16 22 #include <boost/shared_ptr.hpp>
Chris@16 23 #include <boost/weak_ptr.hpp>
Chris@16 24 #include <boost/optional.hpp>
Chris@101 25 #include <boost/property_map/parallel/process_group.hpp>
Chris@16 26 #include <boost/function/function1.hpp>
Chris@16 27 #include <vector>
Chris@16 28 #include <set>
Chris@101 29 #include <boost/property_map/parallel/basic_reduce.hpp>
Chris@101 30 #include <boost/property_map/parallel/detail/untracked_pair.hpp>
Chris@16 31 #include <boost/type_traits/is_same.hpp>
Chris@16 32 #include <boost/property_map/parallel/local_property_map.hpp>
Chris@16 33 #include <map>
Chris@16 34 #include <boost/version.hpp>
Chris@101 35 #include <boost/property_map/parallel/unsafe_serialize.hpp>
Chris@16 36 #include <boost/multi_index_container.hpp>
Chris@16 37 #include <boost/multi_index/hashed_index.hpp>
Chris@16 38 #include <boost/multi_index/member.hpp>
Chris@16 39 #include <boost/multi_index/sequenced_index.hpp>
Chris@16 40
Chris@16 41 // Serialization functions for constructs we use
Chris@16 42 #include <boost/serialization/utility.hpp>
Chris@16 43
Chris@16 44 namespace boost { namespace parallel {
Chris@16 45
Chris@16 46 namespace detail {
Chris@16 47 /**************************************************************************
Chris@16 48 * Metafunction that degrades an Lvalue Property Map category tag to
Chris@16 49 * a Read Write Property Map category tag.
Chris@16 50 **************************************************************************/
Chris@16 51 template<bool IsLvaluePropertyMap>
Chris@16 52 struct make_nonlvalue_property_map
Chris@16 53 {
Chris@16 54 template<typename T> struct apply { typedef T type; };
Chris@16 55 };
Chris@16 56
Chris@16 57 template<>
Chris@16 58 struct make_nonlvalue_property_map<true>
Chris@16 59 {
Chris@16 60 template<typename>
Chris@16 61 struct apply
Chris@16 62 {
Chris@16 63 typedef read_write_property_map_tag type;
Chris@16 64 };
Chris@16 65 };
Chris@16 66
Chris@16 67 /**************************************************************************
Chris@16 68 * Performs a "put" on a property map so long as the property map is
Chris@16 69 * a Writable Property Map or a mutable Lvalue Property Map. This
Chris@16 70 * is required because the distributed property map's message
Chris@16 71 * handler handles "put" messages even for a const property map,
Chris@16 72 * although receipt of a "put" message is ill-formed.
Chris@16 73 **************************************************************************/
Chris@16 74 template<bool IsLvaluePropertyMap>
Chris@16 75 struct maybe_put_in_lvalue_pm
Chris@16 76 {
Chris@16 77 template<typename PropertyMap, typename Key, typename Value>
Chris@16 78 static inline void
Chris@16 79 do_put(PropertyMap, const Key&, const Value&)
Chris@16 80 { BOOST_ASSERT(false); }
Chris@16 81 };
Chris@16 82
Chris@16 83 template<>
Chris@16 84 struct maybe_put_in_lvalue_pm<true>
Chris@16 85 {
Chris@16 86 template<typename PropertyMap, typename Key, typename Value>
Chris@16 87 static inline void
Chris@16 88 do_put(PropertyMap pm, const Key& key, const Value& value)
Chris@101 89 {
Chris@16 90 using boost::put;
Chris@16 91
Chris@101 92 put(pm, key, value);
Chris@16 93 }
Chris@16 94 };
Chris@16 95
Chris@16 96 template<typename PropertyMap, typename Key, typename Value>
Chris@16 97 inline void
Chris@16 98 maybe_put_impl(PropertyMap pm, const Key& key, const Value& value,
Chris@16 99 writable_property_map_tag)
Chris@16 100 {
Chris@16 101 using boost::put;
Chris@16 102
Chris@16 103 put(pm, key, value);
Chris@16 104 }
Chris@16 105
Chris@16 106 template<typename PropertyMap, typename Key, typename Value>
Chris@16 107 inline void
Chris@16 108 maybe_put_impl(PropertyMap pm, const Key& key, const Value& value,
Chris@16 109 lvalue_property_map_tag)
Chris@16 110 {
Chris@16 111 typedef typename property_traits<PropertyMap>::value_type value_type;
Chris@16 112 typedef typename property_traits<PropertyMap>::reference reference;
Chris@16 113 // DPG TBD: Some property maps are improperly characterized as
Chris@16 114 // lvalue_property_maps, when in fact they do not provide true
Chris@16 115 // references. The most typical example is those property maps
Chris@16 116 // built from vector<bool> and its iterators, which deal with
Chris@16 117 // proxies. We don't want to mischaracterize these as not having a
Chris@16 118 // "put" operation, so we only consider an lvalue_property_map as
Chris@16 119 // constant if its reference is const value_type&. In fact, this
Chris@16 120 // isn't even quite correct (think of a
Chris@16 121 // vector<bool>::const_iterator), but at present C++ doesn't
Chris@16 122 // provide us with any alternatives.
Chris@16 123 typedef is_same<const value_type&, reference> is_constant;
Chris@16 124
Chris@16 125 maybe_put_in_lvalue_pm<(!is_constant::value)>::do_put(pm, key, value);
Chris@16 126 }
Chris@16 127
Chris@16 128 template<typename PropertyMap, typename Key, typename Value>
Chris@16 129 inline void
Chris@16 130 maybe_put_impl(PropertyMap, const Key&, const Value&, ...)
Chris@16 131 { BOOST_ASSERT(false); }
Chris@16 132
Chris@16 133 template<typename PropertyMap, typename Key, typename Value>
Chris@16 134 inline void
Chris@16 135 maybe_put(PropertyMap pm, const Key& key, const Value& value)
Chris@16 136 {
Chris@16 137 maybe_put_impl(pm, key, value,
Chris@16 138 typename property_traits<PropertyMap>::category());
Chris@16 139 }
Chris@16 140 } // end namespace detail
Chris@16 141
Chris@16 142 /** The consistency model used by the distributed property map. */
Chris@16 143 enum consistency_model {
Chris@16 144 cm_forward = 1 << 0,
Chris@16 145 cm_backward = 1 << 1,
Chris@16 146 cm_bidirectional = cm_forward | cm_backward,
Chris@16 147 cm_flush = 1 << 2,
Chris@16 148 cm_reset = 1 << 3,
Chris@16 149 cm_clear = 1 << 4
Chris@16 150 };
Chris@16 151
Chris@16 152 /** Distributed property map adaptor.
Chris@16 153 *
Chris@16 154 * The distributed property map adaptor is a property map whose
Chris@16 155 * stored values are distributed across multiple non-overlapping
Chris@16 156 * memory spaces on different processes. Values local to the current
Chris@16 157 * process are stored within a local property map and may be
Chris@16 158 * immediately accessed via @c get and @c put. Values stored on
Chris@16 159 * remote processes may also be access via @c get and @c put, but the
Chris@16 160 * behavior differs slightly:
Chris@16 161 *
Chris@16 162 * - @c put operations update a local ghost cell and send a "put"
Chris@16 163 * message to the process that owns the value. The owner is free to
Chris@16 164 * update its own "official" value or may ignore the put request.
Chris@16 165 *
Chris@16 166 * - @c get operations returns the contents of the local ghost
Chris@16 167 * cell. If no ghost cell is available, one is created using the
Chris@16 168 * default value provided by the "reduce" operation. See, e.g.,
Chris@16 169 * @ref basic_reduce and @ref property_reduce.
Chris@16 170 *
Chris@16 171 * Using distributed property maps requires a bit more care than using
Chris@16 172 * local, sequential property maps. While the syntax and semantics are
Chris@16 173 * similar, distributed property maps may contain out-of-date
Chris@16 174 * information that can only be guaranteed to be synchronized by
Chris@16 175 * calling the @ref synchronize function in all processes.
Chris@16 176 *
Chris@16 177 * To address the issue of out-of-date values, distributed property
Chris@16 178 * maps are supplied with a reduction operation. The reduction
Chris@16 179 * operation has two roles:
Chris@16 180 *
Chris@16 181 * -# When a value is needed for a remote key but no value is
Chris@16 182 * immediately available, the reduction operation provides a
Chris@16 183 * suitable default. For instance, a distributed property map
Chris@16 184 * storing distances may have a reduction operation that returns
Chris@16 185 * an infinite value as the default, whereas a distributed
Chris@16 186 * property map for vertex colors may return white as the
Chris@16 187 * default.
Chris@16 188 *
Chris@16 189 * -# When a value is received from a remote process, the process
Chris@16 190 * owning the key associated with that value must determine which
Chris@16 191 * value---the locally stored value, the value received from a
Chris@16 192 * remote process, or some combination of the two---will be
Chris@16 193 * stored as the "official" value in the property map. The
Chris@16 194 * reduction operation transforms the local and remote values
Chris@16 195 * into the "official" value to be stored.
Chris@16 196 *
Chris@16 197 * @tparam ProcessGroup the type of the process group over which the
Chris@16 198 * property map is distributed and is also the medium for
Chris@16 199 * communication.
Chris@16 200 *
Chris@16 201 * @tparam StorageMap the type of the property map that will
Chris@16 202 * store values for keys local to this processor. The @c value_type of
Chris@16 203 * this property map will become the @c value_type of the distributed
Chris@16 204 * property map. The distributed property map models the same property
Chris@16 205 * map concepts as the @c LocalPropertyMap, with one exception: a
Chris@16 206 * distributed property map cannot be an LvaluePropertyMap (because
Chris@16 207 * remote values are not addressable), and is therefore limited to
Chris@16 208 * ReadWritePropertyMap.
Chris@16 209 */
Chris@16 210 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
Chris@16 211 class distributed_property_map
Chris@16 212 {
Chris@16 213 public:
Chris@16 214 /// The key type of the property map.
Chris@16 215 typedef typename property_traits<GlobalMap>::key_type key_type;
Chris@16 216
Chris@16 217 /// The value type of the property map.
Chris@16 218 typedef typename property_traits<StorageMap>::value_type value_type;
Chris@16 219 typedef typename property_traits<StorageMap>::reference reference;
Chris@16 220 typedef ProcessGroup process_group_type;
Chris@16 221
Chris@16 222 private:
Chris@16 223 typedef distributed_property_map self_type;
Chris@16 224 typedef typename property_traits<StorageMap>::category local_category;
Chris@16 225 typedef typename property_traits<StorageMap>::key_type local_key_type;
Chris@16 226 typedef typename property_traits<GlobalMap>::value_type owner_local_pair;
Chris@16 227 typedef typename ProcessGroup::process_id_type process_id_type;
Chris@16 228
Chris@16 229 enum property_map_messages {
Chris@16 230 /** A request to store a value in a property map. The message
Chris@16 231 * contains a std::pair<key, data>.
Chris@16 232 */
Chris@16 233 property_map_put,
Chris@16 234
Chris@16 235 /** A request to retrieve a particular value in a property
Chris@101 236 * map. The message contains a key. The owner of that key will
Chris@16 237 * reply with a value.
Chris@16 238 */
Chris@16 239 property_map_get,
Chris@16 240
Chris@16 241 /** A request to update values stored on a remote processor. The
Chris@16 242 * message contains a vector of keys for which the source
Chris@16 243 * requests updated values. This message will only be transmitted
Chris@16 244 * during synchronization.
Chris@16 245 */
Chris@16 246 property_map_multiget,
Chris@16 247
Chris@16 248 /** A request to store values in a ghost cell. This message
Chris@16 249 * contains a vector of key/value pairs corresponding to the
Chris@16 250 * sequence of keys sent to the source processor.
Chris@16 251 */
Chris@16 252 property_map_multiget_reply,
Chris@16 253
Chris@16 254 /** The payload containing a vector of local key-value pairs to be
Chris@16 255 * put into the remote property map. A key-value std::pair will be
Chris@16 256 * used to store each local key-value pair.
Chris@16 257 */
Chris@16 258 property_map_multiput
Chris@16 259 };
Chris@16 260
Chris@16 261 // Code from Joaquín M López Muñoz to work around unusual implementation of
Chris@16 262 // std::pair in VC++ 10:
Chris@16 263 template<typename First,typename Second>
Chris@16 264 class pair_first_extractor {
Chris@16 265 typedef std::pair<First,Second> value_type;
Chris@16 266
Chris@16 267 public:
Chris@16 268 typedef First result_type;
Chris@16 269 const result_type& operator()(const value_type& x) const {
Chris@16 270 return x.first;
Chris@16 271 }
Chris@16 272
Chris@16 273 result_type& operator()(value_type& x) const {
Chris@16 274 return x.first;
Chris@16 275 }
Chris@16 276 };
Chris@16 277
Chris@16 278 public:
Chris@16 279 /// The type of the ghost cells
Chris@16 280 typedef multi_index::multi_index_container<
Chris@16 281 std::pair<key_type, value_type>,
Chris@16 282 multi_index::indexed_by<
Chris@16 283 multi_index::sequenced<>,
Chris@16 284 multi_index::hashed_unique<
Chris@16 285 pair_first_extractor<key_type, value_type>
Chris@16 286 >
Chris@16 287 >
Chris@16 288 > ghost_cells_type;
Chris@16 289
Chris@16 290 /// Iterator into the ghost cells
Chris@16 291 typedef typename ghost_cells_type::iterator iterator;
Chris@16 292
Chris@16 293 /// Key-based index into the ghost cells
Chris@16 294 typedef typename ghost_cells_type::template nth_index<1>::type
Chris@16 295 ghost_cells_key_index_type;
Chris@16 296
Chris@16 297 /// Iterator into the ghost cells (by key)
Chris@16 298 typedef typename ghost_cells_key_index_type::iterator key_iterator;
Chris@16 299
Chris@16 300 /** The property map category. A distributed property map cannot be
Chris@16 301 * an Lvalue Property Map, because values on remote processes cannot
Chris@16 302 * be addresses.
Chris@16 303 */
Chris@16 304 typedef typename detail::make_nonlvalue_property_map<
Chris@16 305 (is_base_and_derived<lvalue_property_map_tag, local_category>::value
Chris@16 306 || is_same<lvalue_property_map_tag, local_category>::value)>
Chris@16 307 ::template apply<local_category>::type category;
Chris@16 308
Chris@16 309 /** Default-construct a distributed property map. This function
Chris@16 310 * creates an initialized property map that must be assigned to a
Chris@16 311 * valid value before being used. It is only provided here because
Chris@16 312 * property maps must be Default Constructible.
Chris@16 313 */
Chris@16 314 distributed_property_map() {}
Chris@16 315
Chris@16 316 /** Construct a distributed property map. Builds a distributed
Chris@16 317 * property map communicating over the given process group and using
Chris@16 318 * the given local property map for storage. Since no reduction
Chris@16 319 * operation is provided, the default reduction operation @c
Chris@16 320 * basic_reduce<value_type> is used.
Chris@16 321 */
Chris@16 322 distributed_property_map(const ProcessGroup& pg, const GlobalMap& global,
Chris@16 323 const StorageMap& pm)
Chris@16 324 : data(new data_t(pg, global, pm, basic_reduce<value_type>(), false))
Chris@16 325 {
Chris@16 326 typedef handle_message<basic_reduce<value_type> > Handler;
Chris@16 327
Chris@16 328 data->ghost_cells.reset(new ghost_cells_type());
Chris@16 329 Handler handler(data);
Chris@16 330 data->process_group.replace_handler(handler, true);
Chris@16 331 data->process_group.template get_receiver<Handler>()
Chris@16 332 ->setup_triggers(data->process_group);
Chris@16 333 }
Chris@16 334
Chris@16 335 /** Construct a distributed property map. Builds a distributed
Chris@16 336 * property map communicating over the given process group and using
Chris@16 337 * the given local property map for storage. The given @p reduce
Chris@16 338 * parameter is used as the reduction operation.
Chris@16 339 */
Chris@16 340 template<typename Reduce>
Chris@16 341 distributed_property_map(const ProcessGroup& pg, const GlobalMap& global,
Chris@16 342 const StorageMap& pm,
Chris@16 343 const Reduce& reduce);
Chris@16 344
Chris@16 345 ~distributed_property_map();
Chris@16 346
Chris@16 347 /// Set the reduce operation of the distributed property map.
Chris@16 348 template<typename Reduce>
Chris@16 349 void set_reduce(const Reduce& reduce);
Chris@16 350
Chris@16 351 // Set the consistency model for the distributed property map
Chris@16 352 void set_consistency_model(int model);
Chris@16 353
Chris@16 354 // Get the consistency model
Chris@16 355 int get_consistency_model() const { return data->model; }
Chris@16 356
Chris@16 357 // Set the maximum number of ghost cells that we are allowed to
Chris@16 358 // maintain. If 0, all ghost cells will be retained.
Chris@16 359 void set_max_ghost_cells(std::size_t max_ghost_cells);
Chris@16 360
Chris@16 361 // Clear out all ghost cells
Chris@16 362 void clear();
Chris@16 363
Chris@16 364 // Reset the values in all ghost cells to the default value
Chris@16 365 void reset();
Chris@16 366
Chris@16 367 // Flush all values destined for remote processors
Chris@16 368 void flush();
Chris@16 369
Chris@16 370 reference operator[](const key_type& key) const
Chris@16 371 {
Chris@16 372 owner_local_pair p = get(data->global, key);
Chris@101 373
Chris@16 374 if (p.first == process_id(data->process_group)) {
Chris@16 375 return data->storage[p.second];
Chris@16 376 } else {
Chris@16 377 return cell(key);
Chris@16 378 }
Chris@16 379 }
Chris@16 380
Chris@16 381 process_group_type process_group() const
Chris@16 382 {
Chris@16 383 return data->process_group.base();
Chris@16 384 }
Chris@16 385
Chris@16 386 StorageMap& base() { return data->storage; }
Chris@16 387 const StorageMap& base() const { return data->storage; }
Chris@16 388
Chris@16 389 /** Sends a "put" request.
Chris@16 390 * \internal
Chris@16 391 *
Chris@16 392 */
Chris@101 393 void
Chris@16 394 request_put(process_id_type p, const key_type& k, const value_type& v) const
Chris@101 395 {
Chris@101 396 send(data->process_group, p, property_map_put,
Chris@101 397 boost::parallel::detail::make_untracked_pair(k, v));
Chris@16 398 }
Chris@16 399
Chris@16 400 /** Access the ghost cell for the given key.
Chris@16 401 * \internal
Chris@16 402 */
Chris@16 403 value_type& cell(const key_type& k, bool request_if_missing = true) const;
Chris@16 404
Chris@16 405 /** Perform synchronization
Chris@16 406 * \internal
Chris@16 407 */
Chris@16 408 void do_synchronize();
Chris@16 409
Chris@16 410 const GlobalMap& global() const { return data->global; }
Chris@16 411 GlobalMap& global() { return data->global; }
Chris@16 412
Chris@16 413 struct data_t
Chris@16 414 {
Chris@101 415 data_t(const ProcessGroup& pg, const GlobalMap& global,
Chris@16 416 const StorageMap& pm, const function1<value_type, key_type>& dv,
Chris@16 417 bool has_default_resolver)
Chris@101 418 : process_group(pg), global(global), storage(pm),
Chris@101 419 ghost_cells(), max_ghost_cells(1000000), get_default_value(dv),
Chris@16 420 has_default_resolver(has_default_resolver), model(cm_forward) { }
Chris@16 421
Chris@16 422 /// The process group
Chris@16 423 ProcessGroup process_group;
Chris@16 424
Chris@16 425 /// A mapping from the keys of this property map to the global
Chris@16 426 /// descriptor.
Chris@16 427 GlobalMap global;
Chris@16 428
Chris@16 429 /// Local property map
Chris@16 430 StorageMap storage;
Chris@16 431
Chris@16 432 /// The ghost cells
Chris@16 433 shared_ptr<ghost_cells_type> ghost_cells;
Chris@16 434
Chris@16 435 /// The maximum number of ghost cells we are permitted to hold. If
Chris@16 436 /// zero, we are permitted to have an infinite number of ghost
Chris@16 437 /// cells.
Chris@16 438 std::size_t max_ghost_cells;
Chris@16 439
Chris@16 440 /// Default value for remote ghost cells, as defined by the
Chris@16 441 /// reduction operation.
Chris@16 442 function1<value_type, key_type> get_default_value;
Chris@16 443
Chris@16 444 /// True if this resolver is the "default" resolver, meaning that
Chris@16 445 /// we should not be able to get() a default value; it needs to be
Chris@16 446 /// request()ed first.
Chris@16 447 bool has_default_resolver;
Chris@16 448
Chris@16 449 // Current consistency model
Chris@16 450 int model;
Chris@16 451
Chris@16 452 // Function that resets all of the ghost cells to their default
Chris@16 453 // values. It knows the type of the resolver, so we can eliminate
Chris@16 454 // a large number of calls through function pointers.
Chris@16 455 void (data_t::*reset)();
Chris@16 456
Chris@16 457 // Clear out all ghost cells
Chris@16 458 void clear();
Chris@16 459
Chris@16 460 // Flush all values destined for remote processors
Chris@16 461 void flush();
Chris@16 462
Chris@16 463 // Send out requests to "refresh" the values of ghost cells that
Chris@16 464 // we're holding.
Chris@16 465 void refresh_ghost_cells();
Chris@16 466
Chris@16 467 private:
Chris@16 468 template<typename Resolver> void do_reset();
Chris@16 469
Chris@16 470 friend class distributed_property_map;
Chris@16 471 };
Chris@16 472 friend struct data_t;
Chris@16 473
Chris@16 474 shared_ptr<data_t> data;
Chris@16 475
Chris@16 476 private:
Chris@16 477 // Prunes the least recently used ghost cells until we have @c
Chris@16 478 // max_ghost_cells or fewer ghost cells.
Chris@16 479 void prune_ghost_cells() const;
Chris@16 480
Chris@16 481 /** Handles incoming messages.
Chris@16 482 *
Chris@16 483 * This function object is responsible for handling all incoming
Chris@16 484 * messages for the distributed property map.
Chris@16 485 */
Chris@16 486 template<typename Reduce>
Chris@16 487 struct handle_message
Chris@16 488 {
Chris@16 489 explicit handle_message(const shared_ptr<data_t>& data,
Chris@16 490 const Reduce& reduce = Reduce())
Chris@16 491 : data_ptr(data), reduce(reduce) { }
Chris@16 492
Chris@16 493 void operator()(process_id_type source, int tag);
Chris@16 494
Chris@16 495 /// Individual message handlers
Chris@101 496 void
Chris@101 497 handle_put(int source, int tag,
Chris@101 498 const boost::parallel::detail::untracked_pair<key_type, value_type>& data,
Chris@16 499 trigger_receive_context);
Chris@16 500
Chris@16 501 value_type
Chris@101 502 handle_get(int source, int tag, const key_type& data,
Chris@16 503 trigger_receive_context);
Chris@16 504
Chris@16 505 void
Chris@101 506 handle_multiget(int source, int tag,
Chris@16 507 const std::vector<key_type>& data,
Chris@16 508 trigger_receive_context);
Chris@16 509
Chris@16 510 void
Chris@16 511 handle_multiget_reply
Chris@101 512 (int source, int tag,
Chris@16 513 const std::vector<boost::parallel::detail::untracked_pair<key_type, value_type> >& msg,
Chris@16 514 trigger_receive_context);
Chris@16 515
Chris@16 516 void
Chris@16 517 handle_multiput
Chris@101 518 (int source, int tag,
Chris@16 519 const std::vector<unsafe_pair<local_key_type, value_type> >& data,
Chris@16 520 trigger_receive_context);
Chris@16 521
Chris@16 522 void setup_triggers(process_group_type& pg);
Chris@16 523
Chris@16 524 private:
Chris@16 525 weak_ptr<data_t> data_ptr;
Chris@16 526 Reduce reduce;
Chris@16 527 };
Chris@16 528
Chris@16 529 /* Sets up the next stage in a multi-stage synchronization, for
Chris@16 530 bidirectional consistency. */
Chris@16 531 struct on_synchronize
Chris@16 532 {
Chris@16 533 explicit on_synchronize(const shared_ptr<data_t>& data) : data_ptr(data) { }
Chris@16 534
Chris@16 535 void operator()();
Chris@16 536
Chris@16 537 private:
Chris@16 538 weak_ptr<data_t> data_ptr;
Chris@16 539 };
Chris@16 540 };
Chris@16 541
Chris@16 542 /* An implementation helper macro for the common case of naming
Chris@16 543 distributed property maps with all of the normal template
Chris@16 544 parameters. */
Chris@16 545 #define PBGL_DISTRIB_PMAP \
Chris@16 546 distributed_property_map<ProcessGroup, GlobalMap, StorageMap>
Chris@16 547
Chris@16 548 /* Request that the value for the given remote key be retrieved in
Chris@16 549 the next synchronization round. */
Chris@16 550 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
Chris@16 551 inline void
Chris@16 552 request(const PBGL_DISTRIB_PMAP& pm,
Chris@16 553 typename PBGL_DISTRIB_PMAP::key_type const& key)
Chris@16 554 {
Chris@16 555 if (get(pm.data->global, key).first != process_id(pm.data->process_group))
Chris@16 556 pm.cell(key, false);
Chris@16 557 }
Chris@16 558
Chris@16 559 /** Get the value associated with a particular key. Retrieves the
Chris@16 560 * value associated with the given key. If the key denotes a
Chris@16 561 * locally-owned object, it returns the value from the local property
Chris@16 562 * map; if the key denotes a remotely-owned object, retrieves the
Chris@16 563 * value of the ghost cell for that key, which may be the default
Chris@16 564 * value provided by the reduce operation.
Chris@16 565 *
Chris@16 566 * Complexity: For a local key, O(1) get operations on the underlying
Chris@16 567 * property map. For a non-local key, O(1) accesses to the ghost cells.
Chris@16 568 */
Chris@16 569 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
Chris@16 570 inline
Chris@16 571 typename PBGL_DISTRIB_PMAP::value_type
Chris@16 572 get(const PBGL_DISTRIB_PMAP& pm,
Chris@16 573 typename PBGL_DISTRIB_PMAP::key_type const& key)
Chris@16 574 {
Chris@16 575 using boost::get;
Chris@16 576
Chris@101 577 typename property_traits<GlobalMap>::value_type p =
Chris@16 578 get(pm.data->global, key);
Chris@16 579
Chris@16 580 if (p.first == process_id(pm.data->process_group)) {
Chris@16 581 return get(pm.data->storage, p.second);
Chris@16 582 } else {
Chris@16 583 return pm.cell(key);
Chris@16 584 }
Chris@16 585 }
Chris@16 586
Chris@16 587 /** Put a value associated with the given key into the property map.
Chris@16 588 * When the key denotes a locally-owned object, this operation updates
Chris@16 589 * the underlying local property map. Otherwise, the local ghost cell
Chris@16 590 * is updated and a "put" message is sent to the processor owning this
Chris@16 591 * key.
Chris@16 592 *
Chris@16 593 * Complexity: For a local key, O(1) put operations on the underlying
Chris@16 594 * property map. For a nonlocal key, O(1) accesses to the ghost cells
Chris@16 595 * and will send O(1) messages of size O(sizeof(key) + sizeof(value)).
Chris@16 596 */
Chris@16 597 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
Chris@16 598 void
Chris@16 599 put(const PBGL_DISTRIB_PMAP& pm,
Chris@16 600 typename PBGL_DISTRIB_PMAP::key_type const & key,
Chris@16 601 typename PBGL_DISTRIB_PMAP::value_type const & value)
Chris@16 602 {
Chris@16 603 using boost::put;
Chris@16 604
Chris@101 605 typename property_traits<GlobalMap>::value_type p =
Chris@16 606 get(pm.data->global, key);
Chris@16 607
Chris@16 608 if (p.first == process_id(pm.data->process_group)) {
Chris@16 609 put(pm.data->storage, p.second, value);
Chris@16 610 } else {
Chris@101 611 if (pm.data->model & cm_forward)
Chris@16 612 pm.request_put(p.first, key, value);
Chris@16 613
Chris@16 614 pm.cell(key, false) = value;
Chris@16 615 }
Chris@16 616 }
Chris@16 617
Chris@16 618 /** Put a value associated with a given key into the local view of the
Chris@16 619 * property map. This operation is equivalent to @c put, but with one
Chris@16 620 * exception: no message will be sent to the owning processor in the
Chris@16 621 * case of a remote update. The effect is that any value written via
Chris@16 622 * @c local_put for a remote key may be overwritten in the next
Chris@16 623 * synchronization round.
Chris@16 624 */
Chris@16 625 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
Chris@16 626 void
Chris@16 627 local_put(const PBGL_DISTRIB_PMAP& pm,
Chris@16 628 typename PBGL_DISTRIB_PMAP::key_type const & key,
Chris@16 629 typename PBGL_DISTRIB_PMAP::value_type const & value)
Chris@16 630 {
Chris@16 631 using boost::put;
Chris@16 632
Chris@101 633 typename property_traits<GlobalMap>::value_type p =
Chris@16 634 get(pm.data->global, key);
Chris@16 635
Chris@16 636 if (p.first == process_id(pm.data->process_group))
Chris@16 637 put(pm.data->storage, p.second, value);
Chris@16 638 else pm.cell(key, false) = value;
Chris@16 639 }
Chris@16 640
Chris@16 641 /** Cache the value associated with the given remote key. If the key
Chris@16 642 * is local, ignore the operation. */
Chris@16 643 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
Chris@16 644 inline void
Chris@16 645 cache(const PBGL_DISTRIB_PMAP& pm,
Chris@16 646 typename PBGL_DISTRIB_PMAP::key_type const & key,
Chris@16 647 typename PBGL_DISTRIB_PMAP::value_type const & value)
Chris@16 648 {
Chris@16 649 typename ProcessGroup::process_id_type id = get(pm.data->global, key).first;
Chris@16 650
Chris@16 651 if (id != process_id(pm.data->process_group)) pm.cell(key, false) = value;
Chris@16 652 }
Chris@16 653
Chris@16 654 /// Synchronize the property map.
Chris@16 655 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
Chris@16 656 void
Chris@16 657 synchronize(PBGL_DISTRIB_PMAP& pm)
Chris@16 658 {
Chris@16 659 pm.do_synchronize();
Chris@16 660 }
Chris@16 661
Chris@16 662 /// Create a distributed property map.
Chris@16 663 template<typename ProcessGroup, typename GlobalMap, typename StorageMap>
Chris@16 664 inline distributed_property_map<ProcessGroup, GlobalMap, StorageMap>
Chris@101 665 make_distributed_property_map(const ProcessGroup& pg, GlobalMap global,
Chris@16 666 StorageMap storage)
Chris@16 667 {
Chris@16 668 typedef distributed_property_map<ProcessGroup, GlobalMap, StorageMap>
Chris@16 669 result_type;
Chris@16 670 return result_type(pg, global, storage);
Chris@16 671 }
Chris@16 672
Chris@16 673 /**
Chris@16 674 * \overload
Chris@16 675 */
Chris@101 676 template<typename ProcessGroup, typename GlobalMap, typename StorageMap,
Chris@16 677 typename Reduce>
Chris@16 678 inline distributed_property_map<ProcessGroup, GlobalMap, StorageMap>
Chris@101 679 make_distributed_property_map(const ProcessGroup& pg, GlobalMap global,
Chris@16 680 StorageMap storage, Reduce reduce)
Chris@16 681 {
Chris@16 682 typedef distributed_property_map<ProcessGroup, GlobalMap, StorageMap>
Chris@16 683 result_type;
Chris@16 684 return result_type(pg, global, storage, reduce);
Chris@16 685 }
Chris@16 686
Chris@16 687 } } // end namespace boost::parallel
Chris@16 688
Chris@16 689 #include <boost/property_map/parallel/impl/distributed_property_map.ipp>
Chris@16 690
Chris@16 691 #undef PBGL_DISTRIB_PMAP
Chris@16 692
Chris@16 693 #endif // BOOST_PARALLEL_DISTRIBUTED_PROPERTY_MAP_HPP