]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/graph/example/parallel-compile-time.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / graph / example / parallel-compile-time.cpp
CommitLineData
7c673cae
FG
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
18namespace std
19{
20 template < typename T >
21 std::istream & operator >> (std::istream & in, std::pair < T, T > &p)
22 {
23 in >> p.first >> p.second;
24 return in;
25 }
26}
27
28namespace boost
29{
30 enum vertex_compile_cost_t { vertex_compile_cost };
31 BOOST_INSTALL_PROPERTY(vertex, compile_cost);
32}
33
34using namespace boost;
35
36typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list
37 listS, // Store vertex set in a std::list
38 directedS, // The file dependency graph is directed
39 // vertex properties
40 property < vertex_name_t, std::string,
41 property < vertex_compile_cost_t, float,
42 property < vertex_distance_t, float,
43 property < vertex_color_t, default_color_type > > > >,
44 // an edge property
45 property < edge_weight_t, float > >
46 file_dep_graph2;
47
48typedef graph_traits<file_dep_graph2>::vertex_descriptor vertex_t;
49typedef graph_traits<file_dep_graph2>::edge_descriptor edge_t;
50
51
52template < typename Graph, typename ColorMap, typename Visitor > void
53dfs_v2(const Graph & g,
54 typename graph_traits < Graph >::vertex_descriptor u,
55 ColorMap color, Visitor vis)
56{
57 typedef typename property_traits < ColorMap >::value_type color_type;
58 typedef color_traits < color_type > ColorT;
59 color[u] = ColorT::gray();
60 vis.discover_vertex(u, g);
61 typename graph_traits < Graph >::out_edge_iterator ei, ei_end;
62 for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei)
63 if (color[target(*ei, g)] == ColorT::white()) {
64 vis.tree_edge(*ei, g);
65 dfs_v2(g, target(*ei, g), color, vis);
66 } else if (color[target(*ei, g)] == ColorT::gray())
67 vis.back_edge(*ei, g);
68 else
69 vis.forward_or_cross_edge(*ei, g);
70 color[u] = ColorT::black();
71 vis.finish_vertex(u, g);
72}
73
74template < typename Graph, typename Visitor, typename ColorMap > void
75generic_dfs_v2(const Graph & g, Visitor vis, ColorMap color)
76{
77 typedef typename property_traits <ColorMap >::value_type ColorValue;
78 typedef color_traits < ColorValue > ColorT;
79 typename graph_traits < Graph >::vertex_iterator vi, vi_end;
80 for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
81 color[*vi] = ColorT::white();
82 for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
83 if (color[*vi] == ColorT::white())
84 dfs_v2(g, *vi, color, vis);
85}
86
87
88template < typename OutputIterator >
89struct topo_visitor: public default_dfs_visitor
90{
91 topo_visitor(OutputIterator & order):
92 topo_order(order)
93 {
94 }
95 template < typename Graph > void
96 finish_vertex(typename graph_traits < Graph >::vertex_descriptor u,
97 const Graph &)
98 {
99 *topo_order++ = u;
100 }
101 OutputIterator & topo_order;
102};
103
104template < typename Graph, typename OutputIterator, typename ColorMap > void
105topo_sort(const Graph & g, OutputIterator topo_order, ColorMap color)
106{
107 topo_visitor < OutputIterator > vis(topo_order);
108 generic_dfs_v2(g, vis, color);
109}
110
111
112typedef property_map < file_dep_graph2, vertex_name_t >::type name_map_t;
113typedef property_map < file_dep_graph2, vertex_compile_cost_t >::type
114 compile_cost_map_t;
115typedef property_map < file_dep_graph2, vertex_distance_t >::type distance_map_t;
116typedef property_map < file_dep_graph2, vertex_color_t >::type color_map_t;
117
118
119int
92f5a8d4 120main(int argc, const char** argv)
7c673cae 121{
92f5a8d4 122 std::ifstream file_in(argc >= 2 ? argv[1] : "makefile-dependencies.dat");
7c673cae
FG
123 typedef graph_traits < file_dep_graph2 >::vertices_size_type size_type;
124 size_type n_vertices;
125 file_in >> n_vertices; // read in number of vertices
126 std::istream_iterator < std::pair < size_type,
127 size_type > >input_begin(file_in), input_end;
128#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
129 // VC++ can't handle the iterator constructor
130 file_dep_graph2 g;
131 typedef graph_traits<file_dep_graph2 >::vertex_descriptor vertex_t;
132 std::vector<vertex_t> id2vertex;
133 for (std::size_t v = 0; v < n_vertices; ++v)
134 id2vertex.push_back(add_vertex(g));
135 while (input_begin != input_end) {
136 size_type i, j;
137 boost::tie(i, j) = *input_begin++;
138 add_edge(id2vertex[i], id2vertex[j], g);
139 }
140#else
141 file_dep_graph2 g(input_begin, input_end, n_vertices);
142#endif
143
144 name_map_t
145 name_map =
146 get(vertex_name, g);
147 compile_cost_map_t
148 compile_cost_map =
149 get(vertex_compile_cost, g);
150 distance_map_t
151 distance_map =
152 get(vertex_distance, g);
153 color_map_t
154 color_map =
155 get(vertex_color, g);
156
157 {
92f5a8d4
TL
158 std::ifstream name_in(argc >= 3 ? argv[2] : "makefile-target-names.dat");
159 std::ifstream compile_cost_in(argc >= 4 ? argv[3] : "target-compile-costs.dat");
7c673cae
FG
160 graph_traits < file_dep_graph2 >::vertex_iterator vi, vi_end;
161 for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
162 name_in >> name_map[*vi];
163 compile_cost_in >> compile_cost_map[*vi];
164 }
165
166 }
167 std::vector < vertex_t > topo_order(num_vertices(g));
168 topo_sort(g, topo_order.rbegin(), color_map);
169
170 graph_traits < file_dep_graph2 >::vertex_iterator i, i_end;
171 graph_traits < file_dep_graph2 >::adjacency_iterator vi, vi_end;
172
173 // find source vertices with zero in-degree by marking all vertices with incoming edges
174 for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
175 color_map[*i] = white_color;
176 for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
177 for (boost::tie(vi, vi_end) = adjacent_vertices(*i, g); vi != vi_end; ++vi)
178 color_map[*vi] = black_color;
179
180 // initialize distances to zero, or for source vertices, to the compile cost
181 for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
182 if (color_map[*i] == white_color)
183 distance_map[*i] = compile_cost_map[*i];
184 else
185 distance_map[*i] = 0;
186
187 std::vector < vertex_t >::iterator ui;
188 for (ui = topo_order.begin(); ui != topo_order.end(); ++ui) {
189 vertex_t
190 u = *
191 ui;
192 for (boost::tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi)
193 if (distance_map[*vi] < distance_map[u] + compile_cost_map[*vi])
194 distance_map[*vi] = distance_map[u] + compile_cost_map[*vi];
195 }
196
197 graph_property_iter_range < file_dep_graph2,
198 vertex_distance_t >::iterator ci, ci_end;
199 boost::tie(ci, ci_end) = get_property_iter_range(g, vertex_distance);
200 std::cout << "total (parallel) compile time: "
201 << *std::max_element(ci, ci_end) << std::endl;
202
203 return 0;
204}