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 <simgrid/s4u.hpp>
8 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this example");
11 static void master(std::vector<std::string> args)
13 xbt_assert(args.size() == 5, "The master function expects 4 arguments");
15 long workers_count = std::stol(args[1]);
16 long tasks_count = std::stol(args[2]);
17 double compute_cost = std::stod(args[3]);
18 double communication_cost = std::stod(args[4]);
20 XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, tasks_count);
22 for (int i = 0; i < tasks_count; i++) { /* For each task to be executed: */
23 /* - Select a worker in a round-robin way */
24 std::string worker_rank = std::to_string(i % workers_count);
25 std::string mailbox_name = std::string("worker-") + worker_rank;
26 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name);
28 /* - Send the computation cost to that worker */
29 XBT_INFO("Sending task %d of %ld to mailbox '%s'", i, tasks_count, mailbox->get_cname());
30 mailbox->put(new double(compute_cost), communication_cost);
33 XBT_INFO("All tasks have been dispatched. Request all workers to stop.");
34 for (int i = 0; i < workers_count; i++) {
35 /* The workers stop when receiving a negative compute_cost */
36 std::string mailbox_name = std::string("worker-") + std::to_string(i);
37 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name);
39 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");
48 long id = std::stol(args[1]);
50 const std::string mailbox_name = std::string("worker-") + std::to_string(id);
51 simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name);
55 double* msg = static_cast<double*>(mailbox->get());
59 if (compute_cost > 0) /* If compute_cost is valid, execute a computation of that cost */
60 simgrid::s4u::this_actor::execute(compute_cost);
62 } while (compute_cost > 0); /* Stop when receiving an invalid compute_cost */
64 XBT_INFO("Exiting now.");
69 int main(int argc, char* argv[])
71 simgrid::s4u::Engine e(&argc, argv);
72 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
74 /* Register the functions representing the actors */
75 e.register_function("master", &master);
76 e.register_function("worker", &worker);
78 /* Load the platform description and then deploy the application */
79 e.load_platform(argv[1]);
80 e.load_deployment(argv[2]);
82 /* Run the simulation */
85 XBT_INFO("Simulation is over");