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