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");
13 long number_of_tasks = 0; /* - Number of tasks */
14 double comp_size = 0; /* - Task compute cost */
15 double comm_size = 0; /* - Task communication size */
16 long workers_count = 0; /* - Number of workers */
17 simgrid::s4u::MailboxPtr mailbox = nullptr;
20 explicit Master(std::vector<std::string> args)
22 xbt_assert(args.size() == 5, "The master function expects 4 arguments from the XML deployment file");
24 number_of_tasks = std::stol(args[1]);
25 comp_size = std::stod(args[2]);
26 comm_size = std::stod(args[3]);
27 workers_count = std::stol(args[4]);
29 XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
34 for (int i = 0; i < number_of_tasks; i++) { /* For each task to be executed: */
35 /* - Select a @ref worker in a round-robin way */
36 mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i % workers_count));
38 if (number_of_tasks < 10000 || (number_of_tasks < 100000 && i % 10000 == 0) || i % 100000 == 0)
39 XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", (std::string("Task_") + std::to_string(i)).c_str(),
40 number_of_tasks, mailbox->get_cname());
42 /* - Send the computation amount to the @ref worker */
43 mailbox->put(new double(comp_size), comm_size);
46 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
47 for (int i = 0; i < workers_count; i++) {
48 /* - Eventually tell all the workers to stop by sending a "finalize" task */
49 mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i % workers_count));
50 mailbox->put(new double(-1.0), 0);
57 simgrid::s4u::MailboxPtr mailbox = nullptr;
60 explicit Worker(std::vector<std::string> args)
62 xbt_assert(args.size() == 2, "The worker expects a single argument from the XML deployment file: "
63 "its worker ID (its numerical rank)");
64 id = std::stol(args[1]);
65 mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(id));
70 while (1) { /* The worker waits in an infinite loop for tasks sent by the @ref master */
71 double* task = static_cast<double*>(mailbox->get());
72 xbt_assert(task != nullptr, "mailbox->get() failed");
73 double comp_size = *task;
75 if (comp_size < 0) { /* - Exit when -1.0 is received */
76 XBT_INFO("I'm done. See you!");
79 /* - Otherwise, process the task */
80 simgrid::s4u::this_actor::execute(comp_size);
85 int main(int argc, char* argv[])
87 simgrid::s4u::Engine e(&argc, argv);
88 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
89 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
92 e.load_platform(argv[1]); /* Load the platform description */
93 e.register_actor<Master>("master"); /* Register the class representing the actors */
94 e.register_actor<Worker>("worker");
95 e.load_deployment(argv[2]); /* Deploy the application */
97 e.run(); /** - Run the simulation */
99 XBT_INFO("Simulation time %g", e.get_clock());