Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
11b6eee49f1370394b721bfc659268ee0109bf73
[simgrid.git] / examples / cpp / dag-from-dot / s4u-dag-from-dot.cpp
1 /* simple test trying to load a DOT file.                                   */
2
3 /* Copyright (c) 2010-2021. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "simgrid/s4u.hpp"
10 #include <stdio.h>
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(dag_from_dot, "Logging specific to this example");
13
14 int main(int argc, char** argv)
15 {
16   simgrid::s4u::Engine e(&argc, argv);
17   e.load_platform(argv[1]);
18
19   std::vector<simgrid::s4u::ActivityPtr> dag = simgrid::s4u::create_DAG_from_dot(argv[2]);
20
21   if (dag.empty()) {
22     XBT_CRITICAL("No dot loaded. Do you have a cycle in your graph?");
23     exit(2);
24   }
25
26   XBT_INFO("--------- Display all activities of the loaded DAG -----------");
27   for (const auto& a : dag) {
28     std::string type = "an Exec";
29     std::string task = "flops to execute";
30     if (dynamic_cast<simgrid::s4u::Comm*>(a.get()) != nullptr) {
31       type = "a Comm";
32       task = "bytes to transfer";
33     }
34     XBT_INFO("'%s' is %s: %.0f %s. Dependencies: %s; Ressources: %s", a->get_cname(), type.c_str(), a->get_remaining(),
35              task.c_str(), (a->dependencies_solved() ? "solved" : "NOT solved"),
36              (a->is_assigned() ? "assigned" : "NOT assigned"));
37   }
38
39   XBT_INFO("------------------- Schedule tasks ---------------------------");
40   auto hosts = e.get_all_hosts();
41   auto count = e.get_host_count();
42   int cursor = 0;
43   // Schedule end first
44   static_cast<simgrid::s4u::Exec*>(dag.back().get())->set_host(hosts[0]);
45
46   for (const auto& a : dag) {
47     auto* exec = dynamic_cast<simgrid::s4u::Exec*>(a.get());
48     if (exec != nullptr && exec->get_name() != "end") {
49       exec->set_host(hosts[cursor % count]);
50       cursor++;
51     }
52     auto* comm = dynamic_cast<simgrid::s4u::Comm*>(a.get());
53     if (comm != nullptr) {
54       auto pred = dynamic_cast<simgrid::s4u::Exec*>((*comm->get_dependencies().begin()).get());
55       auto succ = dynamic_cast<simgrid::s4u::Exec*>(comm->get_successors().front().get());
56       comm->set_source(pred->get_host())->set_destination(succ->get_host());
57     }
58   }
59
60   XBT_INFO("------------------- Run the schedule -------------------------");
61   e.run();
62
63   XBT_INFO("-------------- Summary of executed schedule ------------------");
64   for (const auto& a : dag) {
65     const auto* exec = dynamic_cast<simgrid::s4u::Exec*>(a.get());
66     if (exec != nullptr) {
67       XBT_INFO("[%f->%f] '%s' executed on %s", exec->get_start_time(), exec->get_finish_time(), exec->get_cname(),
68                exec->get_host()->get_cname());
69     }
70     const auto* comm = dynamic_cast<simgrid::s4u::Comm*>(a.get());
71     if (comm != nullptr) {
72       XBT_INFO("[%f->%f] '%s' transferred from %s to %s", comm->get_start_time(), comm->get_finish_time(),
73                comm->get_cname(), comm->get_source()->get_cname(), comm->get_destination()->get_cname());
74     }
75   }
76   return 0;
77 }