Topology.cc revision 8255
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
318232SN/A#include "debug/RubyNetwork.hh"
326154SN/A#include "mem/protocol/MachineType.hh"
336154SN/A#include "mem/protocol/Protocol.hh"
347054SN/A#include "mem/protocol/TopologyType.hh"
357054SN/A#include "mem/ruby/common/NetDest.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"
396285SN/A#include "mem/ruby/system/System.hh"
406145SN/A
417055SN/Ausing namespace std;
427055SN/A
437054SN/Aconst int INFINITE_LATENCY = 10000; // Yes, this is a big hack
447054SN/Aconst int DEFAULT_BW_MULTIPLIER = 1;  // Just to be consistent with above :)
456145SN/A
466145SN/A// Note: In this file, we use the first 2*m_nodes SwitchIDs to
476145SN/A// represent the input and output endpoint links.  These really are
486145SN/A// not 'switches', as they will not have a Switch object allocated for
496145SN/A// them. The first m_nodes SwitchIDs are the links into the network,
506145SN/A// the second m_nodes set of SwitchIDs represent the the output queues
516145SN/A// of the network.
526145SN/A
536145SN/A// Helper functions based on chapter 29 of Cormen et al.
547054SN/Avoid extend_shortest_path(Matrix& current_dist, Matrix& latencies,
557054SN/A    Matrix& inter_switches);
567054SN/AMatrix shortest_path(const Matrix& weights, Matrix& latencies,
577054SN/A    Matrix& inter_switches);
587054SN/Abool link_is_shortest_path_to_node(SwitchID src, SwitchID next,
597054SN/A    SwitchID final, const Matrix& weights, const Matrix& dist);
607054SN/ANetDest shortest_path_to_node(SwitchID src, SwitchID next,
617054SN/A    const Matrix& weights, const Matrix& dist);
626145SN/A
636876SN/ATopology::Topology(const Params *p)
646876SN/A    : SimObject(p)
656145SN/A{
666876SN/A    m_print_config = p->print_config;
676879SN/A    m_number_of_switches = p->num_int_nodes;
686881SN/A    // initialize component latencies record
697454SN/A    m_component_latencies.resize(0);
707454SN/A    m_component_inter_switches.resize(0);
716145SN/A
726881SN/A    // Total nodes/controllers in network
736881SN/A    // Must make sure this is called after the State Machine constructors
746879SN/A    m_nodes = MachineType_base_number(MachineType_NUM);
756881SN/A    assert(m_nodes > 1);
766285SN/A
777054SN/A    if (m_nodes != params()->ext_links.size() &&
787054SN/A        m_nodes != params()->ext_links.size()) {
796879SN/A        fatal("m_nodes (%d) != ext_links vector length (%d)\n",
807054SN/A            m_nodes != params()->ext_links.size());
816879SN/A    }
826879SN/A
837054SN/A    // First create the links between the endpoints (i.e. controllers)
847054SN/A    // and the network.
857054SN/A    for (vector<ExtLink*>::const_iterator i = params()->ext_links.begin();
867054SN/A         i != params()->ext_links.end(); ++i) {
877054SN/A        const ExtLinkParams *p = (*i)->params();
887054SN/A        AbstractController *c = p->ext_node;
896881SN/A
907054SN/A        // Store the controller pointers for later
917454SN/A        m_controller_vector.push_back(c);
926881SN/A
937054SN/A        int ext_idx1 =
947054SN/A            MachineType_base_number(c->getMachineType()) + c->getVersion();
957054SN/A        int ext_idx2 = ext_idx1 + m_nodes;
967054SN/A        int int_idx = p->int_node + 2*m_nodes;
976145SN/A
987054SN/A        // create the links in both directions
997054SN/A        addLink(ext_idx1, int_idx, p->latency, p->bw_multiplier, p->weight);
1007054SN/A        addLink(int_idx, ext_idx2, p->latency, p->bw_multiplier, p->weight);
1017054SN/A    }
1026145SN/A
1037054SN/A    for (vector<IntLink*>::const_iterator i = params()->int_links.begin();
1047054SN/A         i != params()->int_links.end(); ++i) {
1057054SN/A        const IntLinkParams *p = (*i)->params();
1067054SN/A        int a = p->node_a + 2*m_nodes;
1077054SN/A        int b = p->node_b + 2*m_nodes;
1086881SN/A
1097054SN/A        // create the links in both directions
1107054SN/A        addLink(a, b, p->latency, p->bw_multiplier, p->weight);
1117054SN/A        addLink(b, a, p->latency, p->bw_multiplier, p->weight);
1127054SN/A    }
1136145SN/A}
1146145SN/A
1156285SN/A
1167054SN/Avoid
1177054SN/ATopology::initNetworkPtr(Network* net_ptr)
1186881SN/A{
1197054SN/A    for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
1206881SN/A        m_controller_vector[cntrl]->initNetworkPtr(net_ptr);
1216881SN/A    }
1226881SN/A}
1236881SN/A
1247054SN/Avoid
1257054SN/ATopology::createLinks(Network *net, bool isReconfiguration)
1267054SN/A{
1277054SN/A    // Find maximum switchID
1287054SN/A    SwitchID max_switch_id = 0;
1297054SN/A    for (int i = 0; i < m_links_src_vector.size(); i++) {
1307054SN/A        max_switch_id = max(max_switch_id, m_links_src_vector[i]);
1317054SN/A        max_switch_id = max(max_switch_id, m_links_dest_vector[i]);
1327054SN/A    }
1336881SN/A
1347054SN/A    // Initialize weight vector
1357054SN/A    Matrix topology_weights;
1367054SN/A    Matrix topology_latency;
1377054SN/A    Matrix topology_bw_multis;
1387054SN/A    int num_switches = max_switch_id+1;
1397454SN/A    topology_weights.resize(num_switches);
1407454SN/A    topology_latency.resize(num_switches);
1417454SN/A    topology_bw_multis.resize(num_switches);
1426145SN/A
1437054SN/A    // FIXME setting the size of a member variable here is a HACK!
1447454SN/A    m_component_latencies.resize(num_switches);
1456145SN/A
1467054SN/A    // FIXME setting the size of a member variable here is a HACK!
1477454SN/A    m_component_inter_switches.resize(num_switches);
1487054SN/A
1497054SN/A    for (int i = 0; i < topology_weights.size(); i++) {
1507454SN/A        topology_weights[i].resize(num_switches);
1517454SN/A        topology_latency[i].resize(num_switches);
1527454SN/A        topology_bw_multis[i].resize(num_switches);
1537454SN/A        m_component_latencies[i].resize(num_switches);
1547054SN/A
1557054SN/A        // FIXME setting the size of a member variable here is a HACK!
1567454SN/A        m_component_inter_switches[i].resize(num_switches);
1577054SN/A
1587054SN/A        for (int j = 0; j < topology_weights[i].size(); j++) {
1597054SN/A            topology_weights[i][j] = INFINITE_LATENCY;
1607054SN/A
1617054SN/A            // initialize to invalid values
1627054SN/A            topology_latency[i][j] = -1;
1637054SN/A            topology_bw_multis[i][j] = -1;
1647054SN/A            m_component_latencies[i][j] = -1;
1657054SN/A
1667054SN/A            // initially assume direct connections / no intermediate
1677054SN/A            // switches between components
1687054SN/A            m_component_inter_switches[i][j] = 0;
1697054SN/A        }
1706145SN/A    }
1716145SN/A
1727054SN/A    // Set identity weights to zero
1737054SN/A    for (int i = 0; i < topology_weights.size(); i++) {
1747054SN/A        topology_weights[i][i] = 0;
1757054SN/A    }
1766145SN/A
1777054SN/A    // Fill in the topology weights and bandwidth multipliers
1787054SN/A    for (int i = 0; i < m_links_src_vector.size(); i++) {
1797054SN/A        int src = m_links_src_vector[i];
1807054SN/A        int dst = m_links_dest_vector[i];
1817054SN/A        topology_weights[src][dst] = m_links_weight_vector[i];
1827054SN/A        topology_latency[src][dst] = m_links_latency_vector[i];
1837054SN/A        m_component_latencies[src][dst] = m_links_latency_vector[i];
1847054SN/A        topology_bw_multis[src][dst] = m_bw_multiplier_vector[i];
1857054SN/A    }
1866145SN/A
1877054SN/A    // Walk topology and hookup the links
1887054SN/A    Matrix dist = shortest_path(topology_weights, m_component_latencies,
1897054SN/A        m_component_inter_switches);
1907054SN/A    for (int i = 0; i < topology_weights.size(); i++) {
1917054SN/A        for (int j = 0; j < topology_weights[i].size(); j++) {
1927054SN/A            int weight = topology_weights[i][j];
1937054SN/A            int bw_multiplier = topology_bw_multis[i][j];
1947054SN/A            int latency = topology_latency[i][j];
1957054SN/A            if (weight > 0 && weight != INFINITE_LATENCY) {
1967054SN/A                NetDest destination_set = shortest_path_to_node(i, j,
1977054SN/A                    topology_weights, dist);
1987054SN/A                assert(latency != -1);
1997054SN/A                makeLink(net, i, j, destination_set, latency, weight,
2007054SN/A                    bw_multiplier, isReconfiguration);
2017054SN/A            }
2027054SN/A        }
2036895SN/A    }
2046895SN/A}
2056895SN/A
2067054SN/ASwitchID
2077054SN/ATopology::newSwitchID()
2087054SN/A{
2097054SN/A    m_number_of_switches++;
2107054SN/A    return m_number_of_switches-1+m_nodes+m_nodes;
2117054SN/A}
2127054SN/A
2137054SN/Avoid
2147054SN/ATopology::addLink(SwitchID src, SwitchID dest, int link_latency)
2157054SN/A{
2167054SN/A    addLink(src, dest, link_latency, DEFAULT_BW_MULTIPLIER, link_latency);
2177054SN/A}
2187054SN/A
2197054SN/Avoid
2207054SN/ATopology::addLink(SwitchID src, SwitchID dest, int link_latency,
2217054SN/A    int bw_multiplier)
2227054SN/A{
2237054SN/A    addLink(src, dest, link_latency, bw_multiplier, link_latency);
2247054SN/A}
2257054SN/A
2267054SN/Avoid
2277054SN/ATopology::addLink(SwitchID src, SwitchID dest, int link_latency,
2287054SN/A    int bw_multiplier, int link_weight)
2297054SN/A{
2307832SN/A    assert(src <= m_number_of_switches+m_nodes+m_nodes);
2317832SN/A    assert(dest <= m_number_of_switches+m_nodes+m_nodes);
2327454SN/A    m_links_src_vector.push_back(src);
2337454SN/A    m_links_dest_vector.push_back(dest);
2347454SN/A    m_links_latency_vector.push_back(link_latency);
2357454SN/A    m_links_weight_vector.push_back(link_weight);
2367454SN/A    m_bw_multiplier_vector.push_back(bw_multiplier);
2377054SN/A}
2387054SN/A
2397054SN/Avoid
2407054SN/ATopology::makeLink(Network *net, SwitchID src, SwitchID dest,
2417054SN/A    const NetDest& routing_table_entry, int link_latency, int link_weight,
2427054SN/A    int bw_multiplier, bool isReconfiguration)
2437054SN/A{
2447054SN/A    // Make sure we're not trying to connect two end-point nodes
2457054SN/A    // directly together
2467054SN/A    assert(src >= 2 * m_nodes || dest >= 2 * m_nodes);
2477054SN/A
2487054SN/A    if (src < m_nodes) {
2497054SN/A        net->makeInLink(src, dest-(2*m_nodes), routing_table_entry,
2507054SN/A            link_latency, bw_multiplier, isReconfiguration);
2517054SN/A    } else if (dest < 2*m_nodes) {
2527054SN/A        assert(dest >= m_nodes);
2537054SN/A        NodeID node = dest-m_nodes;
2547054SN/A        net->makeOutLink(src-(2*m_nodes), node, routing_table_entry,
2557054SN/A            link_latency, link_weight, bw_multiplier, isReconfiguration);
2567054SN/A    } else {
2577054SN/A        assert((src >= 2*m_nodes) && (dest >= 2*m_nodes));
2587054SN/A        net->makeInternalLink(src-(2*m_nodes), dest-(2*m_nodes),
2597054SN/A            routing_table_entry, link_latency, link_weight, bw_multiplier,
2607054SN/A            isReconfiguration);
2617054SN/A    }
2627054SN/A}
2637054SN/A
2647054SN/Avoid
2657054SN/ATopology::printStats(std::ostream& out) const
2667054SN/A{
2677054SN/A    for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
2687054SN/A        m_controller_vector[cntrl]->printStats(out);
2697054SN/A    }
2707054SN/A}
2717054SN/A
2727054SN/Avoid
2737054SN/ATopology::clearStats()
2746895SN/A{
2756895SN/A    for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
2766895SN/A        m_controller_vector[cntrl]->clearStats();
2776895SN/A    }
2786895SN/A}
2796895SN/A
2807054SN/Avoid
2817054SN/ATopology::printConfig(std::ostream& out) const
2826145SN/A{
2837054SN/A    if (m_print_config == false)
2847054SN/A        return;
2856285SN/A
2867054SN/A    assert(m_component_latencies.size() > 0);
2876145SN/A
2887054SN/A    out << "--- Begin Topology Print ---" << endl
2897054SN/A        << endl
2907054SN/A        << "Topology print ONLY indicates the _NETWORK_ latency between two "
2917054SN/A        << "machines" << endl
2927054SN/A        << "It does NOT include the latency within the machines" << endl
2937054SN/A        << endl;
2947054SN/A
2957054SN/A    for (int m = 0; m < MachineType_NUM; m++) {
2967054SN/A        int i_end = MachineType_base_count((MachineType)m);
2977054SN/A        for (int i = 0; i < i_end; i++) {
2987054SN/A            MachineID cur_mach = {(MachineType)m, i};
2997054SN/A            out << cur_mach << " Network Latencies" << endl;
3007054SN/A            for (int n = 0; n < MachineType_NUM; n++) {
3017054SN/A                int j_end = MachineType_base_count((MachineType)n);
3027054SN/A                for (int j = 0; j < j_end; j++) {
3037054SN/A                    MachineID dest_mach = {(MachineType)n, j};
3047054SN/A                    if (cur_mach == dest_mach)
3057054SN/A                        continue;
3067054SN/A
3077054SN/A                    int src = MachineType_base_number((MachineType)m) + i;
3087054SN/A                    int dst = MachineType_base_number(MachineType_NUM) +
3097054SN/A                        MachineType_base_number((MachineType)n) + j;
3107054SN/A                    int link_latency = m_component_latencies[src][dst];
3117054SN/A                    int intermediate_switches =
3127054SN/A                        m_component_inter_switches[src][dst];
3137054SN/A
3147054SN/A                    // NOTE switches are assumed to have single
3157054SN/A                    // cycle latency
3167054SN/A                    out << "  " << cur_mach << " -> " << dest_mach
3177054SN/A                        << " net_lat: "
3187054SN/A                        << link_latency + intermediate_switches << endl;
3197054SN/A                }
3207054SN/A            }
3217054SN/A            out << endl;
3226145SN/A        }
3236145SN/A    }
3246145SN/A
3257054SN/A    out << "--- End Topology Print ---" << endl;
3266145SN/A}
3276145SN/A
3286145SN/A// The following all-pairs shortest path algorithm is based on the
3296145SN/A// discussion from Cormen et al., Chapter 26.1.
3307054SN/Avoid
3317054SN/Aextend_shortest_path(Matrix& current_dist, Matrix& latencies,
3327054SN/A    Matrix& inter_switches)
3337054SN/A{
3347054SN/A    bool change = true;
3357054SN/A    int nodes = current_dist.size();
3366145SN/A
3377054SN/A    while (change) {
3387054SN/A        change = false;
3397054SN/A        for (int i = 0; i < nodes; i++) {
3407054SN/A            for (int j = 0; j < nodes; j++) {
3417054SN/A                int minimum = current_dist[i][j];
3427054SN/A                int previous_minimum = minimum;
3437054SN/A                int intermediate_switch = -1;
3447054SN/A                for (int k = 0; k < nodes; k++) {
3457054SN/A                    minimum = min(minimum,
3467054SN/A                        current_dist[i][k] + current_dist[k][j]);
3477054SN/A                    if (previous_minimum != minimum) {
3487054SN/A                        intermediate_switch = k;
3497054SN/A                        inter_switches[i][j] =
3507054SN/A                            inter_switches[i][k] +
3517054SN/A                            inter_switches[k][j] + 1;
3527054SN/A                    }
3537054SN/A                    previous_minimum = minimum;
3547054SN/A                }
3557054SN/A                if (current_dist[i][j] != minimum) {
3567054SN/A                    change = true;
3577054SN/A                    current_dist[i][j] = minimum;
3587054SN/A                    assert(intermediate_switch >= 0);
3597054SN/A                    assert(intermediate_switch < latencies[i].size());
3607054SN/A                    latencies[i][j] = latencies[i][intermediate_switch] +
3617054SN/A                        latencies[intermediate_switch][j];
3627054SN/A                }
3637054SN/A            }
3646145SN/A        }
3656145SN/A    }
3666145SN/A}
3676145SN/A
3687054SN/AMatrix
3697054SN/Ashortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches)
3706145SN/A{
3717054SN/A    Matrix dist = weights;
3727054SN/A    extend_shortest_path(dist, latencies, inter_switches);
3737054SN/A    return dist;
3746145SN/A}
3756145SN/A
3767054SN/Abool
3777054SN/Alink_is_shortest_path_to_node(SwitchID src, SwitchID next, SwitchID final,
3787054SN/A    const Matrix& weights, const Matrix& dist)
3796145SN/A{
3807054SN/A    return weights[src][next] + dist[next][final] == dist[src][final];
3816145SN/A}
3826145SN/A
3837054SN/ANetDest
3847054SN/Ashortest_path_to_node(SwitchID src, SwitchID next, const Matrix& weights,
3857054SN/A    const Matrix& dist)
3866145SN/A{
3877054SN/A    NetDest result;
3887054SN/A    int d = 0;
3897054SN/A    int machines;
3907054SN/A    int max_machines;
3916145SN/A
3927054SN/A    machines = MachineType_NUM;
3937054SN/A    max_machines = MachineType_base_number(MachineType_NUM);
3946145SN/A
3957054SN/A    for (int m = 0; m < machines; m++) {
3967054SN/A        for (int i = 0; i < MachineType_base_count((MachineType)m); i++) {
3977054SN/A            // we use "d+max_machines" below since the "destination"
3987054SN/A            // switches for the machines are numbered
3997054SN/A            // [MachineType_base_number(MachineType_NUM)...
4007054SN/A            //  2*MachineType_base_number(MachineType_NUM)-1] for the
4017054SN/A            // component network
4027054SN/A            if (link_is_shortest_path_to_node(src, next, d + max_machines,
4037054SN/A                    weights, dist)) {
4047054SN/A                MachineID mach = {(MachineType)m, i};
4057054SN/A                result.add(mach);
4067054SN/A            }
4077054SN/A            d++;
4087054SN/A        }
4096145SN/A    }
4106145SN/A
4117780SN/A    DPRINTF(RubyNetwork, "Returning shortest path\n"
4127780SN/A            "(src-(2*max_machines)): %d, (next-(2*max_machines)): %d, "
4137780SN/A            "src: %d, next: %d, result: %s\n",
4147780SN/A            (src-(2*max_machines)), (next-(2*max_machines)),
4157780SN/A            src, next, result);
4166145SN/A
4177054SN/A    return result;
4186145SN/A}
4196145SN/A
4206876SN/ATopology *
4216876SN/ATopologyParams::create()
4226876SN/A{
4236876SN/A    return new Topology(this);
4246876SN/A}
4256879SN/A
4266879SN/ALink *
4276879SN/ALinkParams::create()
4286879SN/A{
4296879SN/A    return new Link(this);
4306879SN/A}
4316879SN/A
4326879SN/AExtLink *
4336879SN/AExtLinkParams::create()
4346879SN/A{
4356879SN/A    return new ExtLink(this);
4366879SN/A}
4376879SN/A
4386879SN/AIntLink *
4396879SN/AIntLinkParams::create()
4406879SN/A{
4416879SN/A    return new IntLink(this);
4426879SN/A}
443