Logo AND Algorithmique Numérique Distribuée

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