Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
10cb74b0d3a4d1d8d799d7c35c46a40d537672be
[simgrid.git] / examples / python / network-nonlinear / network-nonlinear.py
1 # Copyright (c) 2006-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 """
7 This example shows how to simulate a non-linear resource sharing for network links.
8 """
9
10 import functools
11 import sys
12 from simgrid import Actor, ActivitySet, Engine, Comm, Mailbox, NetZone, Link, LinkInRoute, this_actor
13
14 class Sender:
15     """
16     Send a series of messages to mailbox "receiver"
17     """
18     def __init__(self, msg_count: int, msg_size=int(1e6)):
19         self.msg_count = msg_count
20         self.msg_size = msg_size
21
22     # Actors that are created as object will execute their __call__ method.
23     # So, the following constitutes the main function of the Sender actor.
24     def __call__(self):
25         pending_comms = ActivitySet()
26         mbox = Mailbox.by_name("receiver")
27
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)
34
35         this_actor.info("Done dispatching all messages")
36
37         # Now that all message exchanges were initiated, wait for their completion in one single call
38         pending_comms.wait_all()
39
40         this_actor.info("Goodbye now!")
41
42 class Receiver:
43     """
44     Receiver actor: wait for N messages on the mailbox "receiver"
45     """
46
47     def __init__(self, msg_count=10):
48         self.msg_count = msg_count
49
50     def __call__(self):
51         mbox = Mailbox.by_name("receiver")
52
53         pending_comms = []
54
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)
59
60         while pending_comms:
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]
65
66 ####################################################################################################
67 def link_nonlinear(link: Link, capacity: float, n: int) -> float:
68     """
69     Non-linear resource sharing for links
70
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.
75     """
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
78     # experiments
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))
81     return capacity
82
83 def load_platform():
84     """
85     Create a simple 2-hosts platform
86      ________                 __________
87     | Sender |===============| Receiver |
88     |________|    Link1      |__________|
89
90     """
91     zone = NetZone.create_full_zone("Zone1")
92     sender = zone.create_host("sender", 1).seal()
93     receiver = zone.create_host("receiver", 1).seal()
94
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()
102
103     # create routes between nodes
104     zone.add_route(sender, receiver, [link])
105     zone.seal()
106
107     # create actors Sender/Receiver
108     Actor.create("receiver", receiver, Receiver(9))
109     Actor.create("sender", sender, Sender(9))
110
111 ###################################################################################################
112
113 if __name__ == '__main__':
114     e = Engine(sys.argv)
115
116     # create platform
117     load_platform()
118
119     # runs the simulation
120     e.run()
121
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
126     del e