parallel-compile-time.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //=======================================================================
  2. // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //=======================================================================
  8. #include <boost/config.hpp>
  9. #include <fstream>
  10. #include <iostream>
  11. #include <numeric>
  12. #include <string>
  13. #include <boost/graph/adjacency_list.hpp>
  14. #include <boost/graph/graph_utility.hpp>
  15. #include <boost/graph/property_iter_range.hpp>
  16. #include <boost/graph/depth_first_search.hpp> // for default_dfs_visitor
  17. namespace std
  18. {
  19. template < typename T >
  20. std::istream & operator >> (std::istream & in, std::pair < T, T > &p)
  21. {
  22. in >> p.first >> p.second;
  23. return in;
  24. }
  25. }
  26. namespace boost
  27. {
  28. enum vertex_compile_cost_t { vertex_compile_cost };
  29. BOOST_INSTALL_PROPERTY(vertex, compile_cost);
  30. }
  31. using namespace boost;
  32. typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list
  33. listS, // Store vertex set in a std::list
  34. directedS, // The file dependency graph is directed
  35. // vertex properties
  36. property < vertex_name_t, std::string,
  37. property < vertex_compile_cost_t, float,
  38. property < vertex_distance_t, float,
  39. property < vertex_color_t, default_color_type > > > >,
  40. // an edge property
  41. property < edge_weight_t, float > >
  42. file_dep_graph2;
  43. typedef graph_traits<file_dep_graph2>::vertex_descriptor vertex_t;
  44. typedef graph_traits<file_dep_graph2>::edge_descriptor edge_t;
  45. template < typename Graph, typename ColorMap, typename Visitor > void
  46. dfs_v2(const Graph & g,
  47. typename graph_traits < Graph >::vertex_descriptor u,
  48. ColorMap color, Visitor vis)
  49. {
  50. typedef typename property_traits < ColorMap >::value_type color_type;
  51. typedef color_traits < color_type > ColorT;
  52. color[u] = ColorT::gray();
  53. vis.discover_vertex(u, g);
  54. typename graph_traits < Graph >::out_edge_iterator ei, ei_end;
  55. for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei)
  56. if (color[target(*ei, g)] == ColorT::white()) {
  57. vis.tree_edge(*ei, g);
  58. dfs_v2(g, target(*ei, g), color, vis);
  59. } else if (color[target(*ei, g)] == ColorT::gray())
  60. vis.back_edge(*ei, g);
  61. else
  62. vis.forward_or_cross_edge(*ei, g);
  63. color[u] = ColorT::black();
  64. vis.finish_vertex(u, g);
  65. }
  66. template < typename Graph, typename Visitor, typename ColorMap > void
  67. generic_dfs_v2(const Graph & g, Visitor vis, ColorMap color)
  68. {
  69. typedef typename property_traits <ColorMap >::value_type ColorValue;
  70. typedef color_traits < ColorValue > ColorT;
  71. typename graph_traits < Graph >::vertex_iterator vi, vi_end;
  72. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  73. color[*vi] = ColorT::white();
  74. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  75. if (color[*vi] == ColorT::white())
  76. dfs_v2(g, *vi, color, vis);
  77. }
  78. template < typename OutputIterator >
  79. struct topo_visitor: public default_dfs_visitor
  80. {
  81. topo_visitor(OutputIterator & order):
  82. topo_order(order)
  83. {
  84. }
  85. template < typename Graph > void
  86. finish_vertex(typename graph_traits < Graph >::vertex_descriptor u,
  87. const Graph &)
  88. {
  89. *topo_order++ = u;
  90. }
  91. OutputIterator & topo_order;
  92. };
  93. template < typename Graph, typename OutputIterator, typename ColorMap > void
  94. topo_sort(const Graph & g, OutputIterator topo_order, ColorMap color)
  95. {
  96. topo_visitor < OutputIterator > vis(topo_order);
  97. generic_dfs_v2(g, vis, color);
  98. }
  99. typedef property_map < file_dep_graph2, vertex_name_t >::type name_map_t;
  100. typedef property_map < file_dep_graph2, vertex_compile_cost_t >::type
  101. compile_cost_map_t;
  102. typedef property_map < file_dep_graph2, vertex_distance_t >::type distance_map_t;
  103. typedef property_map < file_dep_graph2, vertex_color_t >::type color_map_t;
  104. int
  105. main(int argc, const char** argv)
  106. {
  107. std::ifstream file_in(argc >= 2 ? argv[1] : "makefile-dependencies.dat");
  108. typedef graph_traits < file_dep_graph2 >::vertices_size_type size_type;
  109. size_type n_vertices;
  110. file_in >> n_vertices; // read in number of vertices
  111. std::istream_iterator < std::pair < size_type,
  112. size_type > >input_begin(file_in), input_end;
  113. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  114. // VC++ can't handle the iterator constructor
  115. file_dep_graph2 g;
  116. typedef graph_traits<file_dep_graph2 >::vertex_descriptor vertex_t;
  117. std::vector<vertex_t> id2vertex;
  118. for (std::size_t v = 0; v < n_vertices; ++v)
  119. id2vertex.push_back(add_vertex(g));
  120. while (input_begin != input_end) {
  121. size_type i, j;
  122. boost::tie(i, j) = *input_begin++;
  123. add_edge(id2vertex[i], id2vertex[j], g);
  124. }
  125. #else
  126. file_dep_graph2 g(input_begin, input_end, n_vertices);
  127. #endif
  128. name_map_t
  129. name_map =
  130. get(vertex_name, g);
  131. compile_cost_map_t
  132. compile_cost_map =
  133. get(vertex_compile_cost, g);
  134. distance_map_t
  135. distance_map =
  136. get(vertex_distance, g);
  137. color_map_t
  138. color_map =
  139. get(vertex_color, g);
  140. {
  141. std::ifstream name_in(argc >= 3 ? argv[2] : "makefile-target-names.dat");
  142. std::ifstream compile_cost_in(argc >= 4 ? argv[3] : "target-compile-costs.dat");
  143. graph_traits < file_dep_graph2 >::vertex_iterator vi, vi_end;
  144. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
  145. name_in >> name_map[*vi];
  146. compile_cost_in >> compile_cost_map[*vi];
  147. }
  148. }
  149. std::vector < vertex_t > topo_order(num_vertices(g));
  150. topo_sort(g, topo_order.rbegin(), color_map);
  151. graph_traits < file_dep_graph2 >::vertex_iterator i, i_end;
  152. graph_traits < file_dep_graph2 >::adjacency_iterator vi, vi_end;
  153. // find source vertices with zero in-degree by marking all vertices with incoming edges
  154. for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
  155. color_map[*i] = white_color;
  156. for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
  157. for (boost::tie(vi, vi_end) = adjacent_vertices(*i, g); vi != vi_end; ++vi)
  158. color_map[*vi] = black_color;
  159. // initialize distances to zero, or for source vertices, to the compile cost
  160. for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
  161. if (color_map[*i] == white_color)
  162. distance_map[*i] = compile_cost_map[*i];
  163. else
  164. distance_map[*i] = 0;
  165. std::vector < vertex_t >::iterator ui;
  166. for (ui = topo_order.begin(); ui != topo_order.end(); ++ui) {
  167. vertex_t
  168. u = *
  169. ui;
  170. for (boost::tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi)
  171. if (distance_map[*vi] < distance_map[u] + compile_cost_map[*vi])
  172. distance_map[*vi] = distance_map[u] + compile_cost_map[*vi];
  173. }
  174. graph_property_iter_range < file_dep_graph2,
  175. vertex_distance_t >::iterator ci, ci_end;
  176. boost::tie(ci, ci_end) = get_property_iter_range(g, vertex_distance);
  177. std::cout << "total (parallel) compile time: "
  178. << *std::max_element(ci, ci_end) << std::endl;
  179. return 0;
  180. }