Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Python: many improvements; add a new example (actor-migration)
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Thu, 3 Jan 2019 20:37:12 +0000 (21:37 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Thu, 3 Jan 2019 20:58:49 +0000 (21:58 +0100)
Also, improve a bit the C++ documentation

examples/python/CMakeLists.txt
examples/python/actor-create/actor-create.py
examples/python/actor-migration/actor-migration.py [new file with mode: 0644]
examples/python/actor-migration/actor-migration.tesh [new file with mode: 0644]
examples/s4u/README.rst
include/simgrid/s4u/Actor.hpp
src/bindings/python/simgrid_python.cpp

index fdfb6ce..435245d 100644 (file)
@@ -1,4 +1,4 @@
-foreach(example actor-create actor-yield exec-basic)
+foreach(example actor-create 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)
 
index df5062a..9139acb 100644 (file)
@@ -25,7 +25,7 @@ from simgrid import *
 def receiver(mailbox_name):
   mailbox = Mailbox.by_name(mailbox_name)
 
-  this_actor.info("Hello s4u, I'm ready to get any message you'd want on {:s}".format(mailbox.get_name()))
+  this_actor.info("Hello s4u, I'm ready to get any message you'd want on {:s}".format(mailbox.name))
 
   msg1 = mailbox.get()
   msg2 = mailbox.get()
diff --git a/examples/python/actor-migration/actor-migration.py b/examples/python/actor-migration/actor-migration.py
new file mode 100644 (file)
index 0000000..7e84fc4
--- /dev/null
@@ -0,0 +1,60 @@
+# 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.
+
+# This example demonstrate the actor migrations.
+#
+# The worker actor first move by itself, and then start an execution.
+# During that execution, the monitor migrates the worker, that wakes up on another host.
+# The execution was of the right amount of flops to take exactly 5 seconds on the first host
+# and 5 other seconds on the second one, so it stops after 10 seconds.
+#
+# Then another migration is done by the monitor while the worker is suspended.
+#
+# Note that worker() takes an uncommon set of parameters,
+# and that this is perfectly accepted by create().
+
+from simgrid import *
+import sys
+
+def worker(first_host, second_host):
+    flop_amount = first_host.speed * 5 + second_host.speed * 5
+
+    this_actor.info("Let's move to {:s} to execute {:.2f} Mflops (5sec on {:s} and 5sec on {:s})".format(first_host.name, flop_amount / 1e6, first_host.name, second_host.name))
+
+    this_actor.migrate(first_host)
+    this_actor.execute(flop_amount)
+
+    this_actor.info("I wake up on {:s}. Let's suspend a bit".format(this_actor.get_host().name))
+
+    this_actor.suspend()
+
+    this_actor.info("I wake up on {:s}".format(this_actor.get_host().name))
+    this_actor.info("Done")
+
+def monitor():
+  boivin    = Host.by_name("Boivin")
+  jacquelin = Host.by_name("Jacquelin")
+  fafard    = Host.by_name("Fafard")
+
+  actor = Actor.create("worker", fafard, worker, boivin, jacquelin)
+
+  this_actor.sleep_for(5)
+
+  this_actor.info("After 5 seconds, move the process to {:s}".format(jacquelin.name))
+  actor.migrate(jacquelin)
+
+  this_actor.sleep_until(15)
+  this_actor.info("At t=15, move the process to {:s} and resume it.".format(fafard.name))
+  actor.migrate(fafard)
+  actor.resume()
+
+if __name__ == '__main__':
+    e = Engine(sys.argv)
+    if len(sys.argv) < 2: raise AssertionError("Usage: actor-migration.py platform_file [other parameters]")
+    e.load_platform(sys.argv[1])   
+
+    Actor.create("monitor", Host.by_name("Boivin"), monitor)
+    e.run()
+
diff --git a/examples/python/actor-migration/actor-migration.tesh b/examples/python/actor-migration/actor-migration.tesh
new file mode 100644 (file)
index 0000000..50745bf
--- /dev/null
@@ -0,0 +1,11 @@
+#!/usr/bin/env tesh
+
+p Testing the actor migration feature 
+
+$ python3 ${bindir:=.}/actor-migration.py ${platfdir}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%P@%h)%e%m%n"
+> [  0.000000] (worker@Fafard) Let's move to Boivin to execute 1177.14 Mflops (5sec on Boivin and 5sec on Jacquelin)
+> [  5.000000] (monitor@Boivin) After 5 seconds, move the process to Jacquelin
+> [ 10.000000] (worker@Jacquelin) I wake up on Jacquelin. Let's suspend a bit
+> [ 15.000000] (monitor@Boivin) At t=15, move the process to Fafard and resume it.
+> [ 15.000000] (worker@Fafard) I wake up on Fafard
+> [ 15.000000] (worker@Fafard) Done
index e04fb14..6d45a6a 100644 (file)
@@ -68,9 +68,11 @@ Inter-Actors Interactions
     |br| `examples/s4u/actor-suspend/s4u-actor-suspend.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-suspend/s4u-actor-suspend.cpp>`_
 
   - **Migrating Actors:**
