Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
65bf93f11fff60d0993a34876b2e05fd8be204bb
[simgrid.git] / examples / s4u / app-masterworkers / s4u-app-masterworkers-class.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 s4u example");
9
10 class Master {
11   long tasks_count                 = 0;
12   double compute_cost              = 0;
13   double communicate_cost          = 0;
14   long workers_count               = 0;
15   simgrid::s4u::MailboxPtr mailbox = nullptr;
16
17 public:
18   explicit Master(std::vector<std::string> args)
19   {
20     xbt_assert(args.size() == 5, "The master actor expects 4 arguments from the XML deployment file");
21
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]);
26
27     XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, tasks_count);
28   }
29
30   void operator()()
31   {
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));
35
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);
40     }
41
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);
47     }
48   }
49 };
50
51 class Worker {
52   long id                          = -1;
53   simgrid::s4u::MailboxPtr mailbox = nullptr;
54
55 public:
56   explicit Worker(std::vector<std::string> args)
57   {
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));
62   }
63
64   void operator()()
65   {
66     double compute_cost;
67     do {
68       double* msg  = static_cast<double*>(mailbox->get());
69       compute_cost = *msg;
70       delete msg;
71
72       if (compute_cost > 0) /* If compute_cost is valid, execute a computation of that cost */
73         simgrid::s4u::this_actor::execute(compute_cost);
74
75     } while (compute_cost > 0); /* Stop when receiving an invalid compute_cost */
76
77     XBT_INFO("Exiting now.");
78   }
79 };
80
81 int main(int argc, char* argv[])
82 {
83   simgrid::s4u::Engine e(&argc, argv);
84   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);
85
86   /* Register the classes representing the actors */
87   e.register_actor<Master>("master");
88   e.register_actor<Worker>("worker");
89
90   /* Load the platform description and then deploy the application */
91   e.load_platform(argv[1]);
92   e.load_deployment(argv[2]);
93
94   /* Run the simulation */
95   e.run();
96
97   XBT_INFO("Simulation is over");
98
99   return 0;
100 }