Topology.cc revision 9593:9441ca79f3c8
1/*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
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
29#include <cassert>
30
31#include "base/trace.hh"
32#include "debug/RubyNetwork.hh"
33#include "mem/protocol/MachineType.hh"
34#include "mem/ruby/common/NetDest.hh"
35#include "mem/ruby/network/BasicLink.hh"
36#include "mem/ruby/network/Network.hh"
37#include "mem/ruby/network/Topology.hh"
38#include "mem/ruby/slicc_interface/AbstractController.hh"
39
40using namespace std;
41
42const int INFINITE_LATENCY = 10000; // Yes, this is a big hack
43
44// Note: In this file, we use the first 2*m_nodes SwitchIDs to
45// represent the input and output endpoint links.  These really are
46// not 'switches', as they will not have a Switch object allocated for
47// them. The first m_nodes SwitchIDs are the links into the network,
48// the second m_nodes set of SwitchIDs represent the the output queues
49// of the network.
50
51// Helper functions based on chapter 29 of Cormen et al.
52void extend_shortest_path(Matrix& current_dist, Matrix& latencies,
53    Matrix& inter_switches);
54Matrix shortest_path(const Matrix& weights, Matrix& latencies,
55    Matrix& inter_switches);
56bool link_is_shortest_path_to_node(SwitchID src, SwitchID next,
57    SwitchID final, const Matrix& weights, const Matrix& dist);
58NetDest shortest_path_to_node(SwitchID src, SwitchID next,
59    const Matrix& weights, const Matrix& dist);
60
61Topology::Topology(const Params *p)
62    : SimObject(p)
63{
64    m_number_of_switches = p->num_routers;
65
66    // initialize component latencies record
67    m_component_latencies.resize(0);
68    m_component_inter_switches.resize(0);
69
70    // Total nodes/controllers in network
71    // Must make sure this is called after the State Machine constructors
72    m_nodes = MachineType_base_number(MachineType_NUM);
73    assert(m_nodes > 1);
74
75    if (m_nodes != params()->ext_links.size() &&
76        m_nodes != params()->ext_links.size()) {
77        fatal("m_nodes (%d) != ext_links vector length (%d)\n",
78              m_nodes, params()->ext_links.size());
79    }
80
81    // analyze both the internal and external links, create data structures
82    // Note that the python created links are bi-directional, but that the
83    // topology and networks utilize uni-directional links.  Thus each
84    // BasicLink is converted to two calls to add link, on for each direction
85    for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin();
86         i != params()->ext_links.end(); ++i) {
87        BasicExtLink *ext_link = (*i);
88        AbstractController *abs_cntrl = ext_link->params()->ext_node;
89        BasicRouter *router = ext_link->params()->int_node;
90
91        // Store the ExtLink pointers for later
92        m_ext_link_vector.push_back(ext_link);
93
94        int ext_idx1 = abs_cntrl->params()->cntrl_id;
95        int ext_idx2 = ext_idx1 + m_nodes;
96        int int_idx = router->params()->router_id + 2*m_nodes;
97
98        // create the internal uni-directional links in both directions
99        //   the first direction is marked: In
100        addLink(ext_idx1, int_idx, ext_link, LinkDirection_In);
101        //   the first direction is marked: Out
102        addLink(int_idx, ext_idx2, ext_link, LinkDirection_Out);
103    }
104
105    for (vector<BasicIntLink*>::const_iterator i = params()->int_links.begin();
106         i != params()->int_links.end(); ++i) {
107        BasicIntLink *int_link = (*i);
108        BasicRouter *router_a = int_link->params()->node_a;
109        BasicRouter *router_b = int_link->params()->node_b;
110
111        // Store the IntLink pointers for later
112        m_int_link_vector.push_back(int_link);
113
114        int a = router_a->params()->router_id + 2*m_nodes;
115        int b = router_b->params()->router_id + 2*m_nodes;
116
117        // create the internal uni-directional links in both directions
118        //   the first direction is marked: In
119        addLink(a, b, int_link, LinkDirection_In);
120        //   the second direction is marked: Out
121        addLink(b, a, int_link, LinkDirection_Out);
122    }
123}
124
125void
126Topology::init()
127{
128}
129
130
131void
132Topology::initNetworkPtr(Network* net_ptr)
133{
134    for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin();
135         i != params()->ext_links.end(); ++i) {
136        BasicExtLink *ext_link = (*i);
137        AbstractController *abs_cntrl = ext_link->params()->ext_node;
138        abs_cntrl->initNetworkPtr(net_ptr);
139    }
140}
141
142void
143Topology::createLinks(Network *net, bool isReconfiguration)
144{
145    // Find maximum switchID
146    SwitchID max_switch_id = 0;
147    for (LinkMap::const_iterator i = m_link_map.begin();
148         i != m_link_map.end(); ++i) {
149        std::pair<int, int> src_dest = (*i).first;
150        max_switch_id = max(max_switch_id, src_dest.first);
151        max_switch_id = max(max_switch_id, src_dest.second);
152    }
153
154    // Initialize weight, latency, and inter switched vectors
155    Matrix topology_weights;
156    int num_switches = max_switch_id+1;
157    topology_weights.resize(num_switches);
158    m_component_latencies.resize(num_switches);
159    m_component_inter_switches.resize(num_switches);
160
161    for (int i = 0; i < topology_weights.size(); i++) {
162        topology_weights[i].resize(num_switches);
163        m_component_latencies[i].resize(num_switches);
164        m_component_inter_switches[i].resize(num_switches);
165
166        for (int j = 0; j < topology_weights[i].size(); j++) {
167            topology_weights[i][j] = INFINITE_LATENCY;
168
169            // initialize to invalid values
170            m_component_latencies[i][j] = -1;
171
172            // initially assume direct connections / no intermediate
173            // switches between components
174            m_component_inter_switches[i][j] = 0;
175        }
176    }
177
178    // Set identity weights to zero
179    for (int i = 0; i < topology_weights.size(); i++) {
180        topology_weights[i][i] = 0;
181    }
182
183    // Fill in the topology weights and bandwidth multipliers
184    for (LinkMap::const_iterator i = m_link_map.begin();
185         i != m_link_map.end(); ++i) {
186        std::pair<int, int> src_dest = (*i).first;
187        BasicLink* link = (*i).second.link;
188        int src = src_dest.first;
189        int dst = src_dest.second;
190        m_component_latencies[src][dst] = link->m_latency;
191        topology_weights[src][dst] = link->m_weight;
192    }
193
194    // Walk topology and hookup the links
195    Matrix dist = shortest_path(topology_weights, m_component_latencies,
196        m_component_inter_switches);
197    for (int i = 0; i < topology_weights.size(); i++) {
198        for (int j = 0; j < topology_weights[i].size(); j++) {
199            int weight = topology_weights[i][j];
200            if (weight > 0 && weight != INFINITE_LATENCY) {
201                NetDest destination_set = shortest_path_to_node(i, j,
202                                                     topology_weights, dist);
203                makeLink(net, i, j, destination_set, isReconfiguration);
204            }
205        }
206    }
207}
208
209void
210Topology::addLink(SwitchID src, SwitchID dest, BasicLink* link,
211                  LinkDirection dir)
212{
213    assert(src <= m_number_of_switches+m_nodes+m_nodes);
214    assert(dest <= m_number_of_switches+m_nodes+m_nodes);
215
216    std::pair<int, int> src_dest_pair;
217    LinkEntry link_entry;
218
219    src_dest_pair.first = src;
220    src_dest_pair.second = dest;
221    link_entry.direction = dir;
222    link_entry.link = link;
223    m_link_map[src_dest_pair] = link_entry;
224}
225
226void
227Topology::makeLink(Network *net, SwitchID src, SwitchID dest,
228                   const NetDest& routing_table_entry, bool isReconfiguration)
229{
230    // Make sure we're not trying to connect two end-point nodes
231    // directly together
232    assert(src >= 2 * m_nodes || dest >= 2 * m_nodes);
233
234    std::pair<int, int> src_dest;
235    LinkEntry link_entry;
236
237    if (src < m_nodes) {
238        src_dest.first = src;
239        src_dest.second = dest;
240        link_entry = m_link_map[src_dest];
241        net->makeInLink(src, dest - (2 * m_nodes), link_entry.link,
242                        link_entry.direction,
243                        routing_table_entry,
244                        isReconfiguration);
245    } else if (dest < 2*m_nodes) {
246        assert(dest >= m_nodes);
247        NodeID node = dest - m_nodes;
248        src_dest.first = src;
249        src_dest.second = dest;
250        link_entry = m_link_map[src_dest];
251        net->makeOutLink(src - (2 * m_nodes), node, link_entry.link,
252                         link_entry.direction,
253                         routing_table_entry,
254                         isReconfiguration);
255    } else {
256        assert((src >= 2 * m_nodes) && (dest >= 2 * m_nodes));
257        src_dest.first = src;
258        src_dest.second = dest;
259        link_entry = m_link_map[src_dest];
260        net->makeInternalLink(src - (2 * m_nodes), dest - (2 * m_nodes),
261                              link_entry.link, link_entry.direction,
262                              routing_table_entry, isReconfiguration);
263    }
264}
265
266// The following all-pairs shortest path algorithm is based on the
267// discussion from Cormen et al., Chapter 26.1.
268void
269extend_shortest_path(Matrix& current_dist, Matrix& latencies,
270    Matrix& inter_switches)
271{
272    bool change = true;
273    int nodes = current_dist.size();
274
275    while (change) {
276        change = false;
277        for (int i = 0; i < nodes; i++) {
278            for (int j = 0; j < nodes; j++) {
279                int minimum = current_dist[i][j];
280                int previous_minimum = minimum;
281                int intermediate_switch = -1;
282                for (int k = 0; k < nodes; k++) {
283                    minimum = min(minimum,
284                        current_dist[i][k] + current_dist[k][j]);
285                    if (previous_minimum != minimum) {
286                        intermediate_switch = k;
287                        inter_switches[i][j] =
288                            inter_switches[i][k] +
289                            inter_switches[k][j] + 1;
290                    }
291                    previous_minimum = minimum;
292                }
293                if (current_dist[i][j] != minimum) {
294                    change = true;
295                    current_dist[i][j] = minimum;
296                    assert(intermediate_switch >= 0);
297                    assert(intermediate_switch < latencies[i].size());
298                    latencies[i][j] = latencies[i][intermediate_switch] +
299                        latencies[intermediate_switch][j];
300                }
301            }
302        }
303    }
304}
305
306Matrix
307shortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches)
308{
309    Matrix dist = weights;
310    extend_shortest_path(dist, latencies, inter_switches);
311    return dist;
312}
313
314bool
315link_is_shortest_path_to_node(SwitchID src, SwitchID next, SwitchID final,
316    const Matrix& weights, const Matrix& dist)
317{
318    return weights[src][next] + dist[next][final] == dist[src][final];
319}
320
321NetDest
322shortest_path_to_node(SwitchID src, SwitchID next, const Matrix& weights,
323    const Matrix& dist)
324{
325    NetDest result;
326    int d = 0;
327    int machines;
328    int max_machines;
329
330    machines = MachineType_NUM;
331    max_machines = MachineType_base_number(MachineType_NUM);
332
333    for (int m = 0; m < machines; m++) {
334        for (int i = 0; i < MachineType_base_count((MachineType)m); i++) {
335            // we use "d+max_machines" below since the "destination"
336            // switches for the machines are numbered
337            // [MachineType_base_number(MachineType_NUM)...
338            //  2*MachineType_base_number(MachineType_NUM)-1] for the
339            // component network
340            if (link_is_shortest_path_to_node(src, next, d + max_machines,
341                    weights, dist)) {
342                MachineID mach = {(MachineType)m, i};
343                result.add(mach);
344            }
345            d++;
346        }
347    }
348
349    DPRINTF(RubyNetwork, "Returning shortest path\n"
350            "(src-(2*max_machines)): %d, (next-(2*max_machines)): %d, "
351            "src: %d, next: %d, result: %s\n",
352            (src-(2*max_machines)), (next-(2*max_machines)),
353            src, next, result);
354
355    return result;
356}
357
358Topology *
359TopologyParams::create()
360{
361    return new Topology(this);
362}
363
364