Topology.cc revision 9356:b279bad40aa3
17032SN/A/* 27032SN/A * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 37032SN/A * All rights reserved. 47032SN/A * 57032SN/A * Redistribution and use in source and binary forms, with or without 67032SN/A * modification, are permitted provided that the following conditions are 77032SN/A * met: redistributions of source code must retain the above copyright 87032SN/A * notice, this list of conditions and the following disclaimer; 97032SN/A * redistributions in binary form must reproduce the above copyright 107032SN/A * notice, this list of conditions and the following disclaimer in the 117032SN/A * documentation and/or other materials provided with the distribution; 127032SN/A * neither the name of the copyright holders nor the names of its 137032SN/A * contributors may be used to endorse or promote products derived from 147032SN/A * this software without specific prior written permission. 157032SN/A * 167032SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 177032SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 187032SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 197032SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 207032SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 217032SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 227032SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 237032SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 247032SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 257032SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 267032SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 277032SN/A */ 287032SN/A 2913774Sandreas.sandberg@arm.com#include <cassert> 3013774Sandreas.sandberg@arm.com 3113774Sandreas.sandberg@arm.com#include "base/trace.hh" 327032SN/A#include "debug/RubyNetwork.hh" 337032SN/A#include "mem/protocol/MachineType.hh" 347032SN/A#include "mem/ruby/common/NetDest.hh" 3513774Sandreas.sandberg@arm.com#include "mem/ruby/network/BasicLink.hh" 369100SBrad.Beckmann@amd.com#include "mem/ruby/network/Network.hh" 379148Spowerjg@cs.wisc.edu#include "mem/ruby/network/Topology.hh" 387540SN/A#include "mem/ruby/slicc_interface/AbstractController.hh" 397540SN/A 409862Snilay@cs.wisc.eduusing namespace std; 4111666Stushar@ece.gatech.edu 4211666Stushar@ece.gatech.educonst int INFINITE_LATENCY = 10000; // Yes, this is a big hack 4311666Stushar@ece.gatech.edu 4411666Stushar@ece.gatech.edu// Note: In this file, we use the first 2*m_nodes SwitchIDs to 4511666Stushar@ece.gatech.edu// represent the input and output endpoint links. These really are 4611666Stushar@ece.gatech.edu// not 'switches', as they will not have a Switch object allocated for 479862Snilay@cs.wisc.edu// them. The first m_nodes SwitchIDs are the links into the network, 489862Snilay@cs.wisc.edu// the second m_nodes set of SwitchIDs represent the the output queues 499862Snilay@cs.wisc.edu// of the network. 509862Snilay@cs.wisc.edu 5111663Stushar@ece.gatech.edu// Helper functions based on chapter 29 of Cormen et al. 527032SN/Avoid extend_shortest_path(Matrix& current_dist, Matrix& latencies, 539100SBrad.Beckmann@amd.com Matrix& inter_switches); 549862Snilay@cs.wisc.eduMatrix shortest_path(const Matrix& weights, Matrix& latencies, 559862Snilay@cs.wisc.edu Matrix& inter_switches); 569862Snilay@cs.wisc.edubool link_is_shortest_path_to_node(SwitchID src, SwitchID next, 5711666Stushar@ece.gatech.edu SwitchID final, const Matrix& weights, const Matrix& dist); 5811666Stushar@ece.gatech.eduNetDest shortest_path_to_node(SwitchID src, SwitchID next, 599100SBrad.Beckmann@amd.com const Matrix& weights, const Matrix& dist); 609862Snilay@cs.wisc.edu 619862Snilay@cs.wisc.eduTopology::Topology(const Params *p) 629100SBrad.Beckmann@amd.com : SimObject(p) 6311663Stushar@ece.gatech.edu{ 6411663Stushar@ece.gatech.edu m_print_config = p->print_config; 6511663Stushar@ece.gatech.edu m_number_of_switches = p->routers.size(); 6611663Stushar@ece.gatech.edu 6711663Stushar@ece.gatech.edu // initialize component latencies record 6811666Stushar@ece.gatech.edu m_component_latencies.resize(0); 6911666Stushar@ece.gatech.edu m_component_inter_switches.resize(0); 7011663Stushar@ece.gatech.edu 7111663Stushar@ece.gatech.edu // Total nodes/controllers in network 7211663Stushar@ece.gatech.edu // Must make sure this is called after the State Machine constructors 7311663Stushar@ece.gatech.edu m_nodes = MachineType_base_number(MachineType_NUM); 7411663Stushar@ece.gatech.edu assert(m_nodes > 1); 7511663Stushar@ece.gatech.edu 7611666Stushar@ece.gatech.edu if (m_nodes != params()->ext_links.size() && 7711666Stushar@ece.gatech.edu m_nodes != params()->ext_links.size()) { 7811663Stushar@ece.gatech.edu fatal("m_nodes (%d) != ext_links vector length (%d)\n", 799862Snilay@cs.wisc.edu m_nodes, params()->ext_links.size()); 80 } 81 82 // analyze both the internal and external links, create data structures 83 // Note that the python created links are bi-directional, but that the 84 // topology and networks utilize uni-directional links. Thus each 85 // BasicLink is converted to two calls to add link, on for each direction 86 for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin(); 87 i != params()->ext_links.end(); ++i) { 88 BasicExtLink *ext_link = (*i); 89 AbstractController *abs_cntrl = ext_link->params()->ext_node; 90 BasicRouter *router = ext_link->params()->int_node; 91 92 // Store the controller and ExtLink pointers for later 93 m_controller_vector.push_back(abs_cntrl); 94 m_ext_link_vector.push_back(ext_link); 95 96 int ext_idx1 = abs_cntrl->params()->cntrl_id; 97 int ext_idx2 = ext_idx1 + m_nodes; 98 int int_idx = router->params()->router_id + 2*m_nodes; 99 100 // create the internal uni-directional links in both directions 101 // the first direction is marked: In 102 addLink(ext_idx1, int_idx, ext_link, LinkDirection_In); 103 // the first direction is marked: Out 104 addLink(int_idx, ext_idx2, ext_link, LinkDirection_Out); 105 } 106 107 for (vector<BasicIntLink*>::const_iterator i = params()->int_links.begin(); 108 i != params()->int_links.end(); ++i) { 109 BasicIntLink *int_link = (*i); 110 BasicRouter *router_a = int_link->params()->node_a; 111 BasicRouter *router_b = int_link->params()->node_b; 112 113 // Store the IntLink pointers for later 114 m_int_link_vector.push_back(int_link); 115 116 int a = router_a->params()->router_id + 2*m_nodes; 117 int b = router_b->params()->router_id + 2*m_nodes; 118 119 // create the internal uni-directional links in both directions 120 // the first direction is marked: In 121 addLink(a, b, int_link, LinkDirection_In); 122 // the second direction is marked: Out 123 addLink(b, a, int_link, LinkDirection_Out); 124 } 125} 126 127void 128Topology::init() 129{ 130} 131 132 133void 134Topology::initNetworkPtr(Network* net_ptr) 135{ 136 for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin(); 137 i != params()->ext_links.end(); ++i) { 138 BasicExtLink *ext_link = (*i); 139 AbstractController *abs_cntrl = ext_link->params()->ext_node; 140 abs_cntrl->initNetworkPtr(net_ptr); 141 } 142} 143 144void 145Topology::createLinks(Network *net, bool isReconfiguration) 146{ 147 // Find maximum switchID 148 SwitchID max_switch_id = 0; 149 for (LinkMap::const_iterator i = m_link_map.begin(); 150 i != m_link_map.end(); ++i) { 151 std::pair<int, int> src_dest = (*i).first; 152 max_switch_id = max(max_switch_id, src_dest.first); 153 max_switch_id = max(max_switch_id, src_dest.second); 154 } 155 156 // Initialize weight, latency, and inter switched vectors 157 Matrix topology_weights; 158 int num_switches = max_switch_id+1; 159 topology_weights.resize(num_switches); 160 m_component_latencies.resize(num_switches); 161 m_component_inter_switches.resize(num_switches); 162 163 for (int i = 0; i < topology_weights.size(); i++) { 164 topology_weights[i].resize(num_switches); 165 m_component_latencies[i].resize(num_switches); 166 m_component_inter_switches[i].resize(num_switches); 167 168 for (int j = 0; j < topology_weights[i].size(); j++) { 169 topology_weights[i][j] = INFINITE_LATENCY; 170 171 // initialize to invalid values 172 m_component_latencies[i][j] = -1; 173 174 // initially assume direct connections / no intermediate 175 // switches between components 176 m_component_inter_switches[i][j] = 0; 177 } 178 } 179 180 // Set identity weights to zero 181 for (int i = 0; i < topology_weights.size(); i++) { 182 topology_weights[i][i] = 0; 183 } 184 185 // Fill in the topology weights and bandwidth multipliers 186 for (LinkMap::const_iterator i = m_link_map.begin(); 187 i != m_link_map.end(); ++i) { 188 std::pair<int, int> src_dest = (*i).first; 189 BasicLink* link = (*i).second.link; 190 int src = src_dest.first; 191 int dst = src_dest.second; 192 m_component_latencies[src][dst] = link->m_latency; 193 topology_weights[src][dst] = link->m_weight; 194 } 195 196 // Walk topology and hookup the links 197 Matrix dist = shortest_path(topology_weights, m_component_latencies, 198 m_component_inter_switches); 199 for (int i = 0; i < topology_weights.size(); i++) { 200 for (int j = 0; j < topology_weights[i].size(); j++) { 201 int weight = topology_weights[i][j]; 202 if (weight > 0 && weight != INFINITE_LATENCY) { 203 NetDest destination_set = shortest_path_to_node(i, j, 204 topology_weights, dist); 205 makeLink(net, i, j, destination_set, isReconfiguration); 206 } 207 } 208 } 209} 210 211void 212Topology::addLink(SwitchID src, SwitchID dest, BasicLink* link, 213 LinkDirection dir) 214{ 215 assert(src <= m_number_of_switches+m_nodes+m_nodes); 216 assert(dest <= m_number_of_switches+m_nodes+m_nodes); 217 218 std::pair<int, int> src_dest_pair; 219 LinkEntry link_entry; 220 221 src_dest_pair.first = src; 222 src_dest_pair.second = dest; 223 link_entry.direction = dir; 224 link_entry.link = link; 225 m_link_map[src_dest_pair] = link_entry; 226} 227 228void 229Topology::makeLink(Network *net, SwitchID src, SwitchID dest, 230 const NetDest& routing_table_entry, bool isReconfiguration) 231{ 232 // Make sure we're not trying to connect two end-point nodes 233 // directly together 234 assert(src >= 2 * m_nodes || dest >= 2 * m_nodes); 235 236 std::pair<int, int> src_dest; 237 LinkEntry link_entry; 238 239 if (src < m_nodes) { 240 src_dest.first = src; 241 src_dest.second = dest; 242 link_entry = m_link_map[src_dest]; 243 net->makeInLink(src, dest - (2 * m_nodes), link_entry.link, 244 link_entry.direction, 245 routing_table_entry, 246 isReconfiguration); 247 } else if (dest < 2*m_nodes) { 248 assert(dest >= m_nodes); 249 NodeID node = dest - m_nodes; 250 src_dest.first = src; 251 src_dest.second = dest; 252 link_entry = m_link_map[src_dest]; 253 net->makeOutLink(src - (2 * m_nodes), node, link_entry.link, 254 link_entry.direction, 255 routing_table_entry, 256 isReconfiguration); 257 } else { 258 assert((src >= 2 * m_nodes) && (dest >= 2 * m_nodes)); 259 src_dest.first = src; 260 src_dest.second = dest; 261 link_entry = m_link_map[src_dest]; 262 net->makeInternalLink(src - (2 * m_nodes), dest - (2 * m_nodes), 263 link_entry.link, link_entry.direction, 264 routing_table_entry, isReconfiguration); 265 } 266} 267 268void 269Topology::printStats(std::ostream& out) const 270{ 271 for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) { 272 m_controller_vector[cntrl]->printStats(out); 273 } 274} 275 276void 277Topology::clearStats() 278{ 279 for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) { 280 m_controller_vector[cntrl]->clearStats(); 281 } 282} 283 284// The following all-pairs shortest path algorithm is based on the 285// discussion from Cormen et al., Chapter 26.1. 286void 287extend_shortest_path(Matrix& current_dist, Matrix& latencies, 288 Matrix& inter_switches) 289{ 290 bool change = true; 291 int nodes = current_dist.size(); 292 293 while (change) { 294 change = false; 295 for (int i = 0; i < nodes; i++) { 296 for (int j = 0; j < nodes; j++) { 297 int minimum = current_dist[i][j]; 298 int previous_minimum = minimum; 299 int intermediate_switch = -1; 300 for (int k = 0; k < nodes; k++) { 301 minimum = min(minimum, 302 current_dist[i][k] + current_dist[k][j]); 303 if (previous_minimum != minimum) { 304 intermediate_switch = k; 305 inter_switches[i][j] = 306 inter_switches[i][k] + 307 inter_switches[k][j] + 1; 308 } 309 previous_minimum = minimum; 310 } 311 if (current_dist[i][j] != minimum) { 312 change = true; 313 current_dist[i][j] = minimum; 314 assert(intermediate_switch >= 0); 315 assert(intermediate_switch < latencies[i].size()); 316 latencies[i][j] = latencies[i][intermediate_switch] + 317 latencies[intermediate_switch][j]; 318 } 319 } 320 } 321 } 322} 323 324Matrix 325shortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches) 326{ 327 Matrix dist = weights; 328 extend_shortest_path(dist, latencies, inter_switches); 329 return dist; 330} 331 332bool 333link_is_shortest_path_to_node(SwitchID src, SwitchID next, SwitchID final, 334 const Matrix& weights, const Matrix& dist) 335{ 336 return weights[src][next] + dist[next][final] == dist[src][final]; 337} 338 339NetDest 340shortest_path_to_node(SwitchID src, SwitchID next, const Matrix& weights, 341 const Matrix& dist) 342{ 343 NetDest result; 344 int d = 0; 345 int machines; 346 int max_machines; 347 348 machines = MachineType_NUM; 349 max_machines = MachineType_base_number(MachineType_NUM); 350 351 for (int m = 0; m < machines; m++) { 352 for (int i = 0; i < MachineType_base_count((MachineType)m); i++) { 353 // we use "d+max_machines" below since the "destination" 354 // switches for the machines are numbered 355 // [MachineType_base_number(MachineType_NUM)... 356 // 2*MachineType_base_number(MachineType_NUM)-1] for the 357 // component network 358 if (link_is_shortest_path_to_node(src, next, d + max_machines, 359 weights, dist)) { 360 MachineID mach = {(MachineType)m, i}; 361 result.add(mach); 362 } 363 d++; 364 } 365 } 366 367 DPRINTF(RubyNetwork, "Returning shortest path\n" 368 "(src-(2*max_machines)): %d, (next-(2*max_machines)): %d, " 369 "src: %d, next: %d, result: %s\n", 370 (src-(2*max_machines)), (next-(2*max_machines)), 371 src, next, result); 372 373 return result; 374} 375 376Topology * 377TopologyParams::create() 378{ 379 return new Topology(this); 380} 381 382