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