Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New function: Engine::track_vetoed_activities()
[simgrid.git] / examples / cpp / dag-simple / s4u-dag-simple.cpp
1 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/s4u.hpp"
7 #include <vector>
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
10
11 int main(int argc, char* argv[])
12 {
13   simgrid::s4u::Engine e(&argc, argv);
14   e.load_platform(argv[1]);
15   std::set<simgrid::s4u::Activity*> vetoed;
16   e.track_vetoed_activities(&vetoed);
17
18   auto fafard = e.host_by_name("Fafard");
19
20   // Display the details on vetoed activities
21   simgrid::s4u::Activity::on_veto.connect([&e](simgrid::s4u::Activity& a) {
22     auto& exec = static_cast<simgrid::s4u::Exec&>(a); // all activities are execs in this example
23
24     XBT_INFO("Activity '%s' vetoed. Dependencies: %s; Ressources: %s", exec.get_cname(),
25              (exec.dependencies_solved() ? "solved" : "NOT solved"),
26              (exec.is_assigned() ? "assigned" : "NOT assigned"));
27   });
28
29   // Define an amount of work that should take 1 second to execute.
30   double computation_amount = fafard->get_speed();
31
32   // Create a small DAG: Two parents and a child
33   simgrid::s4u::ExecPtr first_parent  = simgrid::s4u::Exec::init();
34   simgrid::s4u::ExecPtr second_parent = simgrid::s4u::Exec::init();
35   simgrid::s4u::ExecPtr child         = simgrid::s4u::Exec::init();
36   first_parent->add_successor(child);
37   second_parent->add_successor(child);
38
39   /*
40     std::vector<simgrid::s4u::ExecPtr> pending_execs;
41     pending_execs.push_back(first_parent);
42     pending_execs.push_back(second_parent);
43     pending_execs.push_back(child);
44   */
45
46   // Set the parameters (the name is for logging purposes only)
47   // + First parent ends after 1 second and the Second parent after 2 seconds.
48   first_parent->set_name("parent 1")->set_flops_amount(computation_amount);
49   second_parent->set_name("parent 2")->set_flops_amount(2 * computation_amount);
50   child->set_name("child")->set_flops_amount(computation_amount);
51
52   // Only the parents are scheduled so far
53   first_parent->set_host(fafard);
54   second_parent->set_host(fafard);
55
56   // Start all activities that can actually start.
57   first_parent->vetoable_start();
58   second_parent->vetoable_start();
59   child->vetoable_start();
60
61   while (child->get_state() != simgrid::s4u::Activity::State::FINISHED) {
62     e.run();
63     for (auto* a : vetoed) {
64       auto* exec = static_cast<simgrid::s4u::Exec*>(a);
65
66       // In this simple case, we just assign the child task to a resource when its dependencies are solved
67       if (exec->dependencies_solved() && not exec->is_assigned()) {
68         XBT_INFO("Activity %s's dependencies are resolved. Let's assign it to Fafard.", exec->get_cname());
69         exec->set_host(fafard);
70       } else {
71         XBT_INFO("Activity %s not ready.", exec->get_cname());
72       }
73     }
74     vetoed.clear(); // DON'T FORGET TO CLEAR this set between two calls to run
75   }
76
77   XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());
78
79   return 0;
80 }