1 # Copyright (c) 2010-2022. The SimGrid Team. All rights reserved.
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.
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.
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.
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.
19 from argparse import ArgumentParser
22 from simgrid import Actor, Comm, Engine, Host, this_actor
25 def create_parser() -> ArgumentParser:
26 parser = ArgumentParser()
31 help='path to the platform description'
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))
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)
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)")
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)")
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)")
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)")
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()
71 settings = create_parser().parse_known_args()[0]
73 e.load_platform(settings.platform)
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
82 this_actor.info(f"Total simulation time: {e.clock:.3f}")
85 if __name__ == "__main__":