Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'add_mutex_python_bindings' into 'master'
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Tue, 15 Mar 2022 08:09:00 +0000 (08:09 +0000)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Tue, 15 Mar 2022 08:09:00 +0000 (08:09 +0000)
Add Mutex Python bindings

See merge request simgrid/simgrid!84

ChangeLog
docs/source/app_s4u.rst
examples/README.rst
examples/cpp/synchro-mutex/s4u-synchro-mutex.cpp
examples/python/CMakeLists.txt
examples/python/synchro-mutex/synchro-mutex.py [new file with mode: 0644]
examples/python/synchro-mutex/synchro-mutex.tesh [new file with mode: 0644]
src/bindings/python/simgrid_python.cpp

index 4108344..76fb566 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -74,11 +74,12 @@ Python:
    - Mailbox: Mailbox.by_name()
  - Added the following bindings:
      - this_actor.warning()
-     - Mailbox.put_init() [example: examples/python/comm-waitallfor]
-     - Comm.detach() [example: examples/python/comm-waitallfor]
+     - Mailbox.put_init() [example: examples/python/comm-waitallfor/]
+     - Comm.detach() [example: examples/python/comm-waitallfor/]
      - Comm.wait_for() [example: examples/python/comm-waitfor/]
      - Comm.wait_any_for()
-     - Comm.wait_all_for() [example: examples/python/comm-waitallfor]
+     - Comm.wait_all_for() [example: examples/python/comm-waitallfor/]
+     - Mutex [example: examples/python/synchro-mutex/]
 
 Build System:
  - Remove target "make uninstall" which was incomplete and no longer maintained.
index eb19f07..be1381f 100644 (file)
@@ -2446,7 +2446,15 @@ Synchronization Objects
 ⁣  Mutex
 ==============
 
-.. doxygenclass:: simgrid::s4u::Mutex
+.. tabs::
+
+   .. group-tab:: C++
+
+      .. doxygenclass:: simgrid::s4u::Mutex
+
+   .. group-tab:: Python
+
+      .. autoclass:: simgrid.Mutex
 
 Basic management
 ----------------
@@ -2463,6 +2471,19 @@ Basic management
 
          .. doxygenfunction:: simgrid::s4u::Mutex::create()
 
+      .. group-tab:: Python
+
+         .. code-block:: Python
+
+            from simgrid import Mutex
+            mutex = Mutex()
+
+            # Use a context manager to acquire and automatically release the mutex
+            # when leaving the scope.
+            with mutex:
+                # Access shared resource ...
+                pass
+
       .. group-tab:: C
 
          .. code-block:: C
@@ -2488,6 +2509,12 @@ Locking
          .. doxygenfunction:: simgrid::s4u::Mutex::try_lock()
          .. doxygenfunction:: simgrid::s4u::Mutex::unlock()
 
+      .. group-tab:: Python
+
+         .. automethod:: simgrid.Mutex.lock()
+         .. automethod:: simgrid.Mutex.try_lock()
+         .. automethod:: simgrid.Mutex.unlock()
+
       .. group-tab:: C
 
          .. doxygenfunction:: sg_mutex_lock(sg_mutex_t mutex)
index c11029c..35ac45f 100644 (file)
@@ -641,6 +641,8 @@ Shows how to use :cpp:type:`simgrid::s4u::Mutex` synchronization objects.
 
    .. example-tab:: examples/cpp/synchro-mutex/s4u-synchro-mutex.cpp
 
+   .. example-tab:: examples/python/synchro-mutex/synchro-mutex.py
+
 Semaphore
 ^^^^^^^^^
 
index 52e2595..648024a 100644 (file)
@@ -49,6 +49,7 @@ static void master()
   /* Create the requested amount of actors pairs. Each pair has a specific mutex and cell in `result`. */
   int result[cfg_actor_count.get()];
 
+  sg4::MutexPtr mutex = sg4::Mutex::create();
   for (int i = 0; i < cfg_actor_count; i++) {
     result[i]           = 0;
     sg4::MutexPtr mutex = sg4::Mutex::create();
index aca123e..d5637d0 100644 (file)
@@ -3,7 +3,8 @@ foreach(example actor-create actor-daemon actor-join actor-kill actor-migrate ac
         comm-wait comm-waitall comm-waitallfor comm-waitany comm-waitfor
         exec-async exec-basic exec-dvfs exec-remote
         platform-profile platform-failures
-        network-nonlinear clusters-multicpu io-degradation exec-cpu-nonlinear)
+        network-nonlinear clusters-multicpu io-degradation exec-cpu-nonlinear
+        synchro-mutex)
   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/synchro-mutex/synchro-mutex.py b/examples/python/synchro-mutex/synchro-mutex.py
