Topology.cc revision 11096
16145SN/A/*
26145SN/A * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
36145SN/A * All rights reserved.
46145SN/A *
56145SN/A * Redistribution and use in source and binary forms, with or without
66145SN/A * modification, are permitted provided that the following conditions are
76145SN/A * met: redistributions of source code must retain the above copyright
86145SN/A * notice, this list of conditions and the following disclaimer;
96145SN/A * redistributions in binary form must reproduce the above copyright
106145SN/A * notice, this list of conditions and the following disclaimer in the
116145SN/A * documentation and/or other materials provided with the distribution;
126145SN/A * neither the name of the copyright holders nor the names of its
136145SN/A * contributors may be used to endorse or promote products derived from
146145SN/A * this software without specific prior written permission.
156145SN/A *
166145SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176145SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186145SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196145SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206145SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216145SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226145SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236145SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246145SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256145SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266145SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276145SN/A */
286145SN/A
297832SN/A#include <cassert>
307832SN/A
319356Snilay@cs.wisc.edu#include "base/trace.hh"
328232SN/A#include "debug/RubyNetwork.hh"
337054SN/A#include "mem/ruby/common/NetDest.hh"
348257SBrad.Beckmann@amd.com#include "mem/ruby/network/BasicLink.hh"
358255SBrad.Beckmann@amd.com#include "mem/ruby/network/Topology.hh"
367054SN/A#include "mem/ruby/slicc_interface/AbstractController.hh"
376145SN/A
387055SN/Ausing namespace std;
397055SN/A
407054SN/Aconst int INFINITE_LATENCY = 10000; // Yes, this is a big hack
418257SBrad.Beckmann@amd.com
426145SN/A// Note: In this file, we use the first 2*m_nodes SwitchIDs to
436145SN/A// represent the input and output endpoint links.  These really are
446145SN/A// not 'switches', as they will not have a Switch object allocated for
456145SN/A// them. The first m_nodes SwitchIDs are the links into the network,
466145SN/A// the second m_nodes set of SwitchIDs represent the the output queues
476145SN/A// of the network.
486145SN/A
4911096Snilay@cs.wisc.eduTopology::Topology(uint32_t num_routers,
5011096Snilay@cs.wisc.edu                   const vector<BasicExtLink *> &ext_links,
5111096Snilay@cs.wisc.edu                   const vector<BasicIntLink *> &int_links)
5211096Snilay@cs.wisc.edu    : m_nodes(ext_links.size()), m_number_of_switches(num_routers),
5311096Snilay@cs.wisc.edu      m_ext_link_vector(ext_links), m_int_link_vector(int_links)
546145SN/A{
556881SN/A    // Total nodes/controllers in network
566881SN/A    assert(m_nodes > 1);
576285SN/A
588257SBrad.Beckmann@amd.com    // analyze both the internal and external links, create data structures
598257SBrad.Beckmann@amd.com    // Note that the python created links are bi-directional, but that the
608257SBrad.Beckmann@amd.com    // topology and networks utilize uni-directional links.  Thus each
618257SBrad.Beckmann@amd.com    // BasicLink is converted to two calls to add link, on for each direction
629594Snilay@cs.wisc.edu    for (vector<BasicExtLink*>::const_iterator i = ext_links.begin();
639594Snilay@cs.wisc.edu         i != ext_links.end(); ++i) {
648257SBrad.Beckmann@amd.com        BasicExtLink *ext_link = (*i);
658257SBrad.Beckmann@amd.com        AbstractController *abs_cntrl = ext_link->params()->ext_node;
668257SBrad.Beckmann@amd.com        BasicRouter *router = ext_link->params()->int_node;
676881SN/A
6810078Snilay@cs.wisc.edu        int machine_base_idx = MachineType_base_number(abs_cntrl->getType());
699869Sjthestness@gmail.com        int ext_idx1 = machine_base_idx + abs_cntrl->getVersion();
707054SN/A        int ext_idx2 = ext_idx1 + m_nodes;
718257SBrad.Beckmann@amd.com        int int_idx = router->params()->router_id + 2*m_nodes;
726145SN/A
738257SBrad.Beckmann@amd.com        // create the internal uni-directional links in both directions
748257SBrad.Beckmann@amd.com        //   the first direction is marked: In
758257SBrad.Beckmann@amd.com        addLink(ext_idx1, int_idx, ext_link, LinkDirection_In);
768257SBrad.Beckmann@amd.com        //   the first direction is marked: Out
778257SBrad.Beckmann@amd.com        addLink(int_idx, ext_idx2, ext_link, LinkDirection_Out);
787054SN/A    }
796145SN/A
809594Snilay@cs.wisc.edu    for (vector<BasicIntLink*>::const_iterator i = int_links.begin();
819594Snilay@cs.wisc.edu         i != int_links.end(); ++i) {
828257SBrad.Beckmann@amd.com        BasicIntLink *int_link = (*i);
838257SBrad.Beckmann@amd.com        BasicRouter *router_a = int_link->params()->node_a;
848257SBrad.Beckmann@amd.com        BasicRouter *router_b = int_link->params()->node_b;
856881SN/A
868257SBrad.Beckmann@amd.com        // Store the IntLink pointers for later
878257SBrad.Beckmann@amd.com        m_int_link_vector.push_back(int_link);
888257SBrad.Beckmann@amd.com
898257SBrad.Beckmann@amd.com        int a = router_a->params()->router_id + 2*m_nodes;
908257SBrad.Beckmann@amd.com        int b = router_b->params()->router_id + 2*m_nodes;
918257SBrad.Beckmann@amd.com
928257SBrad.Beckmann@amd.com        // create the internal uni-directional links in both directions
938257SBrad.Beckmann@amd.com        //   the first direction is marked: In
948257SBrad.Beckmann@amd.com        addLink(a, b, int_link, LinkDirection_In);
958257SBrad.Beckmann@amd.com        //   the second direction is marked: Out
968257SBrad.Beckmann@amd.com        addLink(b, a, int_link, LinkDirection_Out);
977054SN/A    }
986145SN/A}
996145SN/A
1008257SBrad.Beckmann@amd.comvoid
1019799Snilay@cs.wisc.eduTopology::createLinks(Network *net)
1027054SN/A{
1037054SN/A    // Find maximum switchID
1047054SN/A    SwitchID max_switch_id = 0;
1058257SBrad.Beckmann@amd.com    for (LinkMap::const_iterator i = m_link_map.begin();
1068257SBrad.Beckmann@amd.com         i != m_link_map.end(); ++i) {
10710005Snilay@cs.wisc.edu        std::pair<SwitchID, SwitchID> src_dest = (*i).first;
1088257SBrad.Beckmann@amd.com        max_switch_id = max(max_switch_id, src_dest.first);
1098257SBrad.Beckmann@amd.com        max_switch_id = max(max_switch_id, src_dest.second);
1107054SN/A    }
1116881SN/A
1128257SBrad.Beckmann@amd.com    // Initialize weight, latency, and inter switched vectors
1137054SN/A    int num_switches = max_switch_id+1;
11411096Snilay@cs.wisc.edu    Matrix topology_weights(num_switches,
11511096Snilay@cs.wisc.edu            vector<int>(num_switches, INFINITE_LATENCY));
11611096Snilay@cs.wisc.edu    Matrix component_latencies(num_switches,
11711096Snilay@cs.wisc.edu            vector<int>(num_switches, -1));
11811096Snilay@cs.wisc.edu    Matrix component_inter_switches(num_switches,
11911096Snilay@cs.wisc.edu            vector<int>(num_switches, 0));
1206145SN/A
1217054SN/A    // Set identity weights to zero
1227054SN/A    for (int i = 0; i < topology_weights.size(); i++) {
1237054SN/A        topology_weights[i][i] = 0;
1247054SN/A    }
1256145SN/A
1267054SN/A    // Fill in the topology weights and bandwidth multipliers
1278257SBrad.Beckmann@amd.com    for (LinkMap::const_iterator i = m_link_map.begin();
1288257SBrad.Beckmann@amd.com         i != m_link_map.end(); ++i) {
1298257SBrad.Beckmann@amd.com        std::pair<int, int> src_dest = (*i).first;
1308257SBrad.Beckmann@amd.com        BasicLink* link = (*i).second.link;
1318257SBrad.Beckmann@amd.com        int src = src_dest.first;
1328257SBrad.Beckmann@amd.com        int dst = src_dest.second;
13311096Snilay@cs.wisc.edu        component_latencies[src][dst] = link->m_latency;
1348257SBrad.Beckmann@amd.com        topology_weights[src][dst] = link->m_weight;
1357054SN/A    }
1368257SBrad.Beckmann@amd.com
1377054SN/A    // Walk topology and hookup the links
13811096Snilay@cs.wisc.edu    Matrix dist = shortest_path(topology_weights, component_latencies,
13911096Snilay@cs.wisc.edu                                component_inter_switches);
14011096Snilay@cs.wisc.edu
1417054SN/A    for (int i = 0; i < topology_weights.size(); i++) {
1427054SN/A        for (int j = 0; j < topology_weights[i].size(); j++) {
1437054SN/A            int weight = topology_weights[i][j];
1447054SN/A            if (weight > 0 && weight != INFINITE_LATENCY) {
1459799Snilay@cs.wisc.edu                NetDest destination_set =
1469799Snilay@cs.wisc.edu                        shortest_path_to_node(i, j, topology_weights, dist);
1479799Snilay@cs.wisc.edu                makeLink(net, i, j, destination_set);
1487054SN/A            }
1497054SN/A        }
1506895SN/A    }
1516895SN/A}
1526895SN/A
1537054SN/Avoid
1548257SBrad.Beckmann@amd.comTopology::addLink(SwitchID src, SwitchID dest, BasicLink* link,
1558257SBrad.Beckmann@amd.com                  LinkDirection dir)
1567054SN/A{
1577832SN/A    assert(src <= m_number_of_switches+m_nodes+m_nodes);
1587832SN/A    assert(dest <= m_number_of_switches+m_nodes+m_nodes);
1598257SBrad.Beckmann@amd.com
1608257SBrad.Beckmann@amd.com    std::pair<int, int> src_dest_pair;
1618257SBrad.Beckmann@amd.com    LinkEntry link_entry;
1628257SBrad.Beckmann@amd.com
1638257SBrad.Beckmann@amd.com    src_dest_pair.first = src;
1648257SBrad.Beckmann@amd.com    src_dest_pair.second = dest;
1658257SBrad.Beckmann@amd.com    link_entry.direction = dir;
1668257SBrad.Beckmann@amd.com    link_entry.link = link;
1678257SBrad.Beckmann@amd.com    m_link_map[src_dest_pair] = link_entry;
1687054SN/A}
1697054SN/A
1707054SN/Avoid
1717054SN/ATopology::makeLink(Network *net, SwitchID src, SwitchID dest,
1729799Snilay@cs.wisc.edu                   const NetDest& routing_table_entry)
1737054SN/A{
1747054SN/A    // Make sure we're not trying to connect two end-point nodes
1757054SN/A    // directly together
1767054SN/A    assert(src >= 2 * m_nodes || dest >= 2 * m_nodes);
1777054SN/A
1788257SBrad.Beckmann@amd.com    std::pair<int, int> src_dest;
1798257SBrad.Beckmann@amd.com    LinkEntry link_entry;
1808257SBrad.Beckmann@amd.com
1817054SN/A    if (src < m_nodes) {
1828257SBrad.Beckmann@amd.com        src_dest.first = src;
1838257SBrad.Beckmann@amd.com        src_dest.second = dest;
1848257SBrad.Beckmann@amd.com        link_entry = m_link_map[src_dest];
1858257SBrad.Beckmann@amd.com        net->makeInLink(src, dest - (2 * m_nodes), link_entry.link,
1869799Snilay@cs.wisc.edu                        link_entry.direction, routing_table_entry);
1877054SN/A    } else if (dest < 2*m_nodes) {
1887054SN/A        assert(dest >= m_nodes);
1898257SBrad.Beckmann@amd.com        NodeID node = dest - m_nodes;
1908257SBrad.Beckmann@amd.com        src_dest.first = src;
1918257SBrad.Beckmann@amd.com        src_dest.second = dest;
1928257SBrad.Beckmann@amd.com        link_entry = m_link_map[src_dest];
1938257SBrad.Beckmann@amd.com        net->makeOutLink(src - (2 * m_nodes), node, link_entry.link,
1949799Snilay@cs.wisc.edu                         link_entry.direction, routing_table_entry);
1957054SN/A    } else {
1968257SBrad.Beckmann@amd.com        assert((src >= 2 * m_nodes) && (dest >= 2 * m_nodes));
1978257SBrad.Beckmann@amd.com        src_dest.first = src;
1988257SBrad.Beckmann@amd.com        src_dest.second = dest;
1998257SBrad.Beckmann@amd.com        link_entry = m_link_map[src_dest];
2008257SBrad.Beckmann@amd.com        net->makeInternalLink(src - (2 * m_nodes), dest - (2 * m_nodes),
2018257SBrad.Beckmann@amd.com                              link_entry.link, link_entry.direction,
2029799Snilay@cs.wisc.edu                              routing_table_entry);
2037054SN/A    }
2047054SN/A}
2057054SN/A
2066145SN/A// The following all-pairs shortest path algorithm is based on the
2076145SN/A// discussion from Cormen et al., Chapter 26.1.
2087054SN/Avoid
20911096Snilay@cs.wisc.eduTopology::extend_shortest_path(Matrix &current_dist, Matrix &latencies,
21011096Snilay@cs.wisc.edu    Matrix &inter_switches)
2117054SN/A{
2127054SN/A    bool change = true;
2137054SN/A    int nodes = current_dist.size();
2146145SN/A
2157054SN/A    while (change) {
2167054SN/A        change = false;
2177054SN/A        for (int i = 0; i < nodes; i++) {
2187054SN/A            for (int j = 0; j < nodes; j++) {
2197054SN/A                int minimum = current_dist[i][j];
2207054SN/A                int previous_minimum = minimum;
2217054SN/A                int intermediate_switch = -1;
2227054SN/A                for (int k = 0; k < nodes; k++) {
2237054SN/A                    minimum = min(minimum,
2247054SN/A                        current_dist[i][k] + current_dist[k][j]);
2257054SN/A                    if (previous_minimum != minimum) {
2267054SN/A                        intermediate_switch = k;
2277054SN/A                        inter_switches[i][j] =
2287054SN/A                            inter_switches[i][k] +
2297054SN/A                            inter_switches[k][j] + 1;
2307054SN/A                    }
2317054SN/A                    previous_minimum = minimum;
2327054SN/A                }
2337054SN/A                if (current_dist[i][j] != minimum) {
2347054SN/A                    change = true;
2357054SN/A                    current_dist[i][j] = minimum;
2367054SN/A                    assert(intermediate_switch >= 0);
2377054SN/A                    assert(intermediate_switch < latencies[i].size());
2387054SN/A                    latencies[i][j] = latencies[i][intermediate_switch] +
2397054SN/A                        latencies[intermediate_switch][j];
2407054SN/A                }
2417054SN/A            }
2426145SN/A        }
2436145SN/A    }
2446145SN/A}
2456145SN/A
2467054SN/AMatrix
24711096Snilay@cs.wisc.eduTopology::shortest_path(const Matrix &weights, Matrix &latencies,
24811096Snilay@cs.wisc.edu                        Matrix &inter_switches)
2496145SN/A{
2507054SN/A    Matrix dist = weights;
2517054SN/A    extend_shortest_path(dist, latencies, inter_switches);
2527054SN/A    return dist;
2536145SN/A}
2546145SN/A
2557054SN/Abool
25611096Snilay@cs.wisc.eduTopology::link_is_shortest_path_to_node(SwitchID src, SwitchID next,
25711096Snilay@cs.wisc.edu                                        SwitchID final, const Matrix &weights,
25811096Snilay@cs.wisc.edu                                        const Matrix &dist)
2596145SN/A{
2607054SN/A    return weights[src][next] + dist[next][final] == dist[src][final];
2616145SN/A}
2626145SN/A
2637054SN/ANetDest
26411096Snilay@cs.wisc.eduTopology::shortest_path_to_node(SwitchID src, SwitchID next,
26511096Snilay@cs.wisc.edu                                const Matrix &weights, const Matrix &dist)
2666145SN/A{
2677054SN/A    NetDest result;
2687054SN/A    int d = 0;
2697054SN/A    int machines;
2707054SN/A    int max_machines;
2716145SN/A
2727054SN/A    machines = MachineType_NUM;
2737054SN/A    max_machines = MachineType_base_number(MachineType_NUM);
2746145SN/A
2757054SN/A    for (int m = 0; m < machines; m++) {
27610005Snilay@cs.wisc.edu        for (NodeID i = 0; i < MachineType_base_count((MachineType)m); i++) {
2777054SN/A            // we use "d+max_machines" below since the "destination"
2787054SN/A            // switches for the machines are numbered
2797054SN/A            // [MachineType_base_number(MachineType_NUM)...
2807054SN/A            //  2*MachineType_base_number(MachineType_NUM)-1] for the
2817054SN/A            // component network
2827054SN/A            if (link_is_shortest_path_to_node(src, next, d + max_machines,
2837054SN/A                    weights, dist)) {
2847054SN/A                MachineID mach = {(MachineType)m, i};
2857054SN/A                result.add(mach);
2867054SN/A            }
2877054SN/A            d++;
2887054SN/A        }
2896145SN/A    }
2906145SN/A
2917780SN/A    DPRINTF(RubyNetwork, "Returning shortest path\n"
2927780SN/A            "(src-(2*max_machines)): %d, (next-(2*max_machines)): %d, "
2937780SN/A            "src: %d, next: %d, result: %s\n",
2947780SN/A            (src-(2*max_machines)), (next-(2*max_machines)),
2957780SN/A            src, next, result);
2966145SN/A
2977054SN/A    return result;
2986145SN/A}
299