Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into jbod
[simgrid.git] / examples / cpp / exec-dependent / s4u-exec-dependent.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 static void worker()
13 {
14   // Define an amount of work that should take 1 second to execute.
15   double computation_amount = sg4::this_actor::get_host()->get_speed();
16
17   // Create a small DAG
18   // + Two parents and a child
19   // + First parent ends after 1 second and the Second parent after 2 seconds.
20   sg4::ExecPtr first_parent = sg4::this_actor::exec_init(computation_amount);
21   sg4::ExecPtr second_parent = sg4::this_actor::exec_init(2 * computation_amount);
22   sg4::ExecPtr child = sg4::Exec::init()->set_flops_amount(computation_amount);
23
24   sg4::ActivitySet pending_execs ({first_parent, second_parent, child});
25
26   // Name the activities (for logging purposes only)
27   first_parent->set_name("parent 1");
28   second_parent->set_name("parent 2");
29   child->set_name("child");
30
31   // Create the dependencies by declaring 'child' as a successor of first_parent and second_parent
32   first_parent->add_successor(child);
33   second_parent->add_successor(child);
34
35   // Start the activities.
36   first_parent->start();
37   second_parent->start();
38   child->start();
39
40   // wait for the completion of all activities
41   while (not pending_execs.empty()) {
42     auto completed_one = pending_execs.wait_any();
43     if (completed_one != nullptr)
44       XBT_INFO("Exec '%s' is complete", completed_one->get_cname());
45   }
46 }
47
48 int main(int argc, char* argv[])
49 {
50   sg4::Engine e(&argc, argv);
51   e.load_platform(argv[1]);
52
53   sg4::Actor::create("worker", e.host_by_name("Fafard"), worker);
54
55   sg4::Exec::on_veto_cb([&e](sg4::Exec& exec) {
56     // First display the situation
57     XBT_INFO("Activity '%s' vetoed. Dependencies: %s; Ressources: %s", exec.get_cname(),
58              (exec.dependencies_solved() ? "solved" : "NOT solved"),
59              (exec.is_assigned() ? "assigned" : "NOT assigned"));
60
61     // In this simple case, we just assign the child task to a resource when its dependencies are solved
62     if (exec.dependencies_solved() && not exec.is_assigned()) {
63       XBT_INFO("Activity %s's dependencies are resolved. Let's assign it to Fafard.", exec.get_cname());
64       exec.set_host(e.host_by_name("Fafard"));
65     }
66   });
67
68   e.run();
69
70   XBT_INFO("Simulation time %g", sg4::Engine::get_clock());
71
72   return 0;
73 }