new file mode 100644 (file)
index 0000000..3f2d9e0
--- /dev/null
@@ -0,0 +1,66 @@
+from argparse import ArgumentParser
+from dataclasses import dataclass
+import sys
+
+from simgrid import Actor, Engine, Host, Mutex, this_actor
+
+
+def create_parser() -> ArgumentParser:
+    parser = ArgumentParser()
+    parser.add_argument(
+        '--platform',
+        type=str,
+        required=True,
+        help='path to the platform description'
+    )
+    parser.add_argument(
+        '--actors',
+        type=int,
+        default=6,
+        help='how many pairs of actors should be started'
+    )
+    return parser
+
+
+@dataclass
+class ResultHolder:
+    value: int
+
+
+def worker_context_manager(mutex: Mutex, result: ResultHolder):
+    with mutex:
+        this_actor.info(f"Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager")
+        result.value += 1
+    this_actor.info(f"I'm done, good bye")
+
+
+def worker(mutex: Mutex, result: ResultHolder):
+    mutex.lock()
+    this_actor.info("Hello simgrid, I'm ready to compute after a regular lock")
+    result.value += 1
+    mutex.unlock()
+    this_actor.info("I'm done, good bye")
+
+
+def master(settings):
+    results = [ResultHolder(value=0) for _ in range(settings.actors)]
+    for i in range(settings.actors):
+        mutex = Mutex()
+        Actor.create(f"worker-{i}(mgr)", Host.by_name("Jupiter"), worker_context_manager, mutex, results[i])
+        Actor.create(f"worker-{i}", Host.by_name("Tremblay"), worker, mutex, results[i])
+    this_actor.sleep_for(10)
+    for i in range(settings.actors):
+        this_actor.info(f"Result[{i}] -> {results[i].value}")
+    this_actor.info("I'm done, good bye")
+
+
+def main():
+    settings = create_parser().parse_known_args()[0]
+    e = Engine(sys.argv)
+    e.load_platform(settings.platform)
+    Actor.create("master", Host.by_name("Tremblay"), master, settings)
+    e.run()
+
+
+if __name__ == "__main__":
+    main()
diff --git a/examples/python/synchro-mutex/synchro-mutex.tesh b/examples/python/synchro-mutex/synchro-mutex.tesh
new file mode 100644 (file)
index 0000000..2271c0a
--- /dev/null
@@ -0,0 +1,42 @@
+#!/usr/bin/env tesh
+
+p Testing Mutex
+
+$ ${pythoncmd:=python3} ${PYTHON_TOOL_OPTIONS:=} ${bindir:=.}/synchro-mutex.py --platform ${platfdir}/two_hosts.xml --actors 0 "--log=root.fmt:[%10.6r]%e(%i:%a@%h)%e%m%n"
+>[ 10.000000] (1:master@Tremblay) I'm done, good bye
+
+$ ${pythoncmd:=python3} ${PYTHON_TOOL_OPTIONS:=} ${bindir:=.}/synchro-mutex.py --platform ${platfdir}/two_hosts.xml --actors 1 "--log=root.fmt:[%10.6r]%e(%i:%a@%h)%e%m%n"
+>[  0.000000] (2:worker-0(mgr)@Jupiter) Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager
+>[  0.000000] (2:worker-0(mgr)@Jupiter) I'm done, good bye
+>[  0.000000] (3:worker-0@Tremblay) Hello simgrid, I'm ready to compute after a regular lock
+>[  0.000000] (3:worker-0@Tremblay) I'm done, good bye
+>[ 10.000000] (1:master@Tremblay) Result[0] -> 2
+>[ 10.000000] (1:master@Tremblay) I'm done, good bye
+
+$ ${pythoncmd:=python3} ${PYTHON_TOOL_OPTIONS:=} ${bindir:=.}/synchro-mutex.py --platform ${platfdir}/two_hosts.xml --actors 5 "--log=root.fmt:[%10.6r]%e(%i:%a@%h)%e%m%n"
+>[  0.000000] (2:worker-0(mgr)@Jupiter) Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager
+>[  0.000000] (2:worker-0(mgr)@Jupiter) I'm done, good bye
+>[  0.000000] (3:worker-0@Tremblay) Hello simgrid, I'm ready to compute after a regular lock
+>[  0.000000] (3:worker-0@Tremblay) I'm done, good bye
+>[  0.000000] (4:worker-1(mgr)@Jupiter) Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager
+>[  0.000000] (4:worker-1(mgr)@Jupiter) I'm done, good bye
+>[  0.000000] (5:worker-1@Tremblay) Hello simgrid, I'm ready to compute after a regular lock
+>[  0.000000] (5:worker-1@Tremblay) I'm done, good bye
+>[  0.000000] (6:worker-2(mgr)@Jupiter) Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager
+>[  0.000000] (6:worker-2(mgr)@Jupiter) I'm done, good bye
+>[  0.000000] (7:worker-2@Tremblay) Hello simgrid, I'm ready to compute after a regular lock
+>[  0.000000] (7:worker-2@Tremblay) I'm done, good bye
+>[  0.000000] (8:worker-3(mgr)@Jupiter) Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager
+>[  0.000000] (8:worker-3(mgr)@Jupiter) I'm done, good bye
+>[  0.000000] (9:worker-3@Tremblay) Hello simgrid, I'm ready to compute after a regular lock
+>[  0.000000] (9:worker-3@Tremblay) I'm done, good bye
+>[  0.000000] (10:worker-4(mgr)@Jupiter) Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager
+>[  0.000000] (10:worker-4(mgr)@Jupiter) I'm done, good bye
+>[  0.000000] (11:worker-4@Tremblay) Hello simgrid, I'm ready to compute after a regular lock
+>[  0.000000] (11:worker-4@Tremblay) I'm done, good bye
+>[ 10.000000] (1:master@Tremblay) Result[0] -> 2
+>[ 10.000000] (1:master@Tremblay) Result[1] -> 2
+>[ 10.000000] (1:master@Tremblay) Result[2] -> 2
+>[ 10.000000] (1:master@Tremblay) Result[3] -> 2
+>[ 10.000000] (1:master@Tremblay) Result[4] -> 2
+>[ 10.000000] (1:master@Tremblay) I'm done, good bye
index 0b1721b..3603e0d 100644 (file)
@@ -33,6 +33,7 @@
 #include <simgrid/s4u/Host.hpp>
 #include <simgrid/s4u/Link.hpp>
 #include <simgrid/s4u/Mailbox.hpp>
