Mesh_westfirst.py revision 11663:cf870cd20cfc
1# Copyright (c) 2010 Advanced Micro Devices, Inc.
2#               2016 Georgia Institute of Technology
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met: redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer;
9# redistributions in binary form must reproduce the above copyright
10# notice, this list of conditions and the following disclaimer in the
11# documentation and/or other materials provided with the distribution;
12# neither the name of the copyright holders nor the names of its
13# contributors may be used to endorse or promote products derived from
14# this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28# Authors: Brad Beckmann
29#          Tushar Krishna
30
31from m5.params import *
32from m5.objects import *
33
34from BaseTopology import SimpleTopology
35
36# Creates a generic Mesh assuming an equal number of cache
37# and directory controllers.
38# West-first routing is enforced (using link weights)
39# to guarantee deadlock freedom.
40# The network randomly chooses between links with the same
41# weight for messages within unordered virtual networks.
42# Within ordered virtual networks, a fixed link direction
43# is always chosen based on which appears first inside the
44# routing table.
45
46class Mesh_westfirst(SimpleTopology):
47    description='Mesh_westfirst'
48
49    def __init__(self, controllers):
50        self.nodes = controllers
51
52    # Makes a generic mesh
53    # assuming an equal number of cache and directory cntrls
54
55    def makeTopology(self, options, network, IntLink, ExtLink, Router):
56        nodes = self.nodes
57
58        num_routers = options.num_cpus
59        num_rows = options.mesh_rows
60
61        # There must be an evenly divisible number of cntrls to routers
62        # Also, obviously the number or rows must be <= the number of routers
63        cntrls_per_router, remainder = divmod(len(nodes), num_routers)
64        assert(num_rows <= num_routers)
65        num_columns = int(num_routers / num_rows)
66        assert(num_columns * num_rows == num_routers)
67
68        # Create the routers in the mesh
69        routers = [Router(router_id=i) for i in range(num_routers)]
70        network.routers = routers
71
72        # link counter to set unique link ids
73        link_count = 0
74
75        # Add all but the remainder nodes to the list of nodes to be uniformly
76        # distributed across the network.
77        network_nodes = []
78        remainder_nodes = []
79        for node_index in xrange(len(nodes)):
80            if node_index < (len(nodes) - remainder):
81                network_nodes.append(nodes[node_index])
82            else:
83                remainder_nodes.append(nodes[node_index])
84
85        # Connect each node to the appropriate router
86        ext_links = []
87        for (i, n) in enumerate(network_nodes):
88            cntrl_level, router_id = divmod(i, num_routers)
89            assert(cntrl_level < cntrls_per_router)
90            ext_links.append(ExtLink(link_id=link_count, ext_node=n,
91                                    int_node=routers[router_id]))
92            link_count += 1
93
94        # Connect the remainding nodes to router 0.  These should only be
95        # DMA nodes.
96        for (i, node) in enumerate(remainder_nodes):
97            assert(node.type == 'DMA_Controller')
98            assert(i < remainder)
99            ext_links.append(ExtLink(link_id=link_count, ext_node=node,
100                                    int_node=routers[0]))
101            link_count += 1
102
103        network.ext_links = ext_links
104
105        # Create the mesh links.
106        int_links = []
107
108        # East output to West input links (weight = 2)
109        for row in xrange(num_rows):
110            for col in xrange(num_columns):
111                if (col + 1 < num_columns):
112                    east_out = col + (row * num_columns)
113                    west_in = (col + 1) + (row * num_columns)
114                    int_links.append(IntLink(link_id=link_count,
115                                             src_node=routers[east_out],
116                                             dst_node=routers[west_in],
117                                             weight=2))
118                    link_count += 1
119
120        # West output to East input links (weight = 1)
121        for row in xrange(num_rows):
122            for col in xrange(num_columns):
123                if (col + 1 < num_columns):
124                    east_in = col + (row * num_columns)
125                    west_out = (col + 1) + (row * num_columns)
126                    int_links.append(IntLink(link_id=link_count,
127                                             src_node=routers[west_out],
128                                             dst_node=routers[east_in],
129                                             weight=1))
130                    link_count += 1
131
132
133        # North output to South input links (weight = 2)
134        for col in xrange(num_columns):
135            for row in xrange(num_rows):
136                if (row + 1 < num_rows):
137                    north_out = col + (row * num_columns)
138                    south_in = col + ((row + 1) * num_columns)
139                    int_links.append(IntLink(link_id=link_count,
140                                             src_node=routers[north_out],
141                                             dst_node=routers[south_in],
142                                             weight=2))
143                    link_count += 1
144
145        # South output to North input links (weight = 2)
146        for col in xrange(num_columns):
147            for row in xrange(num_rows):
148                if (row + 1 < num_rows):
149                    north_in = col + (row * num_columns)
150                    south_out = col + ((row + 1) * num_columns)
151                    int_links.append(IntLink(link_id=link_count,
152                                             src_node=routers[south_out],
153                                             dst_node=routers[north_in],
154                                             weight=2))
155                    link_count += 1
156
157
158        network.int_links = int_links
159