PerfectSwitch.cc revision 8608:02d7ac5fb855
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 <algorithm>
30
31#include "debug/RubyNetwork.hh"
32#include "mem/ruby/buffers/MessageBuffer.hh"
33#include "mem/ruby/network/simple/PerfectSwitch.hh"
34#include "mem/ruby/network/simple/SimpleNetwork.hh"
35#include "mem/ruby/profiler/Profiler.hh"
36#include "mem/ruby/slicc_interface/NetworkMessage.hh"
37#include "mem/ruby/system/System.hh"
38
39using namespace std;
40
41const int PRIORITY_SWITCH_LIMIT = 128;
42
43// Operator for helper class
44bool
45operator<(const LinkOrder& l1, const LinkOrder& l2)
46{
47    return (l1.m_value < l2.m_value);
48}
49
50PerfectSwitch::PerfectSwitch(SwitchID sid, SimpleNetwork* network_ptr)
51{
52    m_virtual_networks = network_ptr->getNumberOfVirtualNetworks();
53    m_switch_id = sid;
54    m_round_robin_start = 0;
55    m_network_ptr = network_ptr;
56    m_wakeups_wo_switch = 0;
57
58    for(int i = 0;i < m_virtual_networks;++i)
59    {
60        m_pending_message_count.push_back(0);
61    }
62}
63
64void
65PerfectSwitch::addInPort(const vector<MessageBuffer*>& in)
66{
67    assert(in.size() == m_virtual_networks);
68    NodeID port = m_in.size();
69    m_in.push_back(in);
70
71    for (int j = 0; j < m_virtual_networks; j++) {
72        m_in[port][j]->setConsumer(this);
73        string desc = csprintf("[Queue from port %s %s %s to PerfectSwitch]",
74            to_string(m_switch_id), to_string(port), to_string(j));
75        m_in[port][j]->setDescription(desc);
76        m_in[port][j]->setIncomingLink(port);
77        m_in[port][j]->setVnet(j);
78    }
79}
80
81void
82PerfectSwitch::addOutPort(const vector<MessageBuffer*>& out,
83    const NetDest& routing_table_entry)
84{
85    assert(out.size() == m_virtual_networks);
86
87    // Setup link order
88    LinkOrder l;
89    l.m_value = 0;
90    l.m_link = m_out.size();
91    m_link_order.push_back(l);
92
93    // Add to routing table
94    m_out.push_back(out);
95    m_routing_table.push_back(routing_table_entry);
96}
97
98void
99PerfectSwitch::clearRoutingTables()
100{
101    m_routing_table.clear();
102}
103
104void
105PerfectSwitch::clearBuffers()
106{
107    for (int i = 0; i < m_in.size(); i++){
108        for(int vnet = 0; vnet < m_virtual_networks; vnet++) {
109            m_in[i][vnet]->clear();
110        }
111    }
112
113    for (int i = 0; i < m_out.size(); i++){
114        for(int vnet = 0; vnet < m_virtual_networks; vnet++) {
115            m_out[i][vnet]->clear();
116        }
117    }
118}
119
120void
121PerfectSwitch::reconfigureOutPort(const NetDest& routing_table_entry)
122{
123    m_routing_table.push_back(routing_table_entry);
124}
125
126PerfectSwitch::~PerfectSwitch()
127{
128}
129
130void
131PerfectSwitch::wakeup()
132{
133    MsgPtr msg_ptr;
134
135    // Give the highest numbered link priority most of the time
136    m_wakeups_wo_switch++;
137    int highest_prio_vnet = m_virtual_networks-1;
138    int lowest_prio_vnet = 0;
139    int decrementer = 1;
140    NetworkMessage* net_msg_ptr = NULL;
141
142    // invert priorities to avoid starvation seen in the component network
143    if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) {
144        m_wakeups_wo_switch = 0;
145        highest_prio_vnet = 0;
146        lowest_prio_vnet = m_virtual_networks-1;
147        decrementer = -1;
148    }
149
150    // For all components incoming queues
151    for (int vnet = highest_prio_vnet;
152         (vnet * decrementer) >= (decrementer * lowest_prio_vnet);
153         vnet -= decrementer) {
154
155        // This is for round-robin scheduling
156        int incoming = m_round_robin_start;
157        m_round_robin_start++;
158        if (m_round_robin_start >= m_in.size()) {
159            m_round_robin_start = 0;
160        }
161
162        if(m_pending_message_count[vnet] > 0) {
163            // for all input ports, use round robin scheduling
164            for (int counter = 0; counter < m_in.size(); counter++) {
165                // Round robin scheduling
166                incoming++;
167                if (incoming >= m_in.size()) {
168                    incoming = 0;
169                }
170
171                // temporary vectors to store the routing results
172                vector<LinkID> output_links;
173                vector<NetDest> output_link_destinations;
174
175                // Is there a message waiting?
176                while (m_in[incoming][vnet]->isReady()) {
177                    DPRINTF(RubyNetwork, "incoming: %d\n", incoming);
178
179                    // Peek at message
180                    msg_ptr = m_in[incoming][vnet]->peekMsgPtr();
181                    net_msg_ptr = safe_cast<NetworkMessage*>(msg_ptr.get());
182                    DPRINTF(RubyNetwork, "Message: %s\n", (*net_msg_ptr));
183
184                    output_links.clear();
185                    output_link_destinations.clear();
186                    NetDest msg_dsts =
187                        net_msg_ptr->getInternalDestination();
188
189                    // Unfortunately, the token-protocol sends some
190                    // zero-destination messages, so this assert isn't valid
191                    // assert(msg_dsts.count() > 0);
192
193                    assert(m_link_order.size() == m_routing_table.size());
194                    assert(m_link_order.size() == m_out.size());
195
196                    if (m_network_ptr->getAdaptiveRouting()) {
197                        if (m_network_ptr->isVNetOrdered(vnet)) {
198                            // Don't adaptively route
199                            for (int out = 0; out < m_out.size(); out++) {
200                                m_link_order[out].m_link = out;
201                                m_link_order[out].m_value = 0;
202                            }
203                        } else {
204                            // Find how clogged each link is
205                            for (int out = 0; out < m_out.size(); out++) {
206                                int out_queue_length = 0;
207                                for (int v = 0; v < m_virtual_networks; v++) {
208                                    out_queue_length += m_out[out][v]->getSize();
209                                }
210                                int value =
211                                    (out_queue_length << 8) | (random() & 0xff);
212                                m_link_order[out].m_link = out;
213                                m_link_order[out].m_value = value;
214                            }
215
216                            // Look at the most empty link first
217                            sort(m_link_order.begin(), m_link_order.end());
218                        }
219                    }
220
221                    for (int i = 0; i < m_routing_table.size(); i++) {
222                        // pick the next link to look at
223                        int link = m_link_order[i].m_link;
224                        NetDest dst = m_routing_table[link];
225                        DPRINTF(RubyNetwork, "dst: %s\n", dst);
226
227                        if (!msg_dsts.intersectionIsNotEmpty(dst))
228                            continue;
229
230                        // Remember what link we're using
231                        output_links.push_back(link);
232
233                        // Need to remember which destinations need this
234                        // message in another vector.  This Set is the
235                        // intersection of the routing_table entry and the
236                        // current destination set.  The intersection must
237                        // not be empty, since we are inside "if"
238                        output_link_destinations.push_back(msg_dsts.AND(dst));
239
240                        // Next, we update the msg_destination not to
241                        // include those nodes that were already handled
242                        // by this link
243                        msg_dsts.removeNetDest(dst);
244                    }
245
246                    assert(msg_dsts.count() == 0);
247                    //assert(output_links.size() > 0);
248
249                    // Check for resources - for all outgoing queues
250                    bool enough = true;
251                    for (int i = 0; i < output_links.size(); i++) {
252                        int outgoing = output_links[i];
253                        if (!m_out[outgoing][vnet]->areNSlotsAvailable(1))
254                            enough = false;
255                        DPRINTF(RubyNetwork, "Checking if node is blocked ..."
256                                "outgoing: %d, vnet: %d, enough: %d\n",
257                                outgoing, vnet, enough);
258                    }
259
260                    // There were not enough resources
261                    if (!enough) {
262                        g_eventQueue_ptr->scheduleEvent(this, 1);
263                        DPRINTF(RubyNetwork, "Can't deliver message since a node "
264                                "is blocked\n");
265                        DPRINTF(RubyNetwork, "Message: %s\n", (*net_msg_ptr));
266                        break; // go to next incoming port
267                    }
268
269                    MsgPtr unmodified_msg_ptr;
270
271                    if (output_links.size() > 1) {
272                        // If we are sending this message down more than
273                        // one link (size>1), we need to make a copy of
274                        // the message so each branch can have a different
275                        // internal destination we need to create an
276                        // unmodified MsgPtr because the MessageBuffer
277                        // enqueue func will modify the message
278
279                        // This magic line creates a private copy of the
280                        // message
281                        unmodified_msg_ptr = msg_ptr->clone();
282                    }
283
284                    // Enqueue it - for all outgoing queues
285                    for (int i=0; i<output_links.size(); i++) {
286                        int outgoing = output_links[i];
287
288                        if (i > 0) {
289                            // create a private copy of the unmodified
290                            // message
291                            msg_ptr = unmodified_msg_ptr->clone();
292                        }
293
294                        // Change the internal destination set of the
295                        // message so it knows which destinations this
296                        // link is responsible for.
297                        net_msg_ptr = safe_cast<NetworkMessage*>(msg_ptr.get());
298                        net_msg_ptr->getInternalDestination() =
299                            output_link_destinations[i];
300
301                        // Enqeue msg
302                        DPRINTF(RubyNetwork, "Enqueuing net msg from "
303                                "inport[%d][%d] to outport [%d][%d].\n",
304                                incoming, vnet, outgoing, vnet);
305
306                        m_out[outgoing][vnet]->enqueue(msg_ptr);
307                    }
308
309                    // Dequeue msg
310                    m_in[incoming][vnet]->pop();
311                    m_pending_message_count[vnet]--;
312                }
313            }
314        }
315    }
316}
317
318void
319PerfectSwitch::storeEventInfo(int info)
320{
321    m_pending_message_count[info]++;
322}
323
324void
325PerfectSwitch::printStats(std::ostream& out) const
326{
327    out << "PerfectSwitch printStats" << endl;
328}
329
330void
331PerfectSwitch::clearStats()
332{
333}
334
335void
336PerfectSwitch::printConfig(std::ostream& out) const
337{
338}
339
340void
341PerfectSwitch::print(std::ostream& out) const
342{
343    out << "[PerfectSwitch " << m_switch_id << "]";
344}
345
346