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