PerfectSwitch.cc (9274:ba635023d4bb) PerfectSwitch.cc (9302:c2e70a9bc340)
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 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}
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 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