Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
first attempt to a DAG loader for s4u. To be improved and polished before release
[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   XBT_INFO("--------- Display all activities of the loaded DAG -----------");
22   for (const auto& a : dag) {
23     std::string type = "an Exec";
24     std::string task = "flops to execute";
25     if (dynamic_cast<simgrid::s4u::Comm*>(a.get()) != nullptr) {
26       type = "a Comm";
27       task = "bytes to transfer";
28     }
29     XBT_INFO("'%s' is %s: %.0f %s. Dependencies: %s; Ressources: %s", a->get_cname(), type.c_str(), a->get_remaining(),
30              task.c_str(), (a->dependencies_solved() ? "solved" : "NOT solved"),
31              (a->is_assigned() ? "assigned" : "NOT assigned"));
32   }
33
34   XBT_INFO("------------------- Schedule tasks ---------------------------");
35   auto hosts = e.get_all_hosts();
36   auto count = e.get_host_count();
37   int cursor = 0;
38   // Schedule end first
39   static_cast<simgrid::s4u::Exec*>(dag.back().get())->set_host(hosts[0]);
40
41   for (const auto& a : dag) {
42     auto* exec = dynamic_cast<simgrid::s4u::Exec*>(a.get());
43     if (exec != nullptr && exec->get_name() != "end") {
44       exec->set_host(hosts[cursor % count]);
45       cursor++;
46     }
47     auto* comm = dynamic_cast<simgrid::s4u::Comm*>(a.get());
48     if (comm != nullptr) {
49       auto pred = dynamic_cast<simgrid::s4u::Exec*>(comm->get_parent().get());
50       auto succ = dynamic_cast<simgrid::s4u::Exec*>(comm->get_child().get());
51       comm->set_from(pred->get_host())->set_to(succ->get_host());
52     }
53   }
54
55   XBT_INFO("------------------- Run the schedule -------------------------");
56   e.run();
57
58   XBT_INFO("-------------- Summary of executed schedule ------------------");
59   for (const auto& a : dag) {
60     auto* exec = dynamic_cast<simgrid::s4u::Exec*>(a.get());
61     if (exec != nullptr) {
62       XBT_INFO("[%f->%f] '%s' executed on %s", exec->get_start_time(), exec->get_finish_time(), exec->get_cname(),
63                exec->get_host()->get_cname());
64     }
65     auto* comm = dynamic_cast<simgrid::s4u::Comm*>(a.get());
66     if (comm != nullptr) {
67       XBT_INFO("%s", comm->get_cname());
68     }
69   }
70   return 0;
71 }