1 /* Copyright (c) 2010-2017. 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. */
7 #include "xbt/sysdep.h"
8 #include <simgrid/s4u.hpp>
11 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this s4u example");
14 long number_of_tasks = 0; /* - Number of tasks */
15 double comp_size = 0; /* - Task compute cost */
16 double comm_size = 0; /* - Task communication size */
17 long workers_count = 0; /* - Number of workers */
18 simgrid::s4u::MailboxPtr mailbox = nullptr;
21 explicit Master(std::vector<std::string> args)
23 xbt_assert(args.size() == 5, "The master function expects 4 arguments from the XML deployment file");
25 number_of_tasks = std::stol(args[1]);
26 comp_size = std::stod(args[2]);
27 comm_size = std::stod(args[3]);
28 workers_count = std::stol(args[4]);
30 XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
35 for (int i = 0; i < number_of_tasks; i++) { /* For each task to be executed: */
36 /* - Select a @ref worker in a round-robin way */
37 mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(i % workers_count));
39 if (number_of_tasks < 10000 || i % 10000 == 0)
40 XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", (std::string("Task_") + std::to_string(i)).c_str(),
41 number_of_tasks, mailbox->getName());
43 /* - Send the computation amount to the @ref worker */
44 mailbox->put(new double(comp_size), comm_size);
47 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
48 for (int i = 0; i < workers_count; i++) {
49 /* - Eventually tell all the workers to stop by sending a "finalize" task */
50 mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(i % workers_count));
51 mailbox->put(new double(-1.0), 0);
58 simgrid::s4u::MailboxPtr mailbox = nullptr;
61 explicit Worker(std::vector<std::string> args)
63 xbt_assert(args.size() == 2, "The worker expects a single argument from the XML deployment file: "
64 "its worker ID (its numerical rank)");
65 id = std::stol(args[1]);
66 mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(id));
71 while (1) { /* The worker waits in an infinite loop for tasks sent by the \ref master */
72 double* task = static_cast<double*>(mailbox->get());
73 xbt_assert(task != nullptr, "mailbox->get() failed");
74 double comp_size = *task;
76 if (comp_size < 0) { /* - Exit when -1.0 is received */
77 XBT_INFO("I'm done. See you!");
80 /* - Otherwise, process the task */
81 simgrid::s4u::this_actor::execute(comp_size);
86 int main(int argc, char* argv[])
88 simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
89 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
90 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
93 e->loadPlatform(argv[1]); /** - Load the platform description */
94 e->registerFunction<Master>("master");
95 e->registerFunction<Worker>("worker"); /** - Register the function to be executed by the processes */
96 e->loadDeployment(argv[2]); /** - Deploy the application */
98 e->run(); /** - Run the simulation */
100 XBT_INFO("Simulation time %g", e->getClock());