PerfectSwitch.cc revision 6372:f1a41ea3bbab
1 2/* 3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer; 10 * redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution; 13 * neither the name of the copyright holders nor the names of its 14 * contributors may be used to endorse or promote products derived from 15 * this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30/* 31 * PerfectSwitch.cc 32 * 33 * Description: See PerfectSwitch.hh 34 * 35 * $Id$ 36 * 37 */ 38 39 40#include "mem/ruby/network/simple/PerfectSwitch.hh" 41#include "mem/ruby/slicc_interface/NetworkMessage.hh" 42#include "mem/ruby/profiler/Profiler.hh" 43#include "mem/ruby/system/System.hh" 44#include "mem/ruby/network/simple/SimpleNetwork.hh" 45#include "mem/gems_common/util.hh" 46#include "mem/ruby/buffers/MessageBuffer.hh" 47#include "mem/protocol/Protocol.hh" 48 49const int PRIORITY_SWITCH_LIMIT = 128; 50 51// Operator for helper class 52bool operator<(const LinkOrder& l1, const LinkOrder& l2) { 53 return (l1.m_value < l2.m_value); 54} 55 56PerfectSwitch::PerfectSwitch(SwitchID sid, SimpleNetwork* network_ptr) 57{ 58 m_virtual_networks = network_ptr->getNumberOfVirtualNetworks(); 59 m_switch_id = sid; 60 m_round_robin_start = 0; 61 m_network_ptr = network_ptr; 62 m_wakeups_wo_switch = 0; 63} 64 65void PerfectSwitch::addInPort(const Vector<MessageBuffer*>& in) 66{ 67 assert(in.size() == m_virtual_networks); 68 NodeID port = m_in.size(); 69 m_in.insertAtBottom(in); 70 for (int j = 0; j < m_virtual_networks; j++) { 71 m_in[port][j]->setConsumer(this); 72 string desc = "[Queue from port " + NodeIDToString(m_switch_id) + " " + NodeIDToString(port) + " " + NodeIDToString(j) + " to PerfectSwitch]"; 73 m_in[port][j]->setDescription(desc); 74 } 75} 76 77void PerfectSwitch::addOutPort(const Vector<MessageBuffer*>& out, const NetDest& routing_table_entry) 78{ 79 assert(out.size() == m_virtual_networks); 80 81 // Setup link order 82 LinkOrder l; 83 l.m_value = 0; 84 l.m_link = m_out.size(); 85 m_link_order.insertAtBottom(l); 86 87 // Add to routing table 88 m_out.insertAtBottom(out); 89 m_routing_table.insertAtBottom(routing_table_entry); 90 91 m_out_link_vec.insertAtBottom(out); 92} 93 94void PerfectSwitch::clearRoutingTables() 95{ 96 m_routing_table.clear(); 97} 98 99void PerfectSwitch::clearBuffers() 100{ 101 for(int i=0; i<m_in.size(); i++){ 102 for(int vnet=0; vnet < m_virtual_networks; vnet++) { 103 m_in[i][vnet]->clear(); 104 } 105 } 106 107 for(int i=0; i<m_out.size(); i++){ 108 for(int vnet=0; vnet < m_virtual_networks; vnet++) { 109 m_out[i][vnet]->clear(); 110 } 111 } 112} 113 114void PerfectSwitch::reconfigureOutPort(const NetDest& routing_table_entry) 115{ 116 m_routing_table.insertAtBottom(routing_table_entry); 117} 118 119PerfectSwitch::~PerfectSwitch() 120{ 121} 122 123void PerfectSwitch::wakeup() 124{ 125 126 DEBUG_EXPR(NETWORK_COMP, MedPrio, m_switch_id); 127 128 MsgPtr msg_ptr; 129 130 // Give the highest numbered link priority most of the time 131 m_wakeups_wo_switch++; 132 int highest_prio_vnet = m_virtual_networks-1; 133 int lowest_prio_vnet = 0; 134 int decrementer = 1; 135 NetworkMessage* net_msg_ptr = NULL; 136 137 // invert priorities to avoid starvation seen in the component network 138 if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) { 139 m_wakeups_wo_switch = 0; 140 highest_prio_vnet = 0; 141 lowest_prio_vnet = m_virtual_networks-1; 142 decrementer = -1; 143 } 144 145 for (int vnet = highest_prio_vnet; (vnet*decrementer) >= (decrementer*lowest_prio_vnet); vnet -= decrementer) { 146 147 // For all components incoming queues 148 int incoming = m_round_robin_start; // This is for round-robin scheduling 149 m_round_robin_start++; 150 if (m_round_robin_start >= m_in.size()) { 151 m_round_robin_start = 0; 152 } 153 154 // for all input ports, use round robin scheduling 155 for (int counter = 0; counter < m_in.size(); counter++) { 156 157 // Round robin scheduling 158 incoming++; 159 if (incoming >= m_in.size()) { 160 incoming = 0; 161 } 162 163 // temporary vectors to store the routing results 164 Vector<LinkID> output_links; 165 Vector<NetDest> output_link_destinations; 166 167 // Is there a message waiting? 168 while (m_in[incoming][vnet]->isReady()) { 169 170 DEBUG_EXPR(NETWORK_COMP, MedPrio, incoming); 171 172 // Peek at message 173 msg_ptr = m_in[incoming][vnet]->peekMsgPtr(); 174 net_msg_ptr = dynamic_cast<NetworkMessage*>(msg_ptr.ref()); 175 DEBUG_EXPR(NETWORK_COMP, MedPrio, *net_msg_ptr); 176 177 output_links.clear(); 178 output_link_destinations.clear(); 179 NetDest msg_destinations = net_msg_ptr->getInternalDestination(); 180 181 // Unfortunately, the token-protocol sends some 182 // zero-destination messages, so this assert isn't valid 183 // assert(msg_destinations.count() > 0); 184 185 assert(m_link_order.size() == m_routing_table.size()); 186 assert(m_link_order.size() == m_out.size()); 187//changed by SS 188 if (m_network_ptr->getAdaptiveRouting()) { 189 if (m_network_ptr->isVNetOrdered(vnet)) { 190 // Don't adaptively route 191 for (int outlink=0; outlink<m_out.size(); outlink++) { 192 m_link_order[outlink].m_link = outlink; 193 m_link_order[outlink].m_value = 0; 194 } 195 } else { 196 // Find how clogged each link is 197 for (int outlink=0; outlink<m_out.size(); outlink++) { 198 int out_queue_length = 0; 199 for (int v=0; v<m_virtual_networks; v++) { 200 out_queue_length += m_out[outlink][v]->getSize(); 201 } 202 m_link_order[outlink].m_link = outlink; 203 m_link_order[outlink].m_value = 0; 204 m_link_order[outlink].m_value |= (out_queue_length << 8); 205 m_link_order[outlink].m_value |= (random() & 0xff); 206 } 207 m_link_order.sortVector(); // Look at the most empty link first 208 } 209 } 210 211 for (int i=0; i<m_routing_table.size(); i++) { 212 // pick the next link to look at 213 int link = m_link_order[i].m_link; 214 215 DEBUG_EXPR(NETWORK_COMP, MedPrio, m_routing_table[link]); 216 217 if (msg_destinations.intersectionIsNotEmpty(m_routing_table[link])) { 218 219 // Remember what link we're using 220 output_links.insertAtBottom(link); 221 222 // Need to remember which destinations need this message 223 // in another vector. This Set is the intersection of the 224 // routing_table entry and the current destination set. 225 // The intersection must not be empty, since we are inside "if" 226 output_link_destinations.insertAtBottom(msg_destinations.AND(m_routing_table[link])); 227 228 // Next, we update the msg_destination not to include 229 // those nodes that were already handled by this link 230 msg_destinations.removeNetDest(m_routing_table[link]); 231 } 232 } 233 234 assert(msg_destinations.count() == 0); 235 //assert(output_links.size() > 0); 236 237 // Check for resources - for all outgoing queues 238 bool enough = true; 239 for (int i=0; i<output_links.size(); i++) { 240 int outgoing = output_links[i]; 241 enough = enough && m_out[outgoing][vnet]->areNSlotsAvailable(1); 242 DEBUG_MSG(NETWORK_COMP, HighPrio, "checking if node is blocked"); 243 DEBUG_EXPR(NETWORK_COMP, HighPrio, outgoing); 244 DEBUG_EXPR(NETWORK_COMP, HighPrio, vnet); 245 DEBUG_EXPR(NETWORK_COMP, HighPrio, enough); 246 } 247 248 // There were not enough resources 249 if(!enough) { 250 g_eventQueue_ptr->scheduleEvent(this, 1); 251 DEBUG_MSG(NETWORK_COMP, HighPrio, "Can't deliver message to anyone since a node is blocked"); 252 DEBUG_EXPR(NETWORK_COMP, HighPrio, *net_msg_ptr); 253 break; // go to next incoming port 254 } 255 256 MsgPtr unmodified_msg_ptr; 257 258 if (output_links.size() > 1) { 259 // If we are sending this message down more than one link 260 // (size>1), we need to make a copy of the message so each 261 // branch can have a different internal destination 262 // we need to create an unmodified MsgPtr because the MessageBuffer enqueue func 263 // will modify the message 264 unmodified_msg_ptr = *(msg_ptr.ref()); // This magic line creates a private copy of the message 265 } 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 msg_ptr = *(unmodified_msg_ptr.ref()); // create a private copy of the unmodified message 273 } 274 275 // Change the internal destination set of the message so it 276 // knows which destinations this link is responsible for. 277 net_msg_ptr = dynamic_cast<NetworkMessage*>(msg_ptr.ref()); 278 net_msg_ptr->getInternalDestination() = output_link_destinations[i]; 279 280 // Enqeue msg 281 DEBUG_NEWLINE(NETWORK_COMP,HighPrio); 282 DEBUG_MSG(NETWORK_COMP,HighPrio,"switch: " + int_to_string(m_switch_id) 283 + " enqueuing net msg from inport[" + int_to_string(incoming) + "][" 284 + int_to_string(vnet) +"] to outport [" + int_to_string(outgoing) 285 + "][" + int_to_string(vnet) +"]" 286 + " time: " + int_to_string(g_eventQueue_ptr->getTime()) + "."); 287 DEBUG_NEWLINE(NETWORK_COMP,HighPrio); 288 289 m_out[outgoing][vnet]->enqueue(msg_ptr); 290 } 291 292 // Dequeue msg 293 m_in[incoming][vnet]->pop(); 294 } 295 } 296 } 297} 298 299void PerfectSwitch::printStats(ostream& out) const 300{ 301 out << "PerfectSwitch printStats" << endl; 302} 303 304void PerfectSwitch::clearStats() 305{ 306} 307 308void PerfectSwitch::printConfig(ostream& out) const 309{ 310} 311 312void PerfectSwitch::print(ostream& out) const 313{ 314 out << "[PerfectSwitch " << m_switch_id << "]"; 315} 316 317