1 # Copyright (c) 2010-2023. The SimGrid Team. All rights reserved.
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the license (GNU LGPL) which comes with this package.
6 from argparse import ArgumentParser
7 from dataclasses import dataclass
10 from simgrid import Actor, Engine, Host, Mutex, this_actor
13 def create_parser() -> ArgumentParser:
14 parser = ArgumentParser()
19 help='path to the platform description'
25 help='how many pairs of actors should be started'
35 def worker_context_manager(mutex: Mutex, result: ResultHolder):
36 # When using a context manager, the lock and the unlock are automatic. This is the easiest approach
38 this_actor.info(f"Hello simgrid, I'm ready to compute after acquiring the mutex from a context manager")
40 this_actor.info(f"I'm done, good bye")
43 def worker(mutex: Mutex, result: ResultHolder):
44 # If you lock your mutex manually, you also have to unlock it.
45 # Beware of exceptions preventing your unlock() from being executed!
47 this_actor.info("Hello simgrid, I'm ready to compute after a regular lock")
50 this_actor.info("I'm done, good bye")
55 settings = create_parser().parse_known_args()[0]
57 e.load_platform(settings.platform)
59 # Create the requested amount of actors pairs. Each pair has a specific mutex and cell in `result`
60 results = [ResultHolder(value=0) for _ in range(settings.actors)]
61 for i in range(settings.actors):
63 Actor.create(f"worker-{i}(mgr)", Host.by_name("Jupiter"), worker_context_manager, mutex, results[i])
64 Actor.create(f"worker-{i}", Host.by_name("Tremblay"), worker, mutex, results[i])
68 for i in range(settings.actors):
69 this_actor.info(f"Result[{i}] -> {results[i].value}")
72 if __name__ == "__main__":