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