Logo AND Algorithmique Numérique Distribuée

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