Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improve handling of dependencies, debug and example
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Fri, 24 Jan 2020 16:26:34 +0000 (17:26 +0100)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Fri, 24 Jan 2020 16:47:47 +0000 (17:47 +0100)
examples/s4u/exec-dependent/s4u-exec-dependent.cpp
examples/s4u/exec-dependent/s4u-exec-dependent.tesh
include/simgrid/s4u/Activity.hpp
src/s4u/s4u_Exec.cpp

index 9f373a1..67b32b9 100644 (file)
@@ -4,38 +4,47 @@
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "simgrid/s4u.hpp"
+#include <vector>
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
 
-simgrid::s4u::ExecPtr child;
-
 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();
 
+  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);
 
-  child = simgrid::s4u::this_actor::exec_init(computation_amount);
+  // Name the activities (for logging purposes only)
+  first_parent->set_name("parent 1");
+  second_parent->set_name("parent 2");
+  child->set_name("child");
 
+  // Create the dependencies by declaring 'child' as a successor of first_parent and second_parent
   first_parent->add_successor(child.get());
   second_parent->add_successor(child.get());
-  second_parent->start();
-  first_parent->wait();
-  second_parent->wait();
-}
 
-static void vetoed_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();
-  while (not child->test()) {
-    if (child->get_state() == simgrid::s4u::Exec::State::STARTING)
-      XBT_INFO("child cannot start yet");
-    else
-      XBT_INFO("child is now in state %d", (int)child->get_state());
-    simgrid::s4u::this_actor::sleep_for(0.25);
+
+  // 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);
   }
-  XBT_INFO("Should be okay now, child is in state %d", (int)child->get_state());
 }
 
 int main(int argc, char* argv[])
@@ -44,7 +53,6 @@ int main(int argc, char* argv[])
   e.load_platform(argv[1]);
 
   simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Fafard"), worker);
-  simgrid::s4u::Actor::create("vetoed_worker", simgrid::s4u::Host::by_name("Fafard"), vetoed_worker);
 
   e.run();
 
index 2c274ff..dab01ae 100644 (file)
@@ -1,21 +1,11 @@
 #!/usr/bin/env tesh
 
-$ ${bindir:=.}/s4u-exec-dependent ${platfdir}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
-> [  0.000000] (2:vetoed_worker@Fafard) child cannot start yet
-> [  0.250000] (2:vetoed_worker@Fafard) child cannot start yet
-> [  0.500000] (2:vetoed_worker@Fafard) child cannot start yet
-> [  0.750000] (2:vetoed_worker@Fafard) child cannot start yet
-> [  1.000000] (2:vetoed_worker@Fafard) child cannot start yet
-> [  1.250000] (2:vetoed_worker@Fafard) child cannot start yet
-> [  1.500000] (2:vetoed_worker@Fafard) child cannot start yet
-> [  1.750000] (2:vetoed_worker@Fafard) child cannot start yet
-> [  2.000000] (2:vetoed_worker@Fafard) child cannot start yet
-> [  2.250000] (2:vetoed_worker@Fafard) child cannot start yet
-> [  2.500000] (2:vetoed_worker@Fafard) child cannot start yet
-> [  2.750000] (2:vetoed_worker@Fafard) child cannot start yet
-> [  3.000000] (2:vetoed_worker@Fafard) child is now in state 2
-> [  3.250000] (2:vetoed_worker@Fafard) child is now in state 2
-> [  3.500000] (2:vetoed_worker@Fafard) child is now in state 2
-> [  3.750000] (2:vetoed_worker@Fafard) child is now in state 2
-> [  4.000000] (2:vetoed_worker@Fafard) Should be okay now, child is in state 5
+! output sort
+$ ${bindir:=.}/s4u-exec-dependent ${platfdir}/small_platform.xml --log=s4u_activity.t:debug "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
+> [  2.000000] (1:worker@Fafard) Remove a dependency from 'parent 1' on 'child'
+> [  2.000000] (1:worker@Fafard) Exec 'parent 1' is complete
+> [  3.000000] (1:worker@Fafard) Remove a dependency from 'parent 2' on 'child'
+> [  3.000000] (1:worker@Fafard) All dependencies are solved, let's start 'child'
+> [  3.000000] (1:worker@Fafard) Exec 'parent 2' is complete
+> [  4.000000] (1:worker@Fafard) Exec 'child' is complete
 > [  4.000000] (0:maestro@) Simulation time 4
index b5f609f..da23b82 100644 (file)
 #include <simgrid/forward.h>
 #include <string>
 #include <vector>
+#include <xbt/log.hpp>
 #include <xbt/signal.hpp>
 
+XBT_LOG_EXTERNAL_CATEGORY(s4u_activity);
+
 namespace simgrid {
 namespace s4u {
 
@@ -89,7 +92,7 @@ private:
 
 template <class AnyActivity> class Activity_T : public Activity {
 private:
-  std::string name_             = "";
+  std::string name_             = "unnamed";
   std::string tracing_category_ = "";
   void* user_data_              = nullptr;
   std::atomic_int_fast32_t refcount_{0};
@@ -124,6 +127,7 @@ public:
   {
     while (has_successors()) {
       AnyActivity* b = get_successor();
+      XBT_CDEBUG(s4u_activity, "Remove a dependency from '%s' on '%s'", get_cname(), b->get_cname());
       b->remove_dependency_on(static_cast<AnyActivity*>(this));
       if (not b->has_dependencies()) {
         b->vetoable_start();
@@ -138,6 +142,7 @@ public:
     if (has_dependencies())
       return static_cast<AnyActivity*>(this);
     set_state(State::STARTED);
+    XBT_CDEBUG(s4u_activity, "All dependencies are solved, let's start '%s'", get_cname());
     static_cast<AnyActivity*>(this)->start();
     return static_cast<AnyActivity*>(this);
   }
index 4ce71e5..ba92439 100644 (file)
@@ -47,7 +47,7 @@ Exec* Exec::wait()
 Exec* Exec::wait_for(double timeout)
 {
   if (state_ == State::INITED)
-    start();
+    vetoable_start();
   simcall_execution_wait(pimpl_, timeout);
   state_ = State::FINISHED;
   on_completion(*Actor::self(), *this);
@@ -60,7 +60,11 @@ int Exec::wait_any_for(std::vector<ExecPtr>* execs, double timeout)
   std::unique_ptr<kernel::activity::ExecImpl* []> rexecs(new kernel::activity::ExecImpl*[execs->size()]);
   std::transform(begin(*execs), end(*execs), rexecs.get(),
                  [](const ExecPtr& exec) { return static_cast<kernel::activity::ExecImpl*>(exec->pimpl_.get()); });
-  return simcall_execution_waitany_for(rexecs.get(), execs->size(), timeout);
+
+  int changed_pos = simcall_execution_waitany_for(rexecs.get(), execs->size(), timeout);
+  if (changed_pos != -1)
+    execs->at(changed_pos)->release_dependencies();
+  return changed_pos;
 }
 
 Exec* Exec::cancel()