Topology.cc revision 9356
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"
336154SN/A#include "mem/protocol/MachineType.hh"
347054SN/A#include "mem/ruby/common/NetDest.hh"
358257SBrad.Beckmann@amd.com#include "mem/ruby/network/BasicLink.hh"
367054SN/A#include "mem/ruby/network/Network.hh"
378255SBrad.Beckmann@amd.com#include "mem/ruby/network/Topology.hh"
387054SN/A#include "mem/ruby/slicc_interface/AbstractController.hh"
396145SN/A
407055SN/Ausing namespace std;
417055SN/A
427054SN/Aconst int INFINITE_LATENCY = 10000; // Yes, this is a big hack
438257SBrad.Beckmann@amd.com
446145SN/A// Note: In this file, we use the first 2*m_nodes SwitchIDs to
456145SN/A// represent the input and output endpoint links.  These really are
466145SN/A// not 'switches', as they will not have a Switch object allocated for
476145SN/A// them. The first m_nodes SwitchIDs are the links into the network,
486145SN/A// the second m_nodes set of SwitchIDs represent the the output queues
496145SN/A// of the network.
506145SN/A
516145SN/A// Helper functions based on chapter 29 of Cormen et al.
527054SN/Avoid extend_shortest_path(Matrix& current_dist, Matrix& latencies,
537054SN/A    Matrix& inter_switches);
547054SN/AMatrix shortest_path(const Matrix& weights, Matrix& latencies,
557054SN/A    Matrix& inter_switches);
567054SN/Abool link_is_shortest_path_to_node(SwitchID src, SwitchID next,
577054SN/A    SwitchID final, const Matrix& weights, const Matrix& dist);
587054SN/ANetDest shortest_path_to_node(SwitchID src, SwitchID next,
597054SN/A    const Matrix& weights, const Matrix& dist);
606145SN/A
616876SN/ATopology::Topology(const Params *p)
626876SN/A    : SimObject(p)
636145SN/A{
646876SN/A    m_print_config = p->print_config;
658257SBrad.Beckmann@amd.com    m_number_of_switches = p->routers.size();
668257SBrad.Beckmann@amd.com
676881SN/A    // initialize component latencies record
687454SN/A    m_component_latencies.resize(0);
697454SN/A    m_component_inter_switches.resize(0);
706145SN/A
716881SN/A    // Total nodes/controllers in network
726881SN/A    // Must make sure this is called after the State Machine constructors
736879SN/A    m_nodes = MachineType_base_number(MachineType_NUM);
746881SN/A    assert(m_nodes > 1);
756285SN/A
767054SN/A    if (m_nodes != params()->ext_links.size() &&
777054SN/A        m_nodes != params()->ext_links.size()) {
786879SN/A        fatal("m_nodes (%d) != ext_links vector length (%d)\n",
799109SBrad.Beckmann@amd.com              m_nodes, params()->ext_links.size());
806879SN/A    }
816879SN/A
828257SBrad.Beckmann@amd.com    // analyze both the internal and external links, create data structures
838257SBrad.Beckmann@amd.com    // Note that the python created links are bi-directional, but that the
848257SBrad.Beckmann@amd.com    // topology and networks utilize uni-directional links.  Thus each
858257SBrad.Beckmann@amd.com    // BasicLink is converted to two calls to add link, on for each direction
868257SBrad.Beckmann@amd.com    for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin();
877054SN/A         i != params()->ext_links.end(); ++i) {
888257SBrad.Beckmann@amd.com        BasicExtLink *ext_link = (*i);
898257SBrad.Beckmann@amd.com        AbstractController *abs_cntrl = ext_link->params()->ext_node;
908257SBrad.Beckmann@amd.com        BasicRouter *router = ext_link->params()->int_node;
916881SN/A
928257SBrad.Beckmann@amd.com        // Store the controller and ExtLink pointers for later
938257SBrad.Beckmann@amd.com        m_controller_vector.push_back(abs_cntrl);
948257SBrad.Beckmann@amd.com        m_ext_link_vector.push_back(ext_link);
956881SN/A
968257SBrad.Beckmann@amd.com        int ext_idx1 = abs_cntrl->params()->cntrl_id;
977054SN/A        int ext_idx2 = ext_idx1 + m_nodes;
988257SBrad.Beckmann@amd.com        int int_idx = router->params()->router_id + 2*m_nodes;
996145SN/A
1008257SBrad.Beckmann@amd.com        // create the internal uni-directional links in both directions
1018257SBrad.Beckmann@amd.com        //   the first direction is marked: In
1028257SBrad.Beckmann@amd.com        addLink(ext_idx1, int_idx, ext_link, LinkDirection_In);
1038257SBrad.Beckmann@amd.com        //   the first direction is marked: Out
1048257SBrad.Beckmann@amd.com        addLink(int_idx, ext_idx2, ext_link, LinkDirection_Out);
1057054SN/A    }
1066145SN/A
1078257SBrad.Beckmann@amd.com    for (vector<BasicIntLink*>::const_iterator i = params()->int_links.begin();
1087054SN/A         i != params()->int_links.end(); ++i) {
1098257SBrad.Beckmann@amd.com        BasicIntLink *int_link = (*i);
1108257SBrad.Beckmann@amd.com        BasicRouter *router_a = int_link->params()->node_a;
1118257SBrad.Beckmann@amd.com        BasicRouter *router_b = int_link->params()->node_b;
1126881SN/A
1138257SBrad.Beckmann@amd.com        // Store the IntLink pointers for later
1148257SBrad.Beckmann@amd.com        m_int_link_vector.push_back(int_link);
1158257SBrad.Beckmann@amd.com
1168257SBrad.Beckmann@amd.com        int a = router_a->params()->router_id + 2*m_nodes;
1178257SBrad.Beckmann@amd.com        int b = router_b->params()->router_id + 2*m_nodes;
1188257SBrad.Beckmann@amd.com
1198257SBrad.Beckmann@amd.com        // create the internal uni-directional links in both directions
1208257SBrad.Beckmann@amd.com        //   the first direction is marked: In
1218257SBrad.Beckmann@amd.com        addLink(a, b, int_link, LinkDirection_In);
1228257SBrad.Beckmann@amd.com        //   the second direction is marked: Out
1238257SBrad.Beckmann@amd.com        addLink(b, a, int_link, LinkDirection_Out);
1247054SN/A    }
1256145SN/A}
1266145SN/A
1278257SBrad.Beckmann@amd.comvoid
1288257SBrad.Beckmann@amd.comTopology::init()
1298257SBrad.Beckmann@amd.com{
1308257SBrad.Beckmann@amd.com}
1318257SBrad.Beckmann@amd.com
1326285SN/A
1337054SN/Avoid
1347054SN/ATopology::initNetworkPtr(Network* net_ptr)
1356881SN/A{
1368257SBrad.Beckmann@amd.com    for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin();
1378257SBrad.Beckmann@amd.com         i != params()->ext_links.end(); ++i) {
1388257SBrad.Beckmann@amd.com        BasicExtLink *ext_link = (*i);
1398257SBrad.Beckmann@amd.com        AbstractController *abs_cntrl = ext_link->params()->ext_node;
1408257SBrad.Beckmann@amd.com        abs_cntrl->initNetworkPtr(net_ptr);
1416881SN/A    }
1426881SN/A}
1436881SN/A
1447054SN/Avoid
1457054SN/ATopology::createLinks(Network *net, bool isReconfiguration)
1467054SN/A{
1477054SN/A    // Find maximum switchID
1487054SN/A    SwitchID max_switch_id = 0;
1498257SBrad.Beckmann@amd.com    for (LinkMap::const_iterator i = m_link_map.begin();
1508257SBrad.Beckmann@amd.com         i != m_link_map.end(); ++i) {
1518257SBrad.Beckmann@amd.com        std::pair<int, int> src_dest = (*i).first;
1528257SBrad.Beckmann@amd.com        max_switch_id = max(max_switch_id, src_dest.first);
1538257SBrad.Beckmann@amd.com        max_switch_id = max(max_switch_id, src_dest.second);
1547054SN/A    }
1556881SN/A
1568257SBrad.Beckmann@amd.com    // Initialize weight, latency, and inter switched vectors
1577054SN/A    Matrix topology_weights;
1587054SN/A    int num_switches = max_switch_id+1;
1597454SN/A    topology_weights.resize(num_switches);
1607454SN/A    m_component_latencies.resize(num_switches);
1617454SN/A    m_component_inter_switches.resize(num_switches);
1627054SN/A
1637054SN/A    for (int i = 0; i < topology_weights.size(); i++) {
1647454SN/A        topology_weights[i].resize(num_switches);
1657454SN/A        m_component_latencies[i].resize(num_switches);
1667454SN/A        m_component_inter_switches[i].resize(num_switches);
1677054SN/A
1687054SN/A        for (int j = 0; j < topology_weights[i].size(); j++) {
1697054SN/A            topology_weights[i][j] = INFINITE_LATENCY;
1707054SN/A
1717054SN/A            // initialize to invalid values
1727054SN/A            m_component_latencies[i][j] = -1;
1737054SN/A
1747054SN/A            // initially assume direct connections / no intermediate
1757054SN/A            // switches between components
1767054SN/A            m_component_inter_switches[i][j] = 0;
1777054SN/A        }
1786145SN/A    }
1796145SN/A
1807054SN/A    // Set identity weights to zero
1817054SN/A    for (int i = 0; i < topology_weights.size(); i++) {
1827054SN/A        topology_weights[i][i] = 0;
1837054SN/A    }
1846145SN/A
1857054SN/A    // Fill in the topology weights and bandwidth multipliers
1868257SBrad.Beckmann@amd.com    for (LinkMap::const_iterator i = m_link_map.begin();
1878257SBrad.Beckmann@amd.com         i != m_link_map.end(); ++i) {
1888257SBrad.Beckmann@amd.com        std::pair<int, int> src_dest = (*i).first;
1898257SBrad.Beckmann@amd.com        BasicLink* link = (*i).second.link;
1908257SBrad.Beckmann@amd.com        int src = src_dest.first;
1918257SBrad.Beckmann@amd.com        int dst = src_dest.second;
1928257SBrad.Beckmann@amd.com        m_component_latencies[src][dst] = link->m_latency;
1938257SBrad.Beckmann@amd.com        topology_weights[src][dst] = link->m_weight;
1947054SN/A    }
1958257SBrad.Beckmann@amd.com
1967054SN/A    // Walk topology and hookup the links
1977054SN/A    Matrix dist = shortest_path(topology_weights, m_component_latencies,
1987054SN/A        m_component_inter_switches);
1997054SN/A    for (int i = 0; i < topology_weights.size(); i++) {
2007054SN/A        for (int j = 0; j < topology_weights[i].size(); j++) {
2017054SN/A            int weight = topology_weights[i][j];
2027054SN/A            if (weight > 0 && weight != INFINITE_LATENCY) {
2037054SN/A                NetDest destination_set = shortest_path_to_node(i, j,
2048257SBrad.Beckmann@amd.com                                                     topology_weights, dist);
2058257SBrad.Beckmann@amd.com                makeLink(net, i, j, destination_set, isReconfiguration);
2067054SN/A            }
2077054SN/A        }
2086895SN/A    }
2096895SN/A}
2106895SN/A
2117054SN/Avoid
2128257SBrad.Beckmann@amd.comTopology::addLink(SwitchID src, SwitchID dest, BasicLink* link,
2138257SBrad.Beckmann@amd.com                  LinkDirection dir)
2147054SN/A{
2157832SN/A    assert(src <= m_number_of_switches+m_nodes+m_nodes);
2167832SN/A    assert(dest <= m_number_of_switches+m_nodes+m_nodes);
2178257SBrad.Beckmann@amd.com
2188257SBrad.Beckmann@amd.com    std::pair<int, int> src_dest_pair;
2198257SBrad.Beckmann@amd.com    LinkEntry link_entry;
2208257SBrad.Beckmann@amd.com
2218257SBrad.Beckmann@amd.com    src_dest_pair.first = src;
2228257SBrad.Beckmann@amd.com    src_dest_pair.second = dest;
2238257SBrad.Beckmann@amd.com    link_entry.direction = dir;
2248257SBrad.Beckmann@amd.com    link_entry.link = link;
2258257SBrad.Beckmann@amd.com    m_link_map[src_dest_pair] = link_entry;
2267054SN/A}
2277054SN/A
2287054SN/Avoid
2297054SN/ATopology::makeLink(Network *net, SwitchID src, SwitchID dest,
2308257SBrad.Beckmann@amd.com                   const NetDest& routing_table_entry, bool isReconfiguration)
2317054SN/A{
2327054SN/A    // Make sure we're not trying to connect two end-point nodes
2337054SN/A    // directly together
2347054SN/A    assert(src >= 2 * m_nodes || dest >= 2 * m_nodes);
2357054SN/A
2368257SBrad.Beckmann@amd.com    std::pair<int, int> src_dest;
2378257SBrad.Beckmann@amd.com    LinkEntry link_entry;
2388257SBrad.Beckmann@amd.com
2397054SN/A    if (src < m_nodes) {
2408257SBrad.Beckmann@amd.com        src_dest.first = src;
2418257SBrad.Beckmann@amd.com        src_dest.second = dest;
2428257SBrad.Beckmann@amd.com        link_entry = m_link_map[src_dest];
2438257SBrad.Beckmann@amd.com        net->makeInLink(src, dest - (2 * m_nodes), link_entry.link,
2448257SBrad.Beckmann@amd.com                        link_entry.direction,
2458257SBrad.Beckmann@amd.com                        routing_table_entry,
2468257SBrad.Beckmann@amd.com                        isReconfiguration);
2477054SN/A    } else if (dest < 2*m_nodes) {
2487054SN/A        assert(dest >= m_nodes);
2498257SBrad.Beckmann@amd.com        NodeID node = dest - m_nodes;
2508257SBrad.Beckmann@amd.com        src_dest.first = src;
2518257SBrad.Beckmann@amd.com        src_dest.second = dest;
2528257SBrad.Beckmann@amd.com        link_entry = m_link_map[src_dest];
2538257SBrad.Beckmann@amd.com        net->makeOutLink(src - (2 * m_nodes), node, link_entry.link,
2548257SBrad.Beckmann@amd.com                         link_entry.direction,
2558257SBrad.Beckmann@amd.com                         routing_table_entry,
2568257SBrad.Beckmann@amd.com                         isReconfiguration);
2577054SN/A    } else {
2588257SBrad.Beckmann@amd.com        assert((src >= 2 * m_nodes) && (dest >= 2 * m_nodes));
2598257SBrad.Beckmann@amd.com        src_dest.first = src;
2608257SBrad.Beckmann@amd.com        src_dest.second = dest;
2618257SBrad.Beckmann@amd.com        link_entry = m_link_map[src_dest];
2628257SBrad.Beckmann@amd.com        net->makeInternalLink(src - (2 * m_nodes), dest - (2 * m_nodes),
2638257SBrad.Beckmann@amd.com                              link_entry.link, link_entry.direction,
2648257SBrad.Beckmann@amd.com                              routing_table_entry, isReconfiguration);
2657054SN/A    }
2667054SN/A}
2677054SN/A
2687054SN/Avoid
2697054SN/ATopology::printStats(std::ostream& out) const
2707054SN/A{
2717054SN/A    for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
2727054SN/A        m_controller_vector[cntrl]->printStats(out);
2737054SN/A    }
2747054SN/A}
2757054SN/A
2767054SN/Avoid
2777054SN/ATopology::clearStats()
2786895SN/A{
2796895SN/A    for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
2806895SN/A        m_controller_vector[cntrl]->clearStats();
2816895SN/A    }
2826895SN/A}
2836895SN/A
2846145SN/A// The following all-pairs shortest path algorithm is based on the
2856145SN/A// discussion from Cormen et al., Chapter 26.1.
2867054SN/Avoid
2877054SN/Aextend_shortest_path(Matrix& current_dist, Matrix& latencies,
2887054SN/A    Matrix& inter_switches)
2897054SN/A{
2907054SN/A    bool change = true;
2917054SN/A    int nodes = current_dist.size();
2926145SN/A
2937054SN/A    while (change) {
2947054SN/A        change = false;
2957054SN/A        for (int i = 0; i < nodes; i++) {
2967054SN/A            for (int j = 0; j < nodes; j++) {
2977054SN/A                int minimum = current_dist[i][j];
2987054SN/A                int previous_minimum = minimum;
2997054SN/A                int intermediate_switch = -1;
3007054SN/A                for (int k = 0; k < nodes; k++) {
3017054SN/A                    minimum = min(minimum,
3027054SN/A                        current_dist[i][k] + current_dist[k][j]);
3037054SN/A                    if (previous_minimum != minimum) {
3047054SN/A                        intermediate_switch = k;
3057054SN/A                        inter_switches[i][j] =
3067054SN/A                            inter_switches[i][k] +
3077054SN/A                            inter_switches[k][j] + 1;
3087054SN/A                    }
3097054SN/A                    previous_minimum = minimum;
3107054SN/A                }
3117054SN/A                if (current_dist[i][j] != minimum) {
3127054SN/A                    change = true;
3137054SN/A                    current_dist[i][j] = minimum;
3147054SN/A                    assert(intermediate_switch >= 0);
3157054SN/A                    assert(intermediate_switch < latencies[i].size());
3167054SN/A                    latencies[i][j] = latencies[i][intermediate_switch] +
3177054SN/A                        latencies[intermediate_switch][j];
3187054SN/A                }
3197054SN/A            }
3206145SN/A        }
3216145SN/A    }
3226145SN/A}
3236145SN/A
3247054SN/AMatrix
3257054SN/Ashortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches)
3266145SN/A{
3277054SN/A    Matrix dist = weights;
3287054SN/A    extend_shortest_path(dist, latencies, inter_switches);
3297054SN/A    return dist;
3306145SN/A}
3316145SN/A
3327054SN/Abool
3337054SN/Alink_is_shortest_path_to_node(SwitchID src, SwitchID next, SwitchID final,
3347054SN/A    const Matrix& weights, const Matrix& dist)
3356145SN/A{
3367054SN/A    return weights[src][next] + dist[next][final] == dist[src][final];
3376145SN/A}
3386145SN/A
3397054SN/ANetDest
3407054SN/Ashortest_path_to_node(SwitchID src, SwitchID next, const Matrix& weights,
3417054SN/A    const Matrix& dist)
3426145SN/A{
3437054SN/A    NetDest result;
3447054SN/A    int d = 0;
3457054SN/A    int machines;
3467054SN/A    int max_machines;
3476145SN/A
3487054SN/A    machines = MachineType_NUM;
3497054SN/A    max_machines = MachineType_base_number(MachineType_NUM);
3506145SN/A
3517054SN/A    for (int m = 0; m < machines; m++) {
3527054SN/A        for (int i = 0; i < MachineType_base_count((MachineType)m); i++) {
3537054SN/A            // we use "d+max_machines" below since the "destination"
3547054SN/A            // switches for the machines are numbered
3557054SN/A            // [MachineType_base_number(MachineType_NUM)...
3567054SN/A            //  2*MachineType_base_number(MachineType_NUM)-1] for the
3577054SN/A            // component network
3587054SN/A            if (link_is_shortest_path_to_node(src, next, d + max_machines,
3597054SN/A                    weights, dist)) {
3607054SN/A                MachineID mach = {(MachineType)m, i};
3617054SN/A                result.add(mach);
3627054SN/A            }
3637054SN/A            d++;
3647054SN/A        }
3656145SN/A    }
3666145SN/A
3677780SN/A    DPRINTF(RubyNetwork, "Returning shortest path\n"
3687780SN/A            "(src-(2*max_machines)): %d, (next-(2*max_machines)): %d, "
3697780SN/A            "src: %d, next: %d, result: %s\n",
3707780SN/A            (src-(2*max_machines)), (next-(2*max_machines)),
3717780SN/A            src, next, result);
3726145SN/A
3737054SN/A    return result;
3746145SN/A}
3756145SN/A
3766876SN/ATopology *
3776876SN/ATopologyParams::create()
3786876SN/A{
3796876SN/A    return new Topology(this);
3806876SN/A}
3816879SN/A
382