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