Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into depencencies
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Wed, 11 Dec 2019 14:13:08 +0000 (15:13 +0100)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Wed, 11 Dec 2019 14:13:08 +0000 (15:13 +0100)
MANIFEST.in
examples/s4u/CMakeLists.txt
examples/s4u/exec-dependent/s4u-exec-dependent.cpp [new file with mode: 0644]
examples/s4u/exec-dependent/s4u-exec-dependent.tesh [new file with mode: 0644]
include/simgrid/s4u/Activity.hpp
src/s4u/s4u_Activity.cpp
src/s4u/s4u_Exec.cpp

index e8e2913..6fe863b 100644 (file)
@@ -368,6 +368,8 @@ include examples/s4u/exec-async/s4u-exec-async.cpp
 include examples/s4u/exec-async/s4u-exec-async.tesh
 include examples/s4u/exec-basic/s4u-exec-basic.cpp
 include examples/s4u/exec-basic/s4u-exec-basic.tesh
+include examples/s4u/exec-dependent/s4u-exec-dependent.cpp
+include examples/s4u/exec-dependent/s4u-exec-dependent.tesh
 include examples/s4u/exec-dvfs/s4u-exec-dvfs.cpp
 include examples/s4u/exec-dvfs/s4u-exec-dvfs.tesh
 include examples/s4u/exec-ptask/s4u-exec-ptask.cpp
index 7565b6c..839e9cb 100644 (file)
@@ -9,7 +9,7 @@ foreach (example actor-create actor-daemon actor-exiting actor-join actor-kill
                  cloud-capping cloud-migration cloud-simple
                  energy-exec energy-boot energy-link energy-vm energy-exec-ptask
                  engine-filtering
-                 exec-async exec-basic exec-dvfs exec-ptask exec-remote exec-waitany
+                 exec-async exec-basic exec-dvfs exec-ptask exec-remote exec-waitany exec-dependent
                  io-async io-file-system io-file-remote io-disk-raw
                  platform-failures platform-profile platform-properties
                  plugin-hostload
diff --git a/examples/s4u/exec-dependent/s4u-exec-dependent.cpp b/examples/s4u/exec-dependent/s4u-exec-dependent.cpp
new file mode 100644 (file)
index 0000000..9f373a1
--- /dev/null
@@ -0,0 +1,54 @@
+/* Copyright (c) 2007-2019. 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/s4u.hpp"
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
+
+simgrid::s4u::ExecPtr child;
+
+static void worker()
+{
+  double computation_amount = simgrid::s4u::this_actor::get_host()->get_speed();
+
+  simgrid::s4u::ExecPtr first_parent  = simgrid::s4u::this_actor::exec_init(computation_amount);
+  simgrid::s4u::ExecPtr second_parent = simgrid::s4u::this_actor::exec_init(2 * computation_amount);
+
+  child = simgrid::s4u::this_actor::exec_init(computation_amount);
+
+  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()
+{
+  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);
+  }
+  XBT_INFO("Should be okay now, child is in state %d", (int)child->get_state());
+}
+
+int main(int argc, char* argv[])
+{
+  simgrid::s4u::Engine e(&argc, 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();
+
+  XBT_INFO("Simulation time %g", e.get_clock());
+
+  return 0;
+}
diff --git a/examples/s4u/exec-dependent/s4u-exec-dependent.tesh b/examples/s4u/exec-dependent/s4u-exec-dependent.tesh
new file mode 100644 (file)
index 0000000..2c274ff
--- /dev/null
@@ -0,0 +1,21 @@
+#!/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
+> [  4.000000] (0:maestro@) Simulation time 4
index c7bc18d..34d41e0 100644 (file)
@@ -7,9 +7,12 @@
 #define SIMGRID_S4U_ACTIVITY_HPP
 
 #include "xbt/asserts.h"
+#include "xbt/log.h"
 #include <atomic>
+#include <set>
 #include <simgrid/forward.h>
 #include <string>
+#include <vector>
 #include <xbt/signal.hpp>
 
 namespace simgrid {
@@ -38,14 +41,13 @@ class XBT_PUBLIC Activity {
 protected:
   Activity()  = default;
   virtual ~Activity() = default;
-
 public:
 #ifndef DOXYGEN
   Activity(Activity const&) = delete;
   Activity& operator=(Activity const&) = delete;
 #endif
 
-  enum class State { INITED = 0, STARTED, CANCELED, ERRORED, FINISHED };
+  enum class State { INITED = 0, STARTING, STARTED, CANCELED, ERRORED, FINISHED };
 
   /** Starts a previously created activity.
    *
@@ -65,6 +67,7 @@ public:
   virtual Activity* cancel() = 0;
   /** Retrieve the current state of the activity */
   Activity::State get_state() { return state_; }
+  void set_state(Activity::State state) { state_ = state; }
   /** Tests whether the given activity is terminated yet. This is a pure function. */
   virtual bool test() = 0;
 
@@ -85,12 +88,31 @@ private:
   double remains_                          = 0;
 };
 
