Topology.cc revision 8257
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" 368257SBrad.Beckmann@amd.com#include "mem/ruby/network/BasicLink.hh" 378257SBrad.Beckmann@amd.com#include "mem/ruby/network/BasicRouter.hh" 387054SN/A#include "mem/ruby/network/Network.hh" 398255SBrad.Beckmann@amd.com#include "mem/ruby/network/Topology.hh" 407054SN/A#include "mem/ruby/slicc_interface/AbstractController.hh" 416285SN/A#include "mem/ruby/system/System.hh" 426145SN/A 437055SN/Ausing namespace std; 447055SN/A 457054SN/Aconst int INFINITE_LATENCY = 10000; // Yes, this is a big hack 468257SBrad.Beckmann@amd.com 478257SBrad.Beckmann@amd.comclass BasicRouter; 486145SN/A 496145SN/A// Note: In this file, we use the first 2*m_nodes SwitchIDs to 506145SN/A// represent the input and output endpoint links. These really are 516145SN/A// not 'switches', as they will not have a Switch object allocated for 526145SN/A// them. The first m_nodes SwitchIDs are the links into the network, 536145SN/A// the second m_nodes set of SwitchIDs represent the the output queues 546145SN/A// of the network. 556145SN/A 566145SN/A// Helper functions based on chapter 29 of Cormen et al. 577054SN/Avoid extend_shortest_path(Matrix& current_dist, Matrix& latencies, 587054SN/A Matrix& inter_switches); 597054SN/AMatrix shortest_path(const Matrix& weights, Matrix& latencies, 607054SN/A Matrix& inter_switches); 617054SN/Abool link_is_shortest_path_to_node(SwitchID src, SwitchID next, 627054SN/A SwitchID final, const Matrix& weights, const Matrix& dist); 637054SN/ANetDest shortest_path_to_node(SwitchID src, SwitchID next, 647054SN/A const Matrix& weights, const Matrix& dist); 656145SN/A 666876SN/ATopology::Topology(const Params *p) 676876SN/A : SimObject(p) 686145SN/A{ 696876SN/A m_print_config = p->print_config; 708257SBrad.Beckmann@amd.com m_number_of_switches = p->routers.size(); 718257SBrad.Beckmann@amd.com 726881SN/A // initialize component latencies record 737454SN/A m_component_latencies.resize(0); 747454SN/A m_component_inter_switches.resize(0); 756145SN/A 766881SN/A // Total nodes/controllers in network 776881SN/A // Must make sure this is called after the State Machine constructors 786879SN/A m_nodes = MachineType_base_number(MachineType_NUM); 796881SN/A assert(m_nodes > 1); 806285SN/A 817054SN/A if (m_nodes != params()->ext_links.size() && 827054SN/A m_nodes != params()->ext_links.size()) { 836879SN/A fatal("m_nodes (%d) != ext_links vector length (%d)\n", 848257SBrad.Beckmann@amd.com m_nodes != params()->ext_links.size()); 856879SN/A } 866879SN/A 878257SBrad.Beckmann@amd.com // analyze both the internal and external links, create data structures 888257SBrad.Beckmann@amd.com // Note that the python created links are bi-directional, but that the 898257SBrad.Beckmann@amd.com // topology and networks utilize uni-directional links. Thus each 908257SBrad.Beckmann@amd.com // BasicLink is converted to two calls to add link, on for each direction 918257SBrad.Beckmann@amd.com for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin(); 927054SN/A i != params()->ext_links.end(); ++i) { 938257SBrad.Beckmann@amd.com BasicExtLink *ext_link = (*i); 948257SBrad.Beckmann@amd.com AbstractController *abs_cntrl = ext_link->params()->ext_node; 958257SBrad.Beckmann@amd.com BasicRouter *router = ext_link->params()->int_node; 966881SN/A 978257SBrad.Beckmann@amd.com // Store the controller and ExtLink pointers for later 988257SBrad.Beckmann@amd.com m_controller_vector.push_back(abs_cntrl); 998257SBrad.Beckmann@amd.com m_ext_link_vector.push_back(ext_link); 1006881SN/A 1018257SBrad.Beckmann@amd.com int ext_idx1 = abs_cntrl->params()->cntrl_id; 1027054SN/A int ext_idx2 = ext_idx1 + m_nodes; 1038257SBrad.Beckmann@amd.com int int_idx = router->params()->router_id + 2*m_nodes; 1046145SN/A 1058257SBrad.Beckmann@amd.com // create the internal uni-directional links in both directions 1068257SBrad.Beckmann@amd.com // the first direction is marked: In 1078257SBrad.Beckmann@amd.com addLink(ext_idx1, int_idx, ext_link, LinkDirection_In); 1088257SBrad.Beckmann@amd.com // the first direction is marked: Out 1098257SBrad.Beckmann@amd.com addLink(int_idx, ext_idx2, ext_link, LinkDirection_Out); 1107054SN/A } 1116145SN/A 1128257SBrad.Beckmann@amd.com for (vector<BasicIntLink*>::const_iterator i = params()->int_links.begin(); 1137054SN/A i != params()->int_links.end(); ++i) { 1148257SBrad.Beckmann@amd.com BasicIntLink *int_link = (*i); 1158257SBrad.Beckmann@amd.com BasicRouter *router_a = int_link->params()->node_a; 1168257SBrad.Beckmann@amd.com BasicRouter *router_b = int_link->params()->node_b; 1176881SN/A 1188257SBrad.Beckmann@amd.com // Store the IntLink pointers for later 1198257SBrad.Beckmann@amd.com m_int_link_vector.push_back(int_link); 1208257SBrad.Beckmann@amd.com 1218257SBrad.Beckmann@amd.com int a = router_a->params()->router_id + 2*m_nodes; 1228257SBrad.Beckmann@amd.com int b = router_b->params()->router_id + 2*m_nodes; 1238257SBrad.Beckmann@amd.com 1248257SBrad.Beckmann@amd.com // create the internal uni-directional links in both directions 1258257SBrad.Beckmann@amd.com // the first direction is marked: In 1268257SBrad.Beckmann@amd.com addLink(a, b, int_link, LinkDirection_In); 1278257SBrad.Beckmann@amd.com // the second direction is marked: Out 1288257SBrad.Beckmann@amd.com addLink(b, a, int_link, LinkDirection_Out); 1297054SN/A } 1306145SN/A} 1316145SN/A 1328257SBrad.Beckmann@amd.comvoid 1338257SBrad.Beckmann@amd.comTopology::init() 1348257SBrad.Beckmann@amd.com{ 1358257SBrad.Beckmann@amd.com} 1368257SBrad.Beckmann@amd.com 1376285SN/A 1387054SN/Avoid 1397054SN/ATopology::initNetworkPtr(Network* net_ptr) 1406881SN/A{ 1418257SBrad.Beckmann@amd.com for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin(); 1428257SBrad.Beckmann@amd.com i != params()->ext_links.end(); ++i) { 1438257SBrad.Beckmann@amd.com BasicExtLink *ext_link = (*i); 1448257SBrad.Beckmann@amd.com AbstractController *abs_cntrl = ext_link->params()->ext_node; 1458257SBrad.Beckmann@amd.com abs_cntrl->initNetworkPtr(net_ptr); 1466881SN/A } 1476881SN/A} 1486881SN/A 1497054SN/Avoid 1507054SN/ATopology::createLinks(Network *net, bool isReconfiguration) 1517054SN/A{ 1527054SN/A // Find maximum switchID 1537054SN/A SwitchID max_switch_id = 0; 1548257SBrad.Beckmann@amd.com for (LinkMap::const_iterator i = m_link_map.begin(); 1558257SBrad.Beckmann@amd.com i != m_link_map.end(); ++i) { 1568257SBrad.Beckmann@amd.com std::pair<int, int> src_dest = (*i).first; 1578257SBrad.Beckmann@amd.com max_switch_id = max(max_switch_id, src_dest.first); 1588257SBrad.Beckmann@amd.com max_switch_id = max(max_switch_id, src_dest.second); 1597054SN/A } 1606881SN/A 1618257SBrad.Beckmann@amd.com // Initialize weight, latency, and inter switched vectors 1627054SN/A Matrix topology_weights; 1637054SN/A int num_switches = max_switch_id+1; 1647454SN/A topology_weights.resize(num_switches); 1657454SN/A m_component_latencies.resize(num_switches); 1667454SN/A m_component_inter_switches.resize(num_switches); 1677054SN/A 1687054SN/A for (int i = 0; i < topology_weights.size(); i++) { 1697454SN/A topology_weights[i].resize(num_switches); 1707454SN/A m_component_latencies[i].resize(num_switches); 1717454SN/A m_component_inter_switches[i].resize(num_switches); 1727054SN/A 1737054SN/A for (int j = 0; j < topology_weights[i].size(); j++) { 1747054SN/A topology_weights[i][j] = INFINITE_LATENCY; 1757054SN/A 1767054SN/A // initialize to invalid values 1777054SN/A m_component_latencies[i][j] = -1; 1787054SN/A 1797054SN/A // initially assume direct connections / no intermediate 1807054SN/A // switches between components 1817054SN/A m_component_inter_switches[i][j] = 0; 1827054SN/A } 1836145SN/A } 1846145SN/A 1857054SN/A // Set identity weights to zero 1867054SN/A for (int i = 0; i < topology_weights.size(); i++) { 1877054SN/A topology_weights[i][i] = 0; 1887054SN/A } 1896145SN/A 1907054SN/A // Fill in the topology weights and bandwidth multipliers 1918257SBrad.Beckmann@amd.com for (LinkMap::const_iterator i = m_link_map.begin(); 1928257SBrad.Beckmann@amd.com i != m_link_map.end(); ++i) { 1938257SBrad.Beckmann@amd.com std::pair<int, int> src_dest = (*i).first; 1948257SBrad.Beckmann@amd.com BasicLink* link = (*i).second.link; 1958257SBrad.Beckmann@amd.com int src = src_dest.first; 1968257SBrad.Beckmann@amd.com int dst = src_dest.second; 1978257SBrad.Beckmann@amd.com m_component_latencies[src][dst] = link->m_latency; 1988257SBrad.Beckmann@amd.com topology_weights[src][dst] = link->m_weight; 1997054SN/A } 2008257SBrad.Beckmann@amd.com 2017054SN/A // Walk topology and hookup the links 2027054SN/A Matrix dist = shortest_path(topology_weights, m_component_latencies, 2037054SN/A m_component_inter_switches); 2047054SN/A for (int i = 0; i < topology_weights.size(); i++) { 2057054SN/A for (int j = 0; j < topology_weights[i].size(); j++) { 2067054SN/A int weight = topology_weights[i][j]; 2077054SN/A if (weight > 0 && weight != INFINITE_LATENCY) { 2087054SN/A NetDest destination_set = shortest_path_to_node(i, j, 2098257SBrad.Beckmann@amd.com topology_weights, dist); 2108257SBrad.Beckmann@amd.com makeLink(net, i, j, destination_set, isReconfiguration); 2117054SN/A } 2127054SN/A } 2136895SN/A } 2146895SN/A} 2156895SN/A 2167054SN/Avoid 2178257SBrad.Beckmann@amd.comTopology::addLink(SwitchID src, SwitchID dest, BasicLink* link, 2188257SBrad.Beckmann@amd.com LinkDirection dir) 2197054SN/A{ 2207832SN/A assert(src <= m_number_of_switches+m_nodes+m_nodes); 2217832SN/A assert(dest <= m_number_of_switches+m_nodes+m_nodes); 2228257SBrad.Beckmann@amd.com 2238257SBrad.Beckmann@amd.com std::pair<int, int> src_dest_pair; 2248257SBrad.Beckmann@amd.com LinkEntry link_entry; 2258257SBrad.Beckmann@amd.com 2268257SBrad.Beckmann@amd.com src_dest_pair.first = src; 2278257SBrad.Beckmann@amd.com src_dest_pair.second = dest; 2288257SBrad.Beckmann@amd.com link_entry.direction = dir; 2298257SBrad.Beckmann@amd.com link_entry.link = link; 2308257SBrad.Beckmann@amd.com m_link_map[src_dest_pair] = link_entry; 2317054SN/A} 2327054SN/A 2337054SN/Avoid 2347054SN/ATopology::makeLink(Network *net, SwitchID src, SwitchID dest, 2358257SBrad.Beckmann@amd.com const NetDest& routing_table_entry, bool isReconfiguration) 2367054SN/A{ 2377054SN/A // Make sure we're not trying to connect two end-point nodes 2387054SN/A // directly together 2397054SN/A assert(src >= 2 * m_nodes || dest >= 2 * m_nodes); 2407054SN/A 2418257SBrad.Beckmann@amd.com std::pair<int, int> src_dest; 2428257SBrad.Beckmann@amd.com LinkEntry link_entry; 2438257SBrad.Beckmann@amd.com 2447054SN/A if (src < m_nodes) { 2458257SBrad.Beckmann@amd.com src_dest.first = src; 2468257SBrad.Beckmann@amd.com src_dest.second = dest; 2478257SBrad.Beckmann@amd.com link_entry = m_link_map[src_dest]; 2488257SBrad.Beckmann@amd.com net->makeInLink(src, dest - (2 * m_nodes), link_entry.link, 2498257SBrad.Beckmann@amd.com link_entry.direction, 2508257SBrad.Beckmann@amd.com routing_table_entry, 2518257SBrad.Beckmann@amd.com isReconfiguration); 2527054SN/A } else if (dest < 2*m_nodes) { 2537054SN/A assert(dest >= m_nodes); 2548257SBrad.Beckmann@amd.com NodeID node = dest - m_nodes; 2558257SBrad.Beckmann@amd.com src_dest.first = src; 2568257SBrad.Beckmann@amd.com src_dest.second = dest; 2578257SBrad.Beckmann@amd.com link_entry = m_link_map[src_dest]; 2588257SBrad.Beckmann@amd.com net->makeOutLink(src - (2 * m_nodes), node, link_entry.link, 2598257SBrad.Beckmann@amd.com link_entry.direction, 2608257SBrad.Beckmann@amd.com routing_table_entry, 2618257SBrad.Beckmann@amd.com isReconfiguration); 2627054SN/A } else { 2638257SBrad.Beckmann@amd.com assert((src >= 2 * m_nodes) && (dest >= 2 * m_nodes)); 2648257SBrad.Beckmann@amd.com src_dest.first = src; 2658257SBrad.Beckmann@amd.com src_dest.second = dest; 2668257SBrad.Beckmann@amd.com link_entry = m_link_map[src_dest]; 2678257SBrad.Beckmann@amd.com net->makeInternalLink(src - (2 * m_nodes), dest - (2 * m_nodes), 2688257SBrad.Beckmann@amd.com link_entry.link, link_entry.direction, 2698257SBrad.Beckmann@amd.com routing_table_entry, isReconfiguration); 2707054SN/A } 2717054SN/A} 2727054SN/A 2737054SN/Avoid 2747054SN/ATopology::printStats(std::ostream& out) const 2757054SN/A{ 2767054SN/A for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) { 2777054SN/A m_controller_vector[cntrl]->printStats(out); 2787054SN/A } 2797054SN/A} 2807054SN/A 2817054SN/Avoid 2827054SN/ATopology::clearStats() 2836895SN/A{ 2846895SN/A for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) { 2856895SN/A m_controller_vector[cntrl]->clearStats(); 2866895SN/A } 2876895SN/A} 2886895SN/A 2897054SN/Avoid 2907054SN/ATopology::printConfig(std::ostream& out) const 2916145SN/A{ 2927054SN/A if (m_print_config == false) 2937054SN/A return; 2946285SN/A 2957054SN/A assert(m_component_latencies.size() > 0); 2966145SN/A 2977054SN/A out << "--- Begin Topology Print ---" << endl 2987054SN/A << endl 2997054SN/A << "Topology print ONLY indicates the _NETWORK_ latency between two " 3007054SN/A << "machines" << endl 3017054SN/A << "It does NOT include the latency within the machines" << endl 3027054SN/A << endl; 3037054SN/A 3047054SN/A for (int m = 0; m < MachineType_NUM; m++) { 3057054SN/A int i_end = MachineType_base_count((MachineType)m); 3067054SN/A for (int i = 0; i < i_end; i++) { 3077054SN/A MachineID cur_mach = {(MachineType)m, i}; 3087054SN/A out << cur_mach << " Network Latencies" << endl; 3097054SN/A for (int n = 0; n < MachineType_NUM; n++) { 3107054SN/A int j_end = MachineType_base_count((MachineType)n); 3117054SN/A for (int j = 0; j < j_end; j++) { 3127054SN/A MachineID dest_mach = {(MachineType)n, j}; 3137054SN/A if (cur_mach == dest_mach) 3147054SN/A continue; 3157054SN/A 3167054SN/A int src = MachineType_base_number((MachineType)m) + i; 3177054SN/A int dst = MachineType_base_number(MachineType_NUM) + 3187054SN/A MachineType_base_number((MachineType)n) + j; 3197054SN/A int link_latency = m_component_latencies[src][dst]; 3207054SN/A int intermediate_switches = 3217054SN/A m_component_inter_switches[src][dst]; 3227054SN/A 3237054SN/A // NOTE switches are assumed to have single 3247054SN/A // cycle latency 3257054SN/A out << " " << cur_mach << " -> " << dest_mach 3267054SN/A << " net_lat: " 3277054SN/A << link_latency + intermediate_switches << endl; 3287054SN/A } 3297054SN/A } 3307054SN/A out << endl; 3316145SN/A } 3326145SN/A } 3336145SN/A 3347054SN/A out << "--- End Topology Print ---" << endl; 3356145SN/A} 3366145SN/A 3376145SN/A// The following all-pairs shortest path algorithm is based on the 3386145SN/A// discussion from Cormen et al., Chapter 26.1. 3397054SN/Avoid 3407054SN/Aextend_shortest_path(Matrix& current_dist, Matrix& latencies, 3417054SN/A Matrix& inter_switches) 3427054SN/A{ 3437054SN/A bool change = true; 3447054SN/A int nodes = current_dist.size(); 3456145SN/A 3467054SN/A while (change) { 3477054SN/A change = false; 3487054SN/A for (int i = 0; i < nodes; i++) { 3497054SN/A for (int j = 0; j < nodes; j++) { 3507054SN/A int minimum = current_dist[i][j]; 3517054SN/A int previous_minimum = minimum; 3527054SN/A int intermediate_switch = -1; 3537054SN/A for (int k = 0; k < nodes; k++) { 3547054SN/A minimum = min(minimum, 3557054SN/A current_dist[i][k] + current_dist[k][j]); 3567054SN/A if (previous_minimum != minimum) { 3577054SN/A intermediate_switch = k; 3587054SN/A inter_switches[i][j] = 3597054SN/A inter_switches[i][k] + 3607054SN/A inter_switches[k][j] + 1; 3617054SN/A } 3627054SN/A previous_minimum = minimum; 3637054SN/A } 3647054SN/A if (current_dist[i][j] != minimum) { 3657054SN/A change = true; 3667054SN/A current_dist[i][j] = minimum; 3677054SN/A assert(intermediate_switch >= 0); 3687054SN/A assert(intermediate_switch < latencies[i].size()); 3697054SN/A latencies[i][j] = latencies[i][intermediate_switch] + 3707054SN/A latencies[intermediate_switch][j]; 3717054SN/A } 3727054SN/A } 3736145SN/A } 3746145SN/A } 3756145SN/A} 3766145SN/A 3777054SN/AMatrix 3787054SN/Ashortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches) 3796145SN/A{ 3807054SN/A Matrix dist = weights; 3817054SN/A extend_shortest_path(dist, latencies, inter_switches); 3827054SN/A return dist; 3836145SN/A} 3846145SN/A 3857054SN/Abool 3867054SN/Alink_is_shortest_path_to_node(SwitchID src, SwitchID next, SwitchID final, 3877054SN/A const Matrix& weights, const Matrix& dist) 3886145SN/A{ 3897054SN/A return weights[src][next] + dist[next][final] == dist[src][final]; 3906145SN/A} 3916145SN/A 3927054SN/ANetDest 3937054SN/Ashortest_path_to_node(SwitchID src, SwitchID next, const Matrix& weights, 3947054SN/A const Matrix& dist) 3956145SN/A{ 3967054SN/A NetDest result; 3977054SN/A int d = 0; 3987054SN/A int machines; 3997054SN/A int max_machines; 4006145SN/A 4017054SN/A machines = MachineType_NUM; 4027054SN/A max_machines = MachineType_base_number(MachineType_NUM); 4036145SN/A 4047054SN/A for (int m = 0; m < machines; m++) { 4057054SN/A for (int i = 0; i < MachineType_base_count((MachineType)m); i++) { 4067054SN/A // we use "d+max_machines" below since the "destination" 4077054SN/A // switches for the machines are numbered 4087054SN/A // [MachineType_base_number(MachineType_NUM)... 4097054SN/A // 2*MachineType_base_number(MachineType_NUM)-1] for the 4107054SN/A // component network 4117054SN/A if (link_is_shortest_path_to_node(src, next, d + max_machines, 4127054SN/A weights, dist)) { 4137054SN/A MachineID mach = {(MachineType)m, i}; 4147054SN/A result.add(mach); 4157054SN/A } 4167054SN/A d++; 4177054SN/A } 4186145SN/A } 4196145SN/A 4207780SN/A DPRINTF(RubyNetwork, "Returning shortest path\n" 4217780SN/A "(src-(2*max_machines)): %d, (next-(2*max_machines)): %d, " 4227780SN/A "src: %d, next: %d, result: %s\n", 4237780SN/A (src-(2*max_machines)), (next-(2*max_machines)), 4247780SN/A src, next, result); 4256145SN/A 4267054SN/A return result; 4276145SN/A} 4286145SN/A 4296876SN/ATopology * 4306876SN/ATopologyParams::create() 4316876SN/A{ 4326876SN/A return new Topology(this); 4336876SN/A} 4346879SN/A 435