connected_components_parallel_search.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // Copyright (C) 2004-2006 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: Brian Barrett
  6. // Douglas Gregor
  7. // Andrew Lumsdaine
  8. #ifndef BOOST_GRAPH_PARALLEL_CC_PS_HPP
  9. #define BOOST_GRAPH_PARALLEL_CC_PS_HPP
  10. #ifndef BOOST_GRAPH_USE_MPI
  11. #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
  12. #endif
  13. #include <boost/assert.hpp>
  14. #include <boost/property_map/property_map.hpp>
  15. #include <boost/property_map/parallel/parallel_property_maps.hpp>
  16. #include <boost/graph/parallel/algorithm.hpp>
  17. #include <boost/pending/indirect_cmp.hpp>
  18. #include <boost/graph/graph_traits.hpp>
  19. #include <boost/graph/overloading.hpp>
  20. #include <boost/graph/distributed/concepts.hpp>
  21. #include <boost/graph/parallel/properties.hpp>
  22. #include <boost/graph/parallel/process_group.hpp>
  23. #include <boost/optional.hpp>
  24. #include <algorithm>
  25. #include <vector>
  26. #include <queue>
  27. #include <limits>
  28. #include <map>
  29. #include <boost/graph/parallel/container_traits.hpp>
  30. #include <boost/graph/iteration_macros.hpp>
  31. // Connected components algorithm based on a parallel search.
  32. //
  33. // Every N nodes starts a parallel search from the first vertex in
  34. // their local vertex list during the first superstep (the other nodes
  35. // remain idle during the first superstep to reduce the number of
  36. // conflicts in numbering the components). At each superstep, all new
  37. // component mappings from remote nodes are handled. If there is no
  38. // work from remote updates, a new vertex is removed from the local
  39. // list and added to the work queue.
  40. //
  41. // Components are allocated from the component_value_allocator object,
  42. // which ensures that a given component number is unique in the
  43. // system, currently by using the rank and number of processes to
  44. // stride allocations.
  45. //
  46. // When two components are discovered to actually be the same
  47. // component, a mapping is created in the collisions object. The
  48. // lower component number is prefered in the resolution, so component
  49. // numbering resolution is consistent. After the search has exhausted
  50. // all vertices in the graph, the mapping is shared with all
  51. // processes, and they independently resolve the comonent mapping (so
  52. // O((N * NP) + (V * NP)) work, in O(N + V) time, where N is the
  53. // number of mappings and V is the number of local vertices). This
  54. // phase can likely be significantly sped up if a clever algorithm for
  55. // the reduction can be found.
  56. namespace boost { namespace graph { namespace distributed {
  57. namespace cc_ps_detail {
  58. // Local object for allocating component numbers. There are two
  59. // places this happens in the code, and I was getting sick of them
  60. // getting out of sync. Components are not tightly packed in
  61. // numbering, but are numbered to ensure each rank has its own
  62. // independent sets of numberings.
  63. template<typename component_value_type>
  64. class component_value_allocator {
  65. public:
  66. component_value_allocator(int num, int size) :
  67. last(0), num(num), size(size)
  68. {
  69. }
  70. component_value_type allocate(void)
  71. {
  72. component_value_type ret = num + (last * size);
  73. last++;
  74. return ret;
  75. }
  76. private:
  77. component_value_type last;
  78. int num;
  79. int size;
  80. };
  81. // Map of the "collisions" between component names in the global
  82. // component mapping. TO make cleanup easier, component numbers
  83. // are added, pointing to themselves, when a new component is
  84. // found. In order to make the results deterministic, the lower
  85. // component number is always taken. The resolver will drill
  86. // through the map until it finds a component entry that points to
  87. // itself as the next value, allowing some cleanup to happen at
  88. // update() time. Attempts are also made to update the mapping
  89. // when new entries are created.
  90. //
  91. // Note that there's an assumption that the entire mapping is
  92. // shared during the end of the algorithm, but before component
  93. // name resolution.
  94. template<typename component_value_type>
  95. class collision_map {
  96. public:
  97. collision_map() : num_unique(0)
  98. {
  99. }
  100. // add new component mapping first time component is used. Own
  101. // function only so that we can sanity check there isn't already
  102. // a mapping for that component number (which would be bad)
  103. void add(const component_value_type &a)
  104. {
  105. BOOST_ASSERT(collisions.count(a) == 0);
  106. collisions[a] = a;
  107. }
  108. // add a mapping between component values saying they're the
  109. // same component
  110. void add(const component_value_type &a, const component_value_type &b)
  111. {
  112. component_value_type high, low, tmp;
  113. if (a > b) {
  114. high = a;
  115. low = b;
  116. } else {
  117. high = b;
  118. low = a;
  119. }
  120. if (collisions.count(high) != 0 && collisions[high] != low) {
  121. tmp = collisions[high];
  122. if (tmp > low) {
  123. collisions[tmp] = low;
  124. collisions[high] = low;
  125. } else {
  126. collisions[low] = tmp;
  127. collisions[high] = tmp;
  128. }
  129. } else {
  130. collisions[high] = low;
  131. }
  132. }
  133. // get the "real" component number for the given component.
  134. // Used to resolve mapping at end of run.
  135. component_value_type update(component_value_type a)
  136. {
  137. BOOST_ASSERT(num_unique > 0);
  138. BOOST_ASSERT(collisions.count(a) != 0);
  139. return collisions[a];
  140. }
  141. // collapse the collisions tree, so that update is a one lookup
  142. // operation. Count unique components at the same time.
  143. void uniqify(void)
  144. {
  145. typename std::map<component_value_type, component_value_type>::iterator i, end;
  146. end = collisions.end();
  147. for (i = collisions.begin() ; i != end ; ++i) {
  148. if (i->first == i->second) {
  149. num_unique++;
  150. } else {
  151. i->second = collisions[i->second];
  152. }
  153. }
  154. }
  155. // get the number of component entries that have an associated
  156. // component number of themselves, which are the real components
  157. // used in the final mapping. This is the number of unique
  158. // components in the graph.
  159. int unique(void)
  160. {
  161. BOOST_ASSERT(num_unique > 0);
  162. return num_unique;
  163. }
  164. // "serialize" into a vector for communication.
  165. std::vector<component_value_type> serialize(void)
  166. {
  167. std::vector<component_value_type> ret;
  168. typename std::map<component_value_type, component_value_type>::iterator i, end;
  169. end = collisions.end();
  170. for (i = collisions.begin() ; i != end ; ++i) {
  171. ret.push_back(i->first);
  172. ret.push_back(i->second);
  173. }
  174. return ret;
  175. }
  176. private:
  177. std::map<component_value_type, component_value_type> collisions;
  178. int num_unique;
  179. };
  180. // resolver to handle remote updates. The resolver will add
  181. // entries into the collisions map if required, and if it is the
  182. // first time the vertex has been touched, it will add the vertex
  183. // to the remote queue. Note that local updates are handled
  184. // differently, in the main loop (below).
  185. // BWB - FIX ME - don't need graph anymore - can pull from key value of Component Map.
  186. template<typename ComponentMap, typename work_queue>
  187. struct update_reducer {
  188. BOOST_STATIC_CONSTANT(bool, non_default_resolver = false);
  189. typedef typename property_traits<ComponentMap>::value_type component_value_type;
  190. typedef typename property_traits<ComponentMap>::key_type vertex_descriptor;
  191. update_reducer(work_queue *q,
  192. cc_ps_detail::collision_map<component_value_type> *collisions,
  193. processor_id_type pg_id) :
  194. q(q), collisions(collisions), pg_id(pg_id)
  195. {
  196. }
  197. // ghost cell initialization routine. This should never be
  198. // called in this imlementation.
  199. template<typename K>
  200. component_value_type operator()(const K&) const
  201. {
  202. return component_value_type(0);
  203. }
  204. // resolver for remote updates. I'm not entirely sure why, but
  205. // I decided to not change the value of the vertex if it's
  206. // already non-infinite. It doesn't matter in the end, as we'll
  207. // touch every vertex in the cleanup phase anyway. If the
  208. // component is currently infinite, set to the new component
  209. // number and add the vertex to the work queue. If it's not
  210. // infinite, we've touched it already so don't add it to the
  211. // work queue. Do add a collision entry so that we know the two
  212. // components are the same.
  213. component_value_type operator()(const vertex_descriptor &v,
  214. const component_value_type& current,
  215. const component_value_type& update) const
  216. {
  217. const component_value_type max = (std::numeric_limits<component_value_type>::max)();
  218. component_value_type ret = current;
  219. if (max == current) {
  220. q->push(v);
  221. ret = update;
  222. } else if (current != update) {
  223. collisions->add(current, update);
  224. }
  225. return ret;
  226. }
  227. // So for whatever reason, the property map can in theory call
  228. // the resolver with a local descriptor in addition to the
  229. // standard global descriptor. As far as I can tell, this code
  230. // path is never taken in this implementation, but I need to
  231. // have this code here to make it compile. We just make a
  232. // global descriptor and call the "real" operator().
  233. template<typename K>
  234. component_value_type operator()(const K& v,
  235. const component_value_type& current,
  236. const component_value_type& update) const
  237. {
  238. return (*this)(vertex_descriptor(pg_id, v), current, update);
  239. }
  240. private:
  241. work_queue *q;
  242. collision_map<component_value_type> *collisions;
  243. boost::processor_id_type pg_id;
  244. };
  245. } // namespace cc_ps_detail
  246. template<typename Graph, typename ComponentMap>
  247. typename property_traits<ComponentMap>::value_type
  248. connected_components_ps(const Graph& g, ComponentMap c)
  249. {
  250. using boost::graph::parallel::process_group;
  251. typedef typename property_traits<ComponentMap>::value_type component_value_type;
  252. typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
  253. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  254. typedef typename boost::graph::parallel::process_group_type<Graph>
  255. ::type process_group_type;
  256. typedef typename process_group_type::process_id_type process_id_type;
  257. typedef std::queue<vertex_descriptor> work_queue;
  258. static const component_value_type max_component =
  259. (std::numeric_limits<component_value_type>::max)();
  260. typename property_map<Graph, vertex_owner_t>::const_type
  261. owner = get(vertex_owner, g);
  262. // standard who am i? stuff
  263. process_group_type pg = process_group(g);
  264. process_id_type id = process_id(pg);
  265. // Initialize every vertex to have infinite component number
  266. BGL_FORALL_VERTICES_T(v, g, Graph) put(c, v, max_component);
  267. vertex_iterator current, end;
  268. boost::tie(current, end) = vertices(g);
  269. cc_ps_detail::component_value_allocator<component_value_type> cva(process_id(pg), num_processes(pg));
  270. cc_ps_detail::collision_map<component_value_type> collisions;
  271. work_queue q; // this is intentionally a local data structure
  272. c.set_reduce(cc_ps_detail::update_reducer<ComponentMap, work_queue>(&q, &collisions, id));
  273. // add starting work
  274. while (true) {
  275. bool useful_found = false;
  276. component_value_type val = cva.allocate();
  277. put(c, *current, val);
  278. collisions.add(val);
  279. q.push(*current);
  280. if (0 != out_degree(*current, g)) useful_found = true;
  281. ++current;
  282. if (useful_found) break;
  283. }
  284. // Run the loop until everyone in the system is done
  285. bool global_done = false;
  286. while (!global_done) {
  287. // drain queue of work for this superstep
  288. while (!q.empty()) {
  289. vertex_descriptor v = q.front();
  290. q.pop();
  291. // iterate through outedges of the vertex currently being
  292. // examined, setting their component to our component. There
  293. // is no way to end up in the queue without having a component
  294. // number already.
  295. BGL_FORALL_ADJ_T(v, peer, g, Graph) {
  296. component_value_type my_component = get(c, v);
  297. // update other vertex with our component information.
  298. // Resolver will handle remote collisions as well as whether
  299. // to put the vertex on the work queue or not. We have to
  300. // handle local collisions and work queue management
  301. if (id == get(owner, peer)) {
  302. if (max_component == get(c, peer)) {
  303. put(c, peer, my_component);
  304. q.push(peer);
  305. } else if (my_component != get(c, peer)) {
  306. collisions.add(my_component, get(c, peer));
  307. }
  308. } else {
  309. put(c, peer, my_component);
  310. }
  311. }
  312. }
  313. // synchronize / start a new superstep.
  314. synchronize(pg);
  315. global_done = all_reduce(pg, (q.empty() && (current == end)), boost::parallel::minimum<bool>());
  316. // If the queue is currently empty, add something to do to start
  317. // the current superstep (supersteps start at the sync, not at
  318. // the top of the while loop as one might expect). Down at the
  319. // bottom of the while loop so that not everyone starts the
  320. // algorithm with something to do, to try to reduce component
  321. // name conflicts
  322. if (q.empty()) {
  323. bool useful_found = false;
  324. for ( ; current != end && !useful_found ; ++current) {
  325. if (max_component == get(c, *current)) {
  326. component_value_type val = cva.allocate();
  327. put(c, *current, val);
  328. collisions.add(val);
  329. q.push(*current);
  330. if (0 != out_degree(*current, g)) useful_found = true;
  331. }
  332. }
  333. }
  334. }
  335. // share component mappings
  336. std::vector<component_value_type> global;
  337. std::vector<component_value_type> mine = collisions.serialize();
  338. all_gather(pg, mine.begin(), mine.end(), global);
  339. for (size_t i = 0 ; i < global.size() ; i += 2) {
  340. collisions.add(global[i], global[i + 1]);
  341. }
  342. collisions.uniqify();
  343. // update the component mappings
  344. BGL_FORALL_VERTICES_T(v, g, Graph) {
  345. put(c, v, collisions.update(get(c, v)));
  346. }
  347. return collisions.unique();
  348. }
  349. } // end namespace distributed
  350. } // end namespace graph
  351. } // end namespace boost
  352. #endif // BOOST_GRAPH_PARALLEL_CC_HPP