Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics in these examples
[simgrid.git] / examples / s4u / app-masterworkers / s4u-app-masterworkers-fun.cpp
1 /* Copyright (c) 2010-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <simgrid/s4u.hpp>
7
8 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this example");
9
10 static void master(std::vector<std::string> args)
11 {
12   xbt_assert(args.size() == 5, "The master function expects 4 arguments");
13
14   long workers_count        = std::stol(args[1]);
15   long tasks_count          = std::stol(args[2]);
16   double compute_cost       = std::stod(args[3]);
17   double communication_cost = std::stod(args[4]);
18
19   XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, tasks_count);
20
21   for (int i = 0; i < tasks_count; i++) { /* For each task to be executed: */
22     /* - Select a worker in a round-robin way */
23     std::string worker_rank          = std::to_string(i % workers_count);
24     std::string mailbox_name         = std::string("worker-") + worker_rank;
25     simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name);
26
27     /* - Send the computation cost to that worker */
28     XBT_INFO("Sending task %d of %ld to mailbox '%s'", i, tasks_count, mailbox->get_cname());
29     mailbox->put(new double(compute_cost), communication_cost);
30   }
31
32   XBT_INFO("All tasks have been dispatched. Request all workers to stop.");
33   for (int i = 0; i < workers_count; i++) {
34     /* The workers stop when receiving a negative compute_cost */
35     std::string mailbox_name         = std::string("worker-") + std::to_string(i);
36     simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name);
37
38     mailbox->put(new double(-1.0), 0);
39   }
40 }
41
42 static void worker(std::vector<std::string> args)
43 {
44   xbt_assert(args.size() == 2, "The worker expects a single argument");
45   long id                          = std::stol(args[1]);
46
47   const std::string mailbox_name   = std::string("worker-") + std::to_string(id);
48   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name);
49
50   double compute_cost;
51   do {
52     double* msg  = static_cast<double*>(mailbox->get());
53     compute_cost = *msg;
54     delete msg;
55
56     if (compute_cost > 0) /* If compute_cost is valid, execute a computation of that cost */
57       simgrid::s4u::this_actor::execute(compute_cost);
58
59   } while (compute_cost > 0); /* Stop when receiving an invalid compute_cost */
60
61   XBT_INFO("Exiting now.");
62 }
63
64 int main(int argc, char* argv[])
65 {
66   simgrid::s4u::Engine e(&argc, argv);
67   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
68
69   /* Register the functions representing the actors */
70   e.register_function("master", &master);
71   e.register_function("worker", &worker);
72
73   /* Load the platform description and then deploy the application */
74   e.load_platform(argv[1]);
75   e.load_deployment(argv[2]);
76
77   /* Run the simulation */
78   e.run();
79
80   XBT_INFO("Simulation is over");
81
82   return 0;
83 }