Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a add_route with s4u::Link instead of LinkInRoute
[simgrid.git] / examples / cpp / platform-failures / s4u-platform-failures.cpp
index 4a5cc0f..f622d90 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2007-2021. 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. */
 #include "simgrid/kernel/ProfileBuilder.hpp"
 #include "simgrid/s4u.hpp"
 
+namespace sg4 = simgrid::s4u;
+
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
 
 static void master(std::vector<std::string> args)
 {
   xbt_assert(args.size() == 5, "Expecting one parameter");
 
-  simgrid::s4u::Mailbox* mailbox;
+  sg4::Mailbox* mailbox;
   long number_of_tasks = std::stol(args[1]);
   double comp_size     = std::stod(args[2]);
   long comm_size       = std::stol(args[3]);
@@ -36,7 +38,7 @@ static void master(std::vector<std::string> args)
   XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
 
   for (int i = 0; i < number_of_tasks; i++) {
-    mailbox         = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i % workers_count));
+    mailbox         = sg4::Mailbox::by_name("worker-" + std::to_string(i % workers_count));
     auto* payload   = new double(comp_size);
     try {
       XBT_INFO("Send a message to %s", mailbox->get_cname());
@@ -54,7 +56,7 @@ static void master(std::vector<std::string> args)
   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
   for (int i = 0; i < workers_count; i++) {
     /* - Eventually tell all the workers to stop by sending a "finalize" task */
-    mailbox         = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(i));
+    mailbox         = sg4::Mailbox::by_name("worker-" + std::to_string(i));
     auto* payload   = new double(-1.0);
     try {
       mailbox->put(payload, 0, 1.0);
@@ -73,8 +75,8 @@ static void master(std::vector<std::string> args)
 static void worker(std::vector<std::string> args)
 {
   xbt_assert(args.size() == 2, "Expecting one parameter");
-  long id                          = std::stol(args[1]);
-  simgrid::s4u::Mailbox* mailbox   = simgrid::s4u::Mailbox::by_name(std::string("worker-") + std::to_string(id));
+  long id               = std::stol(args[1]);
+  sg4::Mailbox* mailbox = sg4::Mailbox::by_name("worker-" + std::to_string(id));
   while (true) {
     try {
       XBT_INFO("Waiting a message on %s", mailbox->get_cname());
@@ -87,7 +89,7 @@ static void worker(std::vector<std::string> args)
       }
       /*  - Otherwise, process the task */
       XBT_INFO("Start execution...");
-      simgrid::s4u::this_actor::execute(comp_size);
+      sg4::this_actor::execute(comp_size);
       XBT_INFO("Execution complete.");
     } catch (const simgrid::NetworkFailureException&) {
       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
@@ -97,12 +99,12 @@ static void worker(std::vector<std::string> args)
 
 int main(int argc, char* argv[])
 {
-  simgrid::s4u::Engine e(&argc, argv);
+  sg4::Engine e(&argc, argv);
 
   // This is how to attach a profile to an host that is created from the XML file.
   // This should be done before calling load_platform(), as the on_creation() event is fired when loading the platform.
   // You can never set a new profile to a resource that already have one.
-  simgrid::s4u::Host::on_creation.connect([](simgrid::s4u::Host& h) {
+  sg4::Host::on_creation_cb([](sg4::Host& h) {
     if (h.get_name() == "Bourrassa") {
       h.set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("bourassa_profile", "67 0\n70 1\n", 0));
     }
@@ -113,8 +115,28 @@ int main(int argc, char* argv[])
   e.register_function("worker", worker);
   e.load_deployment(argv[2]);
 
+  // Add a new host programatically, and attach a state profile to it
+  auto* root     = e.get_netzone_root();
+  auto* lilibeth = root->create_host("Lilibeth", 1e15);
+  auto link      = e.link_by_name("10");
+  root->add_route(e.host_by_name("Tremblay"), lilibeth, {link});
+  lilibeth->set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("lilibeth_profile", R"(
+4 0
+5 1
+)",
+                                                                                    10));
+  lilibeth->seal();
+
+  // Create an actor on that new host, to monitor its own state
+  auto actor = sg4::Actor::create("sleeper", lilibeth, []() {
+    XBT_INFO("Start sleeping...");
+    sg4::this_actor::sleep_for(1);
+    XBT_INFO("done sleeping.");
+  });
+  actor->set_auto_restart(true);
+
   e.run();
 
-  XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());
+  XBT_INFO("Simulation time %g", sg4::Engine::get_clock());
   return 0;
 }