Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / examples / s4u / exec-dependent / s4u-exec-dependent.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 static void worker()
12 {
13   // Define an amount of work that should take 1 second to execute.
14   double computation_amount = simgrid::s4u::this_actor::get_host()->get_speed();
15
16   std::vector<simgrid::s4u::ExecPtr> pending_execs;
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   simgrid::s4u::ExecPtr first_parent  = simgrid::s4u::this_actor::exec_init(computation_amount);
21   pending_execs.push_back(first_parent);
22   simgrid::s4u::ExecPtr second_parent = simgrid::s4u::this_actor::exec_init(2 * computation_amount);
23   pending_execs.push_back(second_parent);
24   simgrid::s4u::ExecPtr child = simgrid::s4u::this_actor::exec_init(computation_amount);
25   pending_execs.push_back(child);
26
27   // Name the activities (for logging purposes only)
28   first_parent->set_name("parent 1");
29   second_parent->set_name("parent 2");
30   child->set_name("child");
31
32   // Create the dependencies by declaring 'child' as a successor of first_parent and second_parent
33   first_parent->add_successor(child);
34   second_parent->add_successor(child);
35
36   // Start the activities.
37   first_parent->start();
38   second_parent->start();
39   // child uses a vetoable start to force it to wait for the completion of its predecessors
40   child->vetoable_start();
41
42   // wait for the completion of all activities
43   while (not pending_execs.empty()) {
44     int changed_pos = simgrid::s4u::Exec::wait_any_for(&pending_execs, -1);
45     XBT_INFO("Exec '%s' is complete", pending_execs[changed_pos]->get_cname());
46     pending_execs.erase(pending_execs.begin() + changed_pos);
47   }
48 }
49
50 int main(int argc, char* argv[])
51 {
52   simgrid::s4u::Engine e(&argc, argv);
53   e.load_platform(argv[1]);
54
55   simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Fafard"), worker);
56
57   e.run();
58
59   XBT_INFO("Simulation time %g", e.get_clock());
60
61   return 0;
62 }