1 # Copyright (c) 2006-2023. 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 example shows how to simulate a non-linear resource sharing for network links.
12 from simgrid import Actor, ActivitySet, Engine, Comm, Mailbox, NetZone, Link, LinkInRoute, this_actor
16 Send a series of messages to mailbox "receiver"
18 def __init__(self, msg_count: int, msg_size=int(1e6)):
19 self.msg_count = msg_count
20 self.msg_size = msg_size
22 # Actors that are created as object will execute their __call__ method.
23 # So, the following constitutes the main function of the Sender actor.
25 pending_comms = ActivitySet()
26 mbox = Mailbox.by_name("receiver")
28 for i in range(self.msg_count):
29 msg = "Message " + str(i)
30 size = self.msg_size * (i + 1)
31 this_actor.info("Send '%s' to '%s, msg size: %d'" % (msg, mbox.name, size))
32 comm = mbox.put_async(msg, size)
33 pending_comms.push(comm)
35 this_actor.info("Done dispatching all messages")
37 # Now that all message exchanges were initiated, wait for their completion in one single call
38 pending_comms.wait_all()
40 this_actor.info("Goodbye now!")
44 Receiver actor: wait for N messages on the mailbox "receiver"
47 def __init__(self, msg_count=10):
48 self.msg_count = msg_count
51 mbox = Mailbox.by_name("receiver")
55 this_actor.info("Wait for %d messages asynchronously" % self.msg_count)
56 for _ in range(self.msg_count):
57 comm = mbox.get_async()
58 pending_comms.append(comm)
61 index = Comm.wait_any(pending_comms)
62 msg = pending_comms[index].get_payload()
63 this_actor.info("I got '%s'." % msg)
64 del pending_comms[index]
66 ####################################################################################################
67 def link_nonlinear(link: Link, capacity: float, n: int) -> float:
69 Non-linear resource sharing for links
71 Note that the callback is called twice in this example:
72 1) link UP: with the number of active flows (from 9 to 1)
73 2) link DOWN: with 0 active flows. A crosstraffic communication is happing
74 in the down link, but it's not considered as an active flow.
76 # emulates a degradation in link according to the number of flows
77 # you probably want something more complex than that and based on real
79 capacity = min(capacity, capacity * (1.0 - (n - 1) / 10.0))
80 this_actor.info("Link %s, %d active communications, new capacity %f" % (link.name, n, capacity))
85 Create a simple 2-hosts platform
87 | Sender |===============| Receiver |
88 |________| Link1 |__________|
91 zone = NetZone.create_full_zone("Zone1")
92 sender = zone.create_host("sender", 1).seal()
93 receiver = zone.create_host("receiver", 1).seal()
95 link = zone.create_split_duplex_link("link1", 1e6)
96 # setting same callbacks (could be different) for link UP/DOWN in split-duplex link
97 link.link_up.set_sharing_policy(Link.SharingPolicy.NONLINEAR,
98 functools.partial(link_nonlinear, link.link_up))
99 link.link_down.set_sharing_policy(Link.SharingPolicy.NONLINEAR,
100 functools.partial(link_nonlinear, link.link_down))
101 link.set_latency(10e-6).seal()
103 # create routes between nodes
104 zone.add_route(sender, receiver, [link])
107 # create actors Sender/Receiver
108 Actor.create("receiver", receiver, Receiver(9))
109 Actor.create("sender", sender, Sender(9))
111 ###################################################################################################
113 if __name__ == '__main__':
119 # runs the simulation
122 # explicitly deleting Engine object to avoid segfault during cleanup phase.
123 # During Engine destruction, the cleanup of std::function linked to link_non_linear callback is called.
124 # If we let the cleanup by itself, it fails trying on its destruction because the python main program
125 # has already freed its variables