Logo AND Algorithmique Numérique Distribuée

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