From 89ce2acd28bc814c5272e3afaf3e826db8aaf910 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sun, 20 Mar 2022 09:24:25 +0100 Subject: [PATCH] also remove the master example from example syncho-mutex.py --- .../python/synchro-mutex/synchro-mutex.py | 24 +++---- .../python/synchro-mutex/synchro-mutex.tesh | 65 +++++++++---------- 2 files changed, 43 insertions(+), 46 deletions(-) diff --git a/examples/python/synchro-mutex/synchro-mutex.py b/examples/python/synchro-mutex/synchro-mutex.py index ab46961ee4..fe84e47e80 100644 --- a/examples/python/synchro-mutex/synchro-mutex.py +++ b/examples/python/synchro-mutex/synchro-mutex.py @@ -33,6 +33,7 @@ class ResultHolder: def worker_context_manager(mutex: Mutex, result: ResultHolder): + # When using a context manager, the lock and the unlock are automatic. This is the easiest approach with mutex: this_actor.info(f"Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager") result.value += 1 @@ -40,6 +41,8 @@ def worker_context_manager(mutex: Mutex, result: ResultHolder): def worker(mutex: Mutex, result: ResultHolder): + # If you lock your mutex manually, you also have to unlock it. + # Beware of exceptions preventing your unlock() from being executed! mutex.lock() this_actor.info("Hello simgrid, I'm ready to compute after a regular lock") result.value += 1 @@ -47,25 +50,24 @@ def worker(mutex: Mutex, result: ResultHolder): this_actor.info("I'm done, good bye") -def master(settings): + +def main(): + settings = create_parser().parse_known_args()[0] + e = Engine(sys.argv) + e.load_platform(settings.platform) + + # Create the requested amount of actors pairs. Each pair has a specific mutex and cell in `result` 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() + for i in range(settings.actors): + this_actor.info(f"Result[{i}] -> {results[i].value}") + if __name__ == "__main__": main() diff --git a/examples/python/synchro-mutex/synchro-mutex.tesh b/examples/python/synchro-mutex/synchro-mutex.tesh index 2271c0a7f1..372a2cae23 100644 --- a/examples/python/synchro-mutex/synchro-mutex.tesh +++ b/examples/python/synchro-mutex/synchro-mutex.tesh @@ -2,41 +2,36 @@ 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 +>[ 0.000000] (1:worker-0(mgr)@Jupiter) Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager +>[ 0.000000] (2:worker-0@Tremblay) Hello simgrid, I'm ready to compute after a regular lock +>[ 0.000000] (1:worker-0(mgr)@Jupiter) I'm done, good bye +>[ 0.000000] (2:worker-0@Tremblay) I'm done, good bye +>[ 0.000000] (0:maestro@) Result[0] -> 2 $ ${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 +>[ 0.000000] (1:worker-0(mgr)@Jupiter) Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager +>[ 0.000000] (3:worker-1(mgr)@Jupiter) Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager +>[ 0.000000] (5:worker-2(mgr)@Jupiter) Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager +>[ 0.000000] (7:worker-3(mgr)@Jupiter) Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager +>[ 0.000000] (9:worker-4(mgr)@Jupiter) Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager +>[ 0.000000] (2:worker-0@Tremblay) Hello simgrid, I'm ready to compute after a regular lock +>[ 0.000000] (1:worker-0(mgr)@Jupiter) I'm done, good bye +>[ 0.000000] (4:worker-1@Tremblay) Hello simgrid, I'm ready to compute after a regular lock +>[ 0.000000] (3:worker-1(mgr)@Jupiter) I'm done, good bye +>[ 0.000000] (6:worker-2@Tremblay) Hello simgrid, I'm ready to compute after a regular lock +>[ 0.000000] (5:worker-2(mgr)@Jupiter) I'm done, good bye +>[ 0.000000] (8:worker-3@Tremblay) Hello simgrid, I'm ready to compute after a regular lock +>[ 0.000000] (7:worker-3(mgr)@Jupiter) I'm done, good bye +>[ 0.000000] (10:worker-4@Tremblay) Hello simgrid, I'm ready to compute after a regular lock +>[ 0.000000] (9:worker-4(mgr)@Jupiter) I'm done, good bye +>[ 0.000000] (2:worker-0@Tremblay) I'm done, good bye +>[ 0.000000] (4:worker-1@Tremblay) I'm done, good bye +>[ 0.000000] (6:worker-2@Tremblay) I'm done, good bye +>[ 0.000000] (8:worker-3@Tremblay) I'm done, good bye +>[ 0.000000] (10:worker-4@Tremblay) I'm done, good bye +>[ 0.000000] (0:maestro@) Result[0] -> 2 +>[ 0.000000] (0:maestro@) Result[1] -> 2 +>[ 0.000000] (0:maestro@) Result[2] -> 2 +>[ 0.000000] (0:maestro@) Result[3] -> 2 +>[ 0.000000] (0:maestro@) Result[4] -> 2 -- 2.20.1