File: comm-host2host.py

package info (click to toggle)
simgrid 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,192 kB
  • sloc: cpp: 124,913; ansic: 66,744; python: 8,560; java: 6,773; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,194; perl: 1,436; makefile: 111; lisp: 49; javascript: 7; sed: 6
file content (86 lines) | stat: -rw-r--r-- 3,504 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright (c) 2010-2025. The SimGrid Team. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the license (GNU LGPL) which comes with this package.

"""
This simple example demonstrates the Comm.sento_init() Comm.sento_async() functions,
that can be used to create a direct communication from one host to another without
relying on the mailbox mechanism.

There is not much to say, actually: The _init variant creates the communication and
leaves it unstarted (in case you want to modify this communication before it starts),
while the _async variant creates and start it. In both cases, you need to wait() it.

It is mostly useful when you want to have a centralized simulation of your settings,
with a central actor declaring all communications occurring on your distributed system.
"""

from argparse import ArgumentParser
import sys

from simgrid import Actor, Comm, Engine, Host, this_actor


def create_parser() -> ArgumentParser:
    parser = ArgumentParser()
    parser.add_argument(
        '--platform',
        type=str,
        required=True,
        help='path to the platform description'
    )
    return parser


def sender(h1: Host, h2: Host, h3: Host, h4: Host):
    this_actor.info(f"Send c12 with sendto_async({h1.name} -> {h2.name}),"
                    f" and c34 with sendto_init({h3.name} -> {h4.name})")
    c12: Comm = Comm.sendto_async(h1, h2, int(1.5e7))
    c34: Comm = Comm.sendto_init(h3, h4)
    c34.set_payload_size(int(1e7))

    # You can also detach() communications that you never plan to test() or wait().
    # Here we create a communication that only slows down the other ones
    noise: Comm = Comm.sendto_init(h1, h2)
    noise.set_payload_size(10000)
    noise.detach()

    this_actor.info(f"After creation, c12 is {c12.state_str} (remaining: {c12.remaining:.2e} bytes);"
                    f" c34 is {c34.state_str} (remaining: {c34.remaining:.2e} bytes)")
    this_actor.sleep_for(1)
    this_actor.info(f"One sec later, c12 is {c12.state_str} (remaining: {c12.remaining:.2e} bytes);"
                    f" c34 is {c34.state_str} (remaining: {c34.remaining:.2e} bytes)")
    c34.start()
    this_actor.info(f"After c34.start(), c12 is {c12.state_str} (remaining: {c12.remaining:.2e} bytes);"
                    f" c34 is {c34.state_str} (remaining: {c34.remaining:.2e} bytes)")
    c12.wait()
    this_actor.info(f"After c12.wait(), c12 is {c12.state_str} (remaining: {c12.remaining:.2e} bytes);"
                    f" c34 is {c34.state_str} (remaining: {c34.remaining:.2e} bytes)")
    c34.wait()
    this_actor.info(f"After c34.wait(), c12 is {c12.state_str} (remaining: {c12.remaining:.2e} bytes);"
                    f" c34 is {c34.state_str} (remaining: {c34.remaining:.2e} bytes)")

    # As usual, you don't have to explicitly start communications that were just init()ed.
    # The wait() will start it automatically.
    c14: Comm = Comm.sendto_init(h1, h4)
    c14.set_payload_size(100).wait()


def main():
    settings = create_parser().parse_known_args()[0]
    e = Engine(sys.argv)
    e.load_platform(settings.platform)
    e.host_by_name("Boivin").add_actor(
        "sender", sender,
        e.host_by_name("Tremblay"),  # h1
        e.host_by_name("Jupiter"),  # h2
        e.host_by_name("Fafard"),  # h3
        e.host_by_name("Ginette")  # h4
    )
    e.run()
    this_actor.info(f"Total simulation time: {e.clock:.3f}")


if __name__ == "__main__":
    main()