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