Topology.cc revision 9109
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" 337054SN/A#include "mem/protocol/TopologyType.hh" 347054SN/A#include "mem/ruby/common/NetDest.hh" 358257SBrad.Beckmann@amd.com#include "mem/ruby/network/BasicLink.hh" 368257SBrad.Beckmann@amd.com#include "mem/ruby/network/BasicRouter.hh" 377054SN/A#include "mem/ruby/network/Network.hh" 388255SBrad.Beckmann@amd.com#include "mem/ruby/network/Topology.hh" 397054SN/A#include "mem/ruby/slicc_interface/AbstractController.hh" 406145SN/A 417055SN/Ausing namespace std; 427055SN/A 437054SN/Aconst int INFINITE_LATENCY = 10000; // Yes, this is a big hack 448257SBrad.Beckmann@amd.com 458257SBrad.Beckmann@amd.comclass BasicRouter; 466145SN/A 476145SN/A// Note: In this file, we use the first 2*m_nodes SwitchIDs to 486145SN/A// represent the input and output endpoint links. These really are 496145SN/A// not 'switches', as they will not have a Switch object allocated for 506145SN/A// them. The first m_nodes SwitchIDs are the links into the network, 516145SN/A// the second m_nodes set of SwitchIDs represent the the output queues 526145SN/A// of the network. 536145SN/A 546145SN/A// Helper functions based on chapter 29 of Cormen et al. 557054SN/Avoid extend_shortest_path(Matrix& current_dist, Matrix& latencies, 567054SN/A Matrix& inter_switches); 577054SN/AMatrix shortest_path(const Matrix& weights, Matrix& latencies, 587054SN/A Matrix& inter_switches); 597054SN/Abool link_is_shortest_path_to_node(SwitchID src, SwitchID next, 607054SN/A SwitchID final, const Matrix& weights, const Matrix& dist); 617054SN/ANetDest shortest_path_to_node(SwitchID src, SwitchID next, 627054SN/A const Matrix& weights, const Matrix& dist); 636145SN/A 646876SN/ATopology::Topology(const Params *p) 656876SN/A : SimObject(p) 666145SN/A{ 676876SN/A m_print_config = p->print_config; 688257SBrad.Beckmann@amd.com m_number_of_switches = p->routers.size(); 698257SBrad.Beckmann@amd.com 706881SN/A // initialize component latencies record 717454SN/A m_component_latencies.resize(0); 727454SN/A m_component_inter_switches.resize(0); 736145SN/A 746881SN/A // Total nodes/controllers in network 756881SN/A // Must make sure this is called after the State Machine constructors 766879SN/A m_nodes = MachineType_base_number(MachineType_NUM); 776881SN/A assert(m_nodes > 1); 786285SN/A 797054SN/A if (m_nodes != params()->ext_links.size() && 807054SN/A m_nodes != params()->ext_links.size()) { 816879SN/A fatal("m_nodes (%d) != ext_links vector length (%d)\n", 829109SBrad.Beckmann@amd.com m_nodes, params()->ext_links.size()); 836879SN/A } 846879SN/A 858257SBrad.Beckmann@amd.com // analyze both the internal and external links, create data structures 868257SBrad.Beckmann@amd.com // Note that the python created links are bi-directional, but that the 878257SBrad.Beckmann@amd.com // topology and networks utilize uni-directional links. Thus each 888257SBrad.Beckmann@amd.com // BasicLink is converted to two calls to add link, on for each direction 898257SBrad.Beckmann@amd.com for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin(); 907054SN/A i != params()->ext_links.end(); ++i) { 918257SBrad.Beckmann@amd.com BasicExtLink *ext_link = (*i); 928257SBrad.Beckmann@amd.com AbstractController *abs_cntrl = ext_link->params()->ext_node; 938257SBrad.Beckmann@amd.com BasicRouter *router = ext_link->params()->int_node; 946881SN/A 958257SBrad.Beckmann@amd.com // Store the controller and ExtLink pointers for later 968257SBrad.Beckmann@amd.com m_controller_vector.push_back(abs_cntrl); 978257SBrad.Beckmann@amd.com m_ext_link_vector.push_back(ext_link); 986881SN/A 998257SBrad.Beckmann@amd.com int ext_idx1 = abs_cntrl->params()->cntrl_id; 1007054SN/A int ext_idx2 = ext_idx1 + m_nodes; 1018257SBrad.Beckmann@amd.com int int_idx = router->params()->router_id + 2*m_nodes; 1026145SN/A 1038257SBrad.Beckmann@amd.com // create the internal uni-directional links in both directions 1048257SBrad.Beckmann@amd.com // the first direction is marked: In 1058257SBrad.Beckmann@amd.com addLink(ext_idx1, int_idx, ext_link, LinkDirection_In); 1068257SBrad.Beckmann@amd.com // the first direction is marked: Out 1078257SBrad.Beckmann@amd.com addLink(int_idx, ext_idx2, ext_link, LinkDirection_Out); 1087054SN/A } 1096145SN/A 1108257SBrad.Beckmann@amd.com for (vector<BasicIntLink*>::const_iterator i = params()->int_links.begin(); 1117054SN/A i != params()->int_links.end(); ++i) { 1128257SBrad.Beckmann@amd.com BasicIntLink *int_link = (*i); 1138257SBrad.Beckmann@amd.com BasicRouter *router_a = int_link->params()->node_a; 1148257SBrad.Beckmann@amd.com BasicRouter *router_b = int_link->params()->node_b; 1156881SN/A 1168257SBrad.Beckmann@amd.com // Store the IntLink pointers for later 1178257SBrad.Beckmann@amd.com m_int_link_vector.push_back(int_link); 1188257SBrad.Beckmann@amd.com 1198257SBrad.Beckmann@amd.com int a = router_a->params()->router_id + 2*m_nodes; 1208257SBrad.Beckmann@amd.com int b = router_b->params()->router_id + 2*m_nodes; 1218257SBrad.Beckmann@amd.com 1228257SBrad.Beckmann@amd.com // create the internal uni-directional links in both directions 1238257SBrad.Beckmann@amd.com // the first direction is marked: In 1248257SBrad.Beckmann@amd.com addLink(a, b, int_link, LinkDirection_In); 1258257SBrad.Beckmann@amd.com // the second direction is marked: Out 1268257SBrad.Beckmann@amd.com addLink(b, a, int_link, LinkDirection_Out); 1277054SN/A } 1286145SN/A} 1296145SN/A 1308257SBrad.Beckmann@amd.comvoid 1318257SBrad.Beckmann@amd.comTopology::init() 1328257SBrad.Beckmann@amd.com{ 1338257SBrad.Beckmann@amd.com} 1348257SBrad.Beckmann@amd.com 1356285SN/A 1367054SN/Avoid 1377054SN/ATopology::initNetworkPtr(Network* net_ptr) 1386881SN/A{ 1398257SBrad.Beckmann@amd.com for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin(); 1408257SBrad.Beckmann@amd.com i != params()->ext_links.end(); ++i) { 1418257SBrad.Beckmann@amd.com BasicExtLink *ext_link = (*i); 1428257SBrad.Beckmann@amd.com AbstractController *abs_cntrl = ext_link->params()->ext_node; 1438257SBrad.Beckmann@amd.com abs_cntrl->initNetworkPtr(net_ptr); 1446881SN/A } 1456881SN/A} 1466881SN/A 1477054SN/Avoid 1487054SN/ATopology::createLinks(Network *net, bool isReconfiguration) 1497054SN/A{ 1507054SN/A // Find maximum switchID 1517054SN/A SwitchID max_switch_id = 0; 1528257SBrad.Beckmann@amd.com for (LinkMap::const_iterator i = m_link_map.begin(); 1538257SBrad.Beckmann@amd.com i != m_link_map.end(); ++i) { 1548257SBrad.Beckmann@amd.com std::pair<int, int> src_dest = (*i).first; 1558257SBrad.Beckmann@amd.com max_switch_id = max(max_switch_id, src_dest.first); 1568257SBrad.Beckmann@amd.com max_switch_id = max(max_switch_id, src_dest.second); 1577054SN/A } 1586881SN/A 1598257SBrad.Beckmann@amd.com // Initialize weight, latency, and inter switched vectors 1607054SN/A Matrix topology_weights; 1617054SN/A int num_switches = max_switch_id+1; 1627454SN/A topology_weights.resize(num_switches); 1637454SN/A m_component_latencies.resize(num_switches); 1647454SN/A m_component_inter_switches.resize(num_switches); 1657054SN/A 1667054SN/A for (int i = 0; i < topology_weights.size(); i++) { 1677454SN/A topology_weights[i].resize(num_switches); 1687454SN/A m_component_latencies[i].resize(num_switches); 1697454SN/A m_component_inter_switches[i].resize(num_switches); 1707054SN/A 1717054SN/A for (int j = 0; j < topology_weights[i].size(); j++) { 1727054SN/A topology_weights[i][j] = INFINITE_LATENCY; 1737054SN/A 1747054SN/A // initialize to invalid values 1757054SN/A m_component_latencies[i][j] = -1; 1767054SN/A 1777054SN/A // initially assume direct connections / no intermediate 1787054SN/A // switches between components 1797054SN/A m_component_inter_switches[i][j] = 0; 1807054SN/A } 1816145SN/A } 1826145SN/A 1837054SN/A // Set identity weights to zero 1847054SN/A for (int i = 0; i < topology_weights.size(); i++) { 1857054SN/A topology_weights[i][i] = 0; 1867054SN/A } 1876145SN/A 1887054SN/A // Fill in the topology weights and bandwidth multipliers 1898257SBrad.Beckmann@amd.com for (LinkMap::const_iterator i = m_link_map.begin(); 1908257SBrad.Beckmann@amd.com i != m_link_map.end(); ++i) { 1918257SBrad.Beckmann@amd.com std::pair<int, int> src_dest = (*i).first; 1928257SBrad.Beckmann@amd.com BasicLink* link = (*i).second.link; 1938257SBrad.Beckmann@amd.com int src = src_dest.first; 1948257SBrad.Beckmann@amd.com int dst = src_dest.second; 1958257SBrad.Beckmann@amd.com m_component_latencies[src][dst] = link->m_latency; 1968257SBrad.Beckmann@amd.com topology_weights[src][dst] = link->m_weight; 1977054SN/A } 1988257SBrad.Beckmann@amd.com 1997054SN/A // Walk topology and hookup the links 2007054SN/A Matrix dist = shortest_path(topology_weights, m_component_latencies, 2017054SN/A m_component_inter_switches); 2027054SN/A for (int i = 0; i < topology_weights.size(); i++) { 2037054SN/A for (int j = 0; j < topology_weights[i].size(); j++) { 2047054SN/A int weight = topology_weights[i][j]; 2057054SN/A if (weight > 0 && weight != INFINITE_LATENCY) { 2067054SN/A NetDest destination_set = shortest_path_to_node(i, j, 2078257SBrad.Beckmann@amd.com topology_weights, dist); 2088257SBrad.Beckmann@amd.com makeLink(net, i, j, destination_set, isReconfiguration); 2097054SN/A } 2107054SN/A } 2116895SN/A } 2126895SN/A} 2136895SN/A 2147054SN/Avoid 2158257SBrad.Beckmann@amd.comTopology::addLink(SwitchID src, SwitchID dest, BasicLink* link, 2168257SBrad.Beckmann@amd.com LinkDirection dir) 2177054SN/A{ 2187832SN/A assert(src <= m_number_of_switches+m_nodes+m_nodes); 2197832SN/A assert(dest <= m_number_of_switches+m_nodes+m_nodes); 2208257SBrad.Beckmann@amd.com 2218257SBrad.Beckmann@amd.com std::pair<int, int> src_dest_pair; 2228257SBrad.Beckmann@amd.com LinkEntry link_entry; 2238257SBrad.Beckmann@amd.com 2248257SBrad.Beckmann@amd.com src_dest_pair.first = src; 2258257SBrad.Beckmann@amd.com src_dest_pair.second = dest; 2268257SBrad.Beckmann@amd.com link_entry.direction = dir; 2278257SBrad.Beckmann@amd.com link_entry.link = link; 2288257SBrad.Beckmann@amd.com m_link_map[src_dest_pair] = link_entry; 2297054SN/A} 2307054SN/A 2317054SN/Avoid 2327054SN/ATopology::makeLink(Network *net, SwitchID src, SwitchID dest, 2338257SBrad.Beckmann@amd.com const NetDest& routing_table_entry, bool isReconfiguration) 2347054SN/A{ 2357054SN/A // Make sure we're not trying to connect two end-point nodes 2367054SN/A // directly together 2377054SN/A assert(src >= 2 * m_nodes || dest >= 2 * m_nodes); 2387054SN/A 2398257SBrad.Beckmann@amd.com std::pair<int, int> src_dest; 2408257SBrad.Beckmann@amd.com LinkEntry link_entry; 2418257SBrad.Beckmann@amd.com 2427054SN/A if (src < m_nodes) { 2438257SBrad.Beckmann@amd.com src_dest.first = src; 2448257SBrad.Beckmann@amd.com src_dest.second = dest; 2458257SBrad.Beckmann@amd.com link_entry = m_link_map[src_dest]; 2468257SBrad.Beckmann@amd.com net->makeInLink(src, dest - (2 * m_nodes), link_entry.link, 2478257SBrad.Beckmann@amd.com link_entry.direction, 2488257SBrad.Beckmann@amd.com routing_table_entry, 2498257SBrad.Beckmann@amd.com isReconfiguration); 2507054SN/A } else if (dest < 2*m_nodes) { 2517054SN/A assert(dest >= m_nodes); 2528257SBrad.Beckmann@amd.com NodeID node = dest - m_nodes; 2538257SBrad.Beckmann@amd.com src_dest.first = src; 2548257SBrad.Beckmann@amd.com src_dest.second = dest; 2558257SBrad.Beckmann@amd.com link_entry = m_link_map[src_dest]; 2568257SBrad.Beckmann@amd.com net->makeOutLink(src - (2 * m_nodes), node, link_entry.link, 2578257SBrad.Beckmann@amd.com link_entry.direction, 2588257SBrad.Beckmann@amd.com routing_table_entry, 2598257SBrad.Beckmann@amd.com isReconfiguration); 2607054SN/A } else { 2618257SBrad.Beckmann@amd.com assert((src >= 2 * m_nodes) && (dest >= 2 * m_nodes)); 2628257SBrad.Beckmann@amd.com src_dest.first = src; 2638257SBrad.Beckmann@amd.com src_dest.second = dest; 2648257SBrad.Beckmann@amd.com link_entry = m_link_map[src_dest]; 2658257SBrad.Beckmann@amd.com net->makeInternalLink(src - (2 * m_nodes), dest - (2 * m_nodes), 2668257SBrad.Beckmann@amd.com link_entry.link, link_entry.direction, 2678257SBrad.Beckmann@amd.com routing_table_entry, isReconfiguration); 2687054SN/A } 2697054SN/A} 2707054SN/A 2717054SN/Avoid 2727054SN/ATopology::printStats(std::ostream& out) const 2737054SN/A{ 2747054SN/A for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) { 2757054SN/A m_controller_vector[cntrl]->printStats(out); 2767054SN/A } 2777054SN/A} 2787054SN/A 2797054SN/Avoid 2807054SN/ATopology::clearStats() 2816895SN/A{ 2826895SN/A for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) { 2836895SN/A m_controller_vector[cntrl]->clearStats(); 2846895SN/A } 2856895SN/A} 2866895SN/A 2877054SN/Avoid 2887054SN/ATopology::printConfig(std::ostream& out) const 2896145SN/A{ 2907054SN/A if (m_print_config == false) 2917054SN/A return; 2926285SN/A 2937054SN/A assert(m_component_latencies.size() > 0); 2946145SN/A 2957054SN/A out << "--- Begin Topology Print ---" << endl 2967054SN/A << endl 2977054SN/A << "Topology print ONLY indicates the _NETWORK_ latency between two " 2987054SN/A << "machines" << endl 2997054SN/A << "It does NOT include the latency within the machines" << endl 3007054SN/A << endl; 3017054SN/A 3027054SN/A for (int m = 0; m < MachineType_NUM; m++) { 3037054SN/A int i_end = MachineType_base_count((MachineType)m); 3047054SN/A for (int i = 0; i < i_end; i++) { 3057054SN/A MachineID cur_mach = {(MachineType)m, i}; 3067054SN/A out << cur_mach << " Network Latencies" << endl; 3077054SN/A for (int n = 0; n < MachineType_NUM; n++) { 3087054SN/A int j_end = MachineType_base_count((MachineType)n); 3097054SN/A for (int j = 0; j < j_end; j++) { 3107054SN/A MachineID dest_mach = {(MachineType)n, j}; 3117054SN/A if (cur_mach == dest_mach) 3127054SN/A continue; 3137054SN/A 3147054SN/A int src = MachineType_base_number((MachineType)m) + i; 3157054SN/A int dst = MachineType_base_number(MachineType_NUM) + 3167054SN/A MachineType_base_number((MachineType)n) + j; 3177054SN/A int link_latency = m_component_latencies[src][dst]; 3187054SN/A int intermediate_switches = 3197054SN/A m_component_inter_switches[src][dst]; 3207054SN/A 3217054SN/A // NOTE switches are assumed to have single 3227054SN/A // cycle latency 3237054SN/A out << " " << cur_mach << " -> " << dest_mach 3247054SN/A << " net_lat: " 3257054SN/A << link_latency + intermediate_switches << endl; 3267054SN/A } 3277054SN/A } 3287054SN/A out << endl; 3296145SN/A } 3306145SN/A } 3316145SN/A 3327054SN/A out << "--- End Topology Print ---" << endl; 3336145SN/A} 3346145SN/A 3356145SN/A// The following all-pairs shortest path algorithm is based on the 3366145SN/A// discussion from Cormen et al., Chapter 26.1. 3377054SN/Avoid 3387054SN/Aextend_shortest_path(Matrix& current_dist, Matrix& latencies, 3397054SN/A Matrix& inter_switches) 3407054SN/A{ 3417054SN/A bool change = true; 3427054SN/A int nodes = current_dist.size(); 3436145SN/A 3447054SN/A while (change) { 3457054SN/A change = false; 3467054SN/A for (int i = 0; i < nodes; i++) { 3477054SN/A for (int j = 0; j < nodes; j++) { 3487054SN/A int minimum = current_dist[i][j]; 3497054SN/A int previous_minimum = minimum; 3507054SN/A int intermediate_switch = -1; 3517054SN/A for (int k = 0; k < nodes; k++) { 3527054SN/A minimum = min(minimum, 3537054SN/A current_dist[i][k] + current_dist[k][j]); 3547054SN/A if (previous_minimum != minimum) { 3557054SN/A intermediate_switch = k; 3567054SN/A inter_switches[i][j] = 3577054SN/A inter_switches[i][k] + 3587054SN/A inter_switches[k][j] + 1; 3597054SN/A } 3607054SN/A previous_minimum = minimum; 3617054SN/A } 3627054SN/A if (current_dist[i][j] != minimum) { 3637054SN/A change = true; 3647054SN/A current_dist[i][j] = minimum; 3657054SN/A assert(intermediate_switch >= 0); 3667054SN/A assert(intermediate_switch < latencies[i].size()); 3677054SN/A latencies[i][j] = latencies[i][intermediate_switch] + 3687054SN/A latencies[intermediate_switch][j]; 3697054SN/A } 3707054SN/A } 3716145SN/A } 3726145SN/A } 3736145SN/A} 3746145SN/A 3757054SN/AMatrix 3767054SN/Ashortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches) 3776145SN/A{ 3787054SN/A Matrix dist = weights; 3797054SN/A extend_shortest_path(dist, latencies, inter_switches); 3807054SN/A return dist; 3816145SN/A} 3826145SN/A 3837054SN/Abool 3847054SN/Alink_is_shortest_path_to_node(SwitchID src, SwitchID next, SwitchID final, 3857054SN/A const Matrix& weights, const Matrix& dist) 3866145SN/A{ 3877054SN/A return weights[src][next] + dist[next][final] == dist[src][final]; 3886145SN/A} 3896145SN/A 3907054SN/ANetDest 3917054SN/Ashortest_path_to_node(SwitchID src, SwitchID next, const Matrix& weights, 3927054SN/A const Matrix& dist) 3936145SN/A{ 3947054SN/A NetDest result; 3957054SN/A int d = 0; 3967054SN/A int machines; 3977054SN/A int max_machines; 3986145SN/A 3997054SN/A machines = MachineType_NUM; 4007054SN/A max_machines = MachineType_base_number(MachineType_NUM); 4016145SN/A 4027054SN/A for (int m = 0; m < machines; m++) { 4037054SN/A for (int i = 0; i < MachineType_base_count((MachineType)m); i++) { 4047054SN/A // we use "d+max_machines" below since the "destination" 4057054SN/A // switches for the machines are numbered 4067054SN/A // [MachineType_base_number(MachineType_NUM)... 4077054SN/A // 2*MachineType_base_number(MachineType_NUM)-1] for the 4087054SN/A // component network 4097054SN/A if (link_is_shortest_path_to_node(src, next, d + max_machines, 4107054SN/A weights, dist)) { 4117054SN/A MachineID mach = {(MachineType)m, i}; 4127054SN/A result.add(mach); 4137054SN/A } 4147054SN/A d++; 4157054SN/A } 4166145SN/A } 4176145SN/A 4187780SN/A DPRINTF(RubyNetwork, "Returning shortest path\n" 4197780SN/A "(src-(2*max_machines)): %d, (next-(2*max_machines)): %d, " 4207780SN/A "src: %d, next: %d, result: %s\n", 4217780SN/A (src-(2*max_machines)), (next-(2*max_machines)), 4227780SN/A src, next, result); 4236145SN/A 4247054SN/A return result; 4256145SN/A} 4266145SN/A 4276876SN/ATopology * 4286876SN/ATopologyParams::create() 4296876SN/A{ 4306876SN/A return new Topology(this); 4316876SN/A} 4326879SN/A 433