Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't give actors a chance to survive their exec if their host is turned off
[simgrid.git] / examples / s4u / platform-failures / s4u-platform-failures.cpp
1 /* Copyright (c) 2007-2019. 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 work with the state profile of an host or a link,
7  * specifying when the resource must be turned on or off.
8  *
9  * To set such a profile, the first way is to use a file in the XML, while the second is to use the programmatic
10  * interface. Once this profile is in place, the resource will automatically be turned on and off.
11  *
12  * The actors running on an host that is turned off will receive a simgrid::HostFailureException. Since we specified
13  * on_failure="RESTART" for each actors in the XML file, they will be automatically restarted when the host starts
14  * again.
15  *
16  * Communications using failed links will .. fail.
17  */
18
19 #include "simgrid/s4u.hpp"
20 #include "xbt/str.h"
21
22 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
23
24 static int master(int argc, char* argv[])
25 {
26   xbt_assert(argc == 5, "Expecting one parameter");
27
28   simgrid::s4u::MailboxPtr mailbox;
29   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
30   double comp_size     = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
31   double comm_size     = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
32   long workers_count   = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");
33
34   XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
35
36   for (int i = 0; i < number_of_tasks; i++) {
37     mailbox         = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i % workers_count));
38     double* payload = new double(comp_size);
39     try {
40       XBT_INFO("Send a message to %s", mailbox->get_cname());
41       mailbox->put(payload, comm_size, 10.0);
42       XBT_INFO("Send to %s completed", mailbox->get_cname());
43     } catch (simgrid::HostFailureException& e) {
44       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
45       return -1;
46     } catch (simgrid::TimeoutError& e) {
47       delete payload;
48       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
49     } catch (xbt_ex& e) {
50       if (e.category != network_error)
51         xbt_die("Unexpected behavior");
52       XBT_INFO("Mmh. The communication with '%s' failed. Nevermind. Let's keep going!", mailbox->get_cname());
53       delete payload;
54     }
55   }
56
57   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
58   for (int i = 0; i < workers_count; i++) {
59     /* - Eventually tell all the workers to stop by sending a "finalize" task */
60     mailbox         = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i));
61     double* payload = new double(-1.0);
62     try {
63       mailbox->put(payload, 0, 1.0);
64     } catch (simgrid::HostFailureException& e) {
65       delete payload;
66       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
67       return -1;
68     } catch (simgrid::TimeoutError& e) {
69       delete payload;
70       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
71     } catch (xbt_ex& e) {
72       delete payload;
73       if (e.category != network_error)
74         xbt_die("Unexpected behavior");
75       XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
76     }
77   }
78
79   XBT_INFO("Goodbye now!");
80   return 0;
81 }
82
83 static int worker(int argc, char* argv[])
84 {
85   xbt_assert(argc == 2, "Expecting one parameter");
86   long id                          = xbt_str_parse_int(argv[1], "Invalid argument %s");
87   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(id));
88   double* payload                  = nullptr;
89   double comp_size                 = -1;
90   while (1) {
91     try {
92       XBT_INFO("Waiting a message on %s", mailbox->get_cname());
93       payload   = static_cast<double*>(mailbox->get());
94       xbt_assert(payload != nullptr, "mailbox->get() failed");
95       comp_size = *payload;
96       delete payload;
97       if (comp_size < 0) { /* - Exit when -1.0 is received */
98         XBT_INFO("I'm done. See you!");
99         break;
100       }
101       /*  - Otherwise, process the task */
102       XBT_INFO("Start execution...");
103       simgrid::s4u::this_actor::execute(comp_size);
104       XBT_INFO("Execution complete.");
105     } catch (simgrid::HostFailureException& e) {
106       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
107       delete payload;
108       return -1;
109     } catch (xbt_ex& e) {
110       if (e.category != network_error)
111         xbt_die("Unexpected behavior. Category: %s", xbt_ex_catname(e.category));
112       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
113     }
114   }
115   return 0;
116 }
117
118 int main(int argc, char* argv[])
119 {
120   simgrid::s4u::Engine e(&argc, argv);
121   e.load_platform(argv[1]);
122   e.register_function("master", master);
123   e.register_function("worker", worker);
124   e.load_deployment(argv[2]);
125
126   e.run();
127
128   XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());
129   return 0;
130 }