Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0699bb0a01dedcc9b713a3df39842277948fd7f8
[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->name());
42
43       /* - Send the task to the @ref worker */
44       char* payload = bprintf("%f", comp_size);
45       mailbox->send(payload, comm_size);
46     }
47
48     XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
49     for (int i = 0; i < workers_count; i++) {
50       /* - Eventually tell all the workers to stop by sending a "finalize" task */
51       mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(i % workers_count));
52       mailbox->send(xbt_strdup("finalize"), 0);
53     }
54   }
55 };
56
57 class Worker {
58   long id                          = -1;
59   simgrid::s4u::MailboxPtr mailbox = nullptr;
60
61 public:
62   explicit Worker(std::vector<std::string> args)
63   {
64     xbt_assert(args.size() == 2, "The worker expects a single argument from the XML deployment file: "
65                                  "its worker ID (its numerical rank)");
66     id      = std::stol(args[1]);
67     mailbox = simgrid::s4u::Mailbox::byName(std::string("worker-") + std::to_string(id));
68   }
69
70   void operator()()
71   {
72     while (1) { /* The worker waits in an infinite loop for tasks sent by the \ref master */
73       char* res = static_cast<char*>(simgrid::s4u::this_actor::recv(mailbox));
74       xbt_assert(res != nullptr, "MSG_task_get failed");
75
76       if (strcmp(res, "finalize") == 0) { /* - Exit if 'finalize' is received */
77         xbt_free(res);
78         break;
79       }
80       /*  - Otherwise, process the task */
81       double comp_size = std::stod(res);
82       xbt_free(res);
83       simgrid::s4u::this_actor::execute(comp_size);
84     }
85     XBT_INFO("I'm done. See you!");
86   }
87 };
88
89 int main(int argc, char* argv[])
90 {
91   simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
92   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
93                        "\tExample: %s msg_platform.xml msg_deployment.xml\n",
94              argv[0], argv[0]);
95
96   e->loadPlatform(argv[1]);              /** - Load the platform description */
97   e->registerFunction<Master>("master");
98   e->registerFunction<Worker>("worker"); /** - Register the function to be executed by the processes */
99   e->loadDeployment(argv[2]);            /** - Deploy the application */
100
101   e->run(); /** - Run the simulation */
102
103   XBT_INFO("Simulation time %g", e->getClock());
104
105   delete e;
106   return 0;
107 }