Topology.cc (9117:49116b947194) Topology.cc (9274:ba635023d4bb)
1/*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <cassert>
30
31#include "debug/RubyNetwork.hh"
32#include "mem/protocol/MachineType.hh"
33#include "mem/ruby/common/NetDest.hh"
34#include "mem/ruby/network/BasicLink.hh"
1/*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <cassert>
30
31#include "debug/RubyNetwork.hh"
32#include "mem/protocol/MachineType.hh"
33#include "mem/ruby/common/NetDest.hh"
34#include "mem/ruby/network/BasicLink.hh"
35#include "mem/ruby/network/BasicRouter.hh"
36#include "mem/ruby/network/Network.hh"
37#include "mem/ruby/network/Topology.hh"
38#include "mem/ruby/slicc_interface/AbstractController.hh"
39
40using namespace std;
41
42const int INFINITE_LATENCY = 10000; // Yes, this is a big hack
43
35#include "mem/ruby/network/Network.hh"
36#include "mem/ruby/network/Topology.hh"
37#include "mem/ruby/slicc_interface/AbstractController.hh"
38
39using namespace std;
40
41const int INFINITE_LATENCY = 10000; // Yes, this is a big hack
42
44class BasicRouter;
45
46// Note: In this file, we use the first 2*m_nodes SwitchIDs to
47// represent the input and output endpoint links. These really are
48// not 'switches', as they will not have a Switch object allocated for
49// them. The first m_nodes SwitchIDs are the links into the network,
50// the second m_nodes set of SwitchIDs represent the the output queues
51// of the network.
52
53// Helper functions based on chapter 29 of Cormen et al.
54void extend_shortest_path(Matrix& current_dist, Matrix& latencies,
55 Matrix& inter_switches);
56Matrix shortest_path(const Matrix& weights, Matrix& latencies,
57 Matrix& inter_switches);
58bool link_is_shortest_path_to_node(SwitchID src, SwitchID next,
59 SwitchID final, const Matrix& weights, const Matrix& dist);
60NetDest shortest_path_to_node(SwitchID src, SwitchID next,
61 const Matrix& weights, const Matrix& dist);
62
63Topology::Topology(const Params *p)
64 : SimObject(p)
65{
66 m_print_config = p->print_config;
67 m_number_of_switches = p->routers.size();
68
69 // initialize component latencies record
70 m_component_latencies.resize(0);
71 m_component_inter_switches.resize(0);
72
73 // Total nodes/controllers in network
74 // Must make sure this is called after the State Machine constructors
75 m_nodes = MachineType_base_number(MachineType_NUM);
76 assert(m_nodes > 1);
77
78 if (m_nodes != params()->ext_links.size() &&
79 m_nodes != params()->ext_links.size()) {
80 fatal("m_nodes (%d) != ext_links vector length (%d)\n",
81 m_nodes, params()->ext_links.size());
82 }
83
84 // analyze both the internal and external links, create data structures
85 // Note that the python created links are bi-directional, but that the
86 // topology and networks utilize uni-directional links. Thus each
87 // BasicLink is converted to two calls to add link, on for each direction
88 for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin();
89 i != params()->ext_links.end(); ++i) {
90 BasicExtLink *ext_link = (*i);
91 AbstractController *abs_cntrl = ext_link->params()->ext_node;
92 BasicRouter *router = ext_link->params()->int_node;
93
94 // Store the controller and ExtLink pointers for later
95 m_controller_vector.push_back(abs_cntrl);
96 m_ext_link_vector.push_back(ext_link);
97
98 int ext_idx1 = abs_cntrl->params()->cntrl_id;
99 int ext_idx2 = ext_idx1 + m_nodes;
100 int int_idx = router->params()->router_id + 2*m_nodes;
101
102 // create the internal uni-directional links in both directions
103 // the first direction is marked: In
104 addLink(ext_idx1, int_idx, ext_link, LinkDirection_In);
105 // the first direction is marked: Out
106 addLink(int_idx, ext_idx2, ext_link, LinkDirection_Out);
107 }
108
109 for (vector<BasicIntLink*>::const_iterator i = params()->int_links.begin();
110 i != params()->int_links.end(); ++i) {
111 BasicIntLink *int_link = (*i);
112 BasicRouter *router_a = int_link->params()->node_a;
113 BasicRouter *router_b = int_link->params()->node_b;
114
115 // Store the IntLink pointers for later
116 m_int_link_vector.push_back(int_link);
117
118 int a = router_a->params()->router_id + 2*m_nodes;
119 int b = router_b->params()->router_id + 2*m_nodes;
120
121 // create the internal uni-directional links in both directions
122 // the first direction is marked: In
123 addLink(a, b, int_link, LinkDirection_In);
124 // the second direction is marked: Out
125 addLink(b, a, int_link, LinkDirection_Out);
126 }
127}
128
129void
130Topology::init()
131{
132}
133
134
135void
136Topology::initNetworkPtr(Network* net_ptr)
137{
138 for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin();
139 i != params()->ext_links.end(); ++i) {
140 BasicExtLink *ext_link = (*i);
141 AbstractController *abs_cntrl = ext_link->params()->ext_node;
142 abs_cntrl->initNetworkPtr(net_ptr);
143 }
144}
145
146void
147Topology::createLinks(Network *net, bool isReconfiguration)
148{
149 // Find maximum switchID
150 SwitchID max_switch_id = 0;
151 for (LinkMap::const_iterator i = m_link_map.begin();
152 i != m_link_map.end(); ++i) {
153 std::pair<int, int> src_dest = (*i).first;
154 max_switch_id = max(max_switch_id, src_dest.first);
155 max_switch_id = max(max_switch_id, src_dest.second);
156 }
157
158 // Initialize weight, latency, and inter switched vectors
159 Matrix topology_weights;
160 int num_switches = max_switch_id+1;
161 topology_weights.resize(num_switches);
162 m_component_latencies.resize(num_switches);
163 m_component_inter_switches.resize(num_switches);
164
165 for (int i = 0; i < topology_weights.size(); i++) {
166 topology_weights[i].resize(num_switches);
167 m_component_latencies[i].resize(num_switches);
168 m_component_inter_switches[i].resize(num_switches);
169
170 for (int j = 0; j < topology_weights[i].size(); j++) {
171 topology_weights[i][j] = INFINITE_LATENCY;
172
173 // initialize to invalid values
174 m_component_latencies[i][j] = -1;
175
176 // initially assume direct connections / no intermediate
177 // switches between components
178 m_component_inter_switches[i][j] = 0;
179 }
180 }
181
182 // Set identity weights to zero
183 for (int i = 0; i < topology_weights.size(); i++) {
184 topology_weights[i][i] = 0;
185 }
186
187 // Fill in the topology weights and bandwidth multipliers
188 for (LinkMap::const_iterator i = m_link_map.begin();
189 i != m_link_map.end(); ++i) {
190 std::pair<int, int> src_dest = (*i).first;
191 BasicLink* link = (*i).second.link;
192 int src = src_dest.first;
193 int dst = src_dest.second;
194 m_component_latencies[src][dst] = link->m_latency;
195 topology_weights[src][dst] = link->m_weight;
196 }
197
198 // Walk topology and hookup the links
199 Matrix dist = shortest_path(topology_weights, m_component_latencies,
200 m_component_inter_switches);
201 for (int i = 0; i < topology_weights.size(); i++) {
202 for (int j = 0; j < topology_weights[i].size(); j++) {
203 int weight = topology_weights[i][j];
204 if (weight > 0 && weight != INFINITE_LATENCY) {
205 NetDest destination_set = shortest_path_to_node(i, j,
206 topology_weights, dist);
207 makeLink(net, i, j, destination_set, isReconfiguration);
208 }
209 }
210 }
211}
212
213void
214Topology::addLink(SwitchID src, SwitchID dest, BasicLink* link,
215 LinkDirection dir)
216{
217 assert(src <= m_number_of_switches+m_nodes+m_nodes);
218 assert(dest <= m_number_of_switches+m_nodes+m_nodes);
219
220 std::pair<int, int> src_dest_pair;
221 LinkEntry link_entry;
222
223 src_dest_pair.first = src;
224 src_dest_pair.second = dest;
225 link_entry.direction = dir;
226 link_entry.link = link;
227 m_link_map[src_dest_pair] = link_entry;
228}
229
230void
231Topology::makeLink(Network *net, SwitchID src, SwitchID dest,
232 const NetDest& routing_table_entry, bool isReconfiguration)
233{
234 // Make sure we're not trying to connect two end-point nodes
235 // directly together
236 assert(src >= 2 * m_nodes || dest >= 2 * m_nodes);
237
238 std::pair<int, int> src_dest;
239 LinkEntry link_entry;
240
241 if (src < m_nodes) {
242 src_dest.first = src;
243 src_dest.second = dest;
244 link_entry = m_link_map[src_dest];
245 net->makeInLink(src, dest - (2 * m_nodes), link_entry.link,
246 link_entry.direction,
247 routing_table_entry,
248 isReconfiguration);
249 } else if (dest < 2*m_nodes) {
250 assert(dest >= m_nodes);
251 NodeID node = dest - m_nodes;
252 src_dest.first = src;
253 src_dest.second = dest;
254 link_entry = m_link_map[src_dest];
255 net->makeOutLink(src - (2 * m_nodes), node, link_entry.link,
256 link_entry.direction,
257 routing_table_entry,
258 isReconfiguration);
259 } else {
260 assert((src >= 2 * m_nodes) && (dest >= 2 * m_nodes));
261 src_dest.first = src;
262 src_dest.second = dest;
263 link_entry = m_link_map[src_dest];
264 net->makeInternalLink(src - (2 * m_nodes), dest - (2 * m_nodes),
265 link_entry.link, link_entry.direction,
266 routing_table_entry, isReconfiguration);
267 }
268}
269
270void
271Topology::printStats(std::ostream& out) const
272{
273 for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
274 m_controller_vector[cntrl]->printStats(out);
275 }
276}
277
278void
279Topology::clearStats()
280{
281 for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
282 m_controller_vector[cntrl]->clearStats();
283 }
284}
285
286// The following all-pairs shortest path algorithm is based on the
287// discussion from Cormen et al., Chapter 26.1.
288void
289extend_shortest_path(Matrix& current_dist, Matrix& latencies,
290 Matrix& inter_switches)
291{
292 bool change = true;
293 int nodes = current_dist.size();
294
295 while (change) {
296 change = false;
297 for (int i = 0; i < nodes; i++) {
298 for (int j = 0; j < nodes; j++) {
299 int minimum = current_dist[i][j];
300 int previous_minimum = minimum;
301 int intermediate_switch = -1;
302 for (int k = 0; k < nodes; k++) {
303 minimum = min(minimum,
304 current_dist[i][k] + current_dist[k][j]);
305 if (previous_minimum != minimum) {
306 intermediate_switch = k;
307 inter_switches[i][j] =
308 inter_switches[i][k] +
309 inter_switches[k][j] + 1;
310 }
311 previous_minimum = minimum;
312 }
313 if (current_dist[i][j] != minimum) {
314 change = true;
315 current_dist[i][j] = minimum;
316 assert(intermediate_switch >= 0);
317 assert(intermediate_switch < latencies[i].size());
318 latencies[i][j] = latencies[i][intermediate_switch] +
319 latencies[intermediate_switch][j];
320 }
321 }
322 }
323 }
324}
325
326Matrix
327shortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches)
328{
329 Matrix dist = weights;
330 extend_shortest_path(dist, latencies, inter_switches);
331 return dist;
332}
333
334bool
335link_is_shortest_path_to_node(SwitchID src, SwitchID next, SwitchID final,
336 const Matrix& weights, const Matrix& dist)
337{
338 return weights[src][next] + dist[next][final] == dist[src][final];
339}
340
341NetDest
342shortest_path_to_node(SwitchID src, SwitchID next, const Matrix& weights,
343 const Matrix& dist)
344{
345 NetDest result;
346 int d = 0;
347 int machines;
348 int max_machines;
349
350 machines = MachineType_NUM;
351 max_machines = MachineType_base_number(MachineType_NUM);
352
353 for (int m = 0; m < machines; m++) {
354 for (int i = 0; i < MachineType_base_count((MachineType)m); i++) {
355 // we use "d+max_machines" below since the "destination"
356 // switches for the machines are numbered
357 // [MachineType_base_number(MachineType_NUM)...
358 // 2*MachineType_base_number(MachineType_NUM)-1] for the
359 // component network
360 if (link_is_shortest_path_to_node(src, next, d + max_machines,
361 weights, dist)) {
362 MachineID mach = {(MachineType)m, i};
363 result.add(mach);
364 }
365 d++;
366 }
367 }
368
369 DPRINTF(RubyNetwork, "Returning shortest path\n"
370 "(src-(2*max_machines)): %d, (next-(2*max_machines)): %d, "
371 "src: %d, next: %d, result: %s\n",
372 (src-(2*max_machines)), (next-(2*max_machines)),
373 src, next, result);
374
375 return result;
376}
377
378Topology *
379TopologyParams::create()
380{
381 return new Topology(this);
382}
383
43// Note: In this file, we use the first 2*m_nodes SwitchIDs to
44// represent the input and output endpoint links. These really are
45// not 'switches', as they will not have a Switch object allocated for
46// them. The first m_nodes SwitchIDs are the links into the network,
47// the second m_nodes set of SwitchIDs represent the the output queues
48// of the network.
49
50// Helper functions based on chapter 29 of Cormen et al.
51void extend_shortest_path(Matrix& current_dist, Matrix& latencies,
52 Matrix& inter_switches);
53Matrix shortest_path(const Matrix& weights, Matrix& latencies,
54 Matrix& inter_switches);
55bool link_is_shortest_path_to_node(SwitchID src, SwitchID next,
56 SwitchID final, const Matrix& weights, const Matrix& dist);
57NetDest shortest_path_to_node(SwitchID src, SwitchID next,
58 const Matrix& weights, const Matrix& dist);
59
60Topology::Topology(const Params *p)
61 : SimObject(p)
62{
63 m_print_config = p->print_config;
64 m_number_of_switches = p->routers.size();
65
66 // initialize component latencies record
67 m_component_latencies.resize(0);
68 m_component_inter_switches.resize(0);
69
70 // Total nodes/controllers in network
71 // Must make sure this is called after the State Machine constructors
72 m_nodes = MachineType_base_number(MachineType_NUM);
73 assert(m_nodes > 1);
74
75 if (m_nodes != params()->ext_links.size() &&
76 m_nodes != params()->ext_links.size()) {
77 fatal("m_nodes (%d) != ext_links vector length (%d)\n",
78 m_nodes, params()->ext_links.size());
79 }
80
81 // analyze both the internal and external links, create data structures
82 // Note that the python created links are bi-directional, but that the
83 // topology and networks utilize uni-directional links. Thus each
84 // BasicLink is converted to two calls to add link, on for each direction
85 for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin();
86 i != params()->ext_links.end(); ++i) {
87 BasicExtLink *ext_link = (*i);
88 AbstractController *abs_cntrl = ext_link->params()->ext_node;
89 BasicRouter *router = ext_link->params()->int_node;
90
91 // Store the controller and ExtLink pointers for later
92 m_controller_vector.push_back(abs_cntrl);
93 m_ext_link_vector.push_back(ext_link);
94
95 int ext_idx1 = abs_cntrl->params()->cntrl_id;
96 int ext_idx2 = ext_idx1 + m_nodes;
97 int int_idx = router->params()->router_id + 2*m_nodes;
98
99 // create the internal uni-directional links in both directions
100 // the first direction is marked: In
101 addLink(ext_idx1, int_idx, ext_link, LinkDirection_In);
102 // the first direction is marked: Out
103 addLink(int_idx, ext_idx2, ext_link, LinkDirection_Out);
104 }
105
106 for (vector<BasicIntLink*>::const_iterator i = params()->int_links.begin();
107 i != params()->int_links.end(); ++i) {
108 BasicIntLink *int_link = (*i);
109 BasicRouter *router_a = int_link->params()->node_a;
110 BasicRouter *router_b = int_link->params()->node_b;
111
112 // Store the IntLink pointers for later
113 m_int_link_vector.push_back(int_link);
114
115 int a = router_a->params()->router_id + 2*m_nodes;
116 int b = router_b->params()->router_id + 2*m_nodes;
117
118 // create the internal uni-directional links in both directions
119 // the first direction is marked: In
120 addLink(a, b, int_link, LinkDirection_In);
121 // the second direction is marked: Out
122 addLink(b, a, int_link, LinkDirection_Out);
123 }
124}
125
126void
127Topology::init()
128{
129}
130
131
132void
133Topology::initNetworkPtr(Network* net_ptr)
134{
135 for (vector<BasicExtLink*>::const_iterator i = params()->ext_links.begin();
136 i != params()->ext_links.end(); ++i) {
137 BasicExtLink *ext_link = (*i);
138 AbstractController *abs_cntrl = ext_link->params()->ext_node;
139 abs_cntrl->initNetworkPtr(net_ptr);
140 }
141}
142
143void
144Topology::createLinks(Network *net, bool isReconfiguration)
145{
146 // Find maximum switchID
147 SwitchID max_switch_id = 0;
148 for (LinkMap::const_iterator i = m_link_map.begin();
149 i != m_link_map.end(); ++i) {
150 std::pair<int, int> src_dest = (*i).first;
151 max_switch_id = max(max_switch_id, src_dest.first);
152 max_switch_id = max(max_switch_id, src_dest.second);
153 }
154
155 // Initialize weight, latency, and inter switched vectors
156 Matrix topology_weights;
157 int num_switches = max_switch_id+1;
158 topology_weights.resize(num_switches);
159 m_component_latencies.resize(num_switches);
160 m_component_inter_switches.resize(num_switches);
161
162 for (int i = 0; i < topology_weights.size(); i++) {
163 topology_weights[i].resize(num_switches);
164 m_component_latencies[i].resize(num_switches);
165 m_component_inter_switches[i].resize(num_switches);
166
167 for (int j = 0; j < topology_weights[i].size(); j++) {
168 topology_weights[i][j] = INFINITE_LATENCY;
169
170 // initialize to invalid values
171 m_component_latencies[i][j] = -1;
172
173 // initially assume direct connections / no intermediate
174 // switches between components
175 m_component_inter_switches[i][j] = 0;
176 }
177 }
178
179 // Set identity weights to zero
180 for (int i = 0; i < topology_weights.size(); i++) {
181 topology_weights[i][i] = 0;
182 }
183
184 // Fill in the topology weights and bandwidth multipliers
185 for (LinkMap::const_iterator i = m_link_map.begin();
186 i != m_link_map.end(); ++i) {
187 std::pair<int, int> src_dest = (*i).first;
188 BasicLink* link = (*i).second.link;
189 int src = src_dest.first;
190 int dst = src_dest.second;
191 m_component_latencies[src][dst] = link->m_latency;
192 topology_weights[src][dst] = link->m_weight;
193 }
194
195 // Walk topology and hookup the links
196 Matrix dist = shortest_path(topology_weights, m_component_latencies,
197 m_component_inter_switches);
198 for (int i = 0; i < topology_weights.size(); i++) {
199 for (int j = 0; j < topology_weights[i].size(); j++) {
200 int weight = topology_weights[i][j];
201 if (weight > 0 && weight != INFINITE_LATENCY) {
202 NetDest destination_set = shortest_path_to_node(i, j,
203 topology_weights, dist);
204 makeLink(net, i, j, destination_set, isReconfiguration);
205 }
206 }
207 }
208}
209
210void
211Topology::addLink(SwitchID src, SwitchID dest, BasicLink* link,
212 LinkDirection dir)
213{
214 assert(src <= m_number_of_switches+m_nodes+m_nodes);
215 assert(dest <= m_number_of_switches+m_nodes+m_nodes);
216
217 std::pair<int, int> src_dest_pair;
218 LinkEntry link_entry;
219
220 src_dest_pair.first = src;
221 src_dest_pair.second = dest;
222 link_entry.direction = dir;
223 link_entry.link = link;
224 m_link_map[src_dest_pair] = link_entry;
225}
226
227void
228Topology::makeLink(Network *net, SwitchID src, SwitchID dest,
229 const NetDest& routing_table_entry, bool isReconfiguration)
230{
231 // Make sure we're not trying to connect two end-point nodes
232 // directly together
233 assert(src >= 2 * m_nodes || dest >= 2 * m_nodes);
234
235 std::pair<int, int> src_dest;
236 LinkEntry link_entry;
237
238 if (src < m_nodes) {
239 src_dest.first = src;
240 src_dest.second = dest;
241 link_entry = m_link_map[src_dest];
242 net->makeInLink(src, dest - (2 * m_nodes), link_entry.link,
243 link_entry.direction,
244 routing_table_entry,
245 isReconfiguration);
246 } else if (dest < 2*m_nodes) {
247 assert(dest >= m_nodes);
248 NodeID node = dest - m_nodes;
249 src_dest.first = src;
250 src_dest.second = dest;
251 link_entry = m_link_map[src_dest];
252 net->makeOutLink(src - (2 * m_nodes), node, link_entry.link,
253 link_entry.direction,
254 routing_table_entry,
255 isReconfiguration);
256 } else {
257 assert((src >= 2 * m_nodes) && (dest >= 2 * m_nodes));
258 src_dest.first = src;
259 src_dest.second = dest;
260 link_entry = m_link_map[src_dest];
261 net->makeInternalLink(src - (2 * m_nodes), dest - (2 * m_nodes),
262 link_entry.link, link_entry.direction,
263 routing_table_entry, isReconfiguration);
264 }
265}
266
267void
268Topology::printStats(std::ostream& out) const
269{
270 for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
271 m_controller_vector[cntrl]->printStats(out);
272 }
273}
274
275void
276Topology::clearStats()
277{
278 for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
279 m_controller_vector[cntrl]->clearStats();
280 }
281}
282
283// The following all-pairs shortest path algorithm is based on the
284// discussion from Cormen et al., Chapter 26.1.
285void
286extend_shortest_path(Matrix& current_dist, Matrix& latencies,
287 Matrix& inter_switches)
288{
289 bool change = true;
290 int nodes = current_dist.size();
291
292 while (change) {
293 change = false;
294 for (int i = 0; i < nodes; i++) {
295 for (int j = 0; j < nodes; j++) {
296 int minimum = current_dist[i][j];
297 int previous_minimum = minimum;
298 int intermediate_switch = -1;
299 for (int k = 0; k < nodes; k++) {
300 minimum = min(minimum,
301 current_dist[i][k] + current_dist[k][j]);
302 if (previous_minimum != minimum) {
303 intermediate_switch = k;
304 inter_switches[i][j] =
305 inter_switches[i][k] +
306 inter_switches[k][j] + 1;
307 }
308 previous_minimum = minimum;
309 }
310 if (current_dist[i][j] != minimum) {
311 change = true;
312 current_dist[i][j] = minimum;
313 assert(intermediate_switch >= 0);
314 assert(intermediate_switch < latencies[i].size());
315 latencies[i][j] = latencies[i][intermediate_switch] +
316 latencies[intermediate_switch][j];
317 }
318 }
319 }
320 }
321}
322
323Matrix
324shortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches)
325{
326 Matrix dist = weights;
327 extend_shortest_path(dist, latencies, inter_switches);
328 return dist;
329}
330
331bool
332link_is_shortest_path_to_node(SwitchID src, SwitchID next, SwitchID final,
333 const Matrix& weights, const Matrix& dist)
334{
335 return weights[src][next] + dist[next][final] == dist[src][final];
336}
337
338NetDest
339shortest_path_to_node(SwitchID src, SwitchID next, const Matrix& weights,
340 const Matrix& dist)
341{
342 NetDest result;
343 int d = 0;
344 int machines;
345 int max_machines;
346
347 machines = MachineType_NUM;
348 max_machines = MachineType_base_number(MachineType_NUM);
349
350 for (int m = 0; m < machines; m++) {
351 for (int i = 0; i < MachineType_base_count((MachineType)m); i++) {
352 // we use "d+max_machines" below since the "destination"
353 // switches for the machines are numbered
354 // [MachineType_base_number(MachineType_NUM)...
355 // 2*MachineType_base_number(MachineType_NUM)-1] for the
356 // component network
357 if (link_is_shortest_path_to_node(src, next, d + max_machines,
358 weights, dist)) {
359 MachineID mach = {(MachineType)m, i};
360 result.add(mach);
361 }
362 d++;
363 }
364 }
365
366 DPRINTF(RubyNetwork, "Returning shortest path\n"
367 "(src-(2*max_machines)): %d, (next-(2*max_machines)): %d, "
368 "src: %d, next: %d, result: %s\n",
369 (src-(2*max_machines)), (next-(2*max_machines)),
370 src, next, result);
371
372 return result;
373}
374
375Topology *
376TopologyParams::create()
377{
378 return new Topology(this);
379}
380