Topology.cc revision 7054
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
39const int INFINITE_LATENCY = 10000; // Yes, this is a big hack
40const int DEFAULT_BW_MULTIPLIER = 1;  // Just to be consistent with above :)
41
42// Note: In this file, we use the first 2*m_nodes SwitchIDs to
43// represent the input and output endpoint links.  These really are
44// not 'switches', as they will not have a Switch object allocated for
45// them. The first m_nodes SwitchIDs are the links into the network,
46// the second m_nodes set of SwitchIDs represent the the output queues
47// of the network.
48
49// Helper functions based on chapter 29 of Cormen et al.
50void extend_shortest_path(Matrix& current_dist, Matrix& latencies,
51    Matrix& inter_switches);
52Matrix shortest_path(const Matrix& weights, Matrix& latencies,
53    Matrix& inter_switches);
54bool link_is_shortest_path_to_node(SwitchID src, SwitchID next,
55    SwitchID final, const Matrix& weights, const Matrix& dist);
56NetDest shortest_path_to_node(SwitchID src, SwitchID next,
57    const Matrix& weights, const Matrix& dist);
58
59Topology::Topology(const Params *p)
60    : SimObject(p)
61{
62    m_print_config = p->print_config;
63    m_number_of_switches = p->num_int_nodes;
64    // initialize component latencies record
65    m_component_latencies.setSize(0);
66    m_component_inter_switches.setSize(0);
67
68    // Total nodes/controllers in network
69    // Must make sure this is called after the State Machine constructors
70    m_nodes = MachineType_base_number(MachineType_NUM);
71    assert(m_nodes > 1);
72
73    if (m_nodes != params()->ext_links.size() &&
74        m_nodes != params()->ext_links.size()) {
75        fatal("m_nodes (%d) != ext_links vector length (%d)\n",
76            m_nodes != params()->ext_links.size());
77    }
78
79    // First create the links between the endpoints (i.e. controllers)
80    // and the network.
81    for (vector<ExtLink*>::const_iterator i = params()->ext_links.begin();
82         i != params()->ext_links.end(); ++i) {
83        const ExtLinkParams *p = (*i)->params();
84        AbstractController *c = p->ext_node;
85
86        // Store the controller pointers for later
87        m_controller_vector.insertAtBottom(c);
88
89        int ext_idx1 =
90            MachineType_base_number(c->getMachineType()) + c->getVersion();
91        int ext_idx2 = ext_idx1 + m_nodes;
92        int int_idx = p->int_node + 2*m_nodes;
93
94        // create the links in both directions
95        addLink(ext_idx1, int_idx, p->latency, p->bw_multiplier, p->weight);
96        addLink(int_idx, ext_idx2, p->latency, p->bw_multiplier, p->weight);
97    }
98
99    for (vector<IntLink*>::const_iterator i = params()->int_links.begin();
100         i != params()->int_links.end(); ++i) {
101        const IntLinkParams *p = (*i)->params();
102        int a = p->node_a + 2*m_nodes;
103        int b = p->node_b + 2*m_nodes;
104
105        // create the links in both directions
106        addLink(a, b, p->latency, p->bw_multiplier, p->weight);
107        addLink(b, a, p->latency, p->bw_multiplier, p->weight);
108    }
109}
110
111
112void
113Topology::initNetworkPtr(Network* net_ptr)
114{
115    for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
116        m_controller_vector[cntrl]->initNetworkPtr(net_ptr);
117    }
118}
119
120void
121Topology::createLinks(Network *net, bool isReconfiguration)
122{
123    // Find maximum switchID
124    SwitchID max_switch_id = 0;
125    for (int i = 0; i < m_links_src_vector.size(); i++) {
126        max_switch_id = max(max_switch_id, m_links_src_vector[i]);
127        max_switch_id = max(max_switch_id, m_links_dest_vector[i]);
128    }
129
130    // Initialize weight vector
131    Matrix topology_weights;
132    Matrix topology_latency;
133    Matrix topology_bw_multis;
134    int num_switches = max_switch_id+1;
135    topology_weights.setSize(num_switches);
136    topology_latency.setSize(num_switches);
137    topology_bw_multis.setSize(num_switches);
138
139    // FIXME setting the size of a member variable here is a HACK!
140    m_component_latencies.setSize(num_switches);
141
142    // FIXME setting the size of a member variable here is a HACK!
143    m_component_inter_switches.setSize(num_switches);
144
145    for (int i = 0; i < topology_weights.size(); i++) {
146        topology_weights[i].setSize(num_switches);
147        topology_latency[i].setSize(num_switches);
148        topology_bw_multis[i].setSize(num_switches);
149        m_component_latencies[i].setSize(num_switches);
150
151        // FIXME setting the size of a member variable here is a HACK!
152        m_component_inter_switches[i].setSize(num_switches);
153
154        for (int j = 0; j < topology_weights[i].size(); j++) {
155            topology_weights[i][j] = INFINITE_LATENCY;
156
157            // initialize to invalid values
158            topology_latency[i][j] = -1;
159            topology_bw_multis[i][j] = -1;
160            m_component_latencies[i][j] = -1;
161
162            // initially assume direct connections / no intermediate
163            // switches between components
164            m_component_inter_switches[i][j] = 0;
165        }
166    }
167
168    // Set identity weights to zero
169    for (int i = 0; i < topology_weights.size(); i++) {
170        topology_weights[i][i] = 0;
171    }
172
173    // Fill in the topology weights and bandwidth multipliers
174    for (int i = 0; i < m_links_src_vector.size(); i++) {
175        int src = m_links_src_vector[i];
176        int dst = m_links_dest_vector[i];
177        topology_weights[src][dst] = m_links_weight_vector[i];
178        topology_latency[src][dst] = m_links_latency_vector[i];
179        m_component_latencies[src][dst] = m_links_latency_vector[i];
180        topology_bw_multis[src][dst] = m_bw_multiplier_vector[i];
181    }
182
183    // Walk topology and hookup the links
184    Matrix dist = shortest_path(topology_weights, m_component_latencies,
185        m_component_inter_switches);
186    for (int i = 0; i < topology_weights.size(); i++) {
187        for (int j = 0; j < topology_weights[i].size(); j++) {
188            int weight = topology_weights[i][j];
189            int bw_multiplier = topology_bw_multis[i][j];
190            int latency = topology_latency[i][j];
191            if (weight > 0 && weight != INFINITE_LATENCY) {
192                NetDest destination_set = shortest_path_to_node(i, j,
193                    topology_weights, dist);
194                assert(latency != -1);
195                makeLink(net, i, j, destination_set, latency, weight,
196                    bw_multiplier, isReconfiguration);
197            }
198        }
199    }
200}
201
202SwitchID
203Topology::newSwitchID()
204{
205    m_number_of_switches++;
206    return m_number_of_switches-1+m_nodes+m_nodes;
207}
208
209void
210Topology::addLink(SwitchID src, SwitchID dest, int link_latency)
211{
212    addLink(src, dest, link_latency, DEFAULT_BW_MULTIPLIER, link_latency);
213}
214
215void
216Topology::addLink(SwitchID src, SwitchID dest, int link_latency,
217    int bw_multiplier)
218{
219    addLink(src, dest, link_latency, bw_multiplier, link_latency);
220}
221
222void
223Topology::addLink(SwitchID src, SwitchID dest, int link_latency,
224    int bw_multiplier, int link_weight)
225{
226    ASSERT(src <= m_number_of_switches+m_nodes+m_nodes);
227    ASSERT(dest <= m_number_of_switches+m_nodes+m_nodes);
228    m_links_src_vector.insertAtBottom(src);
229    m_links_dest_vector.insertAtBottom(dest);
230    m_links_latency_vector.insertAtBottom(link_latency);
231    m_links_weight_vector.insertAtBottom(link_weight);
232    m_bw_multiplier_vector.insertAtBottom(bw_multiplier);
233}
234
235void
236Topology::makeLink(Network *net, SwitchID src, SwitchID dest,
237    const NetDest& routing_table_entry, int link_latency, int link_weight,
238    int bw_multiplier, bool isReconfiguration)
239{
240    // Make sure we're not trying to connect two end-point nodes
241    // directly together
242    assert(src >= 2 * m_nodes || dest >= 2 * m_nodes);
243
244    if (src < m_nodes) {
245        net->makeInLink(src, dest-(2*m_nodes), routing_table_entry,
246            link_latency, bw_multiplier, isReconfiguration);
247    } else if (dest < 2*m_nodes) {
248        assert(dest >= m_nodes);
249        NodeID node = dest-m_nodes;
250        net->makeOutLink(src-(2*m_nodes), node, routing_table_entry,
251            link_latency, link_weight, bw_multiplier, isReconfiguration);
252    } else {
253        assert((src >= 2*m_nodes) && (dest >= 2*m_nodes));
254        net->makeInternalLink(src-(2*m_nodes), dest-(2*m_nodes),
255            routing_table_entry, link_latency, link_weight, bw_multiplier,
256            isReconfiguration);
257    }
258}
259
260void
261Topology::printStats(std::ostream& out) const
262{
263    for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
264        m_controller_vector[cntrl]->printStats(out);
265    }
266}
267
268void
269Topology::clearStats()
270{
271    for (int cntrl = 0; cntrl < m_controller_vector.size(); cntrl++) {
272        m_controller_vector[cntrl]->clearStats();
273    }
274}
275
276void
277Topology::printConfig(std::ostream& out) const
278{
279    using namespace std;
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