Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
py: another example: actor-join
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Thu, 3 Jan 2019 21:37:56 +0000 (22:37 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Thu, 3 Jan 2019 21:37:56 +0000 (22:37 +0100)
examples/python/CMakeLists.txt
examples/python/actor-join/actor-join.py [new file with mode: 0644]
examples/python/actor-join/actor-join.tesh [new file with mode: 0644]
examples/s4u/README.rst
include/simgrid/s4u/Actor.hpp
include/simgrid/s4u/Host.hpp
src/bindings/python/simgrid_python.cpp

index 435245d..b230d30 100644 (file)
@@ -1,4 +1,5 @@
-foreach(example actor-create actor-migration actor-yield exec-basic)
+foreach(example actor-create actor-join actor-migration actor-yield 
+                exec-basic)
   set(tesh_files    ${tesh_files}   ${CMAKE_CURRENT_SOURCE_DIR}/${example}/${example}.tesh)
   set(examples_src  ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/${example}/${example}.py)
 
   set(tesh_files    ${tesh_files}   ${CMAKE_CURRENT_SOURCE_DIR}/${example}/${example}.tesh)
   set(examples_src  ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/${example}/${example}.py)
 
diff --git a/examples/python/actor-join/actor-join.py b/examples/python/actor-join/actor-join.py
new file mode 100644 (file)
index 0000000..bf8cfc2
--- /dev/null
@@ -0,0 +1,53 @@
+# Copyright (c) 2017-2018. 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.
+
+from simgrid import *
+import sys
+
+def sleeper():
+    this_actor.info("Sleeper started")
+    this_actor.sleep_for(3)
+    this_actor.info("I'm done. See you!")
+
+def master():
+  this_actor.info("Start sleeper")
+  actor = Actor.create("sleeper from master", Host.current(), sleeper)
+  this_actor.info("Join the sleeper (timeout 2)")
+  actor.join(2)
+
+  this_actor.info("Start sleeper")
+  actor = Actor.create("sleeper from master", Host.current(), sleeper)
+  this_actor.info("Join the sleeper (timeout 4)")
+  actor.join(4)
+
+  this_actor.info("Start sleeper")
+  actor = Actor.create("sleeper from master", Host.current(), sleeper)
+  this_actor.info("Join the sleeper (timeout 2)")
+  actor.join(2)
+
+  this_actor.info("Start sleeper")
+  actor = Actor.create("sleeper from master", Host.current(), sleeper)
+  this_actor.info("Waiting 4")
+  this_actor.sleep_for(4)
+  this_actor.info("Join the sleeper after its end (timeout 1)")
+  actor.join(1)
+
+  this_actor.info("Goodbye now!")
+
+  this_actor.sleep_for(1)
+
+  this_actor.info("Goodbye now!")
+
+if __name__ == '__main__':
+    e = Engine(sys.argv)
+    if len(sys.argv) < 2: raise AssertionError("Usage: actor-join.py platform_file [other parameters]")
+
+    e.load_platform(sys.argv[1])
+
+    Actor.create("master", Host.by_name("Tremblay"), master)
+
+    e.run()
+
+    this_actor.info("Simulation time {}".format(Engine.get_clock()))
diff --git a/examples/python/actor-join/actor-join.tesh b/examples/python/actor-join/actor-join.tesh
new file mode 100644 (file)
index 0000000..4fc199e
--- /dev/null
@@ -0,0 +1,21 @@
+$ python3 ${bindir:=.}/actor-join.py ${platfdir}/small_platform.xml
+> [Tremblay:master:(1) 0.000000] [python/INFO] Start sleeper
+> [Tremblay:sleeper from master:(2) 0.000000] [python/INFO] Sleeper started
+> [Tremblay:master:(1) 0.000000] [python/INFO] Join the sleeper (timeout 2)
+> [Tremblay:master:(1) 2.000000] [python/INFO] Start sleeper
+> [Tremblay:sleeper from master:(3) 2.000000] [python/INFO] Sleeper started
+> [Tremblay:master:(1) 2.000000] [python/INFO] Join the sleeper (timeout 4)
+> [Tremblay:sleeper from master:(2) 3.000000] [python/INFO] I'm done. See you!
+> [Tremblay:sleeper from master:(3) 5.000000] [python/INFO] I'm done. See you!
+> [Tremblay:master:(1) 5.000000] [python/INFO] Start sleeper
+> [Tremblay:sleeper from master:(4) 5.000000] [python/INFO] Sleeper started
+> [Tremblay:master:(1) 5.000000] [python/INFO] Join the sleeper (timeout 2)
+> [Tremblay:master:(1) 7.000000] [python/INFO] Start sleeper
+> [Tremblay:sleeper from master:(5) 7.000000] [python/INFO] Sleeper started
+> [Tremblay:master:(1) 7.000000] [python/INFO] Waiting 4
+> [Tremblay:sleeper from master:(4) 8.000000] [python/INFO] I'm done. See you!
+> [Tremblay:sleeper from master:(5) 10.000000] [python/INFO] I'm done. See you!
+> [Tremblay:master:(1) 11.000000] [python/INFO] Join the sleeper after its end (timeout 1)
+> [Tremblay:master:(1) 11.000000] [python/INFO] Goodbye now!
+> [Tremblay:master:(1) 12.000000] [python/INFO] Goodbye now!
+> [12.000000] [python/INFO] Simulation time 12.0
index 6d45a6a..48bded2 100644 (file)
@@ -75,9 +75,11 @@ Inter-Actors Interactions
     :py:func:`simgrid.this_actor.migrate()` |py|
 
   - **Waiting for the termination of an actor:** (joining on it)
     :py:func:`simgrid.this_actor.migrate()` |py|
 
   - **Waiting for the termination of an actor:** (joining on it)
-    :cpp:func:`simgrid::s4u::Actor::join()` allows to block the current
-    actor until the end of the receiving actor.
-    |br| `examples/s4u/actor-join/s4u-actor-join.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-join/s4u-actor-join.cpp>`_
+    You can block the current actor until the end of another actor.
+    |br| |cpp| `examples/s4u/actor-join/s4u-actor-join.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-join/s4u-actor-join.cpp>`_
+    :cpp:func:`simgrid::s4u::Actor::join()` |cpp|
+    |br| |py| `examples/python/actor-join/actor-join.py <https://framagit.org/simgrid/simgrid/tree/master/examples/python/actor-join/actor-join.py>`_
+    :cpp:func:`simgrid.Actor.join()` |py|
 
   - **Yielding to other actors**.
     The ```yield()``` function interrupts the execution of the current
 
   - **Yielding to other actors**.
     The ```yield()``` function interrupts the execution of the current
index 64fd9bc..87b0082 100644 (file)
@@ -268,9 +268,16 @@ public:
 
   /** Wait for the actor to finish.
    *
 
   /** Wait for the actor to finish.
    *
-   * This blocks the calling actor until the actor on which we call join() is terminated
+   * Blocks the calling actor until the joined actor is terminated. If actor alice executes bob.join(), then alice is
+   * blocked until bob terminates.
    */
   void join();
    */
   void join();
+
+  /** Wait for the actor to finish, or for the timeout to elapse.
+   *
+   * Blocks the calling actor until the joined actor is terminated. If actor alice executes bob.join(), then alice is
+   * blocked until bob terminates.
+   */
   void join(double timeout);
   Actor* restart();
 
   void join(double timeout);
   Actor* restart();
 
index 332ae45..88ac89d 100644 (file)
@@ -69,7 +69,7 @@ public:
   static Host* by_name_or_null(std::string name);
   /** Retrieve a host from its name, or die */
   static s4u::Host* by_name(std::string name);
   static Host* by_name_or_null(std::string name);
   /** Retrieve a host from its name, or die */
   static s4u::Host* by_name(std::string name);
-  /** Retrieve the host on which the current actor is running */
+  /** Retrieves the host on which the running actor is located */
   static s4u::Host* current();
 
   /** Retrieves the name of that host as a C++ string */
   static s4u::Host* current();
 
   /** Retrieves the name of that host as a C++ string */
index 68fb123..d42f180 100644 (file)
@@ -79,6 +79,7 @@ PYBIND11_MODULE(simgrid, m)
         // Currently this can be dangling, we should wrap this somehow.
         return new simgrid::s4u::Engine(&argc, argv.get());
       }))
         // Currently this can be dangling, we should wrap this somehow.
         return new simgrid::s4u::Engine(&argc, argv.get());
       }))
