1 /* Copyright (c) 2007-2021. 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 a 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, as exemplified in the main() below. Once this profile is in place, the resource will automatically
11 * be turned on and off.
13 * The actors running on a host that is turned off are forcefully killed
14 * once their on_exit callbacks are executed. They cannot avoid this fate.
15 * Since we specified on_failure="RESTART" for each actors in the XML file,
16 * they will be automatically restarted when the host starts again.
18 * Communications using failed links will .. fail.
21 #include "simgrid/kernel/ProfileBuilder.hpp"
22 #include "simgrid/s4u.hpp"
24 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
26 static void master(std::vector<std::string> args)
28 xbt_assert(args.size() == 5, "Expecting one parameter");
30 simgrid::s4u::Mailbox* mailbox;
31 long number_of_tasks = std::stol(args[1]);
32 double comp_size = std::stod(args[2]);
33 long comm_size = std::stol(args[3]);
34 long workers_count = std::stol(args[4]);
36 XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
38 for (int i = 0; i < number_of_tasks; i++) {
39 mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i % workers_count));
40 auto* payload = new double(comp_size);
42 XBT_INFO("Send a message to %s", mailbox->get_cname());
43 mailbox->put(payload, comm_size, 10.0);
44 XBT_INFO("Send to %s completed", mailbox->get_cname());
45 } catch (const simgrid::TimeoutException&) {
47 XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
48 } catch (const simgrid::NetworkFailureException&) {
50 XBT_INFO("Mmh. The communication with '%s' failed. Nevermind. Let's keep going!", mailbox->get_cname());
54 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
55 for (int i = 0; i < workers_count; i++) {
56 /* - Eventually tell all the workers to stop by sending a "finalize" task */
57 mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i));
58 auto* payload = new double(-1.0);
60 mailbox->put(payload, 0, 1.0);
61 } catch (const simgrid::TimeoutException&) {
63 XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
64 } catch (const simgrid::NetworkFailureException&) {
66 XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
70 XBT_INFO("Goodbye now!");
73 static void worker(std::vector<std::string> args)
75 xbt_assert(args.size() == 2, "Expecting one parameter");
76 long id = std::stol(args[1]);
77 simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(id));
80 XBT_INFO("Waiting a message on %s", mailbox->get_cname());
81 auto payload = mailbox->get_unique<double>();
82 xbt_assert(payload != nullptr, "mailbox->get() failed");
83 double comp_size = *payload;
84 if (comp_size < 0) { /* - Exit when -1.0 is received */
85 XBT_INFO("I'm done. See you!");
88 /* - Otherwise, process the task */
89 XBT_INFO("Start execution...");
90 simgrid::s4u::this_actor::execute(comp_size);
91 XBT_INFO("Execution complete.");
92 } catch (const simgrid::NetworkFailureException&) {
93 XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
98 int main(int argc, char* argv[])
100 simgrid::s4u::Engine e(&argc, argv);
102 // This is how to attach a profile to an host that is created from the XML file.
103 // This should be done before calling load_platform(), as the on_creation() event is fired when loading the platform.
104 // You can never set a new profile to a resource that already have one.
105 simgrid::s4u::Host::on_creation.connect([](simgrid::s4u::Host& h) {
106 if (h.get_name() == "Bourrassa") {
107 h.set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("bourassa_profile", "67 0\n70 1\n", 0));
110 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());