Logo AND Algorithmique Numérique Distribuée

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