delta_stepping_shortest_paths.hpp 19 KB

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