Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use vector of strings for args.
[simgrid.git] / examples / s4u / platform-failures / s4u-platform-failures.cpp
1 /* Copyright (c) 2007-2020. 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
22 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
23
24 static void master(std::vector<std::string> args)
25 {
26   xbt_assert(args.size() == 5, "Expecting one parameter");
27
28   simgrid::s4u::Mailbox* mailbox;
29   long number_of_tasks = std::stol(args[1]);
30   double comp_size     = std::stod(args[2]);
31   long comm_size       = std::stol(args[3]);
32   long workers_count   = std::stol(args[4]);
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     auto* 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 (const simgrid::TimeoutException&) {
44       delete payload;
45       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
46     } catch (const simgrid::NetworkFailureException&) {
47       delete payload;
48       XBT_INFO("Mmh. The communication with '%s' failed. Nevermind. Let's keep going!", mailbox->get_cname());
49     }
50   }
51
52   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
53   for (int i = 0; i < workers_count; i++) {
54     /* - Eventually tell all the workers to stop by sending a "finalize" task */
55     mailbox         = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i));
56     auto* payload   = new double(-1.0);
57     try {
58       mailbox->put(payload, 0, 1.0);
59     } catch (const simgrid::TimeoutException&) {
60       delete payload;
61       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
62     } catch (const simgrid::NetworkFailureException&) {
63       delete payload;
64       XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
65     }
66   }
67
68   XBT_INFO("Goodbye now!");
69 }
70
71 static void worker(std::vector<std::string> args)
72 {
73   xbt_assert(args.size() == 2, "Expecting one parameter");
74   long id                          = std::stol(args[1]);
75   simgrid::s4u::Mailbox* mailbox   = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(id));
76   while (true) {
77     try {
78       XBT_INFO("Waiting a message on %s", mailbox->get_cname());
79       const auto* payload = static_cast<double*>(mailbox->get());
80       xbt_assert(payload != nullptr, "mailbox->get() failed");
81       double comp_size = *payload;
82       delete payload;
83       if (comp_size < 0) { /* - Exit when -1.0 is received */
84         XBT_INFO("I'm done. See you!");
85         break;
86       }
87       /*  - Otherwise, process the task */
88       XBT_INFO("Start execution...");
89       simgrid::s4u::this_actor::execute(comp_size);
90       XBT_INFO("Execution complete.");
91     } catch (const simgrid::NetworkFailureException&) {
92       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
93     }
94   }
95 }
96
97 int main(int argc, char* argv[])
98 {
99   simgrid::s4u::Engine e(&argc, argv);
100   e.load_platform(argv[1]);
101   e.register_function("master", master);
102   e.register_function("worker", worker);
103   e.load_deployment(argv[2]);
104
105   e.run();
106
107   XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());
108   return 0;
109 }