Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e352a96582fd9669a328e083951470e6916a660c
[simgrid.git] / examples / python / comm-host2host / comm-host2host.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 """
7 This simple example demonstrates the Comm.sento_init() Comm.sento_async() functions,
8 that can be used to create a direct communication from one host to another without
9 relying on the mailbox mechanism.
10
11 There is not much to say, actually: The _init variant creates the communication and
12 leaves it unstarted (in case you want to modify this communication before it starts),
13 while the _async variant creates and start it. In both cases, you need to wait() it.
14
15 It is mostly useful when you want to have a centralized simulation of your settings,
16 with a central actor declaring all communications occurring on your distributed system.
17 """
18
19 from argparse import ArgumentParser
20 import sys
21
22 from simgrid import Actor, Comm, Engine, Host, this_actor
23
24
25 def create_parser() -> ArgumentParser:
26     parser = ArgumentParser()
27     parser.add_argument(
28         '--platform',
29         type=str,
30         required=True,
31         help='path to the platform description'
32     )
33     return parser
34
35
36 def sender(h1: Host, h2: Host, h3: Host, h4: Host):
37     this_actor.info(f"Send c12 with sendto_async({h1.name} -> {h2.name}),"
38                     f" and c34 with sendto_init({h3.name} -> {h4.name})")
39     c12: Comm = Comm.sendto_async(h1, h2, int(1.5e7))
40     c34: Comm = Comm.sendto_init(h3, h4)
41     c34.set_payload_size(int(1e7))
42
43     # You can also detach() communications that you never plan to test() or wait().
44     # Here we create a communication that only slows down the other ones
45     noise: Comm = Comm.sendto_init(h1, h2)
46     noise.set_payload_size(10000)
47     noise.detach()
48
49     this_actor.info(f"After creation, c12 is {c12.state_str} (remaining: {c12.remaining:.2e} bytes);"
50                     f" c34 is {c34.state_str} (remaining: {c34.remaining:.2e} bytes)")
51     this_actor.sleep_for(1)
52     this_actor.info(f"One sec later, c12 is {c12.state_str} (remaining: {c12.remaining:.2e} bytes);"
53                     f" c34 is {c34.state_str} (remaining: {c34.remaining:.2e} bytes)")
54     c34.start()
55     this_actor.info(f"After c34.start(), c12 is {c12.state_str} (remaining: {c12.remaining:.2e} bytes);"
56                     f" c34 is {c34.state_str} (remaining: {c34.remaining:.2e} bytes)")
57     c12.wait()
58     this_actor.info(f"After c12.wait(), c12 is {c12.state_str} (remaining: {c12.remaining:.2e} bytes);"
59                     f" c34 is {c34.state_str} (remaining: {c34.remaining:.2e} bytes)")
60     c34.wait()
61     this_actor.info(f"After c34.wait(), c12 is {c12.state_str} (remaining: {c12.remaining:.2e} bytes);"
62                     f" c34 is {c34.state_str} (remaining: {c34.remaining:.2e} bytes)")
63
64     # As usual, you don't have to explicitly start communications that were just init()ed.
65     # The wait() will start it automatically.
66     c14: Comm = Comm.sendto_init(h1, h4)
67     c14.set_payload_size(100).wait()
68
69
70 def main():
71     settings = create_parser().parse_known_args()[0]
72     e = Engine(sys.argv)
73     e.load_platform(settings.platform)
74     Actor.create(
75         "sender", e.host_by_name("Boivin"), sender,
76         e.host_by_name("Tremblay"),  # h1
77         e.host_by_name("Jupiter"),  # h2
78         e.host_by_name("Fafard"),  # h3
79         e.host_by_name("Ginette")  # h4
80     )
81     e.run()
82     this_actor.info(f"Total simulation time: {e.clock:.3f}")
83
84
85 if __name__ == "__main__":
86     main()