Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make examples/s4u/app-masterworkers more complex to make simplification exercises...
authorMartin Quinson <martin.quinson@loria.fr>
Thu, 16 Aug 2018 10:40:29 +0000 (12:40 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Thu, 16 Aug 2018 10:45:18 +0000 (12:45 +0200)
examples/s4u/app-masterworkers/s4u-app-masterworkers-class.cpp
examples/s4u/app-masterworkers/s4u-app-masterworkers-fun.cpp
examples/s4u/app-masterworkers/s4u-app-masterworkers.tesh
examples/s4u/app-masterworkers/s4u-app-masterworkers_d.xml

index 65bf93f..81c84cb 100644 (file)
@@ -3,6 +3,9 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+/* This is part of the following tutorial:                                  */
+/*    https://simgrid.frama.io/simgrid/usecase_algorithms.html              */
+
 #include <simgrid/s4u.hpp>
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this s4u example");
@@ -11,27 +14,27 @@ class Master {
   long tasks_count                 = 0;
   double compute_cost              = 0;
   double communicate_cost          = 0;
-  long workers_count               = 0;
-  simgrid::s4u::MailboxPtr mailbox = nullptr;
+  std::vector<simgrid::s4u::MailboxPtr> workers;
 
 public:
   explicit Master(std::vector<std::string> args)
   {
-    xbt_assert(args.size() == 5, "The master actor expects 4 arguments from the XML deployment file");
+    xbt_assert(args.size() > 4, "The master function expects 3 arguments plus the workers' names");
 
-    workers_count    = std::stol(args[1]);
-    tasks_count      = std::stol(args[2]);
-    compute_cost     = std::stod(args[3]);
-    communicate_cost = std::stod(args[4]);
+    tasks_count      = std::stol(args[1]);
+    compute_cost     = std::stod(args[2]);
+    communicate_cost = std::stod(args[3]);
+    for (unsigned int i = 4; i < args.size(); i++)
+      workers.push_back(simgrid::s4u::Mailbox::by_name(args[i]));
 
-    XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, tasks_count);
+    XBT_INFO("Got %zu workers and %ld tasks to process", workers.size(), tasks_count);
   }
 
   void operator()()
   {
     for (int i = 0; i < tasks_count; i++) { /* For each task to be executed: */
       /* - Select a worker in a round-robin way */
-      mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i % workers_count));
+      simgrid::s4u::MailboxPtr mailbox = workers[i % workers.size()];
 
       /* - Send the computation amount to the worker */
       if (tasks_count < 10000 || (tasks_count < 100000 && i % 10000 == 0) || i % 100000 == 0)
@@ -40,25 +43,23 @@ public:
     }
 
     XBT_INFO("All tasks have been dispatched. Request all workers to stop.");
-    for (int i = 0; i < workers_count; i++) {
+    for (unsigned int i = 0; i < workers.size(); i++) {
       /* The workers stop when receiving a negative compute_cost */
-      mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i));
+      simgrid::s4u::MailboxPtr mailbox = workers[i % workers.size()];
       mailbox->put(new double(-1.0), 0);
     }
   }
 };
 
 class Worker {
-  long id                          = -1;
   simgrid::s4u::MailboxPtr mailbox = nullptr;
 
 public:
   explicit Worker(std::vector<std::string> args)
   {
-    xbt_assert(args.size() == 2, "The worker expects a single argument from the XML deployment file: "
-                                 "its worker ID (its numerical rank)");
-    id      = std::stol(args[1]);
-    mailbox = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(id));
+    xbt_assert(args.size() == 1, "The worker expects to not get any argument");
+
+    mailbox = simgrid::s4u::Mailbox::by_name(simgrid::s4u::this_actor::get_host()->get_name());
   }
 
   void operator()()
