Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
More doc for SMPI
[simgrid.git] / teshsuite / s4u / monkey-masterworkers / monkey-masterworkers.cpp
index 183179a..4a4fca3 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
 
 /* 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. */
@@ -9,7 +9,7 @@
  * It is not written to be pleasant to read, but instead to resist the aggressions of the monkey:
  * - Workers keep going until after a global variable `todo` reaches 0.
  * - The master is a daemon that just sends infinitely tasks
- *   (simgrid simulations stop as soon as all non-daemon actors are done).
+ *   (SimGrid simulations stop as soon as all non-daemon actors are done).
  * - The platform is created programmatically to remove path issues and control the problem size.
  *
  * Command-line configuration items:
@@ -33,10 +33,7 @@ static simgrid::config::Flag<double> cfg_deadline{"deadline", "When to fail the
                                                   120};
 static simgrid::config::Flag<int> cfg_task_count{"task-count", "Amount of tasks that must be executed to succeed", 1};
 
-int todo; // remaining amount of tasks to execute, a global variable
-sg4::Mailbox* mailbox; // as a global to reduce the amount of simcalls during actor reboot
-
-XBT_ATTRIB_NORETURN static void master()
+XBT_ATTRIB_NORETURN static void master(sg4::Mailbox* mailbox)
 {
   double comp_size = 1e6;
   long comm_size   = 1e6;
@@ -66,7 +63,7 @@ XBT_ATTRIB_NORETURN static void master()
   THROW_IMPOSSIBLE;
 }
 
-static void worker(int id)
+static void worker(int id, sg4::Mailbox* mailbox, int& todo)
 {
   bool rebooting = sg4::Actor::self()->get_restart_count() > 0;
 
@@ -102,35 +99,34 @@ int main(int argc, char* argv[])
   sg4::Engine e(&argc, argv);
 
   auto* rootzone = sg4::create_full_zone("root");
-  sg4::Host* main; // First host created, where the master will stay
   std::vector<sg4::Host*> worker_hosts;
 
+  int todo = cfg_task_count; // remaining amount of tasks to execute, a shared variable
+  xbt_assert(todo > 0, "Please give more than %d tasks to run", todo);
+
   xbt_assert(cfg_host_count > 2, "You need at least 2 workers (i.e., 3 hosts) or the master will be auto-killed when "
                                  "the only worker gets killed.");
-  for (int i = 0; i < cfg_host_count; i++) {
-    auto hostname = std::string("lilibeth ") + std::to_string(i);
+
+  // get it once for all, to reduce the amount of simcalls during actor reboot
+  sg4::Mailbox* mailbox = sg4::Mailbox::by_name("mailbox");
+
+  sg4::Host* master_host = rootzone->create_host("lilibeth 0", 1e9); // Host where the master will stay
+  for (int i = 1; i < cfg_host_count; i++) {
+    auto hostname = "lilibeth " + std::to_string(i);
     auto* host    = rootzone->create_host(hostname, 1e9);
-    if (i == 0) {
-      main = host;
-    } else {
-      sg4::LinkInRoute link(rootzone->create_link(hostname, "1MBps")->set_latency("24us")->seal());
-      rootzone->add_route(main->get_netpoint(), host->get_netpoint(), nullptr, nullptr, {link}, true);
-      worker_hosts.push_back(host);
-    }
+    auto* link    = rootzone->create_link(hostname, "1MBps")->set_latency("24us")->seal();
+    rootzone->add_route(master_host, host, {link});
+    worker_hosts.push_back(host);
   }
   rootzone->seal();
 
-  sg4::Actor::create("master", main, master)->daemonize()->set_auto_restart(true);
+  sg4::Actor::create("master", master_host, master, mailbox)->daemonize()->set_auto_restart(true);
   int id = 0;
   for (auto* h : worker_hosts) {
-    sg4::Actor::create("worker", h, worker, id)->set_auto_restart(true);
+    sg4::Actor::create("worker", h, worker, id, mailbox, std::ref(todo))->set_auto_restart(true);
     id++;
   }
 
-  todo = cfg_task_count;
-  xbt_assert(todo > 0, "Please give more than %d tasks to run", todo);
-  mailbox = sg4::Mailbox::by_name("mailbox");
-
   e.run();
 
   XBT_INFO("WE SURVIVED!");