Topology.cc revision 9496
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 929496Snilay@cs.wisc.edu // Store the ExtLink pointers for later 938257SBrad.Beckmann@amd.com m_ext_link_vector.push_back(ext_link); 946881SN/A 958257SBrad.Beckmann@amd.com int ext_idx1 = abs_cntrl->params()->cntrl_id; 967054SN/A int ext_idx2 = ext_idx1 + m_nodes; 978257SBrad.Beckmann@amd.com int int_idx = router->params()->router_id + 2*m_nodes; 986145SN/A 998257SBrad.Beckmann@amd.com // create the internal uni-directional links in both directions 1008257SBrad.Beckmann@amd.com // the first direction is marked: In 1018257SBrad.Beckmann@amd.com addLink(ext_idx1, int_idx, ext_link, LinkDirection_In); 1028257SBrad.Beckmann@amd.com // the first direction is marked: Out 1038257SBrad.Beckmann@amd.com addLink(int_idx, ext_idx2, ext_link, LinkDirection_Out); 1047054SN/A } 1056145SN/A 1068257SBrad.Beckmann@amd.com for (vector<BasicIntLink*>::const_iterator i = params()->int_links.begin(); 1077054SN/A i != params()->int_links.end(); ++i) { 1088257SBrad.Beckmann@amd.com BasicIntLink *int_link = (*i); 1098257SBrad.Beckmann@amd.com BasicRouter *router_a = int_link->params()->node_a; 1108257SBrad.Beckmann@amd.com BasicRouter *router_b = int_link->params()->node_b; 1116881SN/A 1128257SBrad.Beckmann@amd.com // Store the IntLink pointers for later 1138257SBrad.Beckmann@amd.com m_int_link_vector.push_back(int_link); 1148257SBrad.Beckmann@amd.com 1158257SBrad.Beckmann@amd.com int a = router_a->params()->router_id + 2*m_nodes; 1168257SBrad.Beckmann@amd.com int b = router_b->params()->router_id + 2*m_nodes; 1178257SBrad.Beckmann@amd.com 1188257SBrad.Beckmann@amd.com // create the internal uni-directional links in both directions 1198257SBrad.Beckmann@amd.com // the first direction is marked: In 1208257SBrad.Beckmann@amd.com addLink(a, b, int_link, LinkDirection_In); 1218257SBrad.Beckmann@amd.com // the second direction is marked: Out 1228257SBrad.Beckmann@amd.com addLink(b, a, int_link, LinkDirection_Out); 1237054SN/A } 1246145SN/A} 1256145SN/A 1268257SBrad.Beckmann@amd.comvoid 1278257SBrad.Beckmann@amd.comTopology::init() 1288257SBrad.Beckmann@amd.com{ 1298257SBrad.Beckmann@amd.com} 1308257SBrad.Beckmann@amd.com 1316285SN/A 1327054SN/Avoid 1337054SN/ATopology::initNetworkPtr(Network* net_ptr) 1346881SN/A{ 1358257SBrad.Beckmann@amd.com for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin(); 1368257SBrad.Beckmann@amd.com i != params()->ext_links.end(); ++i) { 1378257SBrad.Beckmann@amd.com BasicExtLink *ext_link = (*i); 1388257SBrad.Beckmann@amd.com AbstractController *abs_cntrl = ext_link->params()->ext_node; 1398257SBrad.Beckmann@amd.com abs_cntrl->initNetworkPtr(net_ptr); 1406881SN/A } 1416881SN/A} 1426881SN/A 1437054SN/Avoid 1447054SN/ATopology::createLinks(Network *net, bool isReconfiguration) 1457054SN/A{ 1467054SN/A // Find maximum switchID 1477054SN/A SwitchID max_switch_id = 0; 1488257SBrad.Beckmann@amd.com for (LinkMap::const_iterator i = m_link_map.begin(); 1498257SBrad.Beckmann@amd.com i != m_link_map.end(); ++i) { 1508257SBrad.Beckmann@amd.com std::pair<int, int> src_dest = (*i).first; 1518257SBrad.Beckmann@amd.com max_switch_id = max(max_switch_id, src_dest.first); 1528257SBrad.Beckmann@amd.com max_switch_id = max(max_switch_id, src_dest.second); 1537054SN/A } 1546881SN/A 1558257SBrad.Beckmann@amd.com // Initialize weight, latency, and inter switched vectors 1567054SN/A Matrix topology_weights; 1577054SN/A int num_switches = max_switch_id+1; 1587454SN/A topology_weights.resize(num_switches); 1597454SN/A m_component_latencies.resize(num_switches); 1607454SN/A m_component_inter_switches.resize(num_switches); 1617054SN/A 1627054SN/A for (int i = 0; i < topology_weights.size(); i++) { 1637454SN/A topology_weights[i].resize(num_switches); 1647454SN/A m_component_latencies[i].resize(num_switches); 1657454SN/A m_component_inter_switches[i].resize(num_switches); 1667054SN/A 1677054SN/A for (int j = 0; j < topology_weights[i].size(); j++) { 1687054SN/A topology_weights[i][j] = INFINITE_LATENCY; 1697054SN/A 1707054SN/A // initialize to invalid values 1717054SN/A m_component_latencies[i][j] = -1; 1727054SN/A 1737054SN/A // initially assume direct connections / no intermediate 1747054SN/A // switches between components 1757054SN/A m_component_inter_switches[i][j] = 0; 1767054SN/A } 1776145SN/A } 1786145SN/A 1797054SN/A // Set identity weights to zero 1807054SN/A for (int i = 0; i < topology_weights.size(); i++) { 1817054SN/A topology_weights[i][i] = 0; 1827054SN/A } 1836145SN/A 1847054SN/A // Fill in the topology weights and bandwidth multipliers 1858257SBrad.Beckmann@amd.com for (LinkMap::const_iterator i = m_link_map.begin(); 1868257SBrad.Beckmann@amd.com i != m_link_map.end(); ++i) { 1878257SBrad.Beckmann@amd.com std::pair<int, int> src_dest = (*i).first; 1888257SBrad.Beckmann@amd.com BasicLink* link = (*i).second.link; 1898257SBrad.Beckmann@amd.com int src = src_dest.first; 1908257SBrad.Beckmann@amd.com int dst = src_dest.second; 1918257SBrad.Beckmann@amd.com m_component_latencies[src][dst] = link->m_latency; 1928257SBrad.Beckmann@amd.com topology_weights[src][dst] = link->m_weight; 1937054SN/A } 1948257SBrad.Beckmann@amd.com 1957054SN/A // Walk topology and hookup the links 1967054SN/A Matrix dist = shortest_path(topology_weights, m_component_latencies, 1977054SN/A m_component_inter_switches); 1987054SN/A for (int i = 0; i < topology_weights.size(); i++) { 1997054SN/A for (int j = 0; j < topology_weights[i].size(); j++) { 2007054SN/A int weight = topology_weights[i][j]; 2017054SN/A if (weight > 0 && weight != INFINITE_LATENCY) { 2027054SN/A NetDest destination_set = shortest_path_to_node(i, j, 2038257SBrad.Beckmann@amd.com topology_weights, dist); 2048257SBrad.Beckmann@amd.com makeLink(net, i, j, destination_set, isReconfiguration); 2057054SN/A } 2067054SN/A } 2076895SN/A } 2086895SN/A} 2096895SN/A 2107054SN/Avoid 2118257SBrad.Beckmann@amd.comTopology::addLink(SwitchID src, SwitchID dest, BasicLink* link, 2128257SBrad.Beckmann@amd.com LinkDirection dir) 2137054SN/A{ 2147832SN/A assert(src <= m_number_of_switches+m_nodes+m_nodes); 2157832SN/A assert(dest <= m_number_of_switches+m_nodes+m_nodes); 2168257SBrad.Beckmann@amd.com 2178257SBrad.Beckmann@amd.com std::pair<int, int> src_dest_pair; 2188257SBrad.Beckmann@amd.com LinkEntry link_entry; 2198257SBrad.Beckmann@amd.com 2208257SBrad.Beckmann@amd.com src_dest_pair.first = src; 2218257SBrad.Beckmann@amd.com src_dest_pair.second = dest; 2228257SBrad.Beckmann@amd.com link_entry.direction = dir; 2238257SBrad.Beckmann@amd.com link_entry.link = link; 2248257SBrad.Beckmann@amd.com m_link_map[src_dest_pair] = link_entry; 2257054SN/A} 2267054SN/A 2277054SN/Avoid 2287054SN/ATopology::makeLink(Network *net, SwitchID src, SwitchID dest, 2298257SBrad.Beckmann@amd.com const NetDest& routing_table_entry, bool isReconfiguration) 2307054SN/A{ 2317054SN/A // Make sure we're not trying to connect two end-point nodes 2327054SN/A // directly together 2337054SN/A assert(src >= 2 * m_nodes || dest >= 2 * m_nodes); 2347054SN/A 2358257SBrad.Beckmann@amd.com std::pair<int, int> src_dest; 2368257SBrad.Beckmann@amd.com LinkEntry link_entry; 2378257SBrad.Beckmann@amd.com 2387054SN/A if (src < m_nodes) { 2398257SBrad.Beckmann@amd.com src_dest.first = src; 2408257SBrad.Beckmann@amd.com src_dest.second = dest; 2418257SBrad.Beckmann@amd.com link_entry = m_link_map[src_dest]; 2428257SBrad.Beckmann@amd.com net->makeInLink(src, dest - (2 * m_nodes), link_entry.link, 2438257SBrad.Beckmann@amd.com link_entry.direction, 2448257SBrad.Beckmann@amd.com routing_table_entry, 2458257SBrad.Beckmann@amd.com isReconfiguration); 2467054SN/A } else if (dest < 2*m_nodes) { 2477054SN/A assert(dest >= m_nodes); 2488257SBrad.Beckmann@amd.com NodeID node = dest - m_nodes; 2498257SBrad.Beckmann@amd.com src_dest.first = src; 2508257SBrad.Beckmann@amd.com src_dest.second = dest; 2518257SBrad.Beckmann@amd.com link_entry = m_link_map[src_dest]; 2528257SBrad.Beckmann@amd.com net->makeOutLink(src - (2 * m_nodes), node, link_entry.link, 2538257SBrad.Beckmann@amd.com link_entry.direction, 2548257SBrad.Beckmann@amd.com routing_table_entry, 2558257SBrad.Beckmann@amd.com isReconfiguration); 2567054SN/A } else { 2578257SBrad.Beckmann@amd.com assert((src >= 2 * m_nodes) && (dest >= 2 * m_nodes)); 2588257SBrad.Beckmann@amd.com src_dest.first = src; 2598257SBrad.Beckmann@amd.com src_dest.second = dest; 2608257SBrad.Beckmann@amd.com link_entry = m_link_map[src_dest]; 2618257SBrad.Beckmann@amd.com net->makeInternalLink(src - (2 * m_nodes), dest - (2 * m_nodes), 2628257SBrad.Beckmann@amd.com link_entry.link, link_entry.direction, 2638257SBrad.Beckmann@amd.com routing_table_entry, isReconfiguration); 2647054SN/A } 2657054SN/A} 2667054SN/A 2676145SN/A// The following all-pairs shortest path algorithm is based on the 2686145SN/A// discussion from Cormen et al., Chapter 26.1. 2697054SN/Avoid 2707054SN/Aextend_shortest_path(Matrix& current_dist, Matrix& latencies, 2717054SN/A Matrix& inter_switches) 2727054SN/A{ 2737054SN/A bool change = true; 2747054SN/A int nodes = current_dist.size(); 2756145SN/A 2767054SN/A while (change) { 2777054SN/A change = false; 2787054SN/A for (int i = 0; i < nodes; i++) { 2797054SN/A for (int j = 0; j < nodes; j++) { 2807054SN/A int minimum = current_dist[i][j]; 2817054SN/A int previous_minimum = minimum; 2827054SN/A int intermediate_switch = -1; 2837054SN/A for (int k = 0; k < nodes; k++) { 2847054SN/A minimum = min(minimum, 2857054SN/A current_dist[i][k] + current_dist[k][j]); 2867054SN/A if (previous_minimum != minimum) { 2877054SN/A intermediate_switch = k; 2887054SN/A inter_switches[i][j] = 2897054SN/A inter_switches[i][k] + 2907054SN/A inter_switches[k][j] + 1; 2917054SN/A } 2927054SN/A previous_minimum = minimum; 2937054SN/A } 2947054SN/A if (current_dist[i][j] != minimum) { 2957054SN/A change = true; 2967054SN/A current_dist[i][j] = minimum; 2977054SN/A assert(intermediate_switch >= 0); 2987054SN/A assert(intermediate_switch < latencies[i].size()); 2997054SN/A latencies[i][j] = latencies[i][intermediate_switch] + 3007054SN/A latencies[intermediate_switch][j]; 3017054SN/A } 3027054SN/A } 3036145SN/A } 3046145SN/A } 3056145SN/A} 3066145SN/A 3077054SN/AMatrix 3087054SN/Ashortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches) 3096145SN/A{ 3107054SN/A Matrix dist = weights; 3117054SN/A extend_shortest_path(dist, latencies, inter_switches); 3127054SN/A return dist; 3136145SN/A} 3146145SN/A 3157054SN/Abool 3167054SN/Alink_is_shortest_path_to_node(SwitchID src, SwitchID next, SwitchID final, 3177054SN/A const Matrix& weights, const Matrix& dist) 3186145SN/A{ 3197054SN/A return weights[src][next] + dist[next][final] == dist[src][final]; 3206145SN/A} 3216145SN/A 3227054SN/ANetDest 3237054SN/Ashortest_path_to_node(SwitchID src, SwitchID next, const Matrix& weights, 3247054SN/A const Matrix& dist) 3256145SN/A{ 3267054SN/A NetDest result; 3277054SN/A int d = 0; 3287054SN/A int machines; 3297054SN/A int max_machines; 3306145SN/A 3317054SN/A machines = MachineType_NUM; 3327054SN/A max_machines = MachineType_base_number(MachineType_NUM); 3336145SN/A 3347054SN/A for (int m = 0; m < machines; m++) { 3357054SN/A for (int i = 0; i < MachineType_base_count((MachineType)m); i++) { 3367054SN/A // we use "d+max_machines" below since the "destination" 3377054SN/A // switches for the machines are numbered 3387054SN/A // [MachineType_base_number(MachineType_NUM)... 3397054SN/A // 2*MachineType_base_number(MachineType_NUM)-1] for the 3407054SN/A // component network 3417054SN/A if (link_is_shortest_path_to_node(src, next, d + max_machines, 3427054SN/A weights, dist)) { 3437054SN/A MachineID mach = {(MachineType)m, i}; 3447054SN/A result.add(mach); 3457054SN/A } 3467054SN/A d++; 3477054SN/A } 3486145SN/A } 3496145SN/A 3507780SN/A DPRINTF(RubyNetwork, "Returning shortest path\n" 3517780SN/A "(src-(2*max_machines)): %d, (next-(2*max_machines)): %d, " 3527780SN/A "src: %d, next: %d, result: %s\n", 3537780SN/A (src-(2*max_machines)), (next-(2*max_machines)), 3547780SN/A src, next, result); 3556145SN/A 3567054SN/A return result; 3576145SN/A} 3586145SN/A 3596876SN/ATopology * 3606876SN/ATopologyParams::create() 3616876SN/A{ 3626876SN/A return new Topology(this); 3636876SN/A} 3646879SN/A 365