Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
no dangling else
[simgrid.git] / examples / cpp / dag-failure / s4u-dag-failure.cpp
1 /* Copyright (c) 2006-2021. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/s4u.hpp"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(dag_failure, "Logging specific to this example");
10
11 int main(int argc, char** argv)
12 {
13   simgrid::s4u::Engine e(&argc, argv);
14   simgrid::s4u::Engine::set_config("host/model:ptask_L07");
15   e.load_platform(argv[1]);
16
17   auto* faulty = e.host_by_name("Faulty Host");
18   auto* safe   = e.host_by_name("Safe Host");
19   simgrid::s4u::Exec::on_completion.connect([](simgrid::s4u::Exec const& exec) {
20     if (exec.get_state() == simgrid::s4u::Activity::State::FINISHED)
21       XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", exec.get_cname(), exec.get_start_time(),
22                exec.get_finish_time());
23     if (exec.get_state() == simgrid::s4u::Activity::State::FAILED) {
24       if (exec.is_parallel())
25         XBT_INFO("Activity '%s' has failed. %.f %% remain to be done", exec.get_cname(),
26                  100 * exec.get_remaining_ratio());
27       else
28         XBT_INFO("Activity '%s' has failed. %.f flops remain to be done", exec.get_cname(), exec.get_remaining());
29     }
30   });
31
32   /* creation of a single Exec that will poorly fail when the workstation will stop */
33   XBT_INFO("First test: sequential Exec activity");
34   simgrid::s4u::ExecPtr exec =
35       simgrid::s4u::Exec::init()->set_name("Poor task")->set_flops_amount(2e10)->vetoable_start();
36
37   XBT_INFO("Schedule Activity '%s' on 'Faulty Host'", exec->get_cname());
38   exec->set_host(faulty);
39
40   /* Add a child Exec that depends on the Poor task' */
41   simgrid::s4u::ExecPtr child = simgrid::s4u::Exec::init()->set_name("Child")->set_flops_amount(2e10)->set_host(safe);
42   exec->add_successor(child);
43   child->vetoable_start();
44
45   XBT_INFO("Run the simulation");
46   e.run();
47
48   XBT_INFO("let's unschedule Activity '%s' and reschedule it on the 'Safe Host'", exec->get_cname());
49   exec->unset_host();
50   exec->set_host(safe);
51
52   XBT_INFO("Run the simulation again");
53   e.run();
54
55   XBT_INFO("Second test: parallel Exec activity");
56   exec = simgrid::s4u::Exec::init()->set_name("Poor parallel task")->set_flops_amounts({2e10, 2e10})->vetoable_start();
57
58   XBT_INFO("Schedule Activity '%s' on 'Safe Host' and 'Faulty Host'", exec->get_cname());
59   exec->set_hosts({safe, faulty});
60
61   /* Add a child Exec that depends on the Poor task' */
62   child = simgrid::s4u::Exec::init()->set_name("Child")->set_flops_amount(2e10)->set_host(safe);
63   exec->add_successor(child);
64   child->vetoable_start();
65
66   XBT_INFO("Run the simulation");
67   e.run();
68
69   XBT_INFO("let's unschedule Activity '%s' and reschedule it only on the 'Safe Host'", exec->get_cname());
70   exec->unset_host();
71   exec->set_flops_amount(4e10)->set_host(safe);
72
73   XBT_INFO("Run the simulation again");
74   e.run();
75
76   return 0;
77 }