CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
33 
34 // adaptor to use range-based for loops with boost::graph edges(...) and vertices(...) functions
35 template <typename I>
36 struct iterator_pair_as_a_range : std::pair<I, I> {
37 public:
38  using std::pair<I, I>::pair;
39 
40  I begin() { return this->first; }
41  I end() { return this->second; }
42 };
43 
44 template <typename I>
47 }
48 
49 // FIXME
50 // - check that the Source has not already been added
52  // keep track of the Source module id
53  source_ = module.id();
54 
55  // create graph vertex for the source module
56  boost::add_vertex(graph_);
57  graph_.m_graph[module.id()] = {module, edm::EDMModuleType::kSource, true};
58 }
59 
60 // FIXME
61 // - check that the Source has already been added
62 // - check that all module ids are valid (e.g. subprocesses are not being added in
63 // the wrong order)
66  unsigned int pid = registerProcess(context);
67 
68  // work on the full graph (for the main process) or a subgraph (for a subprocess)
69  GraphType& graph = context.isSubProcess() ? graph_.create_subgraph() : graph_.root();
70 
71  // set the graph name property to the process name
72  boost::get_property(graph, boost::graph_name) = context.processName();
73 
74  // create graph vertices associated to all modules in the process
75  unsigned int size = pathsAndConsumes.largestModuleID() - boost::num_vertices(graph) + 1;
76  for (size_t i = 0; i < size; ++i)
77  boost::add_vertex(graph);
78 
79  // set the vertices properties (use the module id as the global index into the graph)
80  std::vector<unsigned int> modules;
81  modules.reserve(size);
82  for (edm::ModuleDescription const* module : pathsAndConsumes.allModules()) {
83  modules.push_back(module->id());
84  graph_.m_graph[module->id()] = {*module, edmModuleTypeEnum(*module), false};
85  }
86 
87  // add graph edges associated to module dependencies
88  for (edm::ModuleDescription const* consumer : pathsAndConsumes.allModules()) {
89  for (edm::ModuleDescription const* module : pathsAndConsumes.modulesWhoseProductsAreConsumedBy(consumer->id())) {
90  // module `consumer' depends on module `module'
91  boost::add_edge(consumer->id(), module->id(), graph_);
92  }
93  }
94 
95  // extract path names from the TriggerNamesService
97 
98  // extract the details of the paths and endpaths: name, modules on the path, and their dependencies
99  size = pathsAndConsumes.paths().size();
100  assert(tns.getTrigPaths().size() == size);
101  std::vector<PathType> paths;
102  paths.reserve(size);
103  for (unsigned int i = 0; i < size; ++i) {
104  std::vector<unsigned int> modules;
105  for (edm::ModuleDescription const* module : pathsAndConsumes.modulesOnPath(i)) {
106  modules.push_back(module->id());
107  // mark the modules in the Paths as scheduled
108  graph_.m_graph[module->id()].scheduled_ = true;
109  }
110  auto deps = dependencies(modules);
111  paths.emplace_back(tns.getTrigPath(i), modules, deps.first, deps.second);
112  }
113  size = pathsAndConsumes.endPaths().size();
114  std::vector<PathType> endPaths;
115  endPaths.reserve(size);
116  for (unsigned int i = 0; i < size; ++i) {
117  std::vector<unsigned int> modules;
118  for (edm::ModuleDescription const* module : pathsAndConsumes.modulesOnEndPath(i)) {
119  modules.push_back(module->id());
120  // mark the modules in the EndPaths as scheduled
121  graph_.m_graph[module->id()].scheduled_ = true;
122  }
123  auto deps = dependencies(modules);
124  endPaths.emplace_back(tns.getEndPath(i), modules, deps.first, deps.second);
125  }
126 
127  // store the description of process, modules and paths
128  process_description_.emplace_back(context.processName(), graph, modules, paths, endPaths);
129  assert(process_description_.size() == pid + 1);
130 
131  // attach a subprocess to its parent
132  if (context.isSubProcess()) {
133  unsigned int parent_pid = processId(context.parentProcessContext());
134  process_description_[parent_pid].subprocesses_.push_back(pid);
135  }
136 }
137 
138 // number of modules stored in the call graph
139 unsigned int ProcessCallGraph::size() const { return boost::num_vertices(graph_); }
140 
141 // retrieve the ModuleDescriptio associated to the given id and vertex
142 edm::ModuleDescription const& ProcessCallGraph::source() const { return graph_.m_graph[source_].module_; }
143 
144 // retrieve the ModuleDescription associated to the given id and vertex
146  return graph_.m_graph[module].module_;
147 }
148 
149 // retrieve the full information for a given module
151  return graph_.m_graph[module];
152 }
153 
154 // find the dependencies of the given module
155 std::vector<unsigned int> ProcessCallGraph::depends(unsigned int module) const {
156  std::vector<unsigned int> colors(boost::num_vertices(graph_));
157  auto colormap = boost::make_container_vertex_map(colors);
158 
159  // depht-first visit all vertices starting from the given module
160  boost::default_dfs_visitor visitor;
161  boost::depth_first_visit(graph_, module, visitor, colormap);
162 
163  // count the visited vertices (the `black' ones) in order to properly size the
164  // output vector; then fill the dependencies with the list of visited nodes
165  unsigned int size = 0;
166  for (unsigned int color : colors)
167  if (boost::black_color == color)
168  ++size;
169  std::vector<unsigned int> dependencies(size);
170  unsigned j = 0;
171  for (unsigned int i = 0; i < colors.size(); ++i)
172  if (boost::black_color == colors[i])
173  dependencies[j++] = i;
174  assert(size == j);
175 
176  return dependencies;
177 }
178 
179 // find the dependencies of all modules in the given path
180 //
181 // return two vector:
182 // - the first lists all the dependencies for the whole path
183 // - the second lists the one-after-the-last dependency index into the first vector for each module
184 std::pair<std::vector<unsigned int>, std::vector<unsigned int>> ProcessCallGraph::dependencies(
185  std::vector<unsigned int> const& path) {
186  std::vector<unsigned int> colors(boost::num_vertices(graph_));
187  auto colormap = boost::make_container_vertex_map(colors);
188 
189  // first, find and count all the path's modules' dependencies
190  boost::default_dfs_visitor visitor;
191  for (unsigned int module : path)
192  boost::depth_first_visit(graph_, module, visitor, colormap);
193 
194  unsigned int size = 0;
195  for (unsigned int color : colors)
196  if (color == 0)
197  ++size;
198 
199  // allocate the output vectors
200  std::vector<unsigned int> dependencies(size);
201  dependencies.resize(0);
202  std::vector<unsigned int> indices(path.size());
203  indices.resize(0);
204 
205  // reset the color map
206  for (unsigned int& color : colors)
207  color = 0;
208 
209  // find again all the dependencies, and record those associated to each module
210  struct record_vertices : boost::default_dfs_visitor {
211  record_vertices(std::vector<unsigned int>& vertices) : vertices_(vertices) {}
212 
213  void discover_vertex(unsigned int vertex, GraphType const& graph) { vertices_.push_back(vertex); }
214 
215  std::vector<unsigned int>& vertices_;
216  };
217  record_vertices recorder(dependencies);
218 
219  for (unsigned int module : path) {
220  // skip modules that have already been added as dependencies
221  if (colors[module] != boost::black_color)
222  boost::depth_first_visit(graph_, module, recorder, colormap);
223  indices.push_back(dependencies.size());
224  }
225 
226  return std::make_pair(dependencies, indices);
227 }
228 
229 // register a (sub)process and assigns it a "process id"
230 // if called with a duplicate process name, returns the original process id
232  static unsigned int s_id = 0;
233 
234  // registerProcess (called by preBeginJob) must be called for the parent process before its subprocess(es)
235  if (context.isSubProcess() and process_id_.find(context.parentProcessContext().processName()) == process_id_.end()) {
237  << "ProcessCallGraph::preBeginJob(): called for subprocess \"" << context.processName() << "\""
238  << " before being called for its parent process \"" << context.parentProcessContext().processName() << "\"";
239  }
240 
241  // registerProcess (called by preBeginJob) should be called once or each (sub)process
242  auto id = process_id_.find(context.processName());
243  if (id != process_id_.end()) {
245  << "ProcessCallGraph::preBeginJob(): called twice for the same "
246  << (context.isSubProcess() ? "subprocess" : "process") << " " << context.processName();
247  }
248 
249  std::tie(id, std::ignore) = process_id_.insert(std::make_pair(context.processName(), s_id++));
250  return id->second;
251 }
252 
253 // retrieve the "process id" of a process, given its ProcessContex
254 // throws an exception if the (sub)process was not registered
256  auto id = process_id_.find(context.processName());
257  if (id == process_id_.end())
259  << "ProcessCallGraph::processId(): unexpected " << (context.isSubProcess() ? "subprocess" : "process") << " "
260  << context.processName();
261  return id->second;
262 }
263 
264 // retrieve the "process id" of a process, given its ProcessContex
265 // throws an exception if the (sub)process was not registered
267  auto id = process_id_.find(processName);
268  if (id == process_id_.end())
270  << "ProcessCallGraph::processId(): unexpected (sub)process " << processName;
271  return id->second;
272 }
273 
274 // retrieve the number of processes
275 std::vector<ProcessCallGraph::ProcessType> const& ProcessCallGraph::processes() const { return process_description_; }
276 
277 // retrieve information about a process, given its "process id"
279  return process_description_.at(pid);
280 }
281 
282 // retrieve information about a process, given its ProcessContex
284  unsigned int pid = processId(context);
285  return process_description_[pid];
286 }
287 
288 // retrieve information about a process, given its ProcessContex
290  unsigned int pid = processId(processName);
291  return process_description_[pid];
292 }
pathNames_ & tns()), endPathNames_(&tns.getEndPaths()), wantSummary_(tns.wantSummary()
Definition: Schedule.cc:691
unsigned int registerProcess(edm::ProcessContext const &)
std::string const & processName() const
std::string const & getTrigPath(size_type const i) const
unsigned int processId(edm::ProcessContext const &) const
std::unordered_map< std::string, unsigned int > process_id_
iterator_pair_as_a_range< I > make_range(std::pair< I, I > p)
std::vector< ModuleDescription const * > const & modulesOnEndPath(unsigned int endPathIndex) const
std::vector< ModuleDescription const * > const & modulesOnPath(unsigned int pathIndex) const
assert(be >=bs)
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)
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 &)
std::vector< ProcessType > process_description_
std::vector< ModuleDescription const * > const & modulesWhoseProductsAreConsumedBy(unsigned int moduleID, BranchType branchType=InEvent) const
std::vector< unsigned int > depends(unsigned int module) const
tuple deps
Definition: symbols.py:61
std::string const & getEndPath(size_type const i) const
Strings const & getTrigPaths() 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
unsigned int source_
list indices
Definition: dqmdumpme.py:50
bool isSubProcess() 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
tuple module
Definition: callgraph.py:69
unsigned int id() const
NodeType const & operator[](unsigned int module) const