Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5bad702acecf2c0b0f3272fdb33cbb9d2be6f0f4
[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([](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   simgrid::s4u::Exec::on_completion.connect([](simgrid::s4u::Exec const& exec) {
30     XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", exec.get_cname(), exec.get_start_time(),
31              exec.get_finish_time());
32   });
33
34   // Define an amount of work that should take 1 second to execute.
35   double computation_amount = fafard->get_speed();
36
37   // Create a small DAG: Two parents and a child
38   simgrid::s4u::ExecPtr first_parent  = simgrid::s4u::Exec::init();
39   simgrid::s4u::ExecPtr second_parent = simgrid::s4u::Exec::init();
40   simgrid::s4u::ExecPtr child         = simgrid::s4u::Exec::init();
41   first_parent->add_successor(child);
42   second_parent->add_successor(child);
43
44   // Set the parameters (the name is for logging purposes only)
45   // + First parent ends after 1 second and the Second parent after 2 seconds.
46   first_parent->set_name("parent 1")->set_flops_amount(computation_amount);
47   second_parent->set_name("parent 2")->set_flops_amount(2 * computation_amount);
48   child->set_name("child")->set_flops_amount(computation_amount);
49
50   // Only the parents are scheduled so far
51   first_parent->set_host(fafard);
52   second_parent->set_host(fafard);
53
54   // Start all activities that can actually start.
55   first_parent->vetoable_start();
56   second_parent->vetoable_start();
57   child->vetoable_start();
58
59   while (child->get_state() != simgrid::s4u::Activity::State::FINISHED) {
60     e.run();
61     for (auto* a : vetoed) {
62       auto* exec = static_cast<simgrid::s4u::Exec*>(a);
63
64       // In this simple case, we just assign the child task to a resource when its dependencies are solved
65       if (exec->dependencies_solved() && not exec->is_assigned()) {
66         XBT_INFO("Activity %s's dependencies are resolved. Let's assign it to Fafard.", exec->get_cname());
67         exec->set_host(fafard);
68       } else {
69         XBT_INFO("Activity %s not ready.", exec->get_cname());
70       }
71     }
72     vetoed.clear(); // DON'T FORGET TO CLEAR this set between two calls to run
73   }
74
75   XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());
76
77   return 0;
78 }