+// template <class AnyActivity> class DependencyGuard {
+// public:
+//  static bool activity_start_vetoer(AnyActivity* a) { return not a->has_dependencies(); }
+//  static void on_activity_done(AnyActivity* a);
+////  {
+////    while (a->has_successors()) {
+////      AnyActivity* b = a->get_successor();
+////      b->remove_dependency_on(a);
+////      if (not b->has_dependencies()) {
+////        XBT_INFO("Activity is done and a successor can start");
+////        b->vetoable_start();
+////      }
+////      a->remove_successor();
+////    }
+////  }
+//};
+
 template <class AnyActivity> class Activity_T : public Activity {
 private:
   std::string name_             = "";
   std::string tracing_category_ = "";
   void* user_data_              = nullptr;
   std::atomic_int_fast32_t refcount_{0};
+  std::vector<AnyActivity*> successors_;
+  std::set<AnyActivity*> dependencies_;
 
 public:
 #ifndef DOXYGEN
@@ -103,6 +125,43 @@ public:
   }
   friend void intrusive_ptr_add_ref(AnyActivity* a) { a->refcount_.fetch_add(1, std::memory_order_relaxed); }
 #endif
+
+  void add_successor(AnyActivity* a)
+  {
+    successors_.push_back(a);
+    a->add_dependency_on(static_cast<AnyActivity*>(this));
+  }
+  void remove_successor() { successors_.pop_back(); }
+  AnyActivity* get_successor() { return successors_.back(); }
+  bool has_successors() { return not successors_.empty(); }
+
+  void add_dependency_on(AnyActivity* a) { dependencies_.insert({a}); }
+  void remove_dependency_on(AnyActivity* a) { dependencies_.erase(a); }
+  bool has_dependencies() { return not dependencies_.empty(); }
+  void release_dependencies()
+  {
+    while (has_successors()) {
+      AnyActivity* b = get_successor();
+      b->remove_dependency_on(static_cast<AnyActivity*>(this));
+      if (not b->has_dependencies()) {
+        // XBT_INFO("Activity is done and a successor can start");
+        b->vetoable_start();
+      }
+      remove_successor();
+    }
+  }
+
+  AnyActivity* vetoable_start()
+  {
+    set_state(State::STARTING);
+    if (has_dependencies())
+      return static_cast<AnyActivity*>(this);
+    //    XBT_INFO("No veto, Activity can start");
+    set_state(State::STARTED);
+    static_cast<AnyActivity*>(this)->start();
+    return static_cast<AnyActivity*>(this);
+  }
+
   AnyActivity* set_name(const std::string& name)
   {
     xbt_assert(get_state() == State::INITED, "Cannot change the name of an activity after its start");
index e52b3f2..14f2880 100644 (file)
@@ -7,7 +7,6 @@
 
 #include "simgrid/s4u/Activity.hpp"
 #include "simgrid/s4u/Engine.hpp"
-
 XBT_LOG_EXTERNAL_CATEGORY(s4u);
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_activity, s4u, "S4U activities");
 
index ffe6962..cf38325 100644 (file)
@@ -22,13 +22,14 @@ Exec::Exec()
 
 bool Exec::test()
 {
-  xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
+  xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
+             state_ == State::FINISHED);
 
   if (state_ == State::FINISHED)
     return true;
 
-  if (state_ == State::INITED)
-    this->start();
+  if (state_ == State::INITED || state_ == State::STARTING)
+    this->vetoable_start();
 
   if (simcall_execution_test(pimpl_)) {
     state_ = State::FINISHED;
@@ -45,6 +46,7 @@ Exec* Exec::wait()
   simcall_execution_wait(pimpl_);
   state_ = State::FINISHED;
   on_completion(*Actor::self(), *this);
+  this->release_dependencies();
   return this;
 }