Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update synchro-mutex.py to match last version of s4u-synchro-mutex.cpp
authorJean-Edouard BOULANGER <jean.edouard.boulanger@gmail.com>
Tue, 15 Mar 2022 07:58:24 +0000 (08:58 +0100)
committerJean-Edouard BOULANGER <jean.edouard.boulanger@gmail.com>
Tue, 15 Mar 2022 07:58:24 +0000 (08:58 +0100)
examples/python/synchro-mutex/synchro-mutex.py
examples/python/synchro-mutex/synchro-mutex.tesh

index ec0d8da..3f2d9e0 100644 (file)
@@ -17,7 +17,7 @@ def create_parser() -> ArgumentParser:
         '--actors',
         type=int,
         default=6,
-        help='how many pairs of actors should be started?'
+        help='how many pairs of actors should be started'
     )
     return parser
 
@@ -28,46 +28,30 @@ class ResultHolder:
 
 
 def worker_context_manager(mutex: Mutex, result: ResultHolder):
-    """ Worker that uses a context manager to acquire/release the shared mutex
-    :param mutex: Shared mutex that guards read/write access to the shared result
-    :param result: Shared result which will be updated by the worker
-    """
-    this_actor.info(f"I just started")
     with mutex:
-        this_actor.info(f"acquired the mutex with context manager")
+        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"updated shared result, which is now {result.value}")
-    this_actor.info(f"released the mutex after leaving the context manager")
-    this_actor.info("Bye now!")
+    this_actor.info(f"I'm done, good bye")
 
 
 def worker(mutex: Mutex, result: ResultHolder):
-    """ Worker that manually acquires/releases the shared mutex
-    :param mutex: Shared mutex that guards read/write access to the shared result
-    :param result: Shared result which will be updated by the worker
-    """
-    this_actor.info(f"I just started")
     mutex.lock()
-    this_actor.info(f"acquired the mutex manually")
+    this_actor.info("Hello simgrid, I'm ready to compute after a regular lock")
     result.value += 1
-    this_actor.info(f"updated shared result, which is now {result.value}")
     mutex.unlock()
-    this_actor.info(f"released the mutex manually")
-    this_actor.info("Bye now!")
+    this_actor.info("I'm done, good bye")
 
 
 def master(settings):
-    """ Spawns `--workers` workers and wait until they have all updated the shared result, then displays it before
-        leaving. Alternatively spawns `worker_context_manager()` and `worker()` workers.
-    :param settings: Simulation settings
-    """
-    result = ResultHolder(value=0)
-    mutex = Mutex()
+    results = [ResultHolder(value=0) for _ in range(settings.actors)]
     for i in range(settings.actors):
-        Actor.create(f"worker-{i}(mgr)", Host.by_name("Jupiter"), worker_context_manager, mutex, result)
-        Actor.create(f"worker-{i}", Host.by_name("Tremblay"), worker, mutex, result)
+        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)
-    this_actor.info(f"The final result is: {result.value}")
+    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():
index 9537cff..2271c0a 100644 (file)
@@ -3,70 +3,40 @@
 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) The final result is: 0
