Logo AND Algorithmique Numérique Distribuée

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