1 /* Copyright (c) 2010-2016. 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->name());
43 /* - Send the task to the @ref worker */
44 char* payload = bprintf("%f", comp_size);
45 simgrid::s4u::this_actor::send(mailbox, payload, comm_size);
48 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
49 for (int i = 0; i < workers_count; i++) {
50 /* - Eventually tell all the workers to stop by sending a "finalize" task */
51 mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(i % workers_count));
52 simgrid::s4u::this_actor::send(mailbox, xbt_strdup("finalize"), 0);
59 simgrid::s4u::MailboxPtr mailbox = nullptr;
62 explicit Worker(std::vector<std::string> args)
64 xbt_assert(args.size() == 2, "The worker expects a single argument from the XML deployment file: "
65 "its worker ID (its numerical rank)");
66 id = std::stol(args[1]);
67 mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(id));
72 while (1) { /* The worker waits in an infinite loop for tasks sent by the \ref master */
73 char* res = static_cast<char*>(simgrid::s4u::this_actor::recv(mailbox));
74 xbt_assert(res != nullptr, "MSG_task_get failed");
76 if (strcmp(res, "finalize") == 0) { /* - Exit if 'finalize' is received */
80 /* - Otherwise, process the task */
81 double comp_size = std::stod(res);
83 simgrid::s4u::this_actor::execute(comp_size);
85 XBT_INFO("I'm done. See you!");
89 int main(int argc, char* argv[])
91 simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
92 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
93 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
96 e->loadPlatform(argv[1]); /** - Load the platform description */
97 e->registerFunction<Master>("master");
98 e->registerFunction<Worker>("worker"); /** - Register the function to be executed by the processes */
99 e->loadDeployment(argv[2]); /** - Deploy the application */
101 e->run(); /** - Run the simulation */
103 XBT_INFO("Simulation time %g", e->getClock());