Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into master
[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/str.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
10
11 static int master(int argc, char* argv[])
12 {
13   xbt_assert(argc == 5, "Expecting one parameter");
14
15   simgrid::s4u::MailboxPtr mailbox;
16   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
17   double comp_size     = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
18   double comm_size     = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
19   long workers_count   = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");
20
21   XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
22
23   for (int i = 0; i < number_of_tasks; i++) {
24     mailbox         = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i % workers_count));
25     double* payload = new double(comp_size);
26     try {
27       XBT_INFO("Send a message to %s", mailbox->get_cname());
28       mailbox->put(payload, comm_size, 10.0);
29       XBT_INFO("Send to %s completed", mailbox->get_cname());
30     } catch (simgrid::HostFailureException& e) {
31       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
32       return -1;
33     } catch (simgrid::TimeoutError& e) {
34       delete payload;
35       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
36     } catch (xbt_ex& e) {
37       switch (e.category) {
38         case network_error:
39           XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
40           break;
41         default:
42           xbt_die("Unexpected behavior");
43       }
44       delete payload;
45     }
46   }
47
48   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
49   for (int i = 0; i < workers_count; i++) {
50     /* - Eventually tell all the workers to stop by sending a "finalize" task */
51     mailbox         = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i));
52     double* payload = new double(-1.0);
53     try {
54       mailbox->put(payload, 0, 1.0);
55     } catch (simgrid::HostFailureException& e) {
56       delete payload;
57       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
58       return -1;
59     } catch (simgrid::TimeoutError& e) {
60       delete payload;
61       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
62     } catch (xbt_ex& e) {
63       delete payload;
64       switch (e.category) {
65         case network_error:
66           XBT_INFO("Mmh. Something went wrong with '%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       XBT_INFO("Waiting a message on %s", mailbox->get_cname());
88       payload   = static_cast<double*>(mailbox->get());
89       comp_size = *payload;
90       xbt_assert(payload != nullptr, "mailbox->get() failed");
91       if (comp_size < 0) { /* - Exit when -1.0 is received */
92         XBT_INFO("I'm done. See you!");
93         delete payload;
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         delete payload;
102       } catch (simgrid::HostFailureException& e) {
103         delete payload;
104         XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
105         return -1;
106       }
107     } catch (simgrid::HostFailureException& e) {
108       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
109       delete payload;
110       return -1;
111     } catch (xbt_ex& e) {
112       switch (e.category) {
113         case network_error:
114           XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
115           break;
116         default:
117           xbt_die("Unexpected behavior. Category: %s", xbt_ex_catname(e.category));
118       }
119     }
120   }
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 }