Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / examples / s4u / app-masterworker / s4u_app-masterworker.cpp
1 /* Copyright (c) 2010-2017. 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 "xbt/str.h"
7 #include "xbt/sysdep.h"
8 #include <simgrid/s4u.hpp>
9 #include <string>
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this s4u example");
12
13 class Master {
14   long number_of_tasks             = 0; /* - Number of tasks      */
15   double comp_size                 = 0; /* - Task compute cost    */
16   double comm_size                 = 0; /* - Task communication size */
17   long workers_count               = 0; /* - Number of workers    */
18   simgrid::s4u::MailboxPtr mailbox = nullptr;
19
20 public:
21   explicit Master(std::vector<std::string> args)
22   {
23     xbt_assert(args.size() == 5, "The master function expects 4 arguments from the XML deployment file");
24
25     number_of_tasks = std::stol(args[1]);
26     comp_size       = std::stod(args[2]);
27     comm_size       = std::stod(args[3]);
28     workers_count   = std::stol(args[4]);
29
30     XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
31   }
32
33   void operator()()
34   {
35     for (int i = 0; i < number_of_tasks; i++) { /* For each task to be executed: */
36       /* - Select a @ref worker in a round-robin way */
37       mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(i % workers_count));
38
39       if (number_of_tasks < 10000 || i % 10000 == 0)
40         XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", (std::string("Task_") + std::to_string(i)).c_str(),
41                  number_of_tasks, mailbox->getName());
42
43       /* - Send the computation amount to the @ref worker */
44       mailbox->put(new double(comp_size), comm_size);
45     }
46
47     XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
48     for (int i = 0; i < workers_count; i++) {
49       /* - Eventually tell all the workers to stop by sending a "finalize" task */
50       mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(i % workers_count));
51       mailbox->put(new double(-1.0), 0);
52     }
53   }
54 };
55
56 class Worker {
57   long id                          = -1;
58   simgrid::s4u::MailboxPtr mailbox = nullptr;
59
60 public:
61   explicit Worker(std::vector<std::string> args)
62   {
63     xbt_assert(args.size() == 2, "The worker expects a single argument from the XML deployment file: "
64                                  "its worker ID (its numerical rank)");
65     id      = std::stol(args[1]);
66     mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(id));
67   }
68
69   void operator()()
70   {
71     while (1) { /* The worker waits in an infinite loop for tasks sent by the \ref master */
72       double* task = static_cast<double*>(mailbox->get());
73       xbt_assert(task != nullptr, "mailbox->get() failed");
74       double comp_size = *task;
75       delete task;
76       if (comp_size < 0) { /* - Exit when -1.0 is received */
77         XBT_INFO("I'm done. See you!");
78         break;
79       }
80       /*  - Otherwise, process the task */
81       simgrid::s4u::this_actor::execute(comp_size);
82     }
83   }
84 };
85
86 int main(int argc, char* argv[])
87 {
88   simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
89   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
90                        "\tExample: %s msg_platform.xml msg_deployment.xml\n",
91              argv[0], argv[0]);
92
93   e->loadPlatform(argv[1]);              /** - Load the platform description */
94   e->registerFunction<Master>("master");
95   e->registerFunction<Worker>("worker"); /** - Register the function to be executed by the processes */
96   e->loadDeployment(argv[2]);            /** - Deploy the application */
97
98   e->run(); /** - Run the simulation */
99
100   XBT_INFO("Simulation time %g", e->getClock());
101
102   delete e;
103   return 0;
104 }