Logo AND Algorithmique Numérique Distribuée

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