Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'simgrid-udpor-integration' into 'master'
[simgrid.git] / examples / python / comm-throttling / comm-throttling.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 Engine, Actor, Comm, 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 sender(mailbox: Mailbox):
24     this_actor.info("Send at full bandwidth")
25
26     # First send a 2.5e8 Bytes payload at full bandwidth (1.25e8 Bps)
27     payload = Engine.clock
28     mailbox.put(payload, int(2.5e8))
29
30     this_actor.info("Throttle the bandwidth at the Comm level")
31     # ... then send it again but throttle the Comm
32     payload = Engine.clock
33     # get a handler on the comm first
34     comm: Comm = mailbox.put_init(payload, int(2.5e8))
35
36     # let throttle the communication. It amounts to set the rate of the comm to half the nominal bandwidth of the link,
37     # i.e., 1.25e8 / 2. This second communication will thus take approximately twice as long as the first one
38     comm.set_rate(int(1.25e8 / 2)).wait()
39
40
41 def receiver(mailbox: Mailbox):
42     # Receive the first payload sent at full bandwidth
43     sender_time = mailbox.get()
44     communication_time = Engine.clock - sender_time
45     this_actor.info(f"Payload received (full bandwidth) in {communication_time} seconds")
46
47     # ... Then receive the second payload sent with a throttled Comm
48     sender_time = mailbox.get()
49     communication_time = Engine.clock - sender_time
50     this_actor.info(f"Payload received (throttled) in {communication_time} seconds")
51
52
53 def main():
54     settings = create_parser().parse_known_args()[0]
55     e = Engine(sys.argv)
56     e.load_platform(settings.platform)
57
58     mailbox = e.mailbox_by_name_or_create("Mailbox")
59
60     Actor.create("sender", e.host_by_name("node-0.simgrid.org"), sender, mailbox)
61     Actor.create("receiver", e.host_by_name("node-1.simgrid.org"), receiver, mailbox)
62
63     e.run()
64
65
66 if __name__ == "__main__":
67     main()