Topology.cc revision 10078
15389Sgblack@eecs.umich.edu/*
25446Sgblack@eecs.umich.edu * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
35389Sgblack@eecs.umich.edu * All rights reserved.
45389Sgblack@eecs.umich.edu *
55389Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
65389Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
75389Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
85389Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
95389Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
105389Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
115389Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
125389Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
135389Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
145389Sgblack@eecs.umich.edu * this software without specific prior written permission.
155389Sgblack@eecs.umich.edu *
165389Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
175389Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
185389Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
195389Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
205389Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215389Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
225389Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235389Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245389Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255389Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265389Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275389Sgblack@eecs.umich.edu */
285389Sgblack@eecs.umich.edu
295389Sgblack@eecs.umich.edu#include <cassert>
305389Sgblack@eecs.umich.edu
315389Sgblack@eecs.umich.edu#include "base/trace.hh"
325389Sgblack@eecs.umich.edu#include "debug/RubyNetwork.hh"
335389Sgblack@eecs.umich.edu#include "mem/protocol/MachineType.hh"
345389Sgblack@eecs.umich.edu#include "mem/ruby/common/NetDest.hh"
355389Sgblack@eecs.umich.edu#include "mem/ruby/network/BasicLink.hh"
365389Sgblack@eecs.umich.edu#include "mem/ruby/network/Topology.hh"
375389Sgblack@eecs.umich.edu#include "mem/ruby/slicc_interface/AbstractController.hh"
385389Sgblack@eecs.umich.edu
395654Sgblack@eecs.umich.eduusing namespace std;
405389Sgblack@eecs.umich.edu
415389Sgblack@eecs.umich.educonst int INFINITE_LATENCY = 10000; // Yes, this is a big hack
425478Snate@binkert.org
435643Sgblack@eecs.umich.edu// Note: In this file, we use the first 2*m_nodes SwitchIDs to
445636Sgblack@eecs.umich.edu// represent the input and output endpoint links.  These really are
455389Sgblack@eecs.umich.edu// not 'switches', as they will not have a Switch object allocated for
465637Sgblack@eecs.umich.edu// them. The first m_nodes SwitchIDs are the links into the network,
475389Sgblack@eecs.umich.edu// the second m_nodes set of SwitchIDs represent the the output queues
485389Sgblack@eecs.umich.edu// of the network.
495389Sgblack@eecs.umich.edu
505389Sgblack@eecs.umich.edu// Helper functions based on chapter 29 of Cormen et al.
515389Sgblack@eecs.umich.eduvoid extend_shortest_path(Matrix& current_dist, Matrix& latencies,
525638Sgblack@eecs.umich.edu    Matrix& inter_switches);
535389Sgblack@eecs.umich.eduMatrix shortest_path(const Matrix& weights, Matrix& latencies,
545389Sgblack@eecs.umich.edu    Matrix& inter_switches);
555446Sgblack@eecs.umich.edubool link_is_shortest_path_to_node(SwitchID src, SwitchID next,
565389Sgblack@eecs.umich.edu    SwitchID final, const Matrix& weights, const Matrix& dist);
575389Sgblack@eecs.umich.eduNetDest shortest_path_to_node(SwitchID src, SwitchID next,
585389Sgblack@eecs.umich.edu    const Matrix& weights, const Matrix& dist);
595389Sgblack@eecs.umich.edu
605446Sgblack@eecs.umich.eduTopology::Topology(uint32_t num_routers, vector<BasicExtLink *> ext_links,
615638Sgblack@eecs.umich.edu                   vector<BasicIntLink *> int_links)
625446Sgblack@eecs.umich.edu    : m_number_of_switches(num_routers)
635446Sgblack@eecs.umich.edu{
645643Sgblack@eecs.umich.edu
655643Sgblack@eecs.umich.edu    // initialize component latencies record
665643Sgblack@eecs.umich.edu    m_component_latencies.resize(0);
675643Sgblack@eecs.umich.edu    m_component_inter_switches.resize(0);
685636Sgblack@eecs.umich.edu
695446Sgblack@eecs.umich.edu    // Total nodes/controllers in network
705446Sgblack@eecs.umich.edu    // Must make sure this is called after the State Machine constructors
715446Sgblack@eecs.umich.edu    m_nodes = MachineType_base_number(MachineType_NUM);
725446Sgblack@eecs.umich.edu    assert(m_nodes > 1);
735446Sgblack@eecs.umich.edu
745635Sgblack@eecs.umich.edu    if (m_nodes != ext_links.size()) {
755635Sgblack@eecs.umich.edu        fatal("m_nodes (%d) != ext_links vector length (%d)\n",
765643Sgblack@eecs.umich.edu              m_nodes, ext_links.size());
775643Sgblack@eecs.umich.edu    }
785643Sgblack@eecs.umich.edu
795643Sgblack@eecs.umich.edu    // analyze both the internal and external links, create data structures
805643Sgblack@eecs.umich.edu    // Note that the python created links are bi-directional, but that the
815643Sgblack@eecs.umich.edu    // topology and networks utilize uni-directional links.  Thus each
825654Sgblack@eecs.umich.edu    // BasicLink is converted to two calls to add link, on for each direction
835643Sgblack@eecs.umich.edu    for (vector<BasicExtLink*>::const_iterator i = ext_links.begin();
845643Sgblack@eecs.umich.edu         i != ext_links.end(); ++i) {
855643Sgblack@eecs.umich.edu        BasicExtLink *ext_link = (*i);
865446Sgblack@eecs.umich.edu        AbstractController *abs_cntrl = ext_link->params()->ext_node;
875446Sgblack@eecs.umich.edu        BasicRouter *router = ext_link->params()->int_node;
885389Sgblack@eecs.umich.edu
895638Sgblack@eecs.umich.edu        // Store the ExtLink pointers for later
905389Sgblack@eecs.umich.edu        m_ext_link_vector.push_back(ext_link);
915389Sgblack@eecs.umich.edu
925389Sgblack@eecs.umich.edu        int machine_base_idx = MachineType_base_number(abs_cntrl->getType());
935389Sgblack@eecs.umich.edu        int ext_idx1 = machine_base_idx + abs_cntrl->getVersion();
945389Sgblack@eecs.umich.edu        int ext_idx2 = ext_idx1 + m_nodes;
955389Sgblack@eecs.umich.edu        int int_idx = router->params()->router_id + 2*m_nodes;
965638Sgblack@eecs.umich.edu
975389Sgblack@eecs.umich.edu        // create the internal uni-directional links in both directions
985389Sgblack@eecs.umich.edu        //   the first direction is marked: In
995389Sgblack@eecs.umich.edu        addLink(ext_idx1, int_idx, ext_link, LinkDirection_In);
1005389Sgblack@eecs.umich.edu        //   the first direction is marked: Out
1015389Sgblack@eecs.umich.edu        addLink(int_idx, ext_idx2, ext_link, LinkDirection_Out);
1025389Sgblack@eecs.umich.edu    }
1035638Sgblack@eecs.umich.edu
1045389Sgblack@eecs.umich.edu    for (vector<BasicIntLink*>::const_iterator i = int_links.begin();
1055389Sgblack@eecs.umich.edu         i != int_links.end(); ++i) {
1065389Sgblack@eecs.umich.edu        BasicIntLink *int_link = (*i);
1075389Sgblack@eecs.umich.edu        BasicRouter *router_a = int_link->params()->node_a;
1085389Sgblack@eecs.umich.edu        BasicRouter *router_b = int_link->params()->node_b;
1095389Sgblack@eecs.umich.edu
1105638Sgblack@eecs.umich.edu        // Store the IntLink pointers for later
1115389Sgblack@eecs.umich.edu        m_int_link_vector.push_back(int_link);
1125389Sgblack@eecs.umich.edu
1135389Sgblack@eecs.umich.edu        int a = router_a->params()->router_id + 2*m_nodes;
1145389Sgblack@eecs.umich.edu        int b = router_b->params()->router_id + 2*m_nodes;
1155389Sgblack@eecs.umich.edu
1165638Sgblack@eecs.umich.edu        // create the internal uni-directional links in both directions
1175389Sgblack@eecs.umich.edu        //   the first direction is marked: In
1185389Sgblack@eecs.umich.edu        addLink(a, b, int_link, LinkDirection_In);
1195389Sgblack@eecs.umich.edu        //   the second direction is marked: Out
1205389Sgblack@eecs.umich.edu        addLink(b, a, int_link, LinkDirection_Out);
1215389Sgblack@eecs.umich.edu    }
1225638Sgblack@eecs.umich.edu}
1235389Sgblack@eecs.umich.edu
1245389Sgblack@eecs.umich.eduvoid
1255389Sgblack@eecs.umich.eduTopology::createLinks(Network *net)
1265389Sgblack@eecs.umich.edu{
1275389Sgblack@eecs.umich.edu    // Find maximum switchID
1285389Sgblack@eecs.umich.edu    SwitchID max_switch_id = 0;
1295389Sgblack@eecs.umich.edu    for (LinkMap::const_iterator i = m_link_map.begin();
1305638Sgblack@eecs.umich.edu         i != m_link_map.end(); ++i) {
1315389Sgblack@eecs.umich.edu        std::pair<SwitchID, SwitchID> src_dest = (*i).first;
1325389Sgblack@eecs.umich.edu        max_switch_id = max(max_switch_id, src_dest.first);
1335389Sgblack@eecs.umich.edu        max_switch_id = max(max_switch_id, src_dest.second);
1345389Sgblack@eecs.umich.edu    }
1355389Sgblack@eecs.umich.edu
1365389Sgblack@eecs.umich.edu    // Initialize weight, latency, and inter switched vectors
1375389Sgblack@eecs.umich.edu    Matrix topology_weights;
1385638Sgblack@eecs.umich.edu    int num_switches = max_switch_id+1;
1395638Sgblack@eecs.umich.edu    topology_weights.resize(num_switches);
1405389Sgblack@eecs.umich.edu    m_component_latencies.resize(num_switches);
1415638Sgblack@eecs.umich.edu    m_component_inter_switches.resize(num_switches);
1425389Sgblack@eecs.umich.edu
143    for (int i = 0; i < topology_weights.size(); i++) {
144        topology_weights[i].resize(num_switches);
145        m_component_latencies[i].resize(num_switches);
146        m_component_inter_switches[i].resize(num_switches);
147
148        for (int j = 0; j < topology_weights[i].size(); j++) {
149            topology_weights[i][j] = INFINITE_LATENCY;
150
151            // initialize to invalid values
152            m_component_latencies[i][j] = -1;
153
154            // initially assume direct connections / no intermediate
155            // switches between components
156            m_component_inter_switches[i][j] = 0;
157        }
158    }
159
160    // Set identity weights to zero
161    for (int i = 0; i < topology_weights.size(); i++) {
162        topology_weights[i][i] = 0;
163    }
164
165    // Fill in the topology weights and bandwidth multipliers
166    for (LinkMap::const_iterator i = m_link_map.begin();
167         i != m_link_map.end(); ++i) {
168        std::pair<int, int> src_dest = (*i).first;
169        BasicLink* link = (*i).second.link;
170        int src = src_dest.first;
171        int dst = src_dest.second;
172        m_component_latencies[src][dst] = link->m_latency;
173        topology_weights[src][dst] = link->m_weight;
174    }
175
176    // Walk topology and hookup the links
177    Matrix dist = shortest_path(topology_weights, m_component_latencies,
178        m_component_inter_switches);
179    for (int i = 0; i < topology_weights.size(); i++) {
180        for (int j = 0; j < topology_weights[i].size(); j++) {
181            int weight = topology_weights[i][j];
182            if (weight > 0 && weight != INFINITE_LATENCY) {
183                NetDest destination_set =
184                        shortest_path_to_node(i, j, topology_weights, dist);
185                makeLink(net, i, j, destination_set);
186            }
187        }
188    }
189}
190
191void
192Topology::addLink(SwitchID src, SwitchID dest, BasicLink* link,
193                  LinkDirection dir)
194{
195    assert(src <= m_number_of_switches+m_nodes+m_nodes);
196    assert(dest <= m_number_of_switches+m_nodes+m_nodes);
197
198    std::pair<int, int> src_dest_pair;
199    LinkEntry link_entry;
200
201    src_dest_pair.first = src;
202    src_dest_pair.second = dest;
203    link_entry.direction = dir;
204    link_entry.link = link;
205    m_link_map[src_dest_pair] = link_entry;
206}
207
208void
209Topology::makeLink(Network *net, SwitchID src, SwitchID dest,
210                   const NetDest& routing_table_entry)
211{
212    // Make sure we're not trying to connect two end-point nodes
213    // directly together
214    assert(src >= 2 * m_nodes || dest >= 2 * m_nodes);
215
216    std::pair<int, int> src_dest;
217    LinkEntry link_entry;
218
219    if (src < m_nodes) {
220        src_dest.first = src;
221        src_dest.second = dest;
222        link_entry = m_link_map[src_dest];
223        net->makeInLink(src, dest - (2 * m_nodes), link_entry.link,
224                        link_entry.direction, routing_table_entry);
225    } else if (dest < 2*m_nodes) {
226        assert(dest >= m_nodes);
227        NodeID node = dest - m_nodes;
228        src_dest.first = src;
229        src_dest.second = dest;
230        link_entry = m_link_map[src_dest];
231        net->makeOutLink(src - (2 * m_nodes), node, link_entry.link,
232                         link_entry.direction, routing_table_entry);
233    } else {
234        assert((src >= 2 * m_nodes) && (dest >= 2 * m_nodes));
235        src_dest.first = src;
236        src_dest.second = dest;
237        link_entry = m_link_map[src_dest];
238        net->makeInternalLink(src - (2 * m_nodes), dest - (2 * m_nodes),
239                              link_entry.link, link_entry.direction,
240                              routing_table_entry);
241    }
242}
243
244// The following all-pairs shortest path algorithm is based on the
245// discussion from Cormen et al., Chapter 26.1.
246void
247extend_shortest_path(Matrix& current_dist, Matrix& latencies,
248    Matrix& inter_switches)
249{
250    bool change = true;
251    int nodes = current_dist.size();
252
253    while (change) {
254        change = false;
255        for (int i = 0; i < nodes; i++) {
256            for (int j = 0; j < nodes; j++) {
257                int minimum = current_dist[i][j];
258                int previous_minimum = minimum;
259                int intermediate_switch = -1;
260                for (int k = 0; k < nodes; k++) {
261                    minimum = min(minimum,
262                        current_dist[i][k] + current_dist[k][j]);
263                    if (previous_minimum != minimum) {
264                        intermediate_switch = k;
265                        inter_switches[i][j] =
266                            inter_switches[i][k] +
267                            inter_switches[k][j] + 1;
268                    }
269                    previous_minimum = minimum;
270                }
271                if (current_dist[i][j] != minimum) {
272                    change = true;
273                    current_dist[i][j] = minimum;
274                    assert(intermediate_switch >= 0);
275                    assert(intermediate_switch < latencies[i].size());
276                    latencies[i][j] = latencies[i][intermediate_switch] +
277                        latencies[intermediate_switch][j];
278                }
279            }
280        }
281    }
282}
283
284Matrix
285shortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches)
286{
287    Matrix dist = weights;
288    extend_shortest_path(dist, latencies, inter_switches);
289    return dist;
290}
291
292bool
293link_is_shortest_path_to_node(SwitchID src, SwitchID next, SwitchID final,
294    const Matrix& weights, const Matrix& dist)
295{
296    return weights[src][next] + dist[next][final] == dist[src][final];
297}
298
299NetDest
300shortest_path_to_node(SwitchID src, SwitchID next, const Matrix& weights,
301    const Matrix& dist)
302{
303    NetDest result;
304    int d = 0;
305    int machines;
306    int max_machines;
307
308    machines = MachineType_NUM;
309    max_machines = MachineType_base_number(MachineType_NUM);
310
311    for (int m = 0; m < machines; m++) {
312        for (NodeID i = 0; i < MachineType_base_count((MachineType)m); i++) {
313            // we use "d+max_machines" below since the "destination"
314            // switches for the machines are numbered
315            // [MachineType_base_number(MachineType_NUM)...
316            //  2*MachineType_base_number(MachineType_NUM)-1] for the
317            // component network
318            if (link_is_shortest_path_to_node(src, next, d + max_machines,
319                    weights, dist)) {
320                MachineID mach = {(MachineType)m, i};
321                result.add(mach);
322            }
323            d++;
324        }
325    }
326
327    DPRINTF(RubyNetwork, "Returning shortest path\n"
328            "(src-(2*max_machines)): %d, (next-(2*max_machines)): %d, "
329            "src: %d, next: %d, result: %s\n",
330            (src-(2*max_machines)), (next-(2*max_machines)),
331            src, next, result);
332
333    return result;
334}
335