Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add remaining Comm bindings and examples
[simgrid.git] / examples / python / comm-pingpong / comm-pingpong.py
1 # Copyright (c) 2010-2022. 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 Engine, Actor, Mailbox, 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     return parser
21
22
23 def pinger(mailbox_in: Mailbox, mailbox_out: Mailbox):
24     this_actor.info(f"Ping from mailbox {mailbox_in.name} to mailbox {mailbox_out.name}")
25
26     # Do the ping with a 1-Byte payload (latency bound) ...
27     payload = Engine.clock
28     mailbox_out.put(payload, 1)
29
30     # ... then wait for the (large) pong
31     sender_time: float = mailbox_in.get()
32     communication_time = Engine.clock - sender_time
33     this_actor.info("Payload received : large communication (bandwidth bound)")
34     this_actor.info(f"Pong time (bandwidth bound): {communication_time:.3f}")
35
36
37 def ponger(mailbox_in: Mailbox, mailbox_out: Mailbox):
38     this_actor.info(f"Pong from mailbox {mailbox_in.name} to mailbox {mailbox_out.name}")
39
40     # Receive the (small) ping first ...
41     sender_time: float = mailbox_in.get()
42     communication_time = Engine.clock - sender_time
43     this_actor.info("Payload received : small communication (latency bound)")
44     this_actor.info(f"Ping time (latency bound) {communication_time:.3f}")
45
46     # ... Then send a 1GB pong back (bandwidth bound)
47     payload = Engine.clock
48     this_actor.info(f"payload = {payload:.3f}")
49     mailbox_out.put(payload, int(1e9))
50
51
52 def main():
53     settings = create_parser().parse_known_args()[0]
54     e = Engine(sys.argv)
55     e.load_platform(settings.platform)
56
57     mb1: Mailbox = e.mailbox_by_name_or_create("Mailbox 1")
58     mb2: Mailbox = e.mailbox_by_name_or_create("Mailbox 2")
59
60     Actor.create("pinger", e.host_by_name("Tremblay"), pinger, mb1, mb2)
61     Actor.create("ponger", e.host_by_name("Jupiter"), ponger, mb2, mb1)
62
63     e.run()
64
65     this_actor.info(f"Total simulation time: {e.clock:.3f}")
66
67
68 if __name__ == "__main__":
69     main()