+#include <simgrid/s4u/Mutex.hpp>
 #include <simgrid/s4u/NetZone.hpp>
 #include <simgrid/version.h>
 
@@ -48,6 +49,8 @@ using simgrid::s4u::Engine;
 using simgrid::s4u::Host;
 using simgrid::s4u::Link;
 using simgrid::s4u::Mailbox;
+using simgrid::s4u::Mutex;
+using simgrid::s4u::MutexPtr;
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(python, "python");
 
@@ -774,6 +777,20 @@ PYBIND11_MODULE(simgrid, m)
       .def("wait", &simgrid::s4u::Exec::wait, py::call_guard<py::gil_scoped_release>(),
            "Block until the completion of that execution.");
 
+  /* Class Mutex */
+  py::class_<Mutex, MutexPtr>(m, "Mutex",
+                              "A classical mutex, but blocking in the simulation world."
+                              "See the C++ documentation for details.")
+      .def(py::init<>(&Mutex::create))
+      .def("lock", &Mutex::lock, py::call_guard<py::gil_scoped_release>(), "Block until the mutex is acquired.")
+      .def("try_lock", &Mutex::try_lock, py::call_guard<py::gil_scoped_release>(),
+           "Try to acquire the mutex. Return true if the mutex was acquired, false otherwise.")
+      .def("unlock", &Mutex::unlock, py::call_guard<py::gil_scoped_release>(), "Release the mutex")
+      // Allow mutexes to be automatically acquired/released with a context manager: `with mutex: ...`
+      .def("__enter__", [](Mutex* self){ self->lock(); }, py::call_guard<py::gil_scoped_release>())
+      .def("__exit__", [](Mutex* self, py::object&, py::object&, py::object&){ self->unlock(); },
+          py::call_guard<py::gil_scoped_release>());
+
   /* Class Actor */
   py::class_<simgrid::s4u::Actor, ActorPtr>(m, "Actor",
                                             "An actor is an independent stream of execution in your distributed "
@@ -824,7 +841,7 @@ PYBIND11_MODULE(simgrid, m)
            "Returns True if that actor is a daemon and will be terminated automatically when the last non-daemon actor "
            "terminates.")
       .def("join", py::overload_cast<double>(&Actor::join, py::const_), py::call_guard<py::gil_scoped_release>(),
-           "Wait for the actor to finish (more info in the C++ documentation).", py::arg("timeout"))
+           "Wait for the actor to finish (more info in the C++ documentation).", py::arg("timeout") = -1)
       .def("kill", &Actor::kill, py::call_guard<py::gil_scoped_release>(), "Kill that actor")
       .def("self", &Actor::self, "Retrieves the current actor.")
       .def("is_suspended", &Actor::is_suspended, "Returns True if that actor is currently suspended.")