SimpleNetwork.cc revision 7055:4e24742201d7
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/Map.hh"
30#include "mem/protocol/MachineType.hh"
31#include "mem/protocol/Protocol.hh"
32#include "mem/protocol/TopologyType.hh"
33#include "mem/ruby/buffers/MessageBuffer.hh"
34#include "mem/ruby/common/NetDest.hh"
35#include "mem/ruby/network/simple/SimpleNetwork.hh"
36#include "mem/ruby/network/simple/Switch.hh"
37#include "mem/ruby/network/simple/Topology.hh"
38#include "mem/ruby/profiler/Profiler.hh"
39#include "mem/ruby/system/System.hh"
40
41using namespace std;
42
43#if 0
44// ***BIG HACK*** - This is actually code that _should_ be in Network.cc
45
46// Note: Moved to Princeton Network
47// calls new to abstract away from the network
48Network*
49Network::createNetwork(int nodes)
50{
51    return new SimpleNetwork(nodes);
52}
53#endif
54
55SimpleNetwork::SimpleNetwork(const Params *p)
56    : Network(p)
57{
58    // Note: the parent Network Object constructor is called before the
59    // SimpleNetwork child constructor.  Therefore, the member variables
60    // used below should already be initialized.
61
62    m_endpoint_switches.setSize(m_nodes);
63
64    m_in_use.setSize(m_virtual_networks);
65    m_ordered.setSize(m_virtual_networks);
66    for (int i = 0; i < m_virtual_networks; i++) {
67        m_in_use[i] = false;
68        m_ordered[i] = false;
69    }
70
71    // Allocate to and from queues
72    m_toNetQueues.setSize(m_nodes);
73    m_fromNetQueues.setSize(m_nodes);
74    for (int node = 0; node < m_nodes; node++) {
75        m_toNetQueues[node].setSize(m_virtual_networks);
76        m_fromNetQueues[node].setSize(m_virtual_networks);
77        for (int j = 0; j < m_virtual_networks; j++) {
78            m_toNetQueues[node][j] = new MessageBuffer(
79                "toNet node "+int_to_string(node)+" j "+int_to_string(j));
80            m_fromNetQueues[node][j] = new MessageBuffer(
81                "fromNet node "+int_to_string(node)+" j "+int_to_string(j));
82        }
83    }
84}
85
86void
87SimpleNetwork::init()
88{
89    Network::init();
90
91    // The topology pointer should have already been initialized in
92    // the parent class network constructor.
93    assert(m_topology_ptr != NULL);
94    int number_of_switches = m_topology_ptr->numSwitches();
95    for (int i = 0; i < number_of_switches; i++) {
96        m_switch_ptr_vector.insertAtBottom(new Switch(i, this));
97    }
98
99    // false because this isn't a reconfiguration
100    m_topology_ptr->createLinks(this, false);
101}
102
103void
104SimpleNetwork::reset()
105{
106    for (int node = 0; node < m_nodes; node++) {
107        for (int j = 0; j < m_virtual_networks; j++) {
108            m_toNetQueues[node][j]->clear();
109            m_fromNetQueues[node][j]->clear();
110        }
111    }
112
113    for(int i = 0; i < m_switch_ptr_vector.size(); i++){
114        m_switch_ptr_vector[i]->clearBuffers();
115    }
116}
117
118SimpleNetwork::~SimpleNetwork()
119{
120    for (int i = 0; i < m_nodes; i++) {
121        m_toNetQueues[i].deletePointers();
122        m_fromNetQueues[i].deletePointers();
123    }
124    m_switch_ptr_vector.deletePointers();
125    m_buffers_to_free.deletePointers();
126    // delete m_topology_ptr;
127}
128
129// From a switch to an endpoint node
130void
131SimpleNetwork::makeOutLink(SwitchID src, NodeID dest,
132    const NetDest& routing_table_entry, int link_latency, int link_weight,
133    int bw_multiplier, bool isReconfiguration)
134{
135    assert(dest < m_nodes);
136    assert(src < m_switch_ptr_vector.size());
137    assert(m_switch_ptr_vector[src] != NULL);
138
139    if (isReconfiguration) {
140        m_switch_ptr_vector[src]->reconfigureOutPort(routing_table_entry);
141        return;
142    }
143
144    m_switch_ptr_vector[src]->addOutPort(m_fromNetQueues[dest],
145        routing_table_entry, link_latency, bw_multiplier);
146    m_endpoint_switches[dest] = m_switch_ptr_vector[src];
147}
148
149// From an endpoint node to a switch
150void
151SimpleNetwork::makeInLink(NodeID src, SwitchID dest,
152    const NetDest& routing_table_entry, int link_latency, int bw_multiplier,
153    bool isReconfiguration)
154{
155    assert(src < m_nodes);
156    if (isReconfiguration) {
157        // do nothing
158        return;
159    }
160
161    m_switch_ptr_vector[dest]->addInPort(m_toNetQueues[src]);
162}
163
164// From a switch to a switch
165void
166SimpleNetwork::makeInternalLink(SwitchID src, SwitchID dest,
167    const NetDest& routing_table_entry, int link_latency, int link_weight,
168    int bw_multiplier, bool isReconfiguration)
169{
170    if (isReconfiguration) {
171        m_switch_ptr_vector[src]->reconfigureOutPort(routing_table_entry);
172        return;
173    }
174
175    // Create a set of new MessageBuffers
176    Vector<MessageBuffer*> queues;
177    for (int i = 0; i < m_virtual_networks; i++) {
178        // allocate a buffer
179        MessageBuffer* buffer_ptr = new MessageBuffer;
180        buffer_ptr->setOrdering(true);
181        if (m_buffer_size > 0) {
182            buffer_ptr->setSize(m_buffer_size);
183        }
184        queues.insertAtBottom(buffer_ptr);
185        // remember to deallocate it
186        m_buffers_to_free.insertAtBottom(buffer_ptr);
187    }
188    // Connect it to the two switches
189    m_switch_ptr_vector[dest]->addInPort(queues);
190    m_switch_ptr_vector[src]->addOutPort(queues, routing_table_entry,
191        link_latency, bw_multiplier);
192}
193
194void
195SimpleNetwork::checkNetworkAllocation(NodeID id, bool ordered, int network_num)
196{
197    ASSERT(id < m_nodes);
198    ASSERT(network_num < m_virtual_networks);
199
200    if (ordered) {
201        m_ordered[network_num] = true;
202    }
203    m_in_use[network_num] = true;
204}
205
206MessageBuffer*
207SimpleNetwork::getToNetQueue(NodeID id, bool ordered, int network_num)
208{
209    checkNetworkAllocation(id, ordered, network_num);
210    return m_toNetQueues[id][network_num];
211}
212
213MessageBuffer*
214SimpleNetwork::getFromNetQueue(NodeID id, bool ordered, int network_num)
215{
216    checkNetworkAllocation(id, ordered, network_num);
217    return m_fromNetQueues[id][network_num];
218}
219
220const Vector<Throttle*>*
221SimpleNetwork::getThrottles(NodeID id) const
222{
223    assert(id >= 0);
224    assert(id < m_nodes);
225    assert(m_endpoint_switches[id] != NULL);
226    return m_endpoint_switches[id]->getThrottles();
227}
228
229void
230SimpleNetwork::printStats(ostream& out) const
231{
232    out << endl;
233    out << "Network Stats" << endl;
234    out << "-------------" << endl;
235    out << endl;
236    for (int i = 0; i < m_switch_ptr_vector.size(); i++) {
237        m_switch_ptr_vector[i]->printStats(out);
238    }
239    m_topology_ptr->printStats(out);
240}
241
242void
243SimpleNetwork::clearStats()
244{
245    for (int i = 0; i < m_switch_ptr_vector.size(); i++) {
246        m_switch_ptr_vector[i]->clearStats();
247    }
248    m_topology_ptr->clearStats();
249}
250
251void
252SimpleNetwork::printConfig(ostream& out) const
253{
254    out << endl;
255    out << "Network Configuration" << endl;
256    out << "---------------------" << endl;
257    out << "network: SIMPLE_NETWORK" << endl;
258    out << "topology: " << m_topology_ptr->getName() << endl;
259    out << endl;
260
261    for (int i = 0; i < m_virtual_networks; i++) {
262        out << "virtual_net_" << i << ": ";
263        if (m_in_use[i]) {
264            out << "active, ";
265            if (m_ordered[i]) {
266                out << "ordered" << endl;
267            } else {
268                out << "unordered" << endl;
269            }
270        } else {
271            out << "inactive" << endl;
272        }
273    }
274    out << endl;
275
276    for(int i = 0; i < m_switch_ptr_vector.size(); i++) {
277        m_switch_ptr_vector[i]->printConfig(out);
278    }
279
280    m_topology_ptr->printConfig(out);
281}
282
283void
284SimpleNetwork::print(ostream& out) const
285{
286    out << "[SimpleNetwork]";
287}
288
289
290SimpleNetwork *
291SimpleNetworkParams::create()
292{
293    return new SimpleNetwork(this);
294}
295