Topology.cc revision 11663
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" 337054SN/A#include "mem/ruby/common/NetDest.hh" 348257SBrad.Beckmann@amd.com#include "mem/ruby/network/BasicLink.hh" 358255SBrad.Beckmann@amd.com#include "mem/ruby/network/Topology.hh" 367054SN/A#include "mem/ruby/slicc_interface/AbstractController.hh" 376145SN/A 387055SN/Ausing namespace std; 397055SN/A 407054SN/Aconst int INFINITE_LATENCY = 10000; // Yes, this is a big hack 418257SBrad.Beckmann@amd.com 426145SN/A// Note: In this file, we use the first 2*m_nodes SwitchIDs to 436145SN/A// represent the input and output endpoint links. These really are 446145SN/A// not 'switches', as they will not have a Switch object allocated for 456145SN/A// them. The first m_nodes SwitchIDs are the links into the network, 466145SN/A// the second m_nodes set of SwitchIDs represent the the output queues 476145SN/A// of the network. 486145SN/A 4911096Snilay@cs.wisc.eduTopology::Topology(uint32_t num_routers, 5011096Snilay@cs.wisc.edu const vector<BasicExtLink *> &ext_links, 5111096Snilay@cs.wisc.edu const vector<BasicIntLink *> &int_links) 5211096Snilay@cs.wisc.edu : m_nodes(ext_links.size()), m_number_of_switches(num_routers), 5311096Snilay@cs.wisc.edu m_ext_link_vector(ext_links), m_int_link_vector(int_links) 546145SN/A{ 556881SN/A // Total nodes/controllers in network 566881SN/A assert(m_nodes > 1); 576285SN/A 5811663Stushar@ece.gatech.edu // analyze both the internal and external links, create data structures. 5911663Stushar@ece.gatech.edu // The python created external links are bi-directional, 6011663Stushar@ece.gatech.edu // and the python created internal links are uni-directional. 6111663Stushar@ece.gatech.edu // The networks and topology utilize uni-directional links. 6211663Stushar@ece.gatech.edu // Thus each external link is converted to two calls to addLink, 6311663Stushar@ece.gatech.edu // one for each direction. 6411663Stushar@ece.gatech.edu // 6511663Stushar@ece.gatech.edu // External Links 669594Snilay@cs.wisc.edu for (vector<BasicExtLink*>::const_iterator i = ext_links.begin(); 679594Snilay@cs.wisc.edu i != ext_links.end(); ++i) { 688257SBrad.Beckmann@amd.com BasicExtLink *ext_link = (*i); 698257SBrad.Beckmann@amd.com AbstractController *abs_cntrl = ext_link->params()->ext_node; 708257SBrad.Beckmann@amd.com BasicRouter *router = ext_link->params()->int_node; 716881SN/A 7210078Snilay@cs.wisc.edu int machine_base_idx = MachineType_base_number(abs_cntrl->getType()); 739869Sjthestness@gmail.com int ext_idx1 = machine_base_idx + abs_cntrl->getVersion(); 747054SN/A int ext_idx2 = ext_idx1 + m_nodes; 758257SBrad.Beckmann@amd.com int int_idx = router->params()->router_id + 2*m_nodes; 766145SN/A 778257SBrad.Beckmann@amd.com // create the internal uni-directional links in both directions 7811663Stushar@ece.gatech.edu // ext to int 7911663Stushar@ece.gatech.edu addLink(ext_idx1, int_idx, ext_link); 8011663Stushar@ece.gatech.edu // int to ext 8111663Stushar@ece.gatech.edu addLink(int_idx, ext_idx2, ext_link); 827054SN/A } 836145SN/A 8411663Stushar@ece.gatech.edu // Internal Links 859594Snilay@cs.wisc.edu for (vector<BasicIntLink*>::const_iterator i = int_links.begin(); 869594Snilay@cs.wisc.edu i != int_links.end(); ++i) { 878257SBrad.Beckmann@amd.com BasicIntLink *int_link = (*i); 8811663Stushar@ece.gatech.edu BasicRouter *router_src = int_link->params()->src_node; 8911663Stushar@ece.gatech.edu BasicRouter *router_dst = int_link->params()->dst_node; 906881SN/A 918257SBrad.Beckmann@amd.com // Store the IntLink pointers for later 928257SBrad.Beckmann@amd.com m_int_link_vector.push_back(int_link); 938257SBrad.Beckmann@amd.com 9411663Stushar@ece.gatech.edu int src = router_src->params()->router_id + 2*m_nodes; 9511663Stushar@ece.gatech.edu int dst = router_dst->params()->router_id + 2*m_nodes; 968257SBrad.Beckmann@amd.com 9711663Stushar@ece.gatech.edu // create the internal uni-directional link from src to dst 9811663Stushar@ece.gatech.edu addLink(src, dst, int_link); 997054SN/A } 1006145SN/A} 1016145SN/A 1028257SBrad.Beckmann@amd.comvoid 1039799Snilay@cs.wisc.eduTopology::createLinks(Network *net) 1047054SN/A{ 1057054SN/A // Find maximum switchID 1067054SN/A SwitchID max_switch_id = 0; 1078257SBrad.Beckmann@amd.com for (LinkMap::const_iterator i = m_link_map.begin(); 1088257SBrad.Beckmann@amd.com i != m_link_map.end(); ++i) { 10910005Snilay@cs.wisc.edu std::pair<SwitchID, SwitchID> src_dest = (*i).first; 1108257SBrad.Beckmann@amd.com max_switch_id = max(max_switch_id, src_dest.first); 11111320Ssteve.reinhardt@amd.com max_switch_id = max(max_switch_id, src_dest.second); 1127054SN/A } 1136881SN/A 1148257SBrad.Beckmann@amd.com // Initialize weight, latency, and inter switched vectors 1157054SN/A int num_switches = max_switch_id+1; 11611096Snilay@cs.wisc.edu Matrix topology_weights(num_switches, 11711096Snilay@cs.wisc.edu vector<int>(num_switches, INFINITE_LATENCY)); 11811096Snilay@cs.wisc.edu Matrix component_latencies(num_switches, 11911096Snilay@cs.wisc.edu vector<int>(num_switches, -1)); 12011096Snilay@cs.wisc.edu Matrix component_inter_switches(num_switches, 12111096Snilay@cs.wisc.edu vector<int>(num_switches, 0)); 1226145SN/A 1237054SN/A // Set identity weights to zero 1247054SN/A for (int i = 0; i < topology_weights.size(); i++) { 1257054SN/A topology_weights[i][i] = 0; 1267054SN/A } 1276145SN/A 1287054SN/A // Fill in the topology weights and bandwidth multipliers 1298257SBrad.Beckmann@amd.com for (LinkMap::const_iterator i = m_link_map.begin(); 1308257SBrad.Beckmann@amd.com i != m_link_map.end(); ++i) { 1318257SBrad.Beckmann@amd.com std::pair<int, int> src_dest = (*i).first; 1328257SBrad.Beckmann@amd.com BasicLink* link = (*i).second.link; 1338257SBrad.Beckmann@amd.com int src = src_dest.first; 1348257SBrad.Beckmann@amd.com int dst = src_dest.second; 13511096Snilay@cs.wisc.edu component_latencies[src][dst] = link->m_latency; 1368257SBrad.Beckmann@amd.com topology_weights[src][dst] = link->m_weight; 1377054SN/A } 13811320Ssteve.reinhardt@amd.com 1397054SN/A // Walk topology and hookup the links 14011096Snilay@cs.wisc.edu Matrix dist = shortest_path(topology_weights, component_latencies, 14111096Snilay@cs.wisc.edu component_inter_switches); 14211096Snilay@cs.wisc.edu 1437054SN/A for (int i = 0; i < topology_weights.size(); i++) { 1447054SN/A for (int j = 0; j < topology_weights[i].size(); j++) { 1457054SN/A int weight = topology_weights[i][j]; 1467054SN/A if (weight > 0 && weight != INFINITE_LATENCY) { 1479799Snilay@cs.wisc.edu NetDest destination_set = 1489799Snilay@cs.wisc.edu shortest_path_to_node(i, j, topology_weights, dist); 1499799Snilay@cs.wisc.edu makeLink(net, i, j, destination_set); 1507054SN/A } 1517054SN/A } 1526895SN/A } 1536895SN/A} 1546895SN/A 1557054SN/Avoid 15611663Stushar@ece.gatech.eduTopology::addLink(SwitchID src, SwitchID dest, BasicLink* link) 1577054SN/A{ 1587832SN/A assert(src <= m_number_of_switches+m_nodes+m_nodes); 1597832SN/A assert(dest <= m_number_of_switches+m_nodes+m_nodes); 16011320Ssteve.reinhardt@amd.com 1618257SBrad.Beckmann@amd.com std::pair<int, int> src_dest_pair; 1628257SBrad.Beckmann@amd.com LinkEntry link_entry; 1638257SBrad.Beckmann@amd.com 1648257SBrad.Beckmann@amd.com src_dest_pair.first = src; 1658257SBrad.Beckmann@amd.com src_dest_pair.second = dest; 1668257SBrad.Beckmann@amd.com link_entry.link = link; 1678257SBrad.Beckmann@amd.com m_link_map[src_dest_pair] = link_entry; 1687054SN/A} 1697054SN/A 1707054SN/Avoid 1717054SN/ATopology::makeLink(Network *net, SwitchID src, SwitchID dest, 1729799Snilay@cs.wisc.edu const NetDest& routing_table_entry) 1737054SN/A{ 1747054SN/A // Make sure we're not trying to connect two end-point nodes 1757054SN/A // directly together 1767054SN/A assert(src >= 2 * m_nodes || dest >= 2 * m_nodes); 1777054SN/A 1788257SBrad.Beckmann@amd.com std::pair<int, int> src_dest; 17911320Ssteve.reinhardt@amd.com LinkEntry link_entry; 1808257SBrad.Beckmann@amd.com 1817054SN/A if (src < m_nodes) { 1828257SBrad.Beckmann@amd.com src_dest.first = src; 1838257SBrad.Beckmann@amd.com src_dest.second = dest; 1848257SBrad.Beckmann@amd.com link_entry = m_link_map[src_dest]; 18511663Stushar@ece.gatech.edu net->makeExtInLink(src, dest - (2 * m_nodes), link_entry.link, 18611663Stushar@ece.gatech.edu routing_table_entry); 1877054SN/A } else if (dest < 2*m_nodes) { 1887054SN/A assert(dest >= m_nodes); 1898257SBrad.Beckmann@amd.com NodeID node = dest - m_nodes; 1908257SBrad.Beckmann@amd.com src_dest.first = src; 1918257SBrad.Beckmann@amd.com src_dest.second = dest; 1928257SBrad.Beckmann@amd.com link_entry = m_link_map[src_dest]; 19311663Stushar@ece.gatech.edu net->makeExtOutLink(src - (2 * m_nodes), node, link_entry.link, 19411663Stushar@ece.gatech.edu routing_table_entry); 1957054SN/A } else { 1968257SBrad.Beckmann@amd.com assert((src >= 2 * m_nodes) && (dest >= 2 * m_nodes)); 1978257SBrad.Beckmann@amd.com src_dest.first = src; 1988257SBrad.Beckmann@amd.com src_dest.second = dest; 1998257SBrad.Beckmann@amd.com link_entry = m_link_map[src_dest]; 2008257SBrad.Beckmann@amd.com net->makeInternalLink(src - (2 * m_nodes), dest - (2 * m_nodes), 20111663Stushar@ece.gatech.edu link_entry.link, 2029799Snilay@cs.wisc.edu routing_table_entry); 2037054SN/A } 2047054SN/A} 2057054SN/A 2066145SN/A// The following all-pairs shortest path algorithm is based on the 2076145SN/A// discussion from Cormen et al., Chapter 26.1. 2087054SN/Avoid 20911096Snilay@cs.wisc.eduTopology::extend_shortest_path(Matrix ¤t_dist, Matrix &latencies, 21011096Snilay@cs.wisc.edu Matrix &inter_switches) 2117054SN/A{ 2127054SN/A bool change = true; 2137054SN/A int nodes = current_dist.size(); 2146145SN/A 2157054SN/A while (change) { 2167054SN/A change = false; 2177054SN/A for (int i = 0; i < nodes; i++) { 2187054SN/A for (int j = 0; j < nodes; j++) { 2197054SN/A int minimum = current_dist[i][j]; 2207054SN/A int previous_minimum = minimum; 2217054SN/A int intermediate_switch = -1; 2227054SN/A for (int k = 0; k < nodes; k++) { 2237054SN/A minimum = min(minimum, 2247054SN/A current_dist[i][k] + current_dist[k][j]); 2257054SN/A if (previous_minimum != minimum) { 2267054SN/A intermediate_switch = k; 2277054SN/A inter_switches[i][j] = 2287054SN/A inter_switches[i][k] + 2297054SN/A inter_switches[k][j] + 1; 2307054SN/A } 2317054SN/A previous_minimum = minimum; 2327054SN/A } 2337054SN/A if (current_dist[i][j] != minimum) { 2347054SN/A change = true; 2357054SN/A current_dist[i][j] = minimum; 2367054SN/A assert(intermediate_switch >= 0); 2377054SN/A assert(intermediate_switch < latencies[i].size()); 2387054SN/A latencies[i][j] = latencies[i][intermediate_switch] + 2397054SN/A latencies[intermediate_switch][j]; 2407054SN/A } 2417054SN/A } 2426145SN/A } 2436145SN/A } 2446145SN/A} 2456145SN/A 2467054SN/AMatrix 24711096Snilay@cs.wisc.eduTopology::shortest_path(const Matrix &weights, Matrix &latencies, 24811096Snilay@cs.wisc.edu Matrix &inter_switches) 2496145SN/A{ 2507054SN/A Matrix dist = weights; 2517054SN/A extend_shortest_path(dist, latencies, inter_switches); 2527054SN/A return dist; 2536145SN/A} 2546145SN/A 2557054SN/Abool 25611096Snilay@cs.wisc.eduTopology::link_is_shortest_path_to_node(SwitchID src, SwitchID next, 25711096Snilay@cs.wisc.edu SwitchID final, const Matrix &weights, 25811096Snilay@cs.wisc.edu const Matrix &dist) 2596145SN/A{ 2607054SN/A return weights[src][next] + dist[next][final] == dist[src][final]; 2616145SN/A} 2626145SN/A 2637054SN/ANetDest 26411096Snilay@cs.wisc.eduTopology::shortest_path_to_node(SwitchID src, SwitchID next, 26511096Snilay@cs.wisc.edu const Matrix &weights, const Matrix &dist) 2666145SN/A{ 2677054SN/A NetDest result; 2687054SN/A int d = 0; 2697054SN/A int machines; 2707054SN/A int max_machines; 2716145SN/A 2727054SN/A machines = MachineType_NUM; 2737054SN/A max_machines = MachineType_base_number(MachineType_NUM); 2746145SN/A 2757054SN/A for (int m = 0; m < machines; m++) { 27610005Snilay@cs.wisc.edu for (NodeID i = 0; i < MachineType_base_count((MachineType)m); i++) { 2777054SN/A // we use "d+max_machines" below since the "destination" 2787054SN/A // switches for the machines are numbered 2797054SN/A // [MachineType_base_number(MachineType_NUM)... 2807054SN/A // 2*MachineType_base_number(MachineType_NUM)-1] for the 2817054SN/A // component network 2827054SN/A if (link_is_shortest_path_to_node(src, next, d + max_machines, 2837054SN/A weights, dist)) { 2847054SN/A MachineID mach = {(MachineType)m, i}; 2857054SN/A result.add(mach); 2867054SN/A } 2877054SN/A d++; 2887054SN/A } 2896145SN/A } 2906145SN/A 2917780SN/A DPRINTF(RubyNetwork, "Returning shortest path\n" 2927780SN/A "(src-(2*max_machines)): %d, (next-(2*max_machines)): %d, " 2937780SN/A "src: %d, next: %d, result: %s\n", 2947780SN/A (src-(2*max_machines)), (next-(2*max_machines)), 2957780SN/A src, next, result); 2966145SN/A 2977054SN/A return result; 2986145SN/A} 299