+>[ 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) I just started
->[  0.000000] (2:worker-0(mgr)@Jupiter) acquired the mutex with context manager
->[  0.000000] (2:worker-0(mgr)@Jupiter) updated shared result, which is now 1
->[  0.000000] (3:worker-0@Tremblay) I just started
->[  0.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
->[  0.000000] (2:worker-0(mgr)@Jupiter) Bye now!
->[  0.000000] (3:worker-0@Tremblay) acquired the mutex manually
->[  0.000000] (3:worker-0@Tremblay) updated shared result, which is now 2
->[  0.000000] (3:worker-0@Tremblay) released the mutex manually
->[  0.000000] (3:worker-0@Tremblay) Bye now!
->[ 10.000000] (1:master@Tremblay) The final result is: 2
+>[  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) I just started
->[  0.000000] (2:worker-0(mgr)@Jupiter) acquired the mutex with context manager
->[  0.000000] (2:worker-0(mgr)@Jupiter) updated shared result, which is now 1
->[  0.000000] (3:worker-0@Tremblay) I just started
->[  0.000000] (2:worker-0(mgr)@Jupiter) released the mutex after leaving the context manager
->[  0.000000] (2:worker-0(mgr)@Jupiter) Bye now!
->[  0.000000] (3:worker-0@Tremblay) acquired the mutex manually
->[  0.000000] (3:worker-0@Tremblay) updated shared result, which is now 2
->[  0.000000] (4:worker-1(mgr)@Jupiter) I just started
->[  0.000000] (3:worker-0@Tremblay) released the mutex manually
->[  0.000000] (3:worker-0@Tremblay) Bye now!
->[  0.000000] (4:worker-1(mgr)@Jupiter) acquired the mutex with context manager
->[  0.000000] (4:worker-1(mgr)@Jupiter) updated shared result, which is now 3
->[  0.000000] (5:worker-1@Tremblay) I just started
->[  0.000000] (4:worker-1(mgr)@Jupiter) released the mutex after leaving the context manager
->[  0.000000] (4:worker-1(mgr)@Jupiter) Bye now!
->[  0.000000] (5:worker-1@Tremblay) acquired the mutex manually
->[  0.000000] (5:worker-1@Tremblay) updated shared result, which is now 4
->[  0.000000] (6:worker-2(mgr)@Jupiter) I just started
->[  0.000000] (5:worker-1@Tremblay) released the mutex manually
->[  0.000000] (5:worker-1@Tremblay) Bye now!
->[  0.000000] (6:worker-2(mgr)@Jupiter) acquired the mutex with context manager
->[  0.000000] (6:worker-2(mgr)@Jupiter) updated shared result, which is now 5
->[  0.000000] (7:worker-2@Tremblay) I just started
->[  0.000000] (6:worker-2(mgr)@Jupiter) released the mutex after leaving the context manager
->[  0.000000] (6:worker-2(mgr)@Jupiter) Bye now!
->[  0.000000] (7:worker-2@Tremblay) acquired the mutex manually
->[  0.000000] (7:worker-2@Tremblay) updated shared result, which is now 6
->[  0.000000] (8:worker-3(mgr)@Jupiter) I just started
->[  0.000000] (7:worker-2@Tremblay) released the mutex manually
->[  0.000000] (7:worker-2@Tremblay) Bye now!
->[  0.000000] (8:worker-3(mgr)@Jupiter) acquired the mutex with context manager
->[  0.000000] (8:worker-3(mgr)@Jupiter) updated shared result, which is now 7
->[  0.000000] (9:worker-3@Tremblay) I just started
->[  0.000000] (8:worker-3(mgr)@Jupiter) released the mutex after leaving the context manager
->[  0.000000] (8:worker-3(mgr)@Jupiter) Bye now!
->[  0.000000] (9:worker-3@Tremblay) acquired the mutex manually
->[  0.000000] (9:worker-3@Tremblay) updated shared result, which is now 8
->[  0.000000] (10:worker-4(mgr)@Jupiter) I just started
->[  0.000000] (9:worker-3@Tremblay) released the mutex manually
->[  0.000000] (9:worker-3@Tremblay) Bye now!
->[  0.000000] (10:worker-4(mgr)@Jupiter) acquired the mutex with context manager
->[  0.000000] (10:worker-4(mgr)@Jupiter) updated shared result, which is now 9
->[  0.000000] (11:worker-4@Tremblay) I just started
->[  0.000000] (10:worker-4(mgr)@Jupiter) released the mutex after leaving the context manager
->[  0.000000] (10:worker-4(mgr)@Jupiter) Bye now!
->[  0.000000] (11:worker-4@Tremblay) acquired the mutex manually
->[  0.000000] (11:worker-4@Tremblay) updated shared result, which is now 10
->[  0.000000] (11:worker-4@Tremblay) released the mutex manually
->[  0.000000] (11:worker-4@Tremblay) Bye now!
->[ 10.000000] (1:master@Tremblay) The final result is: 10
+>[  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