Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
smpi: fix issue with message IDs. In case of persistent request reused multiple times...
[simgrid.git] / examples / cpp / exec-dependent / s4u-exec-dependent.cpp
index 834921df7ae3bd6ace87e57debffa6a5db7abf62..08129e44fc190803a18dc272aca778b11f9541df 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. */
@@ -7,22 +7,21 @@
 #include <vector>
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
+namespace sg4 = simgrid::s4u;
 
 static void worker()
 {
   // Define an amount of work that should take 1 second to execute.
-  double computation_amount = simgrid::s4u::this_actor::get_host()->get_speed();
+  double computation_amount = sg4::this_actor::get_host()->get_speed();
 
-  std::vector<simgrid::s4u::ExecPtr> pending_execs;
   // Create a small DAG
   // + Two parents and a child
   // + First parent ends after 1 second and the Second parent after 2 seconds.
-  simgrid::s4u::ExecPtr first_parent  = simgrid::s4u::this_actor::exec_init(computation_amount);
-  pending_execs.push_back(first_parent);
-  simgrid::s4u::ExecPtr second_parent = simgrid::s4u::this_actor::exec_init(2 * computation_amount);
-  pending_execs.push_back(second_parent);
-  simgrid::s4u::ExecPtr child = simgrid::s4u::this_actor::exec_init(computation_amount);
-  pending_execs.push_back(child);
+  sg4::ExecPtr first_parent = sg4::this_actor::exec_init(computation_amount);
+  sg4::ExecPtr second_parent = sg4::this_actor::exec_init(2 * computation_amount);
+  sg4::ExecPtr child = sg4::Exec::init()->set_flops_amount(computation_amount);
+
+  sg4::ActivitySet pending_execs ({first_parent, second_parent, child});
 
   // Name the activities (for logging purposes only)
   first_parent->set_name("parent 1");
@@ -36,27 +35,39 @@ static void worker()
   // Start the activities.
   first_parent->start();
   second_parent->start();
-  // child uses a vetoable start to force it to wait for the completion of its predecessors
-  child->vetoable_start();
+  child->start();
 
   // wait for the completion of all activities
   while (not pending_execs.empty()) {
-    int changed_pos = simgrid::s4u::Exec::wait_any_for(pending_execs, -1);
-    XBT_INFO("Exec '%s' is complete", pending_execs[changed_pos]->get_cname());
-    pending_execs.erase(pending_execs.begin() + changed_pos);
+    auto completed_one = pending_execs.wait_any();
+    if (completed_one != nullptr)
+      XBT_INFO("Exec '%s' is complete", completed_one->get_cname());
   }
 }
 
 int main(int argc, char* argv[])
 {
-  simgrid::s4u::Engine e(&argc, argv);
+  sg4::Engine e(&argc, argv);
   e.load_platform(argv[1]);
 
-  simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Fafard"), worker);
+  sg4::Actor::create("worker", e.host_by_name("Fafard"), worker);
+
+  sg4::Exec::on_veto_cb([&e](sg4::Exec& exec) {
+    // First display the situation
+    XBT_INFO("Activity '%s' vetoed. Dependencies: %s; Ressources: %s", exec.get_cname(),
+             (exec.dependencies_solved() ? "solved" : "NOT solved"),
+             (exec.is_assigned() ? "assigned" : "NOT assigned"));
+
+    // In this simple case, we just assign the child task to a resource when its dependencies are solved
+    if (exec.dependencies_solved() && not exec.is_assigned()) {
+      XBT_INFO("Activity %s's dependencies are resolved. Let's assign it to Fafard.", exec.get_cname());
+      exec.set_host(e.host_by_name("Fafard"));
+    }
+  });
 
   e.run();
 
-  XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());
+  XBT_INFO("Simulation time %g", sg4::Engine::get_clock());
 
   return 0;
 }