index 0cfae30..e63565f 100644 (file)
@@ -3,6 +3,9 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+/* This is part of the following tutorial:                                  */
+/*    https://simgrid.frama.io/simgrid/usecase_algorithms.html              */
+
 #include <simgrid/s4u.hpp>
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this example");
@@ -10,20 +13,20 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_masterworker, "Messages specific for this e
 // master-begin
 static void master(std::vector<std::string> args)
 {
-  xbt_assert(args.size() == 5, "The master function expects 4 arguments");
+  xbt_assert(args.size() > 4, "The master function expects at least 3 arguments");
 
-  long workers_count        = std::stol(args[1]);
-  long tasks_count          = std::stol(args[2]);
-  double compute_cost       = std::stod(args[3]);
-  double communication_cost = std::stod(args[4]);
+  long tasks_count          = std::stol(args[1]);
+  double compute_cost       = std::stod(args[2]);
+  double communication_cost = std::stod(args[3]);
+  std::vector<simgrid::s4u::MailboxPtr> workers;
+  for (unsigned int i = 4; i < args.size(); i++)
+    workers.push_back(simgrid::s4u::Mailbox::by_name(args[i]));
 
-  XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, tasks_count);
+  XBT_INFO("Got %zu workers and %ld tasks to process", workers.size(), tasks_count);
 
   for (int i = 0; i < tasks_count; i++) { /* For each task to be executed: */
     /* - Select a worker in a round-robin way */
-    std::string worker_rank          = std::to_string(i % workers_count);
-    std::string mailbox_name         = std::string("worker-") + worker_rank;
-    simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name);
+    simgrid::s4u::MailboxPtr mailbox = workers[i % workers.size()];
 
     /* - Send the computation cost to that worker */
     XBT_INFO("Sending task %d of %ld to mailbox '%s'", i, tasks_count, mailbox->get_cname());
@@ -31,10 +34,9 @@ static void master(std::vector<std::string> args)
   }
 
   XBT_INFO("All tasks have been dispatched. Request all workers to stop.");
-  for (int i = 0; i < workers_count; i++) {
+  for (unsigned int i = 0; i < workers.size(); i++) {
     /* The workers stop when receiving a negative compute_cost */
-    std::string mailbox_name         = std::string("worker-") + std::to_string(i);
-    simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name);
+    simgrid::s4u::MailboxPtr mailbox = workers[i % workers.size()];
 
     mailbox->put(new double(-1.0), 0);
   }
