Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make all Activity starts vetoable
[simgrid.git] / examples / cpp / dag-simple / s4u-dag-simple.cpp
1 /* Copyright (c) 2007-2023. 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 namespace sg4 = simgrid::s4u;
11
12 int main(int argc, char* argv[])
13 {
14   sg4::Engine e(&argc, argv);
15   e.load_platform(argv[1]);
16   std::set<sg4::Activity*> vetoed;
17   e.track_vetoed_activities(&vetoed);
18
19   auto fafard = e.host_by_name("Fafard");
20
21   // Display the details on vetoed activities
22   sg4::Activity::on_veto_cb([](const sg4::Activity& a) {
23     const auto& exec = static_cast<const sg4::Exec&>(a); // all activities are execs in this example
24
25     XBT_INFO("Activity '%s' vetoed. Dependencies: %s; Ressources: %s", exec.get_cname(),
26              (exec.dependencies_solved() ? "solved" : "NOT solved"),
27              (exec.is_assigned() ? "assigned" : "NOT assigned"));
28   });
29
30   sg4::Activity::on_completion_cb([](sg4::Activity const& activity) {
31     const auto* exec = dynamic_cast<sg4::Exec const*>(&activity);
32     if (exec == nullptr) // Only Execs are concerned here
33       return;
34     XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", exec->get_cname(), exec->get_start_time(),
35              exec->get_finish_time());
36   });
37
38   // Define an amount of work that should take 1 second to execute.
39   double computation_amount = fafard->get_speed();
40
41   // Create a small DAG: Two parents and a child
42   sg4::ExecPtr first_parent  = sg4::Exec::init();
43   sg4::ExecPtr second_parent = sg4::Exec::init();
44   sg4::ExecPtr child         = sg4::Exec::init();
45   first_parent->add_successor(child);
46   second_parent->add_successor(child);
47
48   // Set the parameters (the name is for logging purposes only)
49   // + First parent ends after 1 second and the Second parent after 2 seconds.
50   first_parent->set_name("parent 1")->set_flops_amount(computation_amount);
51   second_parent->set_name("parent 2")->set_flops_amount(2 * computation_amount);
52   child->set_name("child")->set_flops_amount(computation_amount);
53
54   // Only the parents are scheduled so far
55   first_parent->set_host(fafard);
56   second_parent->set_host(fafard);
57
58   // Start all activities that can actually start.
59   first_parent->start();
60   second_parent->start();
61   child->start();
62
63   while (child->get_state() != sg4::Activity::State::FINISHED) {
64     e.run();
65     for (auto* a : vetoed) {
66       auto* exec = static_cast<sg4::Exec*>(a);
67
68       // In this simple case, we just assign the child task to a resource when its dependencies are solved
69       if (exec->dependencies_solved() && not exec->is_assigned()) {
70         XBT_INFO("Activity %s's dependencies are resolved. Let's assign it to Fafard.", exec->get_cname());
71         exec->set_host(fafard);
72       } else {
73         XBT_INFO("Activity %s not ready.", exec->get_cname());
74       }
75     }
76     vetoed.clear(); // DON'T FORGET TO CLEAR this set between two calls to run
77   }
78
79   XBT_INFO("Simulation time %g", sg4::Engine::get_clock());
80
81   return 0;
82 }