Logo AND Algorithmique Numérique Distribuée

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