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