PerfectSwitch.cc revision 6154:6bb54dcb940e
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.C 32 * 33 * Description: See PerfectSwitch.h 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 = NUMBER_OF_VIRTUAL_NETWORKS; // FIXME - pass me as a parameter? 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 if (g_PRINT_TOPOLOGY) { 92 m_out_link_vec.insertAtBottom(out); 93 } 94} 95 96void PerfectSwitch::clearRoutingTables() 97{ 98 m_routing_table.clear(); 99} 100 101void PerfectSwitch::clearBuffers() 102{ 103 for(int i=0; i<m_in.size(); i++){ 104 for(int vnet=0; vnet < m_virtual_networks; vnet++) { 105 m_in[i][vnet]->clear(); 106 } 107 } 108 109 for(int i=0; i<m_out.size(); i++){ 110 for(int vnet=0; vnet < m_virtual_networks; vnet++) { 111 m_out[i][vnet]->clear(); 112 } 113 } 114} 115 116void PerfectSwitch::reconfigureOutPort(const NetDest& routing_table_entry) 117{ 118 m_routing_table.insertAtBottom(routing_table_entry); 119} 120 121PerfectSwitch::~PerfectSwitch() 122{ 123} 124 125void PerfectSwitch::wakeup() 126{ 127 128 DEBUG_EXPR(NETWORK_COMP, MedPrio, m_switch_id); 129 130 MsgPtr msg_ptr; 131 132 // Give the highest numbered link priority most of the time 133 m_wakeups_wo_switch++; 134 int highest_prio_vnet = m_virtual_networks-1; 135 int lowest_prio_vnet = 0; 136 int decrementer = 1; 137 bool schedule_wakeup = false; 138 NetworkMessage* net_msg_ptr = NULL; 139 140 // invert priorities to avoid starvation seen in the component network 141 if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) { 142 m_wakeups_wo_switch = 0; 143 highest_prio_vnet = 0; 144 lowest_prio_vnet = m_virtual_networks-1; 145 decrementer = -1; 146 } 147 148 for (int vnet = highest_prio_vnet; (vnet*decrementer) >= (decrementer*lowest_prio_vnet); vnet -= decrementer) { 149 150 // For all components incoming queues 151 int incoming = m_round_robin_start; // This is for round-robin scheduling 152 m_round_robin_start++; 153 if (m_round_robin_start >= m_in.size()) { 154 m_round_robin_start = 0; 155 } 156 157 // for all input ports, use round robin scheduling 158 for (int counter = 0; counter < m_in.size(); counter++) { 159 160 // Round robin scheduling 161 incoming++; 162 if (incoming >= m_in.size()) { 163 incoming = 0; 164 } 165 166 // temporary vectors to store the routing results 167 Vector<LinkID> output_links; 168 Vector<NetDest> output_link_destinations; 169 170 // Is there a message waiting? 171 while (m_in[incoming][vnet]->isReady()) { 172 173 DEBUG_EXPR(NETWORK_COMP, MedPrio, incoming); 174 175 // Peek at message 176 msg_ptr = m_in[incoming][vnet]->peekMsgPtr(); 177 net_msg_ptr = dynamic_cast<NetworkMessage*>(msg_ptr.ref()); 178 DEBUG_EXPR(NETWORK_COMP, MedPrio, *net_msg_ptr); 179 180 output_links.clear(); 181 output_link_destinations.clear(); 182 NetDest msg_destinations = net_msg_ptr->getInternalDestination(); 183 184 // Unfortunately, the token-protocol sends some 185 // zero-destination messages, so this assert isn't valid 186 // assert(msg_destinations.count() > 0); 187 188 assert(m_link_order.size() == m_routing_table.size()); 189 assert(m_link_order.size() == m_out.size()); 190 191 if (g_adaptive_routing) { 192 if (m_network_ptr->isVNetOrdered(vnet)) { 193 // Don't adaptively route 194 for (int outlink=0; outlink<m_out.size(); outlink++) { 195 m_link_order[outlink].m_link = outlink; 196 m_link_order[outlink].m_value = 0; 197 } 198 } else { 199 // Find how clogged each link is 200 for (int outlink=0; outlink<m_out.size(); outlink++) { 201 int out_queue_length = 0; 202 for (int v=0; v<m_virtual_networks; v++) { 203 out_queue_length += m_out[outlink][v]->getSize(); 204 } 205 m_link_order[outlink].m_link = outlink; 206 m_link_order[outlink].m_value = 0; 207 m_link_order[outlink].m_value |= (out_queue_length << 8); 208 m_link_order[outlink].m_value |= (random() & 0xff); 209 } 210 m_link_order.sortVector(); // Look at the most empty link first 211 } 212 } 213 214 for (int i=0; i<m_routing_table.size(); i++) { 215 // pick the next link to look at 216 int link = m_link_order[i].m_link; 217 218 DEBUG_EXPR(NETWORK_COMP, MedPrio, m_routing_table[link]); 219 220 if (msg_destinations.intersectionIsNotEmpty(m_routing_table[link])) { 221 222 // Remember what link we're using 223 output_links.insertAtBottom(link); 224 225 // Need to remember which destinations need this message 226 // in another vector. This Set is the intersection of the 227 // routing_table entry and the current destination set. 228 // The intersection must not be empty, since we are inside "if" 229 output_link_destinations.insertAtBottom(msg_destinations.AND(m_routing_table[link])); 230 231 // Next, we update the msg_destination not to include 232 // those nodes that were already handled by this link 233 msg_destinations.removeNetDest(m_routing_table[link]); 234 } 235 } 236 237 assert(msg_destinations.count() == 0); 238 //assert(output_links.size() > 0); 239 240 // Check for resources - for all outgoing queues 241 bool enough = true; 242 for (int i=0; i<output_links.size(); i++) { 243 int outgoing = output_links[i]; 244 enough = enough && m_out[outgoing][vnet]->areNSlotsAvailable(1); 245 DEBUG_MSG(NETWORK_COMP, HighPrio, "checking if node is blocked"); 246 DEBUG_EXPR(NETWORK_COMP, HighPrio, outgoing); 247 DEBUG_EXPR(NETWORK_COMP, HighPrio, vnet); 248 DEBUG_EXPR(NETWORK_COMP, HighPrio, enough); 249 } 250 251 // There were not enough resources 252 if(!enough) { 253 g_eventQueue_ptr->scheduleEvent(this, 1); 254 DEBUG_MSG(NETWORK_COMP, HighPrio, "Can't deliver message to anyone since a node is blocked"); 255 DEBUG_EXPR(NETWORK_COMP, HighPrio, *net_msg_ptr); 256 break; // go to next incoming port 257 } 258 259 MsgPtr unmodified_msg_ptr; 260 261 if (output_links.size() > 1) { 262 // If we are sending this message down more than one link 263 // (size>1), we need to make a copy of the message so each 264 // branch can have a different internal destination 265 // we need to create an unmodified MsgPtr because the MessageBuffer enqueue func 266 // will modify the message 267 unmodified_msg_ptr = *(msg_ptr.ref()); // This magic line creates a private copy of the message 268 } 269 270 // Enqueue it - for all outgoing queues 271 for (int i=0; i<output_links.size(); i++) { 272 int outgoing = output_links[i]; 273 274 if (i > 0) { 275 msg_ptr = *(unmodified_msg_ptr.ref()); // create a private copy of the unmodified message 276 } 277 278 // Change the internal destination set of the message so it 279 // knows which destinations this link is responsible for. 280 net_msg_ptr = dynamic_cast<NetworkMessage*>(msg_ptr.ref()); 281 net_msg_ptr->getInternalDestination() = output_link_destinations[i]; 282 283 // Enqeue msg 284 DEBUG_NEWLINE(NETWORK_COMP,HighPrio); 285 DEBUG_MSG(NETWORK_COMP,HighPrio,"switch: " + int_to_string(m_switch_id) 286 + " enqueuing net msg from inport[" + int_to_string(incoming) + "][" 287 + int_to_string(vnet) +"] to outport [" + int_to_string(outgoing) 288 + "][" + int_to_string(vnet) +"]" 289 + " time: " + int_to_string(g_eventQueue_ptr->getTime()) + "."); 290 DEBUG_NEWLINE(NETWORK_COMP,HighPrio); 291 292 m_out[outgoing][vnet]->enqueue(msg_ptr); 293 } 294 295 // Dequeue msg 296 m_in[incoming][vnet]->pop(); 297 } 298 } 299 } 300} 301 302void PerfectSwitch::printStats(ostream& out) const 303{ 304 out << "PerfectSwitch printStats" << endl; 305} 306 307void PerfectSwitch::clearStats() 308{ 309} 310 311void PerfectSwitch::printConfig(ostream& out) const 312{ 313} 314 315void PerfectSwitch::print(ostream& out) const 316{ 317 out << "[PerfectSwitch " << m_switch_id << "]"; 318} 319 320