Cluster.py revision 10088:eca928d8a4ab
1# Copyright (c) 2012 Advanced Micro Devices, Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Jason Power
28
29
30from BaseTopology import BaseTopology
31
32class Cluster(BaseTopology):
33    """ A cluster is a group of nodes which are all one hop from eachother
34        Clusters can also contain other clusters
35        When creating this kind of topology, return a single cluster (usually
36        the root cluster) from create_system in configs/ruby/<protocol>.py
37    """
38
39    _num_int_links = 0
40    _num_ext_links = 0
41    _num_routers = 0
42
43    # Below methods for auto counting
44    @classmethod
45    def num_int_links(cls):
46        cls._num_int_links += 1
47        return cls._num_int_links - 1
48    @classmethod
49    def num_ext_links(cls):
50        cls._num_ext_links += 1
51        return cls._num_ext_links - 1
52    @classmethod
53    def num_routers(cls):
54        cls._num_routers += 1
55        return cls._num_routers - 1
56
57    def __init__(self, intBW=0, extBW=0, intLatency=0, extLatency=0):
58        """ internalBandwidth is bandwidth of all links within the cluster
59            externalBandwidth is bandwidth from this cluster to any cluster
60                connecting to it.
61            internal/externalLatency are similar
62            **** When creating a cluster with sub-clusters, the sub-cluster
63                 external bandwidth overrides the internal bandwidth of the
64                 super cluster
65        """
66        self.nodes = []
67        self.router = None # created in makeTopology
68        self.intBW = intBW
69        self.extBW = extBW
70        self.intLatency = intLatency
71        self.extLatency = extLatency
72
73    def add(self, node):
74        self.nodes.append(node)
75
76    def makeTopology(self, options, network, IntLink, ExtLink, Router):
77        """ Recursively make all of the links and routers
78        """
79
80        # make a router to connect all of the nodes
81        self.router = Router(router_id=self.num_routers())
82        network.routers.append(self.router)
83
84        for node in self.nodes:
85            if type(node) == Cluster:
86                node.makeTopology(options, network, IntLink, ExtLink, Router)
87
88                # connect this cluster to the router
89                link = IntLink(link_id=self.num_int_links(), node_a=self.router,
90                        node_b=node.router)
91
92                if node.extBW:
93                    link.bandwidth_factor = node.extBW
94
95                # if there is an interanl b/w for this node
96                # and no ext b/w to override
97                elif self.intBW:
98                    link.bandwidth_factor = self.intBW
99
100                if node.extLatency:
101                    link.latency = node.extLatency
102                elif self.intLatency:
103                    link.latency = self.intLatency
104
105                network.int_links.append(link)
106            else:
107                # node is just a controller,
108                # connect it to the router via a ext_link
109                link = ExtLink(link_id=self.num_ext_links(), ext_node=node,
110                        int_node=self.router)
111
112                if self.intBW:
113                    link.bandwidth_factor = self.intBW
114                if self.intLatency:
115                    link.latency = self.intLatency
116
117                network.ext_links.append(link)
118
119    def __len__(self):
120        return len([i for i in self.nodes if type(i) != Cluster]) + \
121               sum([len(i) for i in self.nodes if type(i) == Cluster])
122