Logo AND Algorithmique Numérique Distribuée

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