Topology.cc revision 7055
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 "mem/gems_common/util.hh"
30#include "mem/protocol/MachineType.hh"
31#include "mem/protocol/Protocol.hh"
32#include "mem/protocol/TopologyType.hh"
33#include "mem/ruby/common/NetDest.hh"
34#include "mem/ruby/network/Network.hh"
35#include "mem/ruby/network/simple/Topology.hh"
36#include "mem/ruby/slicc_interface/AbstractController.hh"
37#include "mem/ruby/system/System.hh"
38
39using namespace std;
40
41const int INFINITE_LATENCY = 10000; // Yes, this is a big hack
42const int DEFAULT_BW_MULTIPLIER = 1;  // Just to be consistent with above :)
43
44// Note: In this file, we use the first 2*m_nodes SwitchIDs to
45// represent the input and output endpoint links.  These really are
46// not 'switches', as they will not have a Switch object allocated for
47// them. The first m_nodes SwitchIDs are the links into the network,
48// the second m_nodes set of SwitchIDs represent the the output queues
49// of the network.
50
51// Helper functions based on chapter 29 of Cormen et al.
52void extend_shortest_path(Matrix& current_dist, Matrix& latencies,
53    Matrix& inter_switches);
54Matrix shortest_path(const Matrix& weights, Matrix& latencies,
55    Matrix& inter_switches);
56bool link_is_shortest_path_to_node(SwitchID src, SwitchID next,
57    SwitchID final, const Matrix& weights, const Matrix& dist);
58NetDest shortest_path_to_node(SwitchID src, SwitchID next,
59    const Matrix& weights, const Matrix& dist);
60
61Topology::Topology(const Params *p)
62    : SimObject(p)
63{
64    m_print_config = p->print_config;
65    m_number_of_switches = p->num_int_nodes;
66    // initialize component latencies record
67    m_component_latencies.setSize(0);
68    m_component_inter_switches.setSize(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    // First create the links between the endpoints (i.e. controllers)
82    // and the network.
83    for (vector<ExtLink*>::const_iterator i = params()->ext_links.begin();
84         i != params()->ext_links.end(); ++i) {
85        const ExtLinkParams *p = (*i)->params();
86        AbstractController *c = p->ext_node;
87
88        // Store the controller pointers for later
89        m_controller_vector.insertAtBottom(c);
90
91        int ext_idx1 =
92            MachineType_base_number(c->getMachineType()) + c->getVersion();
93        int ext_idx2 = ext_idx1 + m_nodes;
94        int int_idx = p->int_node + 2*m_nodes;
95
96        // create the links in both directions
97        addLink(ext_idx1, int_idx, p->latency, p->bw_multiplier, p->weight);
98        addLink(int_idx, ext_idx2, p->latency, p->bw_multiplier, p->weight);
99    }
100
101    for (vector<IntLink*>::const_iterator i = params()->int_links.begin();
102         i != params()->int_links.end(); ++i) {
103        const IntLinkParams *p = (*i)->params();
104        int a = p->node_a + 2*m_nodes;
105        int b = p->node_b + 2*m_nodes;
106
107        // create the links in both directions
108        addLink(a, b, p->latency, p->bw_multiplier, p->weight);
109        addLink(b, a, p->latency, p->bw_multiplier, p->weight);
110    }
111}
112
113
114void
115Topology::initNetworkPtr(Network* net_ptr)
116{
117    for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
118        m_controller_vector[cntrl]->initNetworkPtr(net_ptr);
119    }
120}
121
122void
123Topology::createLinks(Network *net, bool isReconfiguration)
124{
125    // Find maximum switchID
126    SwitchID max_switch_id = 0;
127    for (int i = 0; i < m_links_src_vector.size(); i++) {
128        max_switch_id = max(max_switch_id, m_links_src_vector[i]);
129        max_switch_id = max(max_switch_id, m_links_dest_vector[i]);
130    }
131
132    // Initialize weight vector
133    Matrix topology_weights;
134    Matrix topology_latency;
135    Matrix topology_bw_multis;
136    int num_switches = max_switch_id+1;
137    topology_weights.setSize(num_switches);
138    topology_latency.setSize(num_switches);
139    topology_bw_multis.setSize(num_switches);
140
141    // FIXME setting the size of a member variable here is a HACK!
142    m_component_latencies.setSize(num_switches);
143
144    // FIXME setting the size of a member variable here is a HACK!
145    m_component_inter_switches.setSize(num_switches);
146
147    for (int i = 0; i < topology_weights.size(); i++) {
148        topology_weights[i].setSize(num_switches);
149        topology_latency[i].setSize(num_switches);
150        topology_bw_multis[i].setSize(num_switches);
151        m_component_latencies[i].setSize(num_switches);
152
153        // FIXME setting the size of a member variable here is a HACK!
154        m_component_inter_switches[i].setSize(num_switches);
155
156        for (int j = 0; j < topology_weights[i].size(); j++) {
157            topology_weights[i][j] = INFINITE_LATENCY;
158
159            // initialize to invalid values
160            topology_latency[i][j] = -1;
161            topology_bw_multis[i][j] = -1;
162            m_component_latencies[i][j] = -1;
163
164            // initially assume direct connections / no intermediate
165            // switches between components
166            m_component_inter_switches[i][j] = 0;
167        }
168    }
169
170    // Set identity weights to zero
171    for (int i = 0; i < topology_weights.size(); i++) {
172        topology_weights[i][i] = 0;
173    }
174
175    // Fill in the topology weights and bandwidth multipliers
176    for (int i = 0; i < m_links_src_vector.size(); i++) {
177        int src = m_links_src_vector[i];
178        int dst = m_links_dest_vector[i];
179        topology_weights[src][dst] = m_links_weight_vector[i];
180        topology_latency[src][dst] = m_links_latency_vector[i];
181        m_component_latencies[src][dst] = m_links_latency_vector[i];
182        topology_bw_multis[src][dst] = m_bw_multiplier_vector[i];
183    }
184
185    // Walk topology and hookup the links
186    Matrix dist = shortest_path(topology_weights, m_component_latencies,
187        m_component_inter_switches);
188    for (int i = 0; i < topology_weights.size(); i++) {
189        for (int j = 0; j < topology_weights[i].size(); j++) {
190            int weight = topology_weights[i][j];
191            int bw_multiplier = topology_bw_multis[i][j];
192            int latency = topology_latency[i][j];
193            if (weight > 0 && weight != INFINITE_LATENCY) {
194                NetDest destination_set = shortest_path_to_node(i, j,
195                    topology_weights, dist);
196                assert(latency != -1);
197                makeLink(net, i, j, destination_set, latency, weight,
198                    bw_multiplier, isReconfiguration);
199            }
200        }
201    }
202}
203
204SwitchID
205Topology::newSwitchID()
206{
207    m_number_of_switches++;
208    return m_number_of_switches-1+m_nodes+m_nodes;
209}
210
211void
212Topology::addLink(SwitchID src, SwitchID dest, int link_latency)
213{
214    addLink(src, dest, link_latency, DEFAULT_BW_MULTIPLIER, link_latency);
215}
216
217void
218Topology::addLink(SwitchID src, SwitchID dest, int link_latency,
219    int bw_multiplier)
220{
221    addLink(src, dest, link_latency, bw_multiplier, link_latency);
222}
223
224void
225Topology::addLink(SwitchID src, SwitchID dest, int link_latency,
226    int bw_multiplier, int link_weight)
227{
228    ASSERT(src <= m_number_of_switches+m_nodes+m_nodes);
229    ASSERT(dest <= m_number_of_switches+m_nodes+m_nodes);
230    m_links_src_vector.insertAtBottom(src);
231    m_links_dest_vector.insertAtBottom(dest);
232    m_links_latency_vector.insertAtBottom(link_latency);
233    m_links_weight_vector.insertAtBottom(link_weight);
234    m_bw_multiplier_vector.insertAtBottom(bw_multiplier);
235}
236
237void
238Topology::makeLink(Network *net, SwitchID src, SwitchID dest,
239    const NetDest& routing_table_entry, int link_latency, int link_weight,
240    int bw_multiplier, bool isReconfiguration)
241{
242    // Make sure we're not trying to connect two end-point nodes
243    // directly together
244    assert(src >= 2 * m_nodes || dest >= 2 * m_nodes);
245
246    if (src < m_nodes) {
247        net->makeInLink(src, dest-(2*m_nodes), routing_table_entry,
248            link_latency, bw_multiplier, isReconfiguration);
249    } else if (dest < 2*m_nodes) {
250        assert(dest >= m_nodes);
251        NodeID node = dest-m_nodes;
252        net->makeOutLink(src-(2*m_nodes), node, routing_table_entry,
253            link_latency, link_weight, bw_multiplier, isReconfiguration);
254    } else {
255        assert((src >= 2*m_nodes) && (dest >= 2*m_nodes));
256        net->makeInternalLink(src-(2*m_nodes), dest-(2*m_nodes),
257            routing_table_entry, link_latency, link_weight, bw_multiplier,
258            isReconfiguration);
259    }
260}
261
262void
263Topology::printStats(std::ostream& out) const
264{
265    for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
266        m_controller_vector[cntrl]->printStats(out);
267    }
268}
269
270void
271Topology::clearStats()
272{
273    for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
274        m_controller_vector[cntrl]->clearStats();
275    }
276}
277
278void
279Topology::printConfig(std::ostream& out) const
280{
281    if (m_print_config == false)
282        return;
283
284    assert(m_component_latencies.size() > 0);
285
286    out << "--- Begin Topology Print ---" << endl
287        << endl
288        << "Topology print ONLY indicates the _NETWORK_ latency between two "
289        << "machines" << endl
290        << "It does NOT include the latency within the machines" << endl
291        << endl;
292
293    for (int m = 0; m < MachineType_NUM; m++) {
294        int i_end = MachineType_base_count((MachineType)m);
295        for (int i = 0; i < i_end; i++) {
296            MachineID cur_mach = {(MachineType)m, i};
297            out << cur_mach << " Network Latencies" << endl;
298            for (int n = 0; n < MachineType_NUM; n++) {
299                int j_end = MachineType_base_count((MachineType)n);
300                for (int j = 0; j < j_end; j++) {
301                    MachineID dest_mach = {(MachineType)n, j};
302                    if (cur_mach == dest_mach)
303                        continue;
304
305                    int src = MachineType_base_number((MachineType)m) + i;
306                    int dst = MachineType_base_number(MachineType_NUM) +
307                        MachineType_base_number((MachineType)n) + j;
308                    int link_latency = m_component_latencies[src][dst];
309                    int intermediate_switches =
310                        m_component_inter_switches[src][dst];
311
312                    // NOTE switches are assumed to have single
313                    // cycle latency
314                    out << "  " << cur_mach << " -> " << dest_mach
315                        << " net_lat: "
316                        << link_latency + intermediate_switches << endl;
317                }
318            }
319            out << endl;
320        }
321    }
322
323    out << "--- End Topology Print ---" << endl;
324}
325
326// The following all-pairs shortest path algorithm is based on the
327// discussion from Cormen et al., Chapter 26.1.
328void
329extend_shortest_path(Matrix& current_dist, Matrix& latencies,
330    Matrix& inter_switches)
331{
332    bool change = true;
333    int nodes = current_dist.size();
334
335    while (change) {
336        change = false;
337        for (int i = 0; i < nodes; i++) {
338            for (int j = 0; j < nodes; j++) {
339                int minimum = current_dist[i][j];
340                int previous_minimum = minimum;
341                int intermediate_switch = -1;
342                for (int k = 0; k < nodes; k++) {
343                    minimum = min(minimum,
344                        current_dist[i][k] + current_dist[k][j]);
345                    if (previous_minimum != minimum) {
346                        intermediate_switch = k;
347                        inter_switches[i][j] =
348                            inter_switches[i][k] +
349                            inter_switches[k][j] + 1;
350                    }
351                    previous_minimum = minimum;
352                }
353                if (current_dist[i][j] != minimum) {
354                    change = true;
355                    current_dist[i][j] = minimum;
356                    assert(intermediate_switch >= 0);
357                    assert(intermediate_switch < latencies[i].size());
358                    latencies[i][j] = latencies[i][intermediate_switch] +
359                        latencies[intermediate_switch][j];
360                }
361            }
362        }
363    }
364}
365
366Matrix
367shortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches)
368{
369    Matrix dist = weights;
370    extend_shortest_path(dist, latencies, inter_switches);
371    return dist;
372}
373
374bool
375link_is_shortest_path_to_node(SwitchID src, SwitchID next, SwitchID final,
376    const Matrix& weights, const Matrix& dist)
377{
378    return weights[src][next] + dist[next][final] == dist[src][final];
379}
380
381NetDest
382shortest_path_to_node(SwitchID src, SwitchID next, const Matrix& weights,
383    const Matrix& dist)
384{
385    NetDest result;
386    int d = 0;
387    int machines;
388    int max_machines;
389
390    machines = MachineType_NUM;
391    max_machines = MachineType_base_number(MachineType_NUM);
392
393    for (int m = 0; m < machines; m++) {
394        for (int i = 0; i < MachineType_base_count((MachineType)m); i++) {
395            // we use "d+max_machines" below since the "destination"
396            // switches for the machines are numbered
397            // [MachineType_base_number(MachineType_NUM)...
398            //  2*MachineType_base_number(MachineType_NUM)-1] for the
399            // component network
400            if (link_is_shortest_path_to_node(src, next, d + max_machines,
401                    weights, dist)) {
402                MachineID mach = {(MachineType)m, i};
403                result.add(mach);
404            }
405            d++;
406        }
407    }
408
409    DEBUG_MSG(NETWORK_COMP, MedPrio, "returning shortest path");
410    DEBUG_EXPR(NETWORK_COMP, MedPrio, (src-(2*max_machines)));
411    DEBUG_EXPR(NETWORK_COMP, MedPrio, (next-(2*max_machines)));
412    DEBUG_EXPR(NETWORK_COMP, MedPrio, src);
413    DEBUG_EXPR(NETWORK_COMP, MedPrio, next);
414    DEBUG_EXPR(NETWORK_COMP, MedPrio, result);
415    DEBUG_NEWLINE(NETWORK_COMP, MedPrio);
416
417    return result;
418}
419
420Topology *
421TopologyParams::create()
422{
423    return new Topology(this);
424}
425
426Link *
427LinkParams::create()
428{
429    return new Link(this);
430}
431
432ExtLink *
433ExtLinkParams::create()
434{
435    return new ExtLink(this);
436}
437
438IntLink *
439IntLinkParams::create()
440{
441    return new IntLink(this);
442}
443