Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Pylint examples/*.py.
[simgrid.git] / examples / python / platform-profile / platform-profile.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 example demonstrates how to attach a profile to a host or a link, to specify external changes to the resource
8 speed.
9 The first way to do so is to use a file in the XML, while the second is to use the programmatic interface.
10 """
11
12 import sys
13 from simgrid import Actor, Engine, Host, Link, this_actor
14
15 def watcher():
16     jupiter = Host.by_name("Jupiter")
17     fafard = Host.by_name("Fafard")
18     lilibeth = Host.by_name("Lilibeth")
19     link1 = Link.by_name("1")
20     link2 = Link.by_name("2")
21
22     (links, lat) = jupiter.route_to(fafard)
23     path = ""
24     for l in links:
25         path += ("" if not path else ", ") + "link '" + l.name + "'"
26     this_actor.info(f"Path from Jupiter to Fafard: {path} (latency: {lat:.6f}s).")
27
28     for _ in range(10):
29         this_actor.info("Fafard: %.0fMflops, Jupiter: %4.0fMflops, Lilibeth: %3.1fMflops, \
30 Link1: (%.2fMB/s %.0fms), Link2: (%.2fMB/s %.0fms)" % (fafard.speed * fafard.available_speed / 1000000,
31                                                        jupiter.speed * jupiter.available_speed / 1000000,
32                                                        lilibeth.speed * lilibeth.available_speed / 1000000,
33                                                        link1.bandwidth / 1000, link1.latency * 1000,
34                                                        link2.bandwidth / 1000, link2.latency * 1000))
35         this_actor.sleep_for(1)
36
37 if __name__ == '__main__':
38     e = Engine(sys.argv)
39     # Load the platform description
40     e.load_platform(sys.argv[1])
41
42     # Add a new host programmatically, and attach a simple speed profile to it (alternate between full and half speed
43     # every two seconds
44     lili = e.netzone_root.create_host("Lilibeth", 25e6)
45     lili.set_speed_profile("""0 1.0
46     2 0.5""", 2)
47     lili.seal()
48
49     # Add a watcher of the changes
50     Actor.create("watcher", Host.by_name("Fafard"), watcher)
51
52     e.run()