Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[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 #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::TimeoutException&) {
45       delete payload;
46       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
47     } catch (const simgrid::NetworkFailureException&) {
48       delete payload;
49       XBT_INFO("Mmh. The communication with '%s' failed. Nevermind. Let's keep going!", mailbox->get_cname());
50     }
51   }
52
53   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
54   for (int i = 0; i < workers_count; i++) {
55     /* - Eventually tell all the workers to stop by sending a "finalize" task */
56     mailbox         = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i));
57     double* payload = new double(-1.0);
58     try {
59       mailbox->put(payload, 0, 1.0);
60     } catch (const simgrid::TimeoutException&) {
61       delete payload;
62       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
63     } catch (const simgrid::NetworkFailureException&) {
64       delete payload;
65       XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
66     }
67   }
68
69   XBT_INFO("Goodbye now!");
70   return 0;
71 }
72
73 static int worker(int argc, char* argv[])
74 {
75   xbt_assert(argc == 2, "Expecting one parameter");
76   long id                          = xbt_str_parse_int(argv[1], "Invalid argument %s");
77   simgrid::s4u::Mailbox* mailbox   = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(id));
78   const double* payload            = nullptr;
79   double comp_size                 = -1;
80   while (1) {
81     try {
82       XBT_INFO("Waiting a message on %s", mailbox->get_cname());
83       payload   = static_cast<double*>(mailbox->get());
84       xbt_assert(payload != nullptr, "mailbox->get() failed");
85       comp_size = *payload;
86       delete payload;
87       if (comp_size < 0) { /* - Exit when -1.0 is received */
88         XBT_INFO("I'm done. See you!");
89         break;
90       }
91       /*  - Otherwise, process the task */
92       XBT_INFO("Start execution...");
93       simgrid::s4u::this_actor::execute(comp_size);
94       XBT_INFO("Execution complete.");
95     } catch (const simgrid::NetworkFailureException&) {
96       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
97     }
98   }
99   return 0;
100 }
101
102 int main(int argc, char* argv[])
103 {
104   simgrid::s4u::Engine e(&argc, argv);
105   e.load_platform(argv[1]);
106   e.register_function("master", master);
107   e.register_function("worker", worker);
108   e.load_deployment(argv[2]);
109
110   e.run();
111
112   XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());
113   return 0;
114 }