21 #include <ModuleUtils.h> 24 #include <boost/graph/graphviz.hpp> 25 #include <boost/graph/topological_sort.hpp> 37 typedef boost::graph_traits<Graph>::out_edge_iterator out_edge_iterator_t;
38 typedef boost::graph_traits<Graph>::in_edge_iterator in_edge_iterator_t;
41 using std::runtime_error::runtime_error;
45 using std::runtime_error::runtime_error;
55 bool isConnectedToByOut(Graph& g, vertex_t vertex, vertex_t to) {
56 out_edge_iterator_t o, o_end;
57 std::tie(o, o_end) = boost::out_edges(vertex, g);
59 for (; o != o_end; ++o) {
60 vertex_t target = boost::target(*o, g);
64 if (isConnectedToByOut(g, target, to))
78 bool isConnectedToByIn(Graph& g, vertex_t vertex, vertex_t to) {
79 in_edge_iterator_t i, i_end;
80 std::tie(i, i_end) = boost::in_edges(vertex, g);
82 for (; i != i_end; ++i) {
83 vertex_t source = boost::source(*i, g);
87 if (isConnectedToByIn(g, source, to))
101 bool isConnectedTo(Graph& g, vertex_t vertex, vertex_t to) {
102 if (isConnectedToByOut(g, vertex, to))
105 return isConnectedToByIn(g, vertex, to);
115 bool isConnectedDirectlyTo(Graph& g, vertex_t from, vertex_t to) {
116 out_edge_iterator_t o, o_end;
117 std::tie(o, o_end) = boost::out_edges(from, g);
119 for (; o != o_end; ++o) {
120 vertex_t target = boost::target(*o, g);
141 auto it = std::find_if(looper_path.elements.begin(), looper_path.elements.end(),
142 [&module_name](
const std::string& m) {
143 return m == module_name;
146 return it != looper_path.elements.end();
149 momemta::ModuleList::value_type get_module_def(
const std::string& module_type,
150 const momemta::ModuleList& available_modules) {
152 auto it = std::find_if(available_modules.begin(), available_modules.end(),
153 [&module_type](
const momemta::ModuleList::value_type& m) {
154 return m.name == module_type;
157 assert(it != available_modules.end());
164 auto& storage = module_decls;
167 auto map_it = storage.find(path);
168 if (map_it == storage.end()) {
170 sorted_execution_paths.emplace_back(path);
173 storage[path].emplace_back(decl);
175 map_it->second.emplace_back(decl);
179 const std::vector<uuid>& ComputationGraph::getPaths()
const {
180 return sorted_execution_paths;
183 const std::vector<Configuration::ModuleDecl>& ComputationGraph::getDecls(
const uuid& path)
const {
184 return module_decls.at(path);
187 void ComputationGraph::initialize(PoolPtr pool) {
188 const auto& execution_paths = sorted_execution_paths;
191 std::map<uuid, std::vector<ModulePtr>> module_instances;
199 for (
auto it = execution_paths.rbegin(); it != execution_paths.rend(); ++it) {
201 const auto& modules = getDecls(*it);
202 for (
auto module_decl_it = modules.begin(); module_decl_it != modules.end(); ++module_decl_it) {
204 std::unique_ptr<ParameterSet> params(module_decl_it->parameters->clone());
206 if (module_decl_it->type ==
"Looper") {
212 params->raw_set(
"path",
Path(module_instances.at(config_path_id)));
216 module_instances[*it].push_back(ModuleFactory::get().create(module_decl_it->type, pool, *params));
218 LOG(fatal) <<
"Exception while trying to create module " << module_decl_it->type
219 <<
"::" << module_decl_it->name
220 <<
". See message above for a (possible) more detailed description of the error.";
221 std::rethrow_exception(std::current_exception());
226 modules = module_instances[DEFAULT_EXECUTION_PATH];
229 void ComputationGraph::configure() {
230 for (
auto& module: modules)
234 void ComputationGraph::finish() {
235 for (
auto& module: modules)
239 void ComputationGraph::beginIntegration() {
240 for (
auto& module: modules)
241 module->beginIntegration();
244 void ComputationGraph::endIntegration() {
245 for (
auto& module: modules)
246 module->endIntegration();
249 Module::Status ComputationGraph::execute() {
250 for (
auto& module: modules)
251 module->beginPoint();
253 for (
auto& module: modules) {
255 auto start = high_resolution_clock::now();
257 auto status = module->work();
259 module_timings[module.get()] += high_resolution_clock::now() - start;
262 if (status == Module::Status::NEXT) {
264 return Module::Status::NEXT;
265 }
else if (status == Module::Status::ABORT) {
267 return Module::Status::ABORT;
271 for (
auto& module: modules)
274 return Module::Status::OK;
278 void ComputationGraph::logTimings()
const {
279 LOG(info) <<
"Time spent evaluating modules (more details for loopers below):";
280 for (
auto it: module_timings) {
281 LOG(info) <<
" " << it.first->name() <<
": " << duration_cast<duration<double>>(it.second).count() <<
"s";
286 void ComputationGraph::setNDimensions(
size_t n) {
290 size_t ComputationGraph::getNDimensions()
const {
294 ComputationGraphBuilder::ComputationGraphBuilder(
const momemta::ModuleList& available_modules,
296 available_modules(available_modules), configuration(configuration) { }
301 const auto& requested_modules = configuration.
getModules();
304 for (
const auto& module: requested_modules) {
305 vertex_t v = boost::add_vertex(g);
309 vertex.name = module.name;
310 vertex.type = module.type;
313 vertex.def = get_module_def(module.type, available_modules);
316 vertex.decl = module;
318 vertices.emplace(module.name, v);
322 typename boost::graph_traits<Graph>::vertex_iterator vtx_it, vtx_it_end;
323 for (std::tie(vtx_it, vtx_it_end) = boost::vertices(g); vtx_it != vtx_it_end; vtx_it++) {
325 const auto& vertex = g[*vtx_it];
328 for (
const auto& output: vertex.def.outputs) {
331 typename boost::graph_traits<Graph>::vertex_iterator test_vtx_it, test_vtx_it_end;
332 for (std::tie(test_vtx_it, test_vtx_it_end) = boost::vertices(g); test_vtx_it != test_vtx_it_end;
335 const auto& test_module = g[*test_vtx_it];
338 if (test_module.name == vertex.name)
342 const auto& test_module_def = test_module.def;
343 for (
const auto& input: test_module_def.inputs) {
346 momemta::getInputTagsForInput(input, *test_module.decl.parameters);
352 for (
const auto& inputTag: *inputTags) {
353 if (vertex.name == inputTag.module && output.name == inputTag.parameter) {
359 std::tie(e, inserted) = boost::add_edge(*vtx_it, vertices.at(test_module.name), g);
364 edge.description = inputTag.parameter;
365 if (inputTag.isIndexed()) {
366 edge.description +=
"[" + std::to_string(inputTag.index) +
"]";
377 for (
const auto& vertex: vertices) {
378 if (g[vertex.second].type !=
"Looper")
381 const auto& looper_vtx = vertex.second;
382 const auto& looper_decl = g[looper_vtx].decl;
388 for (
const auto& m: looper_path.elements) {
390 auto module_it = vertices.find(m);
391 if (module_it == vertices.end()) {
392 LOG(warning) <<
"Module '" << m <<
"' present in Looper '" << looper_decl.
name 393 <<
"' execution path does not exists";
397 auto module_vertex = module_it->second;
398 if (!isConnectedDirectlyTo(g, looper_vtx, module_vertex)) {
401 std::tie(e, inserted) = boost::add_edge(looper_vtx, module_vertex, g);
402 g[e].description =
"virtual link (module in path)";
407 out_edge_iterator_t e, e_end;
408 std::tie(e, e_end) = boost::out_edges(looper_vtx, g);
411 for (; e != e_end; ++e) {
412 auto target = boost::target(*e, g);
415 in_edge_iterator_t i, i_end;
416 std::tie(i, i_end) = boost::in_edges(target, g);
418 for (; i != i_end; ++i) {
419 auto source = boost::source(*i, g);
421 if (source == looper_vtx)
425 if (!isConnectedTo(g, source, looper_vtx)) {
428 std::tie(e, inserted) = boost::add_edge(source, looper_vtx, g);
429 g[e].description =
"virtual link";
449 out_edge_iterator_t o, o_end;
450 std::tie(o, o_end) = boost::out_edges(vertices.at(
"cuba"), g);
452 std::set<size_t> unique_cuba_indices;
453 for (; o != o_end; ++o) {
454 const auto& inputTag = g[*o].tag;
457 if (inputTag.parameter !=
"ps_points")
460 assert(inputTag.isIndexed());
461 unique_cuba_indices.emplace(inputTag.index);
464 size_t n_dimensions = unique_cuba_indices.size();
471 std::unordered_map<size_t, size_t> new_indices_mapping;
472 size_t current_index = 0;
473 std::tie(o, o_end) = boost::out_edges(vertices.at(
"cuba"), g);
474 for (; o != o_end; ++o) {
475 const auto& module_vertex = g[boost::target(*o, g)];
477 for (
const auto& input: module_vertex.def.inputs) {
481 momemta::getInputTagsForInput(input, *module_vertex.decl.parameters);
487 bool update_decl =
false;
488 std::vector<InputTag> updatedInputTags;
489 for (
const auto& inputTag: *inputTags) {
491 auto updatedInputTag = inputTag;
494 if (inputTag.module ==
"cuba" && inputTag.parameter ==
"ps_points") {
497 auto it = new_indices_mapping.find(inputTag.index);
498 if (it == new_indices_mapping.end()) {
499 new_indices_mapping.emplace(inputTag.index, current_index);
500 updatedInputTag.index = current_index;
503 updatedInputTag.index = it->second;
505 updatedInputTag.update();
508 updatedInputTags.push_back(updatedInputTag);
513 momemta::setInputTagsForInput(input, *module_vertex.decl.parameters, updatedInputTags);
520 const auto& execution_paths = configuration.
getPaths();
523 computationGraph->setNDimensions(n_dimensions);
524 for (
auto vertex: sorted_vertices) {
529 static auto find_module_in_path = [&vertex,
this](std::shared_ptr<ExecutionPath> p) ->
bool {
531 auto it = std::find_if(p->elements.begin(), p->elements.end(),
532 [&vertex,
this](
const std::string& element) ->
bool {
533 return g[vertex].name == element;
537 return it != p->elements.end();
541 auto execution_path_it = std::find_if(execution_paths.begin(), execution_paths.end(), find_module_in_path);
542 auto execution_path = execution_path_it == execution_paths.end() ? DEFAULT_EXECUTION_PATH :
543 (*execution_path_it)->id;
546 assert(!computationGraph->getPaths().empty() || execution_path == DEFAULT_EXECUTION_PATH);
548 computationGraph->addDecl(execution_path, g[vertex].decl);
551 graph_created =
true;
553 return computationGraph;
556 void ComputationGraphBuilder::prune_graph() {
559 for (
auto it = vertices.begin(), ite = vertices.end(); it != ite;) {
560 if (boost::out_degree(it->second, g) == 0) {
562 auto& vertex = g[it->second];
565 if (vertex.def.internal || vertex.def.sticky) {
571 LOG(info) <<
"Module '" << it->first <<
"' output is not used by any other module. Removing it from the configuration.";
572 boost::clear_vertex(it->second, g);
573 boost::remove_vertex(it->second, g);
575 it = vertices.erase(it);
582 typename boost::graph_traits<Graph>::vertex_iterator vtx_it, vtx_it_end;
583 for (std::tie(vtx_it, vtx_it_end) = boost::vertices(g); vtx_it != vtx_it_end; vtx_it++) {
584 g[*vtx_it].id =
id++;
588 void ComputationGraphBuilder::sort_graph() {
589 const std::vector<std::shared_ptr<ExecutionPath>>& execution_paths = configuration.
getPaths();
592 boost::topological_sort(g, std::front_inserter(sorted_vertices),
593 boost::vertex_index_map(boost::get(&Vertex::id, g)));
596 LOG(fatal) <<
"Exception while sorting the graph. Graphviz representation saved as graph.debug";
601 sorted_vertices.erase(std::remove_if(sorted_vertices.begin(), sorted_vertices.end(),
602 [
this](
const vertex_t& vertex) ->
bool {
603 return g[vertex].def.internal;
604 }), sorted_vertices.end());
607 void ComputationGraphBuilder::validate() {
609 auto log_and_throw_unresolved_input = [](
const std::string& module_name,
const InputTag& input) {
610 LOG(fatal) <<
"Module '" << module_name <<
"' requested a non-existing input (" << input.toString() <<
")";
611 throw unresolved_input(
"Module '" + module_name +
"' requested a non-existing input (" + input.toString() +
")");
615 for (
auto it = vertices.begin(), ite = vertices.end(); it != ite; it++) {
617 const auto& vertex = g[it->second];
619 if (vertex.def.internal)
622 const auto& inputs = vertex.def.inputs;
624 for (
const auto& input_def: inputs) {
628 momemta::getInputTagsForInput(input_def, *vertex.decl.parameters);
634 for (
const auto& inputTag: *inputTags) {
635 auto target_it = vertices.find(inputTag.module);
637 if (target_it == vertices.end()) {
639 log_and_throw_unresolved_input(it->first, inputTag);
644 const auto& target_module_outputs = g[target_it->second].def.outputs;
645 auto output_it = std::find_if(target_module_outputs.begin(),
646 target_module_outputs.end(),
647 [&inputTag](
const ArgDef& output) {
648 return inputTag.parameter == output.name;
651 if (output_it == target_module_outputs.end()) {
653 log_and_throw_unresolved_input(it->first, inputTag);
660 std::map<vertex_t, std::vector<vertex_t>> modules_not_in_path;
661 for (
const auto& vertex: sorted_vertices) {
662 if (g[vertex].type ==
"Looper") {
664 const auto& decl = g[vertex].decl;
666 out_edge_iterator_t e, e_end;
667 std::tie(e, e_end) = boost::out_edges(vertex, g);
670 for (; e != e_end; ++e) {
671 auto target = boost::target(*e, g);
674 if (! checkInPath(decl, g[target].name)) {
675 auto& loopers = modules_not_in_path[target];
676 auto it = std::find(loopers.begin(), loopers.end(), vertex);
677 if (it == loopers.end())
678 loopers.push_back(vertex);
680 modules_not_in_path.erase(target);
686 if (modules_not_in_path.size() != 0) {
688 auto it = modules_not_in_path.begin();
690 auto target = g[it->first];
692 std::stringstream loopers;
693 for (
size_t i = 0; i < it->second.size(); i++) {
694 loopers <<
"'" << g[it->second[i]].name <<
"'";
695 if (i != it->second.size() - 1)
699 std::string plural = it->second.size() > 1?
"s" :
"";
700 std::string one_of_the = it->second.size() > 1 ?
"one of the" :
"the";
702 LOG(fatal) <<
"Module '" << target.name <<
"' is configured to use Looper " << loopers.str()
703 <<
" output" << plural <<
", but is not actually part of the Looper" << plural <<
" execution path. This will lead to undefined " 704 <<
"behavior. You can fix the issue by adding the module '" 706 <<
"' to " << one_of_the <<
" Looper" << plural <<
" execution path";
720 const std::vector<std::shared_ptr<ExecutionPath>>& paths):
721 graph(g), paths(paths) {}
725 void writeVertex(std::ostream& out,
const vertex_t& v)
const {
726 std::string shape =
"ellipse";
727 std::string color =
"black";
728 std::string style =
"solid";
729 std::string extra =
"";
731 if (graph[v].def.internal) {
737 if (graph[v].type ==
"Looper") {
742 out <<
"[shape=\"" << shape <<
"\",color=\"" << color <<
"\",style=\"" << style
743 <<
"\",label=\"" << graph[v].name <<
"\"";
745 if (!extra.empty()) {
753 void writeEdge(std::ostream& out,
const edge_t& e)
const {
754 std::string color =
"black";
755 std::string style =
"solid";
756 std::string extra =
"";
760 extra =
"constraint=false";
763 out <<
"[color=\"" << color <<
"\",style=\"" << style
764 <<
"\",label=\"" << graph[e].description <<
"\"";
766 if (!extra.empty()) {
774 void writeGraph(std::ostream& out)
const {
777 size_t color_index = 0;
778 for (
const auto& path: paths) {
780 out <<
"subgraph cluster_" << index <<
" {" << std::endl;
782 out << R
"(style=filled; fillcolor=")" << colors[color_index] << R"(";)" << std::endl; 783 path_colors.emplace(path->id, colors[color_index]); 785 auto looper_vertex = find_looper(path->id);
787 if (looper_vertex != boost::graph_traits<Graph>::null_vertex())
788 out <<
" label=\"" << graph[looper_vertex].name <<
" execution path\";" << std::endl;
791 for (
const auto& e: path->elements) {
792 auto vertex = find_vertex(e);
793 if (vertex != boost::graph_traits<Graph>::null_vertex())
794 out << graph[vertex].id <<
"; ";
798 out <<
"}" << std::endl;
802 if (color_index >= colors.size())
808 vertex_t find_vertex(
const std::string& name)
const {
809 typename boost::graph_traits<Graph>::vertex_iterator vtx_it, vtx_it_end;
811 for(boost::tie(vtx_it, vtx_it_end) = boost::vertices(graph); vtx_it != vtx_it_end; ++vtx_it) {
812 if (graph[*vtx_it].name == name)
816 return boost::graph_traits<Graph>::null_vertex();
819 vertex_t find_looper(
const uuid& path)
const {
820 typename boost::graph_traits<Graph>::vertex_iterator vtx_it, vtx_it_end;
822 for(boost::tie(vtx_it, vtx_it_end) = boost::vertices(graph); vtx_it != vtx_it_end; ++vtx_it) {
823 if (graph[*vtx_it].type ==
"Looper") {
824 const auto& looper_path = graph[*vtx_it].decl.parameters->get<
ExecutionPath>(
"path");
825 if (looper_path.id == path)
830 return boost::graph_traits<Graph>::null_vertex();
834 const std::vector<std::shared_ptr<ExecutionPath>> paths;
836 mutable std::unordered_map<uuid, std::string,
837 boost::hash<uuid>> path_colors;
839 const std::array<std::string, 5> colors = {{
853 void operator()(std::ostream& out,
const vertex_t& v)
const {
854 writer->writeVertex(out, v);
857 void operator()(std::ostream& out,
const edge_t& e)
const {
858 writer->writeEdge(out, e);
861 void operator()(std::ostream& out)
const {
862 writer->writeGraph(out);
866 std::shared_ptr<graph_writer> writer;
869 void graphviz_export(
const Graph& g,
870 const std::vector<std::shared_ptr<ExecutionPath>>& paths,
871 const std::string& filename) {
873 std::ofstream f(filename.c_str());
874 auto writer = std::make_shared<graph_writer>(g, paths);
883 graphviz_export(g, configuration.getPaths(), output);
size_t getNDimensions() const
std::string name
Name of the module (user-defined from the configuration file)
std::shared_ptr< ComputationGraph > build()
Build the computation graph.
std::shared_ptr< ParameterSet > parameters
Module's parameters, as parsed from the configuration file.
std::vector< std::shared_ptr< ExecutionPath > > getPaths() const
Defines an input / output.
void exportGraph(const std::string &output) const
Export a GraphViz representation of the computation graph to a file.
A frozen snapshot of the configuration file.
A module declaration, defined from the configuration file.
const std::vector< ModuleDecl > & getModules() const