@@ -44,11 +46,10 @@ static void master(std::vector<std::string> args)
 // worker-begin
 static void worker(std::vector<std::string> args)
 {
-  xbt_assert(args.size() == 2, "The worker expects a single argument");
-  long id                          = std::stol(args[1]);
+  xbt_assert(args.size() == 1, "The worker expects no argument");
 
-  const std::string mailbox_name   = std::string("worker-") + std::to_string(id);
-  simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name);
+  auto my_host                     = simgrid::s4u::this_actor::get_host();
+  simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(my_host->get_name());
 
   double compute_cost;
   do {
index 206dd72..4abff13 100644 (file)
@@ -5,26 +5,26 @@ p Testing a simple master/workers example application
 ! output sort 19
 $ $SG_TEST_EXENV ${bindir:=.}/s4u-app-masterworkers-class$EXEEXT ${platfdir}/small_platform.xml s4u-app-masterworkers_d.xml --trace "--log=root.fmt:[%10.6r]%e(%P@%h)%e%m%n"
 > [  0.000000] (master@Tremblay) Got 5 workers and 20 tasks to process
-> [  0.000000] (master@Tremblay) Sending task 0 of 20 to mailbox 'worker-0'
-> [  0.002265] (master@Tremblay) Sending task 1 of 20 to mailbox 'worker-1'
-> [  0.171420] (master@Tremblay) Sending task 2 of 20 to mailbox 'worker-2'
-> [  0.329817] (master@Tremblay) Sending task 3 of 20 to mailbox 'worker-3'
-> [  0.453549] (master@Tremblay) Sending task 4 of 20 to mailbox 'worker-4'
-> [  0.586168] (master@Tremblay) Sending task 5 of 20 to mailbox 'worker-0'
-> [  0.588433] (master@Tremblay) Sending task 6 of 20 to mailbox 'worker-1'
-> [  0.995917] (master@Tremblay) Sending task 7 of 20 to mailbox 'worker-2'
-> [  1.154314] (master@Tremblay) Sending task 8 of 20 to mailbox 'worker-3'
-> [  1.608379] (master@Tremblay) Sending task 9 of 20 to mailbox 'worker-4'
-> [  1.749885] (master@Tremblay) Sending task 10 of 20 to mailbox 'worker-0'
-> [  1.752150] (master@Tremblay) Sending task 11 of 20 to mailbox 'worker-1'
-> [  1.921304] (master@Tremblay) Sending task 12 of 20 to mailbox 'worker-2'
-> [  2.079701] (master@Tremblay) Sending task 13 of 20 to mailbox 'worker-3'
-> [  2.763209] (master@Tremblay) Sending task 14 of 20 to mailbox 'worker-4'
-> [  2.913601] (master@Tremblay) Sending task 15 of 20 to mailbox 'worker-0'
-> [  2.915867] (master@Tremblay) Sending task 16 of 20 to mailbox 'worker-1'
-> [  3.085021] (master@Tremblay) Sending task 17 of 20 to mailbox 'worker-2'
-> [  3.243418] (master@Tremblay) Sending task 18 of 20 to mailbox 'worker-3'
-> [  3.918038] (master@Tremblay) Sending task 19 of 20 to mailbox 'worker-4'
+> [  0.000000] (master@Tremblay) Sending task 0 of 20 to mailbox 'Tremblay'
+> [  0.002265] (master@Tremblay) Sending task 1 of 20 to mailbox 'Jupiter'
+> [  0.171420] (master@Tremblay) Sending task 2 of 20 to mailbox 'Fafard'
+> [  0.329817] (master@Tremblay) Sending task 3 of 20 to mailbox 'Ginette'
+> [  0.453549] (master@Tremblay) Sending task 4 of 20 to mailbox 'Bourassa'
+> [  0.586168] (master@Tremblay) Sending task 5 of 20 to mailbox 'Tremblay'
+> [  0.588433] (master@Tremblay) Sending task 6 of 20 to mailbox 'Jupiter'
+> [  0.995917] (master@Tremblay) Sending task 7 of 20 to mailbox 'Fafard'
+> [  1.154314] (master@Tremblay) Sending task 8 of 20 to mailbox 'Ginette'
+> [  1.608379] (master@Tremblay) Sending task 9 of 20 to mailbox 'Bourassa'
+> [  1.749885] (master@Tremblay) Sending task 10 of 20 to mailbox 'Tremblay'
+> [  1.752150] (master@Tremblay) Sending task 11 of 20 to mailbox 'Jupiter'
+> [  1.921304] (master@Tremblay) Sending task 12 of 20 to mailbox 'Fafard'
+> [  2.079701] (master@Tremblay) Sending task 13 of 20 to mailbox 'Ginette'
+> [  2.763209] (master@Tremblay) Sending task 14 of 20 to mailbox 'Bourassa'
+> [  2.913601] (master@Tremblay) Sending task 15 of 20 to mailbox 'Tremblay'
+> [  2.915867] (master@Tremblay) Sending task 16 of 20 to mailbox 'Jupiter'
+> [  3.085021] (master@Tremblay) Sending task 17 of 20 to mailbox 'Fafard'
+> [  3.243418] (master@Tremblay) Sending task 18 of 20 to mailbox 'Ginette'
+> [  3.918038] (master@Tremblay) Sending task 19 of 20 to mailbox 'Bourassa'
 > [  4.077318] (master@Tremblay) All tasks have been dispatched. Request all workers to stop.
 > [  4.077513] (worker@Tremblay) Exiting now.
 > [  4.096528] (worker@Jupiter) Exiting now.
@@ -36,26 +36,26 @@ $ $SG_TEST_EXENV ${bindir:=.}/s4u-app-masterworkers-class$EXEEXT ${platfdir}/sma
 ! output sort 19
 $ $SG_TEST_EXENV ${bindir:=.}/s4u-app-masterworkers-fun$EXEEXT ${platfdir}/small_platform.xml s4u-app-masterworkers_d.xml --trace "--log=root.fmt:[%10.6r]%e(%P@%h)%e%m%n"
 > [  0.000000] (master@Tremblay) Got 5 workers and 20 tasks to process
-> [  0.000000] (master@Tremblay) Sending task 0 of 20 to mailbox 'worker-0'
-> [  0.002265] (master@Tremblay) Sending task 1 of 20 to mailbox 'worker-1'
-> [  0.171420] (master@Tremblay) Sending task 2 of 20 to mailbox 'worker-2'
-> [  0.329817] (master@Tremblay) Sending task 3 of 20 to mailbox 'worker-3'
-> [  0.453549] (master@Tremblay) Sending task 4 of 20 to mailbox 'worker-4'
-> [  0.586168] (master@Tremblay) Sending task 5 of 20 to mailbox 'worker-0'
-> [  0.588433] (master@Tremblay) Sending task 6 of 20 to mailbox 'worker-1'
-> [  0.995917] (master@Tremblay) Sending task 7 of 20 to mailbox 'worker-2'
-> [  1.154314] (master@Tremblay) Sending task 8 of 20 to mailbox 'worker-3'
-> [  1.608379] (master@Tremblay) Sending task 9 of 20 to mailbox 'worker-4'
-> [  1.749885] (master@Tremblay) Sending task 10 of 20 to mailbox 'worker-0'
-> [  1.752150] (master@Tremblay) Sending task 11 of 20 to mailbox 'worker-1'
-> [  1.921304] (master@Tremblay) Sending task 12 of 20 to mailbox 'worker-2'
-> [  2.079701] (master@Tremblay) Sending task 13 of 20 to mailbox 'worker-3'
-> [  2.763209] (master@Tremblay) Sending task 14 of 20 to mailbox 'worker-4'
-> [  2.913601] (master@Tremblay) Sending task 15 of 20 to mailbox 'worker-0'
-> [  2.915867] (master@Tremblay) Sending task 16 of 20 to mailbox 'worker-1'
-> [  3.085021] (master@Tremblay) Sending task 17 of 20 to mailbox 'worker-2'
-> [  3.243418] (master@Tremblay) Sending task 18 of 20 to mailbox 'worker-3'
-> [  3.918038] (master@Tremblay) Sending task 19 of 20 to mailbox 'worker-4'
+> [  0.000000] (master@Tremblay) Sending task 0 of 20 to mailbox 'Tremblay'
+> [  0.002265] (master@Tremblay) Sending task 1 of 20 to mailbox 'Jupiter'
+> [  0.171420] (master@Tremblay) Sending task 2 of 20 to mailbox 'Fafard'
+> [  0.329817] (master@Tremblay) Sending task 3 of 20 to mailbox 'Ginette'
+> [  0.453549] (master@Tremblay) Sending task 4 of 20 to mailbox 'Bourassa'
+> [  0.586168] (master@Tremblay) Sending task 5 of 20 to mailbox 'Tremblay'
+> [  0.588433] (master@Tremblay) Sending task 6 of 20 to mailbox 'Jupiter'
+> [  0.995917] (master@Tremblay) Sending task 7 of 20 to mailbox 'Fafard'
+> [  1.154314] (master@Tremblay) Sending task 8 of 20 to mailbox 'Ginette'
+> [  1.608379] (master@Tremblay) Sending task 9 of 20 to mailbox 'Bourassa'
+> [  1.749885] (master@Tremblay) Sending task 10 of 20 to mailbox 'Tremblay'
+> [  1.752150] (master@Tremblay) Sending task 11 of 20 to mailbox 'Jupiter'
+> [  1.921304] (master@Tremblay) Sending task 12 of 20 to mailbox 'Fafard'
+> [  2.079701] (master@Tremblay) Sending task 13 of 20 to mailbox 'Ginette'
+> [  2.763209] (master@Tremblay) Sending task 14 of 20 to mailbox 'Bourassa'
+> [  2.913601] (master@Tremblay) Sending task 15 of 20 to mailbox 'Tremblay'
+> [  2.915867] (master@Tremblay) Sending task 16 of 20 to mailbox 'Jupiter'
+> [  3.085021] (master@Tremblay) Sending task 17 of 20 to mailbox 'Fafard'
+> [  3.243418] (master@Tremblay) Sending task 18 of 20 to mailbox 'Ginette'
+> [  3.918038] (master@Tremblay) Sending task 19 of 20 to mailbox 'Bourassa'
 > [  4.077318] (master@Tremblay) All tasks have been dispatched. Request all workers to stop.
 > [  4.077513] (worker@Tremblay) Exiting now.
 > [  4.096528] (worker@Jupiter) Exiting now.
index 29fddca..292e3bb 100644 (file)
@@ -3,25 +3,20 @@
 <platform version="4.1">
   <!-- The master actor (with some arguments) -->
   <actor host="Tremblay" function="master">
-    <argument value="5"/>         <!-- Number of workers -->
     <argument value="20"/>        <!-- Number of tasks -->
     <argument value="50000000"/>  <!-- Computation size of tasks -->
     <argument value="1000000"/>   <!-- Communication size of tasks -->
+    <!-- name of hosts on which the workers are running -->
+    <argument value="Tremblay"/>
+    <argument value="Jupiter" />
+    <argument value="Fafard" />
+    <argument value="Ginette" />
+    <argument value="Bourassa" />
   </actor>
-  <!-- The worker processes (with mailbox to listen on as argument) -->
-  <actor host="Tremblay" function="worker">
-    <argument value="0"/> 
-  </actor>
-  <actor host="Jupiter" function="worker">
-    <argument value="1"/> 
-  </actor>
-  <actor host="Fafard" function="worker">
-    <argument value="2"/> 
-  </actor>
-  <actor host="Ginette" function="worker">
-    <argument value="3"/> 
-  </actor>
-  <actor host="Bourassa" function="worker">
-    <argument value="4"/> 
-  </actor>
+  <!-- The worker processes (with no argument) -->
+  <actor host="Tremblay" function="worker" />
+  <actor host="Jupiter" function="worker" />
+  <actor host="Fafard" function="worker" />
+  <actor host="Ginette" function="worker" />
+  <actor host="Bourassa" function="worker" />
 </platform>