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::TimeoutException&) {
46 XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
47 } catch (const simgrid::NetworkFailureException&) {
49 XBT_INFO("Mmh. The communication with '%s' failed. Nevermind. Let's keep going!", mailbox->get_cname());
53 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
54 for (int i = 0; i < workers_count; i++) {
55 /* - Eventually tell all the workers to stop by sending a "finalize" task */
56 mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i));
57 double* payload = new double(-1.0);
59 mailbox->put(payload, 0, 1.0);
60 } catch (const simgrid::TimeoutException&) {
62 XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
63 } catch (const simgrid::NetworkFailureException&) {
65 XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
69 XBT_INFO("Goodbye now!");
73 static int worker(int argc, char* argv[])
75 xbt_assert(argc == 2, "Expecting one parameter");
76 long id = xbt_str_parse_int(argv[1], "Invalid argument %s");
77 simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(id));
78 double* payload = nullptr;
79 double comp_size = -1;
82 XBT_INFO("Waiting a message on %s", mailbox->get_cname());
83 payload = static_cast<double*>(mailbox->get());
84 xbt_assert(payload != nullptr, "mailbox->get() failed");
87 if (comp_size < 0) { /* - Exit when -1.0 is received */
88 XBT_INFO("I'm done. See you!");
91 /* - Otherwise, process the task */
92 XBT_INFO("Start execution...");
93 simgrid::s4u::this_actor::execute(comp_size);
94 XBT_INFO("Execution complete.");
95 } catch (const simgrid::NetworkFailureException&) {
96 XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
102 int main(int argc, char* argv[])
104 simgrid::s4u::Engine e(&argc, argv);
105 e.load_platform(argv[1]);
106 e.register_function("master", master);
107 e.register_function("worker", worker);
108 e.load_deployment(argv[2]);
112 XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());