Logo AND Algorithmique Numérique Distribuée

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