Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add Barrier Python bindings
[simgrid.git] / examples / python / synchro-barrier / synchro-barrier.py
1 from argparse import ArgumentParser
2 import sys
3
4 from simgrid import Actor, Barrier, Engine, Host, this_actor
5
6
7 def create_parser() -> ArgumentParser:
8     parser = ArgumentParser()
9     parser.add_argument(
10         '--platform',
11         type=str,
12         required=True,
13         help='path to the platform description'
14     )
15     parser.add_argument(
16         "--actors",
17         type=int,
18         required=True,
19         help="Number of actors to start"
20     )
21     return parser
22
23
24 def worker(barrier: Barrier):
25     """ Wait on the barrier and exits.
26     :param barrier: Barrier to be awaited
27     """
28     this_actor.info(f"Waiting on the barrier")
29     barrier.wait()
30     this_actor.info("Bye")
31
32
33 def master(actor_count: int):
34     """ Create barrier with `actor_count` expected actors, spawns `actor_count - 1` workers, then wait on the barrier
35     and exits.
36     :param actor_count: Spawn actor_count-1 workers and do a barrier with them
37     """
38     barrier = Barrier(actor_count)
39     workers_count = actor_count - 1
40     this_actor.info(f"Spawning {workers_count} workers")
41     for i in range(workers_count):
42         Actor.create(f"worker-{i}", Host.by_name("Jupiter"), worker, barrier)
43     this_actor.info(f"Waiting on the barrier")
44     barrier.wait()
45     this_actor.info("Bye")
46
47
48 def main():
49     settings = create_parser().parse_known_args()[0]
50     if settings.actors < 1:
51         raise ValueError("--actors must be greater than 0")
52     e = Engine(sys.argv)
53     e.load_platform(settings.platform)
54     Actor.create("master", Host.by_name("Tremblay"), master, settings.actors)
55     e.run()
56
57
58 if __name__ == "__main__":
59     main()