1 /* Copyright (c) 2010-2018. 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 "xbt/sysdep.h"
7 #include <simgrid/s4u.hpp>
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this s4u example");
12 static void master(std::vector<std::string> args)
14 xbt_assert(args.size() == 5, "The master function expects 4 arguments from the XML deployment file");
16 long number_of_tasks = std::stol(args[1]);
17 double comp_size = std::stod(args[2]); /* - Task compute cost */
18 double comm_size = std::stod(args[3]); /* - Task communication size */
19 long workers_count = std::stol(args[4]);
21 XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
23 simgrid::s4u::MailboxPtr mailbox = nullptr;
25 for (int i = 0; i < number_of_tasks; i++) { /* For each task to be executed: */
26 /* - Select a @ref worker in a round-robin way */
27 mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i % workers_count));
29 if (number_of_tasks < 10000 || (number_of_tasks < 100000 && i % 10000 == 0) || i % 100000 == 0)
30 XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", (std::string("Task_") + std::to_string(i)).c_str(),
31 number_of_tasks, mailbox->get_cname());
33 /* - Send the computation amount to the @ref worker */
34 mailbox->put(new double(comp_size), comm_size);
37 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
38 for (int i = 0; i < workers_count; i++) {
39 /* - Eventually tell all the workers to stop by sending a "finalize" task */
40 mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i % workers_count));
41 mailbox->put(new double(-1.0), 0);
45 static void worker(std::vector<std::string> args)
47 xbt_assert(args.size() == 2, "The worker expects a single argument from the XML deployment file: "
48 "its worker ID (its numerical rank)");
49 long id = std::stol(args[1]);
50 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(id));
52 while (1) { /* The worker waits in an infinite loop for tasks sent by the @ref master */
53 double* task = static_cast<double*>(mailbox->get());
54 xbt_assert(task != nullptr, "mailbox->get() failed");
55 double comp_size = *task;
57 if (comp_size < 0) { /* - Exit when -1.0 is received */
58 XBT_INFO("I'm done. See you!");
61 /* - Otherwise, process the task */
62 simgrid::s4u::this_actor::execute(comp_size);
66 int main(int argc, char* argv[])
68 simgrid::s4u::Engine e(&argc, argv);
70 "Usage: %s platform_file deployment_file\n"
71 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
74 e.load_platform(argv[1]); /* Load the platform description */
75 e.register_function("master", &master);
76 e.register_function("worker", &worker);
77 e.load_deployment(argv[2]); /* Deploy the application */
79 e.run(); /** - Run the simulation */
81 XBT_INFO("Simulation time %g", e.get_clock());