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
30from __future__ import print_function
31from __future__ import absolute_import
32
33from m5.params import *
34from m5.objects import *
35
36from .BaseTopology import SimpleTopology
37
38# Creates a generic Mesh assuming an equal number of cache
39# and directory controllers.
40# West-first routing is enforced (using link weights)
41# to guarantee deadlock freedom.
42# The network randomly chooses between links with the same
43# weight for messages within unordered virtual networks.
44# Within ordered virtual networks, a fixed link direction
45# is always chosen based on which appears first inside the
46# routing table.
47
48class Mesh_westfirst(SimpleTopology):
49    description='Mesh_westfirst'
50
51    def __init__(self, controllers):
52        self.nodes = controllers
53
54    # Makes a generic mesh
55    # assuming an equal number of cache and directory cntrls
56
57    def makeTopology(self, options, network, IntLink, ExtLink, Router):
58        nodes = self.nodes
59
60        num_routers = options.num_cpus
61        num_rows = options.mesh_rows
62
63        # default values for link latency and router latency.
64        # Can be over-ridden on a per link/router basis
65        link_latency = options.link_latency # used by simple and garnet
66        router_latency = options.router_latency # only used by garnet
67
68        # There must be an evenly divisible number of cntrls to routers
69        # Also, obviously the number or rows must be <= the number of routers
70        cntrls_per_router, remainder = divmod(len(nodes), num_routers)
71        assert(num_rows > 0 and num_rows <= num_routers)
72        num_columns = int(num_routers / num_rows)
73        assert(num_columns * num_rows == num_routers)
74
75        # Create the routers in the mesh
76        routers = [Router(router_id=i, latency=router_latency) \
77            for i in range(num_routers)]
78        network.routers = routers
79
80        # link counter to set unique link ids
81        link_count = 0
82
83        # Add all but the remainder nodes to the list of nodes to be uniformly
84        # distributed across the network.
85        network_nodes = []
86        remainder_nodes = []
87        for node_index in range(len(nodes)):
88            if node_index < (len(nodes) - remainder):
89                network_nodes.append(nodes[node_index])
90            else:
91                remainder_nodes.append(nodes[node_index])
92
93        # Connect each node to the appropriate router
94        ext_links = []
95        for (i, n) in enumerate(network_nodes):
96            cntrl_level, router_id = divmod(i, num_routers)
97            assert(cntrl_level < cntrls_per_router)
98            ext_links.append(ExtLink(link_id=link_count, ext_node=n,
99                                    int_node=routers[router_id],
100                                    latency = link_latency))
101            link_count += 1
102
103        # Connect the remainding nodes to router 0.  These should only be
104        # DMA nodes.
105        for (i, node) in enumerate(remainder_nodes):
106            assert(node.type == 'DMA_Controller')
107            assert(i < remainder)
108            ext_links.append(ExtLink(link_id=link_count, ext_node=node,
109                                    int_node=routers[0],
110                                    latency = link_latency))
111            link_count += 1
112
113        network.ext_links = ext_links
114
115        # Create the mesh links.
116        int_links = []
117
118        # East output to West input links (weight = 2)
119        for row in range(num_rows):
120            for col in range(num_columns):
121                if (col + 1 < num_columns):
122                    east_out = col + (row * num_columns)
123                    west_in = (col + 1) + (row * num_columns)
124                    int_links.append(IntLink(link_id=link_count,
125                                             src_node=routers[east_out],
126                                             dst_node=routers[west_in],
127                                             latency = link_latency,
128                                             weight=2))
129                    link_count += 1
130
131        # West output to East input links (weight = 1)
132        for row in range(num_rows):
133            for col in range(num_columns):
134                if (col + 1 < num_columns):
135                    east_in = col + (row * num_columns)
136                    west_out = (col + 1) + (row * num_columns)
137                    int_links.append(IntLink(link_id=link_count,
138                                             src_node=routers[west_out],
139                                             dst_node=routers[east_in],
140                                             latency = link_latency,
141                                             weight=1))
142                    link_count += 1
143
144
145        # North output to South input links (weight = 2)
146        for col in range(num_columns):
147            for row in range(num_rows):
148                if (row + 1 < num_rows):
149                    north_out = col + (row * num_columns)
150                    south_in = col + ((row + 1) * num_columns)
151                    int_links.append(IntLink(link_id=link_count,
152                                             src_node=routers[north_out],
153                                             dst_node=routers[south_in],
154                                             latency = link_latency,
155                                             weight=2))
156                    link_count += 1
157
158        # South output to North input links (weight = 2)
159        for col in range(num_columns):
160            for row in range(num_rows):
161                if (row + 1 < num_rows):
162                    north_in = col + (row * num_columns)
163                    south_out = col + ((row + 1) * num_columns)
164                    int_links.append(IntLink(link_id=link_count,
165                                             src_node=routers[south_out],
166                                             dst_node=routers[north_in],
167                                             latency = link_latency,
168                                             weight=2))
169                    link_count += 1
170
171
172        network.int_links = int_links
173