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