CMS 3D CMS Logo

ProcessCallGraph.cc
Go to the documentation of this file.
1 /*
2  *
3  */
4 
5 #include <cassert>
6 #include <iostream>
7 #include <string>
8 #include <type_traits>
9 #include <vector>
10 
11 // boost optional (used by boost graph) results in some false positives with -Wmaybe-uninitialized
12 #pragma GCC diagnostic push
13 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
14 #include <boost/graph/depth_first_search.hpp>
15 #pragma GCC diagnostic pop
16 
31 
32 
34 
35 
36 // adaptor to use range-based for loops with boost::graph edges(...) and vertices(...) functions
37 template <typename I>
38 struct iterator_pair_as_a_range : std::pair<I, I>
39 {
40 public:
41  using std::pair<I, I>::pair;
42 
43  I begin() { return this->first; }
44  I end() { return this->second; }
45 };
46 
47 template <typename I>
49 {
51 }
52 
53 
54 // FIXME
55 // - check that the Source has not already been added
56 void
58 {
59  // keep track of the Source module id
60  source_ = module.id();
61 
62  // create graph vertex for the source module
63  boost::add_vertex(graph_);
64  graph_.m_graph[module.id()] = { module, edm::EDMModuleType::kSource, true };
65 }
66 
67 
68 // FIXME
69 // - check that the Source has already been added
70 // - check that all module ids are valid (e.g. subprocesses are not being added in
71 // the wrong order)
72 void
74 {
75  unsigned int pid = registerProcess(context);
76 
77  // work on the full graph (for the main process) or a subgraph (for a subprocess)
78  GraphType & graph = context.isSubProcess() ? graph_.create_subgraph() : graph_.root();
79 
80  // set the graph name property to the process name
81  boost::get_property(graph, boost::graph_name) = context.processName();
82 
83  // create graph vertices associated to all modules in the process
84  auto size = pathsAndConsumes.allModules().size();
85  for (size_t i = 0; i < size; ++i)
86  boost::add_vertex(graph);
87 
88  // set the vertices properties (use the module id as the global index into the graph)
89  std::vector<unsigned int> modules;
90  modules.reserve(size);
91  for (edm::ModuleDescription const * module: pathsAndConsumes.allModules()) {
92  modules.push_back(module->id());
93  graph_.m_graph[module->id()] = { *module, edmModuleTypeEnum(*module), false };
94  }
95 
96  // add graph edges associated to module dependencies
97  for (edm::ModuleDescription const * consumer: pathsAndConsumes.allModules()) {
98  for (edm::ModuleDescription const * module: pathsAndConsumes.modulesWhoseProductsAreConsumedBy(consumer->id())) {
99  // module `consumer' depends on module `module'
100  boost::add_edge(consumer->id(), module->id(), graph_);
101  }
102  }
103 
104  // extract path names from the TriggerNamesService
106 
107  // extract the details of the paths and endpaths: name, modules on the path, and their dependencies
108  size = pathsAndConsumes.paths().size();
109  assert (tns.getTrigPaths().size() == size);
110  std::vector<PathType> paths;
111  paths.reserve(size);
112  for (unsigned int i = 0; i < size; ++i) {
113  std::vector<unsigned int> modules;
114  for (edm::ModuleDescription const * module: pathsAndConsumes.modulesOnPath(i)) {
115  modules.push_back(module->id());
116  // mark the modules in the Paths as scheduled
117  graph_.m_graph[module->id()].scheduled_ = true;
118  }
119  auto deps = dependencies(modules);
120  paths.emplace_back(tns.getTrigPath(i), modules, deps.first, deps.second);
121  }
122  size = pathsAndConsumes.endPaths().size();
123  std::vector<PathType> endPaths;
124  endPaths.reserve(size);
125  for (unsigned int i = 0; i < size; ++i) {
126  std::vector<unsigned int> modules;
127  for (edm::ModuleDescription const * module: pathsAndConsumes.modulesOnEndPath(i)) {
128  modules.push_back(module->id());
129  // mark the modules in the EndPaths as scheduled
130  graph_.m_graph[module->id()].scheduled_ = true;
131  }
132  auto deps = dependencies(modules);
133  endPaths.emplace_back(tns.getEndPath(i), modules, deps.first, deps.second);
134  }
135 
136  // store the description of process, modules and paths
137  process_description_.emplace_back(
138  context.processName(),
139  graph,
140  modules,
141  paths,
142  endPaths);
143  assert(process_description_.size() == pid+1);
144 
145  // attach a subprocess to its parent
146  if (context.isSubProcess()) {
147  unsigned int parent_pid = processId(context.parentProcessContext());
148  process_description_[parent_pid].subprocesses_.push_back(pid);
149  }
150 }
151 
152 
153 // number of modules stored in the call graph
154 unsigned int
156 {
157  return boost::num_vertices(graph_);
158 }
159 
160 
161 // retrieve the ModuleDescriptio associated to the given id and vertex
164 {
165  return graph_.m_graph[source_].module_;
166 }
167 
168 
169 // retrieve the ModuleDescription associated to the given id and vertex
171 ProcessCallGraph::module(unsigned int module) const
172 {
173  return graph_.m_graph[module].module_;
174 }
175 
176 
177 // retrieve the full information for a given module
180 {
181  return graph_.m_graph[module];
182 }
183 
184 
185 // find the dependencies of the given module
186 std::vector<unsigned int>
188 {
189  std::vector<unsigned int> colors(boost::num_vertices(graph_));
190  auto colormap = boost::make_container_vertex_map(colors);
191 
192  // depht-first visit all vertices starting from the given module
193  boost::default_dfs_visitor visitor;
194  boost::depth_first_visit(graph_, module, visitor, colormap);
195 
196  // count the visited vertices (the `black' ones) in order to properly size the
197  // output vector; then fill the dependencies with the list of visited nodes
198  unsigned int size = 0;
199  for (unsigned int color : colors)
200  if (boost::black_color == color)
201  ++size;
202  std::vector<unsigned int> dependencies(size);
203  unsigned j = 0;
204  for (unsigned int i = 0; i < colors.size(); ++i)
205  if (boost::black_color == colors[i])
206  dependencies[j++] = i;
207  assert(size == j);
208 
209  return dependencies;
210 }
211 
212 // find the dependencies of all modules in the given path
213 //
214 // return two vector:
215 // - the first lists all the dependencies for the whole path
216 // - the second lists the one-after-the-last dependency index into the first vector for each module
217 std::pair<std::vector<unsigned int>, std::vector<unsigned int>>
218 ProcessCallGraph::dependencies(std::vector<unsigned int> const & path)
219 {
220  std::vector<unsigned int> colors(boost::num_vertices(graph_));
221  auto colormap = boost::make_container_vertex_map(colors);
222 
223  // first, find and count all the path's modules' dependencies
224  boost::default_dfs_visitor visitor;
225  for (unsigned int module: path)
226  boost::depth_first_visit(graph_, module, visitor, colormap);
227 
228  unsigned int size = 0;
229  for (unsigned int color : colors)
230  if (color == 0)
231  ++size;
232 
233  // allocate the output vectors
234  std::vector<unsigned int> dependencies(size);
235  dependencies.resize(0);
236  std::vector<unsigned int> indices(path.size());
237  indices.resize(0);
238 
239  // reset the color map
240  for (unsigned int & color : colors)
241  color = 0;
242 
243  // find again all the dependencies, and record those associated to each module
244  struct record_vertices : boost::default_dfs_visitor {
245  record_vertices(std::vector<unsigned int> & vertices) :
246  vertices_(vertices) { }
247 
248  void discover_vertex(unsigned int vertex, GraphType const& graph) {
249  vertices_.push_back(vertex);
250  }
251 
252  std::vector<unsigned int> & vertices_;
253  };
254  record_vertices recorder(dependencies);
255 
256  for (unsigned int module: path) {
257  // skip modules that have already been added as dependencies
258  if (colors[module] != boost::black_color)
259  boost::depth_first_visit(graph_, module, recorder, colormap);
260  indices.push_back(dependencies.size());
261  }
262 
263  return std::make_pair(dependencies, indices);
264 
265 }
266 
267 // register a (sub)process and assigns it a "process id"
268 // if called with a duplicate process name, returns the original process id
270 {
271  static unsigned int s_id = 0;
272 
273  // registerProcess (called by preBeginJob) must be called for the parent process before its subprocess(es)
274  if (context.isSubProcess() and process_id_.find(context.parentProcessContext().processName()) == process_id_.end()) {
276  << "ProcessCallGraph::preBeginJob(): called for subprocess \"" << context.processName() << "\""
277  << " before being called for its parent process \"" << context.parentProcessContext().processName() << "\"";
278  }
279 
280  // registerProcess (called by preBeginJob) should be called once or each (sub)process
281  auto id = process_id_.find(context.processName());
282  if (id != process_id_.end()) {
284  << "ProcessCallGraph::preBeginJob(): called twice for the same "
285  << (context.isSubProcess() ? "subprocess" : "process") << " " << context.processName();
286  }
287 
288  std::tie(id, std::ignore) = process_id_.insert(std::make_pair(context.processName(), s_id++));
289  return id->second;
290 }
291 
292 
293 // retrieve the "process id" of a process, given its ProcessContex
294 // throws an exception if the (sub)process was not registered
295 unsigned int ProcessCallGraph::processId(edm::ProcessContext const & context) const
296 {
297  auto id = process_id_.find(context.processName());
298  if (id == process_id_.end())
300  << "ProcessCallGraph::processId(): unexpected " << (context.isSubProcess() ? "subprocess" : "process") << " " << context.processName();
301  return id->second;
302 }
303 
304 
305 // retrieve the "process id" of a process, given its ProcessContex
306 // throws an exception if the (sub)process was not registered
308 {
309  auto id = process_id_.find(processName);
310  if (id == process_id_.end())
312  << "ProcessCallGraph::processId(): unexpected (sub)process " << processName;
313  return id->second;
314 }
315 
316 
317 // retrieve the number of processes
318 std::vector<ProcessCallGraph::ProcessType> const & ProcessCallGraph::processes() const
319 {
320  return process_description_;
321 }
322 
323 
324 // retrieve information about a process, given its "process id"
326 {
327  return process_description_.at(pid);
328 }
329 
330 
331 // retrieve information about a process, given its ProcessContex
333 {
334  unsigned int pid = processId(context);
335  return process_description_[pid];
336 }
337 
338 
339 // retrieve information about a process, given its ProcessContex
341 {
342  unsigned int pid = processId(processName);
343  return process_description_[pid];
344 }
size
Write out results.
unsigned int registerProcess(edm::ProcessContext const &)
std::string const & processName() const
unsigned int processId(edm::ProcessContext const &) const
std::vector< ModuleDescription const * > const & modulesOnEndPath(unsigned int endPathIndex) const
std::vector< ModuleDescription const * > const & modulesOnPath(unsigned int pathIndex) const
edm::ModuleDescription const & source() const
ProcessContext const & parentProcessContext() const
std::vector< ModuleDescription const * > const & allModules() const
EDMModuleType edmModuleTypeEnum(edm::ModuleDescription const &module)
std::vector< std::string > const & endPaths() const
U second(std::pair< T, U > const &p)
dependencies
Definition: symbols.py:20
vector< Color_t > colors
static const edm::ProductID s_id
Definition: EventBase.cc:27
void preBeginJob(edm::PathsAndConsumesOfModulesBase const &, edm::ProcessContext const &)
std::vector< ProcessType > const & processes() const
ProcessType const & processDescription(unsigned int) const
const std::complex< double > I
Definition: I.h:8
void preSourceConstruction(edm::ModuleDescription const &)
boost::subgraph< boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, NodeType, boost::property< boost::edge_index_t, int >, boost::property< boost::graph_name_t, std::string > >> GraphType
def ignore(seq)
std::vector< unsigned int > depends(unsigned int module) const
std::vector< ModuleDescription const * > const & modulesWhoseProductsAreConsumedBy(unsigned int moduleID) const
std::vector< std::string > const & paths() const
std::pair< std::vector< unsigned int >, std::vector< unsigned int > > dependencies(std::vector< unsigned int > const &path)
unsigned int size() const
edm::ModuleDescription const & module(unsigned int module) const
iterator_pair_as_a_range< I > make_range(std::pair< I, I > p)
bool isSubProcess() const
Definition: vlib.h:208
unsigned int id() const
NodeType const & operator[](unsigned int module) const