Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / examples / cpp / platform-failures / s4u-platform-failures.cpp
1 /* Copyright (c) 2007-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 work with the state profile of a 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, as exemplified in the main() below. Once this profile is in place, the resource will automatically
11  * be turned on and off.
12  *
13  * The actors running on a host that is turned off are forcefully killed
14  * once their on_exit callbacks are executed. They cannot avoid this fate.
15  * Since we specified on_failure="RESTART" for each actors in the XML file,
16  * they will be automatically restarted when the host starts again.
17  *
18  * Communications using failed links will .. fail.
19  */
20
21 #include "simgrid/kernel/ProfileBuilder.hpp"
22 #include "simgrid/s4u.hpp"
23
24 namespace sg4 = simgrid::s4u;
25
26 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
27
28 static void master(std::vector<std::string> args)
29 {
30   xbt_assert(args.size() == 5, "Expecting one parameter");
31
32   sg4::Mailbox* mailbox;
33   long number_of_tasks = std::stol(args[1]);
34   double comp_size     = std::stod(args[2]);
35   long comm_size       = std::stol(args[3]);
36   long workers_count   = std::stol(args[4]);
37
38   XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
39
40   for (int i = 0; i < number_of_tasks; i++) {
41     mailbox         = sg4::Mailbox::by_name("worker-" + std::to_string(i % workers_count));
42     auto* payload   = new double(comp_size);
43     try {
44       XBT_INFO("Send a message to %s", mailbox->get_cname());
45       mailbox->put(payload, comm_size, 10.0);
46       XBT_INFO("Send to %s completed", mailbox->get_cname());
47     } catch (const simgrid::TimeoutException&) {
48       delete payload;
49       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
50     } catch (const simgrid::NetworkFailureException&) {
51       delete payload;
52       XBT_INFO("Mmh. The communication with '%s' failed. Nevermind. Let's keep going!", mailbox->get_cname());
53     }
54   }
55
56   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
57   for (int i = 0; i < workers_count; i++) {
58     /* - Eventually tell all the workers to stop by sending a "finalize" task */
59     mailbox         = sg4::Mailbox::by_name("worker-" + std::to_string(i));
60     auto* payload   = new double(-1.0);
61     try {
62       mailbox->put(payload, 0, 1.0);
63     } catch (const simgrid::TimeoutException&) {
64       delete payload;
65       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
66     } catch (const simgrid::NetworkFailureException&) {
67       delete payload;
68       XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
69     }
70   }
71
72   XBT_INFO("Goodbye now!");
73 }
74
75 static void worker(std::vector<std::string> args)
76 {
77   xbt_assert(args.size() == 2, "Expecting one parameter");
78   long id               = std::stol(args[1]);
79   sg4::Mailbox* mailbox = sg4::Mailbox::by_name("worker-" + std::to_string(id));
80   while (true) {
81     try {
82       XBT_INFO("Waiting a message on %s", mailbox->get_cname());
83       auto payload = mailbox->get_unique<double>();
84       xbt_assert(payload != nullptr, "mailbox->get() failed");
85       double comp_size = *payload;
86       if (comp_size < 0) { /* - Exit when -1.0 is received */
87         XBT_INFO("I'm done. See you!");
88         break;
89       }
90       /*  - Otherwise, process the task */
91       XBT_INFO("Start execution...");
92       sg4::this_actor::execute(comp_size);
93       XBT_INFO("Execution complete.");
94     } catch (const simgrid::NetworkFailureException&) {
95       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
96     }
97   }
98 }
99
100 int main(int argc, char* argv[])
101 {
102   sg4::Engine e(&argc, argv);
103
104   // This is how to attach a profile to an host that is created from the XML file.
105   // This should be done before calling load_platform(), as the on_creation() event is fired when loading the platform.
106   // You can never set a new profile to a resource that already have one.
107   sg4::Host::on_creation_cb([](sg4::Host& h) {
108     if (h.get_name() == "Bourrassa") {
109       h.set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("bourassa_profile", "67 0\n70 1\n", 0));
110     }
111   });
112   e.load_platform(argv[1]);
113
114   e.register_function("master", master);
115   e.register_function("worker", worker);
116   e.load_deployment(argv[2]);
117
118   // Add a new host programatically, and attach a state profile to it
119   auto* root     = e.get_netzone_root();
120   auto* lilibeth = root->create_host("Lilibeth", 1e15);
121   auto link      = e.link_by_name("10");
122   root->add_route(e.host_by_name("Tremblay"), lilibeth, {link});
123   lilibeth->set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("lilibeth_profile", R"(
124 4 0
125 5 1
126 )",
127                                                                                     10));
128   lilibeth->seal();
129
130   // Create an actor on that new host, to monitor its own state
131   auto actor = sg4::Actor::create("sleeper", lilibeth, []() {
132     XBT_INFO("Start sleeping...");
133     sg4::this_actor::sleep_for(1);
134     XBT_INFO("done sleeping.");
135   });
136   actor->set_auto_restart(true);
137
138   e.run();
139
140   XBT_INFO("Simulation time %g", sg4::Engine::get_clock());
141   return 0;
142 }