Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Deprecate the bindings of a deprecated function
[simgrid.git] / examples / python / clusters-multicpu / clusters-multicpu.py
index 53feb77..0d6ff44 100644 (file)
@@ -1,17 +1,19 @@
-# Copyright (c) 2006-2021. The SimGrid Team. All rights reserved.
+# Copyright (c) 2006-2023. 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 example shows how to build a torus cluster with multi-core hosts.
-#
-# However, each leaf in the torus is a StarZone, composed of several CPUs
-#
-# Each actor runs in a specific CPU. One sender broadcasts a message to all receivers.
+"""
+This example shows how to build a torus cluster with multi-core hosts.
+
+However, each leaf in the torus is a StarZone, composed of several CPUs
+
+Each actor runs in a specific CPU. One sender broadcasts a message to all receivers.
+"""
 
-import simgrid
 import sys
 import typing
+import simgrid
 
 
 class Sender:
@@ -26,19 +28,19 @@ class Sender:
     # Actors that are created as object will execute their __call__ method.
     # So, the following constitutes the main function of the Sender actor.
     def __call__(self):
-        pending_comms = []
+        pending_comms = simgrid.ActivitySet()
         mboxes = []
 
         for host in self.hosts:
             msg = "Hello, I'm alive and running on " + simgrid.this_actor.get_host().name
             mbox = simgrid.Mailbox.by_name(host.name)
             mboxes.append(mbox)
-            pending_comms.append(mbox.put_async(msg, self.msg_size))
+            pending_comms.push(mbox.put_async(msg, self.msg_size))
 
         simgrid.this_actor.info("Done dispatching all messages")
 
         # Now that all message exchanges were initiated, wait for their completion in one single call
-        simgrid.Comm.wait_all(pending_comms)
+        pending_comms.wait_all()
 
         simgrid.this_actor.info("Goodbye now!")
 
@@ -56,8 +58,9 @@ class Receiver:
 #####################################################################################################
 
 
-def create_hostzone(zone: simgrid.NetZone, coord: typing.List[int], id: int) -> typing.Tuple[simgrid.NetPoint, simgrid.NetPoint]:
-    """
+def create_hostzone(zone: simgrid.NetZone, coord: typing.List[int], ident: int) -> typing.Tuple[simgrid.NetPoint,
+                                                                                                simgrid.NetPoint]:
+    r"""
     Callback to set a cluster leaf/element
 
     In our example, each leaf if a StarZone, composed of 8 CPUs.
@@ -77,7 +80,7 @@ def create_hostzone(zone: simgrid.NetZone, coord: typing.List[int], id: int) ->
 
     :param zone: Cluster netzone being created (usefull to create the hosts/links inside it)
     :param coord: Coordinates in the cluster
-    :param id: Internal identifier in the torus (for information)
+    :param ident: Internal identifier in the torus (for information)
     :return netpoint, gateway: the netpoint to the StarZone and CPU0 as gateway
     """
     num_cpus = 8     # Number of CPUs in the zone
@@ -85,7 +88,7 @@ def create_hostzone(zone: simgrid.NetZone, coord: typing.List[int], id: int) ->
     link_bw = "100GBps"  # Link bw connecting the CPU
     link_lat = "1ns"  # Link latency
 
-    hostname = "host" + str(id)
+    hostname = "host" + str(ident)
     # create the StarZone
     host_zone = simgrid.NetZone.create_star_zone(hostname)
     # setting my Torus parent zone
@@ -98,39 +101,38 @@ def create_hostzone(zone: simgrid.NetZone, coord: typing.List[int], id: int) ->
         host = host_zone.create_host(cpu_name, speed).seal()
         # the first CPU is the gateway
         if i == 0:
-            gateway = host
+            gateway = host.netpoint
         # create split-duplex link
         link = host_zone.create_split_duplex_link("link-" + cpu_name, link_bw)
         link.set_latency(link_lat).seal()
         # connecting CPU to outer world
-        host_zone.add_route(host.get_netpoint(), None, None, None, [
-                            simgrid.LinkInRoute(link, simgrid.LinkInRoute.Direction.UP)], True)
+        host_zone.add_route(host, None, [simgrid.LinkInRoute(link, simgrid.LinkInRoute.Direction.UP)], True)
 
     # seal newly created netzone
     host_zone.seal()
-    return host_zone.get_netpoint(), gateway.get_netpoint()
+    return host_zone.netpoint, gateway
 
 #####################################################################################################
 
 
-def create_limiter(zone: simgrid.NetZone, coord: typing.List[int], id: int) -> simgrid.Link:
+def create_limiter(zone: simgrid.NetZone, coord: typing.List[int], ident: int) -> simgrid.Link:
     """
     Callback to create limiter link (1Gbs) for each netpoint
 
     The coord parameter depends on the cluster being created:
     - Torus: Direct translation of the Torus' dimensions, e.g. (0, 0, 0) for a 3-D Torus
