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