delta_stepping_shortest_paths.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // Copyright (C) 2007 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. /**************************************************************************
  8. * This source file implements the Delta-stepping algorithm: *
  9. * *
  10. * Ulrich Meyer and Peter Sanders. Parallel Shortest Path for Arbitrary *
  11. * Graphs. In Proceedings from the 6th International Euro-Par *
  12. * Conference on Parallel Processing, pages 461--470, 2000. *
  13. * *
  14. * Ulrich Meyer, Peter Sanders: [Delta]-stepping: A Parallelizable *
  15. * Shortest Path Algorithm. J. Algorithms 49(1): 114-152, 2003. *
  16. * *
  17. * There are several potential optimizations we could still perform for *
  18. * this algorithm implementation: *
  19. * *
  20. * - Implement "shortcuts", which bound the number of reinsertions *
  21. * in a single bucket (to one). The computation of shortcuts looks *
  22. * expensive in a distributed-memory setting, but it could be *
  23. * ammortized over many queries. *
  24. * *
  25. * - The size of the "buckets" data structure can be bounded to *
  26. * max_edge_weight/delta buckets, if we treat the buckets as a *
  27. * circular array. *
  28. * *
  29. * - If we partition light/heavy edges ahead of time, we could improve *
  30. * relaxation performance by only iterating over the right portion *
  31. * of the out-edge list each time. *
  32. * *
  33. * - Implement a more sophisticated algorithm for guessing "delta", *
  34. * based on the shortcut-finding algorithm. *
  35. **************************************************************************/
  36. #ifndef BOOST_GRAPH_DELTA_STEPPING_SHORTEST_PATHS_HPP
  37. #define BOOST_GRAPH_DELTA_STEPPING_SHORTEST_PATHS_HPP
  38. #ifndef BOOST_GRAPH_USE_MPI
  39. #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
  40. #endif
  41. #include <boost/config.hpp>
  42. #include <boost/graph/graph_traits.hpp>
  43. #include <boost/property_map/property_map.hpp>
  44. #include <boost/property_map/parallel/parallel_property_maps.hpp>
  45. #include <boost/graph/iteration_macros.hpp>
  46. #include <limits>
  47. #include <list>
  48. #include <vector>
  49. #include <boost/graph/parallel/container_traits.hpp>
  50. #include <boost/graph/parallel/properties.hpp>
  51. #include <boost/graph/distributed/detail/dijkstra_shortest_paths.hpp>
  52. #include <utility> // for std::pair
  53. #include <functional> // for std::logical_or
  54. #include <boost/graph/parallel/algorithm.hpp> // for all_reduce
  55. #include <cassert>
  56. #include <algorithm> // for std::min, std::max
  57. #include <boost/graph/parallel/simple_trigger.hpp>
  58. #ifdef PBGL_DELTA_STEPPING_DEBUG
  59. # include <iostream> // for std::cerr
  60. #endif
  61. namespace boost { namespace graph { namespace distributed {
  62. template<typename Graph, typename PredecessorMap, typename DistanceMap,
  63. typename EdgeWeightMap>
  64. class delta_stepping_impl {
  65. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  66. typedef typename graph_traits<Graph>::degree_size_type Degree;
  67. typedef typename property_traits<EdgeWeightMap>::value_type Dist;
  68. typedef typename boost::graph::parallel::process_group_type<Graph>::type
  69. ProcessGroup;
  70. typedef std::list<Vertex> Bucket;
  71. typedef typename Bucket::iterator BucketIterator;
  72. typedef typename std::vector<Bucket*>::size_type BucketIndex;
  73. typedef detail::dijkstra_msg_value<DistanceMap, PredecessorMap> MessageValue;
  74. enum {
  75. // Relax a remote vertex. The message contains a pair<Vertex,
  76. // MessageValue>, the first part of which is the vertex whose
  77. // tentative distance is being relaxed and the second part
  78. // contains either the new distance (if there is no predecessor
  79. // map) or a pair with the distance and predecessor.
  80. msg_relax
  81. };
  82. public:
  83. delta_stepping_impl(const Graph& g,
  84. PredecessorMap predecessor,
  85. DistanceMap distance,
  86. EdgeWeightMap weight,
  87. Dist delta);
  88. delta_stepping_impl(const Graph& g,
  89. PredecessorMap predecessor,
  90. DistanceMap distance,
  91. EdgeWeightMap weight);
  92. void run(Vertex s);
  93. private:
  94. // Relax the edge (u, v), creating a new best path of distance x.
  95. void relax(Vertex u, Vertex v, Dist x);
  96. // Synchronize all of the processes, by receiving all messages that
  97. // have not yet been received.
  98. void synchronize();
  99. // Handle a relax message that contains only the target and
  100. // distance. This kind of message will be received when the
  101. // predecessor map is a dummy_property_map.
  102. void handle_relax_message(Vertex v, Dist x) { relax(v, v, x); }
  103. // Handle a relax message that contains the source (predecessor),
  104. // target, and distance. This kind of message will be received when
  105. // the predecessor map is not a dummy_property_map.
  106. void handle_relax_message(Vertex v, const std::pair<Dist, Vertex>& p)
  107. { relax(p.second, v, p.first); }
  108. // Setup triggers for msg_relax messages
  109. void setup_triggers();
  110. void handle_msg_relax(int /*source*/, int /*tag*/,
  111. const std::pair<Vertex, typename MessageValue::type>& data,
  112. trigger_receive_context)
  113. { handle_relax_message(data.first, data.second); }
  114. const Graph& g;
  115. PredecessorMap predecessor;
  116. DistanceMap distance;
  117. EdgeWeightMap weight;
  118. Dist delta;
  119. ProcessGroup pg;
  120. typename property_map<Graph, vertex_owner_t>::const_type owner;
  121. typename property_map<Graph, vertex_local_t>::const_type local;
  122. // A "property map" that contains the position of each vertex in
  123. // whatever bucket it resides in.
  124. std::vector<BucketIterator> position_in_bucket;
  125. // Bucket data structure. The ith bucket contains all local vertices
  126. // with (tentative) distance in the range [i*delta,
  127. // (i+1)*delta).
  128. std::vector<Bucket*> buckets;
  129. // This "dummy" list is used only so that we can initialize the
  130. // position_in_bucket property map with non-singular iterators. This
  131. // won't matter for most implementations of the C++ Standard
  132. // Library, but it avoids undefined behavior and allows us to run
  133. // with library "debug modes".
  134. std::list<Vertex> dummy_list;
  135. // A "property map" that states which vertices have been deleted
  136. // from the bucket in this iteration.
  137. std::vector<bool> vertex_was_deleted;
  138. };
  139. template<typename Graph, typename PredecessorMap, typename DistanceMap,
  140. typename EdgeWeightMap>
  141. delta_stepping_impl<Graph, PredecessorMap, DistanceMap, EdgeWeightMap>::
  142. delta_stepping_impl(const Graph& g,
  143. PredecessorMap predecessor,
  144. DistanceMap distance,
  145. EdgeWeightMap weight,
  146. Dist delta)
  147. : g(g),
  148. predecessor(predecessor),
  149. distance(distance),
  150. weight(weight),
  151. delta(delta),
  152. pg(boost::graph::parallel::process_group_adl(g), attach_distributed_object()),
  153. owner(get(vertex_owner, g)),
  154. local(get(vertex_local, g))
  155. {
  156. setup_triggers();
  157. }
  158. template<typename Graph, typename PredecessorMap, typename DistanceMap,
  159. typename EdgeWeightMap>
  160. delta_stepping_impl<Graph, PredecessorMap, DistanceMap, EdgeWeightMap>::
  161. delta_stepping_impl(const Graph& g,
  162. PredecessorMap predecessor,
  163. DistanceMap distance,
  164. EdgeWeightMap weight)
  165. : g(g),
  166. predecessor(predecessor),
  167. distance(distance),
  168. weight(weight),
  169. pg(boost::graph::parallel::process_group_adl(g), attach_distributed_object()),
  170. owner(get(vertex_owner, g)),
  171. local(get(vertex_local, g))
  172. {
  173. using boost::parallel::all_reduce;
  174. using boost::parallel::maximum;
  175. using std::max;
  176. // Compute the maximum edge weight and degree
  177. Dist max_edge_weight = 0;
  178. Degree max_degree = 0;
  179. BGL_FORALL_VERTICES_T(u, g, Graph) {
  180. max_degree = max BOOST_PREVENT_MACRO_SUBSTITUTION (max_degree, out_degree(u, g));
  181. BGL_FORALL_OUTEDGES_T(u, e, g, Graph)
  182. max_edge_weight = max BOOST_PREVENT_MACRO_SUBSTITUTION (max_edge_weight, get(weight, e));
  183. }
  184. max_edge_weight = all_reduce(pg, max_edge_weight, maximum<Dist>());
  185. max_degree = all_reduce(pg, max_degree, maximum<Degree>());
  186. // Take a guess at delta, based on what works well for random
  187. // graphs.
  188. delta = max_edge_weight / max_degree;
  189. if (delta == 0)
  190. delta = 1;
  191. setup_triggers();
  192. }
  193. template<typename Graph, typename PredecessorMap, typename DistanceMap,
  194. typename EdgeWeightMap>
  195. void
  196. delta_stepping_impl<Graph, PredecessorMap, DistanceMap, EdgeWeightMap>::
  197. run(Vertex s)
  198. {
  199. Dist inf = (std::numeric_limits<Dist>::max)();
  200. // None of the vertices are stored in the bucket.
  201. position_in_bucket.clear();
  202. position_in_bucket.resize(num_vertices(g), dummy_list.end());
  203. // None of the vertices have been deleted
  204. vertex_was_deleted.clear();
  205. vertex_was_deleted.resize(num_vertices(g), false);
  206. // No path from s to any other vertex, yet
  207. BGL_FORALL_VERTICES_T(v, g, Graph)
  208. put(distance, v, inf);
  209. // The distance to the starting node is zero
  210. if (get(owner, s) == process_id(pg))
  211. // Put "s" into its bucket (bucket 0)
  212. relax(s, s, 0);
  213. else
  214. // Note that we know the distance to s is zero
  215. cache(distance, s, 0);
  216. BucketIndex max_bucket = (std::numeric_limits<BucketIndex>::max)();
  217. BucketIndex current_bucket = 0;
  218. do {
  219. // Synchronize with all of the other processes.
  220. synchronize();
  221. // Find the next bucket that has something in it.
  222. while (current_bucket < buckets.size()
  223. && (!buckets[current_bucket] || buckets[current_bucket]->empty()))
  224. ++current_bucket;
  225. if (current_bucket >= buckets.size())
  226. current_bucket = max_bucket;
  227. #ifdef PBGL_DELTA_STEPPING_DEBUG
  228. std::cerr << "#" << process_id(pg) << ": lowest bucket is #"
  229. << current_bucket << std::endl;
  230. #endif
  231. // Find the smallest bucket (over all processes) that has vertices
  232. // that need to be processed.
  233. using boost::parallel::all_reduce;
  234. using boost::parallel::minimum;
  235. current_bucket = all_reduce(pg, current_bucket, minimum<BucketIndex>());
  236. if (current_bucket == max_bucket)
  237. // There are no non-empty buckets in any process; exit.
  238. break;
  239. #ifdef PBGL_DELTA_STEPPING_DEBUG
  240. if (process_id(pg) == 0)
  241. std::cerr << "Processing bucket #" << current_bucket << std::endl;
  242. #endif
  243. // Contains the set of vertices that have been deleted in the
  244. // relaxation of "light" edges. Note that we keep track of which
  245. // vertices were deleted with the property map
  246. // "vertex_was_deleted".
  247. std::vector<Vertex> deleted_vertices;
  248. // Repeatedly relax light edges
  249. bool nonempty_bucket;
  250. do {
  251. // Someone has work to do in this bucket.
  252. if (current_bucket < buckets.size() && buckets[current_bucket]) {
  253. Bucket& bucket = *buckets[current_bucket];
  254. // For each element in the bucket
  255. while (!bucket.empty()) {
  256. Vertex u = bucket.front();
  257. #ifdef PBGL_DELTA_STEPPING_DEBUG
  258. std::cerr << "#" << process_id(pg) << ": processing vertex "
  259. << get(vertex_global, g, u).second << "@"
  260. << get(vertex_global, g, u).first
  261. << std::endl;
  262. #endif
  263. // Remove u from the front of the bucket
  264. bucket.pop_front();
  265. // Insert u into the set of deleted vertices, if it hasn't
  266. // been done already.
  267. if (!vertex_was_deleted[get(local, u)]) {
  268. vertex_was_deleted[get(local, u)] = true;
  269. deleted_vertices.push_back(u);
  270. }
  271. // Relax each light edge.
  272. Dist u_dist = get(distance, u);
  273. BGL_FORALL_OUTEDGES_T(u, e, g, Graph)
  274. if (get(weight, e) <= delta) // light edge
  275. relax(u, target(e, g), u_dist + get(weight, e));
  276. }
  277. }
  278. // Synchronize with all of the other processes.
  279. synchronize();
  280. // Is the bucket empty now?
  281. nonempty_bucket = (current_bucket < buckets.size()
  282. && buckets[current_bucket]
  283. && !buckets[current_bucket]->empty());
  284. } while (all_reduce(pg, nonempty_bucket, std::logical_or<bool>()));
  285. // Relax heavy edges for each of the vertices that we previously
  286. // deleted.
  287. for (typename std::vector<Vertex>::iterator iter = deleted_vertices.begin();
  288. iter != deleted_vertices.end(); ++iter) {
  289. // Relax each heavy edge.
  290. Vertex u = *iter;
  291. Dist u_dist = get(distance, u);
  292. BGL_FORALL_OUTEDGES_T(u, e, g, Graph)
  293. if (get(weight, e) > delta) // heavy edge
  294. relax(u, target(e, g), u_dist + get(weight, e));
  295. }
  296. // Go to the next bucket: the current bucket must already be empty.
  297. ++current_bucket;
  298. } while (true);
  299. // Delete all of the buckets.
  300. for (typename std::vector<Bucket*>::iterator iter = buckets.begin();
  301. iter != buckets.end(); ++iter) {
  302. if (*iter) {
  303. delete *iter;
  304. *iter = 0;
  305. }
  306. }
  307. }
  308. template<typename Graph, typename PredecessorMap, typename DistanceMap,
  309. typename EdgeWeightMap>
  310. void
  311. delta_stepping_impl<Graph, PredecessorMap, DistanceMap, EdgeWeightMap>::
  312. relax(Vertex u, Vertex v, Dist x)
  313. {
  314. #ifdef PBGL_DELTA_STEPPING_DEBUG
  315. std::cerr << "#" << process_id(pg) << ": relax("
  316. << get(vertex_global, g, u).second << "@"
  317. << get(vertex_global, g, u).first << ", "
  318. << get(vertex_global, g, v).second << "@"
  319. << get(vertex_global, g, v).first << ", "
  320. << x << ")" << std::endl;
  321. #endif
  322. if (x < get(distance, v)) {
  323. // We're relaxing the edge to vertex v.
  324. if (get(owner, v) == process_id(pg)) {
  325. // Compute the new bucket index for v
  326. BucketIndex new_index = static_cast<BucketIndex>(x / delta);
  327. // Make sure there is enough room in the buckets data structure.
  328. if (new_index >= buckets.size()) buckets.resize(new_index + 1, 0);
  329. // Make sure that we have allocated the bucket itself.
  330. if (!buckets[new_index]) buckets[new_index] = new Bucket;
  331. if (get(distance, v) != (std::numeric_limits<Dist>::max)()
  332. && !vertex_was_deleted[get(local, v)]) {
  333. // We're moving v from an old bucket into a new one. Compute
  334. // the old index, then splice it in.
  335. BucketIndex old_index
  336. = static_cast<BucketIndex>(get(distance, v) / delta);
  337. buckets[new_index]->splice(buckets[new_index]->end(),
  338. *buckets[old_index],
  339. position_in_bucket[get(local, v)]);
  340. } else {
  341. // We're inserting v into a bucket for the first time. Put it
  342. // at the end.
  343. buckets[new_index]->push_back(v);
  344. }
  345. // v is now at the last position in the new bucket
  346. position_in_bucket[get(local, v)] = buckets[new_index]->end();
  347. --position_in_bucket[get(local, v)];
  348. // Update predecessor and tentative distance information
  349. put(predecessor, v, u);
  350. put(distance, v, x);
  351. } else {
  352. #ifdef PBGL_DELTA_STEPPING_DEBUG
  353. std::cerr << "#" << process_id(pg) << ": sending relax("
  354. << get(vertex_global, g, u).second << "@"
  355. << get(vertex_global, g, u).first << ", "
  356. << get(vertex_global, g, v).second << "@"
  357. << get(vertex_global, g, v).first << ", "
  358. << x << ") to " << get(owner, v) << std::endl;
  359. #endif
  360. // The vertex is remote: send a request to the vertex's owner
  361. send(pg, get(owner, v), msg_relax,
  362. std::make_pair(v, MessageValue::create(x, u)));
  363. // Cache tentative distance information
  364. cache(distance, v, x);
  365. }
  366. }
  367. }
  368. template<typename Graph, typename PredecessorMap, typename DistanceMap,
  369. typename EdgeWeightMap>
  370. void
  371. delta_stepping_impl<Graph, PredecessorMap, DistanceMap, EdgeWeightMap>::
  372. synchronize()
  373. {
  374. using boost::parallel::synchronize;
  375. // Synchronize at the process group level.
  376. synchronize(pg);
  377. // Receive any relaxation request messages.
  378. // typedef typename ProcessGroup::process_id_type process_id_type;
  379. // while (optional<std::pair<process_id_type, int> > stp = probe(pg)) {
  380. // // Receive the relaxation message
  381. // assert(stp->second == msg_relax);
  382. // std::pair<Vertex, typename MessageValue::type> data;
  383. // receive(pg, stp->first, stp->second, data);
  384. // // Turn it into a "relax" call
  385. // handle_relax_message(data.first, data.second);
  386. // }
  387. }
  388. template<typename Graph, typename PredecessorMap, typename DistanceMap,
  389. typename EdgeWeightMap>
  390. void
  391. delta_stepping_impl<Graph, PredecessorMap, DistanceMap, EdgeWeightMap>::
  392. setup_triggers()
  393. {
  394. using boost::graph::parallel::simple_trigger;
  395. simple_trigger(pg, msg_relax, this,
  396. &delta_stepping_impl::handle_msg_relax);
  397. }
  398. template<typename Graph, typename PredecessorMap, typename DistanceMap,
  399. typename EdgeWeightMap>
  400. void
  401. delta_stepping_shortest_paths
  402. (const Graph& g,
  403. typename graph_traits<Graph>::vertex_descriptor s,
  404. PredecessorMap predecessor, DistanceMap distance, EdgeWeightMap weight,
  405. typename property_traits<EdgeWeightMap>::value_type delta)
  406. {
  407. // The "distance" map needs to act like one, retrieving the default
  408. // value of infinity.
  409. set_property_map_role(vertex_distance, distance);
  410. // Construct the implementation object, which will perform all of
  411. // the actual work.
  412. delta_stepping_impl<Graph, PredecessorMap, DistanceMap, EdgeWeightMap>
  413. impl(g, predecessor, distance, weight, delta);
  414. // Run the delta-stepping algorithm. The results will show up in
  415. // "predecessor" and "weight".
  416. impl.run(s);
  417. }
  418. template<typename Graph, typename PredecessorMap, typename DistanceMap,
  419. typename EdgeWeightMap>
  420. void
  421. delta_stepping_shortest_paths
  422. (const Graph& g,
  423. typename graph_traits<Graph>::vertex_descriptor s,
  424. PredecessorMap predecessor, DistanceMap distance, EdgeWeightMap weight)
  425. {
  426. // The "distance" map needs to act like one, retrieving the default
  427. // value of infinity.
  428. set_property_map_role(vertex_distance, distance);
  429. // Construct the implementation object, which will perform all of
  430. // the actual work.
  431. delta_stepping_impl<Graph, PredecessorMap, DistanceMap, EdgeWeightMap>
  432. impl(g, predecessor, distance, weight);
  433. // Run the delta-stepping algorithm. The results will show up in
  434. // "predecessor" and "weight".
  435. impl.run(s);
  436. }
  437. } } } // end namespace boost::graph::distributed
  438. #endif // BOOST_GRAPH_DELTA_STEPPING_SHORTEST_PATHS_HPP