-    - Fat-Tree: A pair (level in the tree, id), e.g. (0, 0) for first leaf in the tree and (1,0) for the first switch at
-    level 1.
+    - Fat-Tree: A pair (level in the tree, ident), e.g. (0, 0) for first leaf in the tree and (1,0) for the first switch
+    at level 1.
     - Dragonfly: a tuple (group, chassis, blades/routers, nodes), e.g. (0, 0, 0, 0) for first node in the cluster. To
     identify the router inside a (group, chassis, blade), we use MAX_UINT in the last parameter (e.g. 0, 0, 0,
     4294967295).
 
     :param zone: Torus netzone being created (usefull to create the hosts/links inside it)
     :param coord: Coordinates in the cluster
-    :param id: Internal identifier in the torus (for information)
+    :param ident: Internal identifier in the torus (for information)
     :return: Limiter link
     """
-    return zone.create_link("limiter-" + str(id), [1e9]).seal()
+    return zone.create_link("limiter-" + str(ident), [1e9]).seal()
 
 
 def create_torus_cluster():
@@ -169,14 +171,15 @@ def create_torus_cluster():
     Cluster</a>
     """
     # create the torus cluster, 10Gbs link between elements in the cluster
-    simgrid.NetZone.create_torus_zone("cluster", None, [2, 2, 2], simgrid.ClusterCallbacks(create_hostzone, None, create_limiter), 10e9, 10e-6,
+    simgrid.NetZone.create_torus_zone("cluster", None, [2, 2, 2],
+                                      simgrid.ClusterCallbacks(create_hostzone, None, create_limiter), 10e9, 10e-6,
                                       simgrid.Link.SharingPolicy.SPLITDUPLEX).seal()
 
 #####################################################################################################
 
 
 def create_fat_tree_cluster():
-    """
+    r"""
     Creates a Fat-Tree cluster
 
     Creates a Fat-Tree cluster with 2 levels and 6 nodes
@@ -224,14 +227,15 @@ def create_fat_tree_cluster():
     Cluster</a>
     """
     # create the fat tree cluster, 10Gbs link between elements in the cluster
-    simgrid.NetZone.create_fatTree_zone("cluster", None, simgrid.FatTreeParams(2, [2, 3], [1, 2], [1, 1]), simgrid.ClusterCallbacks(create_hostzone, None, create_limiter), 10e9,
-                                        10e-6, simgrid.Link.SharingPolicy.SPLITDUPLEX).seal()
+    simgrid.NetZone.create_fatTree_zone("cluster", None, simgrid.FatTreeParams(2, [2, 3], [1, 2], [1, 1]),
+                                        simgrid.ClusterCallbacks(create_hostzone, None, create_limiter), 10e9, 10e-6,
+                                        simgrid.Link.SharingPolicy.SPLITDUPLEX).seal()
 
 #####################################################################################################
 
 
 def create_dragonfly_cluster():
-    """
+    r"""
     Creates a Dragonfly cluster
 
     Creates a Dragonfly cluster with 2 groups and 16 nodes
@@ -269,13 +273,14 @@ def create_dragonfly_cluster():
     Cluster</a>
     """
     # create the dragonfly cluster, 10Gbs link between elements in the cluster
-    simgrid.NetZone.create_dragonfly_zone("cluster", None, simgrid.DragonflyParams([2, 2], [2, 1], [2, 2], 2), simgrid.ClusterCallbacks(
-        create_hostzone, None, create_limiter), 10e9, 10e-6, simgrid.Link.SharingPolicy.SPLITDUPLEX).seal()
+    simgrid.NetZone.create_dragonfly_zone("cluster", None, simgrid.DragonflyParams([2, 2], [2, 1], [2, 2], 2),
+                                          simgrid.ClusterCallbacks(create_hostzone, None, create_limiter), 10e9, 10e-6,
+                                          simgrid.Link.SharingPolicy.SPLITDUPLEX).seal()
 
 ###################################################################################################
 
 
-if __name__ == '__main__':
+def main():
     e = simgrid.Engine(sys.argv)
     platform = sys.argv[1]
 
@@ -289,7 +294,7 @@ if __name__ == '__main__':
     else:
         sys.exit("invalid param")
 
-    host_list = e.get_all_hosts()
+    host_list = e.all_hosts
     # create the sender actor running on first host
     simgrid.Actor.create("sender", host_list[0], Sender(host_list))
     # create receiver in every host
@@ -298,3 +303,6 @@ if __name__ == '__main__':
 
     # runs the simulation
     e.run()
+
+if __name__ == '__main__':
+    main()