Logo AND Algorithmique Numérique Distribuée

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