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 #include "simgrid/s4u.hpp"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
11 static int master(int argc, char* argv[])
13 xbt_assert(argc == 5, "Expecting one parameter");
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");
21 XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
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);
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!");
33 } catch (simgrid::TimeoutError& e) {
35 XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
37 if (e.category != network_error)
38 xbt_die("Unexpected behavior");
39 XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
44 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
45 for (int i = 0; i < workers_count; i++) {
46 /* - Eventually tell all the workers to stop by sending a "finalize" task */
47 mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i));
48 double* payload = new double(-1.0);
50 mailbox->put(payload, 0, 1.0);
51 } catch (simgrid::HostFailureException& e) {
53 XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
55 } catch (simgrid::TimeoutError& e) {
57 XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
60 if (e.category != network_error)
61 xbt_die("Unexpected behavior");
62 XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox->get_cname());
66 XBT_INFO("Goodbye now!");
70 static int worker(int argc, char* argv[])
72 xbt_assert(argc == 2, "Expecting one parameter");
73 long id = xbt_str_parse_int(argv[1], "Invalid argument %s");
74 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(id));
75 double* payload = nullptr;
76 double comp_size = -1;
79 XBT_INFO("Waiting a message on %s", mailbox->get_cname());
80 payload = static_cast<double*>(mailbox->get());
81 xbt_assert(payload != nullptr, "mailbox->get() failed");
83 if (comp_size < 0) { /* - Exit when -1.0 is received */
84 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.");
93 } catch (simgrid::HostFailureException& e) {
94 XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
98 if (e.category != network_error)
99 xbt_die("Unexpected behavior. Category: %s", xbt_ex_catname(e.category));
100 XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
106 int main(int argc, char* argv[])
108 simgrid::s4u::Engine e(&argc, argv);
109 e.load_platform(argv[1]);
110 e.register_function("master", master);
111 e.register_function("worker", worker);
112 e.load_deployment(argv[2]);
116 XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());