Topology.cc revision 6876
1
2/*
3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
31 * Topology.cc
32 *
33 * Description: See Topology.hh
34 *
35 * $Id$
36 *
37 * */
38
39#include "mem/ruby/network/simple/Topology.hh"
40#include "mem/ruby/common/NetDest.hh"
41#include "mem/ruby/network/Network.hh"
42#include "mem/protocol/TopologyType.hh"
43#include "mem/gems_common/util.hh"
44#include "mem/protocol/MachineType.hh"
45#include "mem/protocol/Protocol.hh"
46#include "mem/ruby/system/System.hh"
47#include <string>
48
49static const int INFINITE_LATENCY = 10000; // Yes, this is a big hack
50static const int DEFAULT_BW_MULTIPLIER = 1;  // Just to be consistent with above :)
51
52// Note: In this file, we use the first 2*m_nodes SwitchIDs to
53// represent the input and output endpoint links.  These really are
54// not 'switches', as they will not have a Switch object allocated for
55// them. The first m_nodes SwitchIDs are the links into the network,
56// the second m_nodes set of SwitchIDs represent the the output queues
57// of the network.
58
59// Helper functions based on chapter 29 of Cormen et al.
60static void extend_shortest_path(Matrix& current_dist, Matrix& latencies, Matrix& inter_switches);
61static Matrix shortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches);
62static bool link_is_shortest_path_to_node(SwitchID src, SwitchID next, SwitchID final, const Matrix& weights, const Matrix& dist);
63static NetDest shortest_path_to_node(SwitchID src, SwitchID next, const Matrix& weights, const Matrix& dist);
64
65Topology::Topology(const Params *p)
66    : SimObject(p)
67{
68//    m_network_ptr = p->network;
69    m_connections = p->connections;
70    m_print_config = p->print_config;
71  m_nodes = MachineType_base_number(MachineType_NUM);
72  m_number_of_switches = 0;
73}
74
75void Topology::init()
76{
77}
78
79void Topology::makeTopology()
80{
81  /*
82  if (m_nodes == 1) {
83    SwitchID id = newSwitchID();
84    addLink(0, id, m_network_ptr->getOffChipLinkLatency());
85    addLink(id, 1, m_network_ptr->getOffChipLinkLatency());
86    return;
87  }
88  */
89  assert(m_nodes > 1);
90
91  Vector< Vector < SwitchID > > nodePairs;  // node pairs extracted from the file
92  Vector<int> latencies;  // link latencies for each link extracted
93  Vector<int> bw_multis;  // bw multipliers for each link extracted
94  Vector<int> weights;  // link weights used to enfore e-cube deadlock free routing
95  Vector< SwitchID > int_network_switches;  // internal switches extracted from the file
96  Vector<bool> endpointConnectionExist;  // used to ensure all endpoints are connected to the network
97
98  endpointConnectionExist.setSize(m_nodes);
99
100  // initialize endpoint check vector
101  for (int k = 0; k < endpointConnectionExist.size(); k++) {
102    endpointConnectionExist[k] = false;
103  }
104
105  stringstream networkFile( m_connections );
106
107  string line = "";
108
109  while (!networkFile.eof()) {
110
111    Vector < SwitchID > nodes;
112    nodes.setSize(2);
113    int latency = -1;  // null latency
114    int weight = -1;  // null weight
115    int bw_multiplier = DEFAULT_BW_MULTIPLIER;  // default multiplier incase the network file doesn't define it
116    int i = 0;  // node pair index
117    int varsFound = 0;  // number of varsFound on the line
118    int internalNodes = 0;  // used to determine if the link is between 2 internal nodes
119    std::getline(networkFile, line, '\n');
120    string varStr = string_split(line, ' ');
121
122    // parse the current line in the file
123    while (varStr != "") {
124      string label = string_split(varStr, ':');
125
126      // valid node labels
127      if (label == "ext_node" || label == "int_node") {
128        ASSERT(i < 2); // one link between 2 switches per line
129        varsFound++;
130        bool isNewIntSwitch = true;
131        if (label == "ext_node") { // input link to node
132          MachineType machine = string_to_MachineType(string_split(varStr, ':'));
133          string nodeStr = string_split(varStr, ':');
134          nodes[i] = MachineType_base_number(machine)
135            + atoi(nodeStr.c_str());
136
137          // in nodes should be numbered 0 to m_nodes-1
138          ASSERT(nodes[i] >= 0 && nodes[i] < m_nodes);
139          isNewIntSwitch = false;
140          endpointConnectionExist[nodes[i]] = true;
141        }
142        if (label == "int_node") { // interior node
143          nodes[i] = atoi((string_split(varStr, ':')).c_str())+m_nodes*2;
144          // in nodes should be numbered >= m_nodes*2
145          ASSERT(nodes[i] >= m_nodes*2);
146          for (int k = 0; k < int_network_switches.size(); k++) {
147            if (int_network_switches[k] == nodes[i]) {
148              isNewIntSwitch = false;
149            }
150          }
151          if (isNewIntSwitch) {  // if internal switch
152            m_number_of_switches++;
153            int_network_switches.insertAtBottom(nodes[i]);
154          }
155          internalNodes++;
156        }
157        i++;
158      } else if (label == "link_latency") {
159        latency = atoi((string_split(varStr, ':')).c_str());
160        varsFound++;
161      } else if (label == "bw_multiplier") {  // not necessary, defaults to DEFAULT_BW_MULTIPLIER
162        bw_multiplier = atoi((string_split(varStr, ':')).c_str());
163      } else if (label == "link_weight") {  // not necessary, defaults to link_latency
164        weight = atoi((string_split(varStr, ':')).c_str());
165      } else {
166        cerr << "Error: Unexpected Identifier: " << label << endl;
167        exit(1);
168      }
169      varStr = string_split(line, ' ');
170    }
171    if (varsFound == 3) { // all three necessary link variables where found so add the link
172      nodePairs.insertAtBottom(nodes);
173      latencies.insertAtBottom(latency);
174      if (weight != -1) {
175        weights.insertAtBottom(weight);
176      } else {
177        weights.insertAtBottom(latency);
178      }
179      bw_multis.insertAtBottom(bw_multiplier);
180      Vector < SwitchID > otherDirectionNodes;
181      otherDirectionNodes.setSize(2);
182      otherDirectionNodes[0] = nodes[1];
183      if (internalNodes == 2) {  // this is an internal link
184        otherDirectionNodes[1] = nodes[0];
185      } else {
186        otherDirectionNodes[1] = nodes[0]+m_nodes;
187      }
188      nodePairs.insertAtBottom(otherDirectionNodes);
189      latencies.insertAtBottom(latency);
190      if (weight != -1) {
191        weights.insertAtBottom(weight);
192      } else {
193        weights.insertAtBottom(latency);
194      }
195      bw_multis.insertAtBottom(bw_multiplier);
196    } else {
197      if (varsFound != 0) {  // if this is not a valid link, then no vars should have been found
198        cerr << "Error in line: " << line << endl;
199        exit(1);
200      }
201    }
202  } // end of file
203
204  // makes sure all enpoints are connected in the soon to be created network
205  for (int k = 0; k < endpointConnectionExist.size(); k++) {
206    if (endpointConnectionExist[k] == false) {
207      cerr << "Error: Unconnected Endpoint: " << k << endl;
208      exit(1);
209    }
210  }
211
212  ASSERT(nodePairs.size() == latencies.size() && latencies.size() == bw_multis.size() && latencies.size() == weights.size())
213  for (int k = 0; k < nodePairs.size(); k++) {
214    ASSERT(nodePairs[k].size() == 2);
215    addLink(nodePairs[k][0], nodePairs[k][1], latencies[k], bw_multis[k], weights[k]);
216  }
217
218  // initialize component latencies record
219  m_component_latencies.setSize(0);
220  m_component_inter_switches.setSize(0);
221}
222
223
224void Topology::createLinks(bool isReconfiguration)
225{
226  // Find maximum switchID
227
228  SwitchID max_switch_id = 0;
229  for (int i=0; i<m_links_src_vector.size(); i++) {
230    max_switch_id = max(max_switch_id, m_links_src_vector[i]);
231    max_switch_id = max(max_switch_id, m_links_dest_vector[i]);
232  }
233
234  // Initialize weight vector
235  Matrix topology_weights;
236  Matrix topology_latency;
237  Matrix topology_bw_multis;
238  int num_switches = max_switch_id+1;
239  topology_weights.setSize(num_switches);
240  topology_latency.setSize(num_switches);
241  topology_bw_multis.setSize(num_switches);
242  m_component_latencies.setSize(num_switches);  // FIXME setting the size of a member variable here is a HACK!
243  m_component_inter_switches.setSize(num_switches);  // FIXME setting the size of a member variable here is a HACK!
244  for(int i=0; i<topology_weights.size(); i++) {
245    topology_weights[i].setSize(num_switches);
246    topology_latency[i].setSize(num_switches);
247    topology_bw_multis[i].setSize(num_switches);
248    m_component_latencies[i].setSize(num_switches);
249    m_component_inter_switches[i].setSize(num_switches);  // FIXME setting the size of a member variable here is a HACK!
250    for(int j=0; j<topology_weights[i].size(); j++) {
251      topology_weights[i][j] = INFINITE_LATENCY;
252      topology_latency[i][j] = -1;  // initialize to an invalid value
253      topology_bw_multis[i][j] = -1;  // initialize to an invalid value
254      m_component_latencies[i][j] = -1; // initialize to an invalid value
255      m_component_inter_switches[i][j] = 0;  // initially assume direct connections / no intermediate switches between components
256    }
257  }
258
259  // Set identity weights to zero
260  for(int i=0; i<topology_weights.size(); i++) {
261    topology_weights[i][i] = 0;
262  }
263
264  // Fill in the topology weights and bandwidth multipliers
265  for (int i=0; i<m_links_src_vector.size(); i++) {
266    topology_weights[m_links_src_vector[i]][m_links_dest_vector[i]] = m_links_weight_vector[i];
267    topology_latency[m_links_src_vector[i]][m_links_dest_vector[i]] = m_links_latency_vector[i];
268    m_component_latencies[m_links_src_vector[i]][m_links_dest_vector[i]] = m_links_latency_vector[i];  // initialize to latency vector
269    topology_bw_multis[m_links_src_vector[i]][m_links_dest_vector[i]] = m_bw_multiplier_vector[i];
270  }
271
272  // Walk topology and hookup the links
273  Matrix dist = shortest_path(topology_weights, m_component_latencies, m_component_inter_switches);
274  for(int i=0; i<topology_weights.size(); i++) {
275    for(int j=0; j<topology_weights[i].size(); j++) {
276      int weight = topology_weights[i][j];
277      int bw_multiplier = topology_bw_multis[i][j];
278      int latency = topology_latency[i][j];
279      if (weight > 0 && weight != INFINITE_LATENCY) {
280        NetDest destination_set = shortest_path_to_node(i, j, topology_weights, dist);
281        assert(latency != -1);
282        makeLink(i, j, destination_set, latency, weight, bw_multiplier, isReconfiguration);
283      }
284    }
285  }
286}
287
288SwitchID Topology::newSwitchID()
289{
290  m_number_of_switches++;
291  return m_number_of_switches-1+m_nodes+m_nodes;
292}
293
294void Topology::addLink(SwitchID src, SwitchID dest, int link_latency)
295{
296  addLink(src, dest, link_latency, DEFAULT_BW_MULTIPLIER, link_latency);
297}
298
299void Topology::addLink(SwitchID src, SwitchID dest, int link_latency, int bw_multiplier)
300{
301  addLink(src, dest, link_latency, bw_multiplier, link_latency);
302}
303
304void Topology::addLink(SwitchID src, SwitchID dest, int link_latency, int bw_multiplier, int link_weight)
305{
306  ASSERT(src <= m_number_of_switches+m_nodes+m_nodes);
307  ASSERT(dest <= m_number_of_switches+m_nodes+m_nodes);
308  m_links_src_vector.insertAtBottom(src);
309  m_links_dest_vector.insertAtBottom(dest);
310  m_links_latency_vector.insertAtBottom(link_latency);
311  m_links_weight_vector.insertAtBottom(link_weight);
312  m_bw_multiplier_vector.insertAtBottom(bw_multiplier);
313}
314
315void Topology::makeLink(SwitchID src, SwitchID dest, const NetDest& routing_table_entry, int link_latency, int link_weight, int bw_multiplier, bool isReconfiguration)
316{
317  // Make sure we're not trying to connect two end-point nodes directly together
318  assert((src >= 2*m_nodes) || (dest >= 2*m_nodes));
319
320  if (src < m_nodes) {
321    m_network_ptr->makeInLink(src, dest-(2*m_nodes), routing_table_entry, link_latency, bw_multiplier, isReconfiguration);
322  } else if (dest < 2*m_nodes) {
323    assert(dest >= m_nodes);
324    NodeID node = dest-m_nodes;
325    m_network_ptr->makeOutLink(src-(2*m_nodes), node, routing_table_entry, link_latency, link_weight, bw_multiplier, isReconfiguration);
326  } else {
327    assert((src >= 2*m_nodes) && (dest >= 2*m_nodes));
328    m_network_ptr->makeInternalLink(src-(2*m_nodes), dest-(2*m_nodes), routing_table_entry, link_latency, link_weight, bw_multiplier, isReconfiguration);
329  }
330}
331
332void Topology::printConfig(ostream& out) const
333{
334  if (m_print_config == false) return;
335
336  assert(m_component_latencies.size() > 0);
337
338  out << "--- Begin Topology Print ---" << endl;
339  out << endl;
340  out << "Topology print ONLY indicates the _NETWORK_ latency between two machines" << endl;
341  out << "It does NOT include the latency within the machines" << endl;
342  out << endl;
343  for (int m=0; m<MachineType_NUM; m++) {
344    for (int i=0; i<MachineType_base_count((MachineType)m); i++) {
345      MachineID cur_mach = {(MachineType)m, i};
346      out << cur_mach << " Network Latencies" << endl;
347      for (int n=0; n<MachineType_NUM; n++) {
348        for (int j=0; j<MachineType_base_count((MachineType)n); j++) {
349          MachineID dest_mach = {(MachineType)n, j};
350          if (cur_mach != dest_mach) {
351            int link_latency = m_component_latencies[MachineType_base_number((MachineType)m)+i][MachineType_base_number(MachineType_NUM)+MachineType_base_number((MachineType)n)+j];
352            int intermediate_switches = m_component_inter_switches[MachineType_base_number((MachineType)m)+i][MachineType_base_number(MachineType_NUM)+MachineType_base_number((MachineType)n)+j];
353            out << "  " << cur_mach << " -> " << dest_mach << " net_lat: "
354                << link_latency+intermediate_switches << endl;  // NOTE switches are assumed to have single cycle latency
355          }
356        }
357      }
358      out << endl;
359    }
360  }
361
362  out << "--- End Topology Print ---" << endl;
363}
364
365/**************************************************************************/
366
367// The following all-pairs shortest path algorithm is based on the
368// discussion from Cormen et al., Chapter 26.1.
369
370static void extend_shortest_path(Matrix& current_dist, Matrix& latencies, Matrix& inter_switches)
371{
372  bool change = true;
373  int nodes = current_dist.size();
374
375  while (change) {
376    change = false;
377    for (int i=0; i<nodes; i++) {
378      for (int j=0; j<nodes; j++) {
379        int minimum = current_dist[i][j];
380        int previous_minimum = minimum;
381        int intermediate_switch = -1;
382        for (int k=0; k<nodes; k++) {
383          minimum = min(minimum, current_dist[i][k] + current_dist[k][j]);
384          if (previous_minimum != minimum) {
385            intermediate_switch = k;
386            inter_switches[i][j] = inter_switches[i][k] + inter_switches[k][j] + 1;
387          }
388          previous_minimum = minimum;
389        }
390        if (current_dist[i][j] != minimum) {
391          change = true;
392          current_dist[i][j] = minimum;
393          assert(intermediate_switch >= 0);
394          assert(intermediate_switch < latencies[i].size());
395          latencies[i][j] = latencies[i][intermediate_switch] + latencies[intermediate_switch][j];
396        }
397      }
398    }
399  }
400}
401
402static Matrix shortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches)
403{
404  Matrix dist = weights;
405  extend_shortest_path(dist, latencies, inter_switches);
406  return dist;
407}
408
409static bool link_is_shortest_path_to_node(SwitchID src, SwitchID next, SwitchID final,
410                                          const Matrix& weights, const Matrix& dist)
411{
412  return (weights[src][next] + dist[next][final] == dist[src][final]);
413}
414
415static NetDest shortest_path_to_node(SwitchID src, SwitchID next,
416                                     const Matrix& weights, const Matrix& dist)
417{
418  NetDest result;
419  int d = 0;
420  int machines;
421  int max_machines;
422
423  machines = MachineType_NUM;
424  max_machines = MachineType_base_number(MachineType_NUM);
425
426  for (int m=0; m<machines; m++) {
427    for (int i=0; i<MachineType_base_count((MachineType)m); i++) {
428      // we use "d+max_machines" below since the "destination" switches for the machines are numbered
429      // [MachineType_base_number(MachineType_NUM)...2*MachineType_base_number(MachineType_NUM)-1]
430      // for the component network
431      if (link_is_shortest_path_to_node(src, next,
432                                        d+max_machines,
433                                        weights, dist)) {
434        MachineID mach = {(MachineType)m, i};
435        result.add(mach);
436      }
437      d++;
438    }
439  }
440
441  DEBUG_MSG(NETWORK_COMP, MedPrio, "returning shortest path");
442  DEBUG_EXPR(NETWORK_COMP, MedPrio, (src-(2*max_machines)));
443  DEBUG_EXPR(NETWORK_COMP, MedPrio, (next-(2*max_machines)));
444  DEBUG_EXPR(NETWORK_COMP, MedPrio, src);
445  DEBUG_EXPR(NETWORK_COMP, MedPrio, next);
446  DEBUG_EXPR(NETWORK_COMP, MedPrio, result);
447  DEBUG_NEWLINE(NETWORK_COMP, MedPrio);
448
449  return result;
450}
451
452Topology *
453TopologyParams::create()
454{
455    return new Topology(this);
456}
457