-    Actors can move or be moved from a host to another with
-    :cpp:func:`simgrid::s4u::this_actor::migrate()`.
-    |br| `examples/s4u/actor-migration/s4u-actor-migration.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-migration/s4u-actor-migration.cpp>`_
+    Actors can move or be moved from a host to another very easily.
+    |br| |cpp| `examples/s4u/actor-migration/s4u-actor-migration.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-migration/s4u-actor-migration.cpp>`_
+    :cpp:func:`simgrid::s4u::this_actor::migrate()` |cpp|
+    |br|  |py| `examples/python/actor-migration/actor-migration.py <https://framagit.org/simgrid/simgrid/tree/master/examples/python/actor-migration/actor-migration.py>`_
+    :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
@@ -82,9 +84,9 @@ Inter-Actors Interactions
     actor, leaving a chance to the other actors that are ready to run
     at this timestamp. 
     |br| |cpp| `examples/s4u/actor-yield/s4u-actor-yield.cpp <https://framagit.org/simgrid/simgrid/tree/master/examples/s4u/actor-yield/s4u-actor-yield.cpp>`_
-    :cpp:func:`simgrid::s4u::this_actor::yield()`
+    :cpp:func:`simgrid::s4u::this_actor::yield()` |cpp|
     |br|  |py| `examples/python/actor-yield/actor-yield.py <https://framagit.org/simgrid/simgrid/tree/master/examples/python/actor-yield/actor-yield.py>`_
-    :py:func:`simgrid.this_actor.yield_()`
+    :py:func:`simgrid.this_actor.yield_()` |py|
 
 Traces Replay as a Workload
 ---------------------------
