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