Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1dd509a9410bfbb3f87ab49a4ab08a5178a75b9e
[simgrid.git] / examples / cpp / exec-failure / s4u-exec-failure.cpp
1 /* Copyright (c) 2021-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 /* This example shows how to serialize a set of communications going through a link
7  *
8  * As for the other asynchronous examples, the sender initiates all the messages it wants to send and
9  * pack the resulting simgrid::s4u::CommPtr objects in a vector.
10  * At the same time, the receiver starts receiving all messages asynchronously. Without serialization,
11  * all messages would be received at the same timestamp in the receiver.
12  *
13  * However, as they will be serialized in a link of the platform, the messages arrive 2 by 2.
14  *
15  * The sender then blocks until all ongoing communication terminate, using simgrid::s4u::Comm::wait_all()
16  */
17
18 #include <simgrid/s4u.hpp>
19 #include "simgrid/kernel/ProfileBuilder.hpp"
20
21 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_exec_failure, "Messages specific for this s4u example");
22 namespace sg4 = simgrid::s4u;
23
24 static void dispatcher(std::vector<sg4::Host*> const& hosts)
25 {
26   std::vector<sg4::ExecPtr> pending_execs;
27   for (auto* host: hosts) {
28     XBT_INFO("Initiating asynchronous exec on %s", host->get_cname());
29     // Computing 20 flops on an host which speed is 1f takes 20 seconds (when it does not fail)
30     auto exec = sg4::this_actor::exec_init(20)->set_host(host);
31     pending_execs.push_back(exec);
32     exec->start();
33   }
34
35   XBT_INFO("---------------------------------");
36   XBT_INFO("Wait on the first exec, which host is turned off at t=10 by the another actor.");
37   try {
38     pending_execs[0]->wait();
39     xbt_assert("This wait was not supposed to succeed.");
40   } catch (const simgrid::HostFailureException&) {
41     XBT_INFO("Dispatcher has experienced a host failure exception, so it knows that something went wrong.");
42   }
43
44   XBT_INFO("State of each exec:");
45   for (auto const& exec : pending_execs) 
46     XBT_INFO("  Exec on %s has state: %s", exec->get_host()->get_cname(), exec->get_state_str());
47
48   XBT_INFO("---------------------------------");
49   XBT_INFO("Wait on the second exec, which host is turned off at t=12 by the state profile.");
50   try {
51     pending_execs[1]->wait();
52     xbt_assert("This wait was not supposed to succeed.");
53   } catch (const simgrid::HostFailureException&) {
54     XBT_INFO("Dispatcher has experienced a host failure exception, so it knows that something went wrong.");
55   }
56   XBT_INFO("State of each exec:");
57   for (auto const& exec : pending_execs) 
58     XBT_INFO("  Exec on %s has state: %s", exec->get_host()->get_cname(), exec->get_state_str());
59
60   XBT_INFO("---------------------------------");
61   XBT_INFO("Wait on the third exec, which should succeed.");
62   try {
63     pending_execs[2]->wait();
64     XBT_INFO("No exception occured.");
65   } catch (const simgrid::HostFailureException&) {
66     xbt_assert("This wait was not supposed to fail.");
67   }
68   XBT_INFO("State of each exec:");
69   for (auto const& exec : pending_execs) 
70     XBT_INFO("  Exec on %s has state: %s", exec->get_host()->get_cname(), exec->get_state_str());
71 }
72
73 static void host_killer(sg4::Host* to_kill)
74 {
75   sg4::this_actor::sleep_for(10.0);
76   XBT_INFO("HostKiller turns off the host '%s'.", to_kill->get_cname());
77   to_kill->turn_off();
78 }
79
80 int main(int argc, char** argv)
81 {
82   sg4::Engine engine(&argc, argv);
83
84   auto* zone  = sg4::create_full_zone("world");
85   std::vector<sg4::Host*> hosts;
86   for (auto name : {"Host1", "Host2", "Host3"}) {
87     auto* host = zone->create_host(name, "1f");
88     hosts.push_back(host);
89   }
90   /* Attaching a state profile (ie a list of events changing the on/off state of the resource) to host3.
91    * The syntax of the profile (second parameter) is a list of: "date state\n"
92    *   The R"(   )" thing is the C++ way of writing multiline strings, including literals \n.
93    *     You'd have the same behavior by using "12 0\n20 1\n" instead.
94    *   So here, the link is turned off at t=12 and back on at t=20.
95    * The last parameter is the period of that profile, meaning that it loops after 30 seconds.
96    */
97   hosts[1]->set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("profile name", R"(
98 12 0
99 20 1
100 )",                                                                               30));
101
102   zone->seal();
103
104   sg4::Actor::create("Dispatcher", hosts[2], dispatcher, hosts);
105   sg4::Actor::create("HostKiller", hosts[2], host_killer, hosts[0]);
106
107   engine.run();
108
109   return 0;
110 }