index 2be0ba3..64fd9bc 100644 (file)
@@ -387,8 +387,9 @@ namespace this_actor {
 
 XBT_PUBLIC bool is_maestro();
 
-/** Block the actor sleeping for that amount of seconds (may throws hostFailure) */
+/** Block the actor sleeping for that amount of seconds (may throw hostFailure) */
 XBT_PUBLIC void sleep_for(double duration);
+/** Block the actor sleeping until the specified timestamp (may throw hostFailure) */
 XBT_PUBLIC void sleep_until(double timeout);
 
 template <class Rep, class Period> inline void sleep_for(std::chrono::duration<Rep, Period> duration)
@@ -499,13 +500,13 @@ XBT_PUBLIC const char* get_cname();
 /** @brief Returns the name of the host on which the actor is running. */
 XBT_PUBLIC Host* get_host();
 
-/** @brief Suspend the actor. */
+/** @brief Suspend the actor, that is blocked until resume()ed by another actor. */
 XBT_PUBLIC void suspend();
 
 /** @brief Yield the actor. */
 XBT_PUBLIC void yield();
 
-/** @brief Resume the actor. */
+/** @brief Resume the actor, that was suspend()ed previously. */
 XBT_PUBLIC void resume();
 
 XBT_PUBLIC bool is_suspended();
index 38b0287..68fb123 100644 (file)
@@ -3,26 +3,18 @@
 /* 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 <functional>
 #include <memory>
 #include <string>
 #include <vector>
 
-#include <pybind11/functional.h>
 #include <pybind11/pybind11.h>
 #include <pybind11/stl.h>
 
-#include <simgrid/config.h>
-#include <xbt/log.h>
-#include <xbt/string.hpp>
-
 #include <simgrid/s4u/Actor.hpp>
 #include <simgrid/s4u/Engine.hpp>
 #include <simgrid/s4u/Host.hpp>
 #include <simgrid/s4u/Mailbox.hpp>
 
-#include <boost/intrusive_ptr.hpp>
-
 namespace py = pybind11;
 using simgrid::s4u::Actor;
 using simgrid::s4u::ActorPtr;
@@ -55,12 +47,23 @@ PYBIND11_MODULE(simgrid, m)
   m.attr("simgrid_version") = simgrid_version;
 
   /* this_actor namespace */
+  void (*sleep_for_fun)(double) = &simgrid::s4u::this_actor::sleep_for; // pick the right overload
+  void (*sleep_until_fun)(double) = &simgrid::s4u::this_actor::sleep_until;
+
   py::module m2 = m.def_submodule("this_actor", "Bindings of the s4u::this_actor namespace.");
   m2.def("info", [](char* s) { XBT_INFO("%s", s); }, "Display a logging message of default priority.");
   m2.def("execute", py::overload_cast<double, double>(&simgrid::s4u::this_actor::execute),
          "Block the current actor, computing the given amount of flops at the given priority, see :cpp:func:`void "
          "simgrid::s4u::this_actor::execute(double, double)`",
          py::arg("flops"), py::arg("priority") = 1);
+  m2.def("get_host", &simgrid::s4u::this_actor::get_host, "Retrives host on which the current actor is located");
+  m2.def("migrate", &simgrid::s4u::this_actor::migrate, "Moves the current actor to another host, see :cpp:func:`void simgrid::s4u::this_actor::migrate()`",
+      py::arg("dest"));
+  m2.def("sleep_for", sleep_for_fun,
+      "Block the actor sleeping for that amount of seconds, see :cpp:func:`void simgrid::s4u::this_actor::sleep_for`", py::arg("duration"));
+  m2.def("sleep_until", sleep_until_fun,
+      "Block the actor sleeping until the specified timestamp, see :cpp:func:`void simgrid::s4u::this_actor::sleep_until`", py::arg("duration"));
+  m2.def("suspend", &simgrid::s4u::this_actor::suspend, "Suspend the current actor, that is blocked until resume()ed by another actor. see :cpp:func:`void simgrid::s4u::this_actor::suspend`");
   m2.def("yield_", &simgrid::s4u::this_actor::yield,
          "Yield the actor, see :cpp:func:`void simgrid::s4u::this_actor::yield()`");
 
@@ -105,20 +108,18 @@ PYBIND11_MODULE(simgrid, m)
       ;
 
   /* Class Host */
-  auto get_name = [](const Host* self) {
-    return self->get_name();
-  };
   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("get_name", &Host::get_name, "Retrieve the name of this host")
-      .def_property_readonly("name", 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")
       .def_property_readonly("speed", &Host::get_speed,
           "Get the peak computing speed in flops/s at the current pstate, taking the external load into account, see :cpp:func:`simgrid::s4u::Host::get_speed()`");
 
   /* Class Mailbox */
   py::class_<simgrid::s4u::Mailbox, std::unique_ptr<Mailbox, py::nodelete>>(m, "Mailbox", "Mailbox, see :ref:`class s4u::Mailbox <API_s4u_Mailbox>`")
       .def("by_name", &Mailbox::by_name, "Retrieve a Mailbox from its name, see :cpp:func:`simgrid::s4u::Mailbox::by_name()`")
-      .def("get_name", &Mailbox::get_name, "Retrieves the name of that host, see :cpp:func:`simgrid::s4u::Mailbox::get_name()`")
+      .def_property_readonly("name", &Mailbox::get_name, "Retrieves the name of that mailbox, see :cpp:func:`simgrid::s4u::Mailbox::get_name()`")
       .def("put", [](Mailbox self, py::object data, int size) {
         data.inc_ref();
         self.put(data.ptr(), size);
@@ -149,5 +150,11 @@ PYBIND11_MODULE(simgrid, m)
                  throw pybind11::error_already_set();
              });
            },
-           "Create an actor from a function or an object, see :cpp:func:`simgrid::s4u::Actor::create()`");
+           "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("migrate", &Actor::migrate, "Moves that actor to another host, see :cpp:func:`void simgrid::s4u::Actor::migrate()`",
+               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()`");
+
 }