PerfectSwitch.cc (9117:49116b947194) PerfectSwitch.cc (9171:ae88ecf37145)
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"
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) {
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"
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);
263 scheduleEvent(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::print(std::ostream& out) const
338{
339 out << "[PerfectSwitch " << m_switch_id << "]";
340}
341
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::print(std::ostream& out) const
338{
339 out << "[PerfectSwitch " << m_switch_id << "]";
340}
341