+      .def("get_clock", &Engine::get_clock, "Retrieve the simulation time")
       .def("load_platform", &Engine::load_platform,
           "Load a platform file describing the environment, see :cpp:func:`simgrid::s4u::Engine::load_platform()`")
       .def("load_deployment", &Engine::load_deployment,
       .def("load_platform", &Engine::load_platform,
           "Load a platform file describing the environment, see :cpp:func:`simgrid::s4u::Engine::load_platform()`")
       .def("load_deployment", &Engine::load_deployment,
@@ -109,7 +110,8 @@ PYBIND11_MODULE(simgrid, m)
 
   /* Class Host */
   py::class_<simgrid::s4u::Host, std::unique_ptr<Host, py::nodelete>>(m, "Host", "Simulation Engine, see :ref:`class s4u::Host <API_s4u_Host>`")
 
   /* Class Host */
   py::class_<simgrid::s4u::Host, std::unique_ptr<Host, py::nodelete>>(m, "Host", "Simulation Engine, see :ref:`class s4u::Host <API_s4u_Host>`")
-      .def("by_name", &Host::by_name, "Retrieve a host from its name, or die")
+      .def("by_name", &Host::by_name, "Retrieves a host from its name, or die")
+      .def("current", &Host::current, "Retrieves the host on which the running actor is located, see :cpp:func:`simgrid::s4u::Host::current()`")
       .def_property_readonly("name", [](Host* self) -> const std::string {
            return static_cast<std::string>(self->get_name());
         }, "Retrieve the name of this host")
       .def_property_readonly("name", [](Host* self) -> const std::string {
            return static_cast<std::string>(self->get_name());
         }, "Retrieve the name of this host")
@@ -152,8 +154,10 @@ PYBIND11_MODULE(simgrid, m)
            },
            "Create an actor from a function or an object, see :cpp:func:`simgrid::s4u::Actor::create()`")
       .def_property("host", &Actor::get_host, &Actor::migrate, "The host on which this actor is located")
            },
            "Create an actor from a function or an object, see :cpp:func:`simgrid::s4u::Actor::create()`")
       .def_property("host", &Actor::get_host, &Actor::migrate, "The host on which this actor is located")
+      .def("join", py::overload_cast<double>(&Actor::join), "Wait for the actor to finish, see :cpp:func:`void simgrid::s4u::Actor::join(double)`",
+          py::arg("timeout"))
       .def("migrate", &Actor::migrate, "Moves that actor to another host, see :cpp:func:`void simgrid::s4u::Actor::migrate()`",
       .def("migrate", &Actor::migrate, "Moves that actor to another host, see :cpp:func:`void simgrid::s4u::Actor::migrate()`",
-               py::arg("dest"))
+          py::arg("dest"))
       .def("suspend", &Actor::suspend, "Suspend that actor, that is blocked until resume()ed by another actor. See :cpp:func:`void simgrid::s4u::Actor::suspend()`")
       .def("resume", &Actor::resume, "Resume that actor, that was previously suspend()ed. See :cpp:func:`void simgrid::s4u::Actor::suspend()`");
 
       .def("suspend", &Actor::suspend, "Suspend that actor, that is blocked until resume()ed by another actor. See :cpp:func:`void simgrid::s4u::Actor::suspend()`")
       .def("resume", &Actor::resume, "Resume that actor, that was previously suspend()ed. See :cpp:func:`void simgrid::s4u::Actor::suspend()`");