1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved. */
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. */
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.
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.
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.
17 * Communications using failed links will .. fail.
20 #include "simgrid/s4u.hpp"
23 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
25 static int master(int argc, char* argv[])
27 xbt_assert(argc == 5, "Expecting one parameter");
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");
35 XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
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);
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::TimeoutError&) {
46 XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
48 if (e.category != network_error)
49 xbt_die("Unexpected behavior");
50 XBT_INFO("Mmh. The communication with '%s' failed. Nevermind. Let's keep going!", mailbox->get_cname());
55 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
56 for (int i = 0; i < workers_count; i++) {
57 /* - Eventually tell all the workers to stop by sending a "finalize" task */
58 mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i));
59 double* payload = new double(-1.0);
61 mailbox->put(payload, 0, 1.0);
62 } catch (const simgrid::TimeoutError&) {
64 XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
67 if (e.category != network_error)
68 xbt_die("Unexpected behavior");
69 XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
73 XBT_INFO("Goodbye now!");
77 static int worker(int argc, char* argv[])
79 xbt_assert(argc == 2, "Expecting one parameter");
80 long id = xbt_str_parse_int(argv[1], "Invalid argument %s");
81 simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(id));
82 double* payload = nullptr;
83 double comp_size = -1;
86 XBT_INFO("Waiting a message on %s", mailbox->get_cname());
87 payload = static_cast<double*>(mailbox->get());
88 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!");
95 /* - Otherwise, process the task */
96 XBT_INFO("Start execution...");
97 simgrid::s4u::this_actor::execute(comp_size);
98 XBT_INFO("Execution complete.");
100 if (e.category != network_error)
101 xbt_die("Unexpected behavior. Category: %s", xbt_ex_catname(e.category));
102 XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
108 int main(int argc, char* argv[])
110 simgrid::s4u::Engine e(&argc, argv);
111 e.load_platform(argv[1]);
112 e.register_function("master", master);
113 e.register_function("worker", worker);
114 e.load_deployment(argv[2]);
118 XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());