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