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