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 s4u example");
12 double compute_cost = 0;
13 double communicate_cost = 0;
14 long workers_count = 0;
15 simgrid::s4u::MailboxPtr mailbox = nullptr;
18 explicit Master(std::vector<std::string> args)
20 xbt_assert(args.size() == 5, "The master actor expects 4 arguments from the XML deployment file");
22 workers_count = std::stol(args[1]);
23 tasks_count = std::stol(args[2]);
24 compute_cost = std::stod(args[3]);
25 communicate_cost = std::stod(args[4]);
27 XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, tasks_count);
32 for (int i = 0; i < tasks_count; i++) { /* For each task to be executed: */
33 /* - Select a worker in a round-robin way */
34 mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i % workers_count));
36 /* - Send the computation amount to the worker */
37 if (tasks_count < 10000 || (tasks_count < 100000 && i % 10000 == 0) || i % 100000 == 0)
38 XBT_INFO("Sending task %d of %ld to mailbox '%s'", i, tasks_count, mailbox->get_cname());
39 mailbox->put(new double(compute_cost), communicate_cost);
42 XBT_INFO("All tasks have been dispatched. Request all workers to stop.");
43 for (int i = 0; i < workers_count; i++) {
44 /* The workers stop when receiving a negative compute_cost */
45 mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i));
46 mailbox->put(new double(-1.0), 0);
53 simgrid::s4u::MailboxPtr mailbox = nullptr;
56 explicit Worker(std::vector<std::string> args)
58 xbt_assert(args.size() == 2, "The worker expects a single argument from the XML deployment file: "
59 "its worker ID (its numerical rank)");
60 id = std::stol(args[1]);
61 mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(id));
68 double* msg = static_cast<double*>(mailbox->get());
72 if (compute_cost > 0) /* If compute_cost is valid, execute a computation of that cost */
73 simgrid::s4u::this_actor::execute(compute_cost);
75 } while (compute_cost > 0); /* Stop when receiving an invalid compute_cost */
77 XBT_INFO("Exiting now.");
81 int main(int argc, char* argv[])
83 simgrid::s4u::Engine e(&argc, argv);
84 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
86 /* Register the classes representing the actors */
87 e.register_actor<Master>("master");
88 e.register_actor<Worker>("worker");
90 /* Load the platform description and then deploy the application */
91 e.load_platform(argv[1]);
92 e.load_deployment(argv[2]);
94 /* Run the simulation */
97 XBT_INFO("Simulation is over");