PerfectSwitch.cc revision 8485:7a9a7f2a3d46
111527Sdavid.guillen@arm.com/* 211527Sdavid.guillen@arm.com * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 311527Sdavid.guillen@arm.com * All rights reserved. 411527Sdavid.guillen@arm.com * 511527Sdavid.guillen@arm.com * Redistribution and use in source and binary forms, with or without 611527Sdavid.guillen@arm.com * modification, are permitted provided that the following conditions are 711527Sdavid.guillen@arm.com * met: redistributions of source code must retain the above copyright 811527Sdavid.guillen@arm.com * notice, this list of conditions and the following disclaimer; 911527Sdavid.guillen@arm.com * redistributions in binary form must reproduce the above copyright 1011527Sdavid.guillen@arm.com * notice, this list of conditions and the following disclaimer in the 1111527Sdavid.guillen@arm.com * documentation and/or other materials provided with the distribution; 1211527Sdavid.guillen@arm.com * neither the name of the copyright holders nor the names of its 1311527Sdavid.guillen@arm.com * contributors may be used to endorse or promote products derived from 1411527Sdavid.guillen@arm.com * this software without specific prior written permission. 1511527Sdavid.guillen@arm.com * 1611527Sdavid.guillen@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1711527Sdavid.guillen@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1811527Sdavid.guillen@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1911527Sdavid.guillen@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2011527Sdavid.guillen@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2111527Sdavid.guillen@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2211527Sdavid.guillen@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2311527Sdavid.guillen@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2411527Sdavid.guillen@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2511527Sdavid.guillen@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2611527Sdavid.guillen@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2711527Sdavid.guillen@arm.com */ 2811527Sdavid.guillen@arm.com 2911527Sdavid.guillen@arm.com#include <algorithm> 3011527Sdavid.guillen@arm.com 3111527Sdavid.guillen@arm.com#include "debug/RubyNetwork.hh" 3211527Sdavid.guillen@arm.com#include "mem/ruby/buffers/MessageBuffer.hh" 3311527Sdavid.guillen@arm.com#include "mem/ruby/network/simple/PerfectSwitch.hh" 3411527Sdavid.guillen@arm.com#include "mem/ruby/network/simple/SimpleNetwork.hh" 3511527Sdavid.guillen@arm.com#include "mem/ruby/profiler/Profiler.hh" 3611527Sdavid.guillen@arm.com#include "mem/ruby/slicc_interface/NetworkMessage.hh" 3711527Sdavid.guillen@arm.com#include "mem/ruby/system/System.hh" 3811527Sdavid.guillen@arm.com 3911527Sdavid.guillen@arm.comusing namespace std; 4011527Sdavid.guillen@arm.com 4111527Sdavid.guillen@arm.comconst int PRIORITY_SWITCH_LIMIT = 128; 4211527Sdavid.guillen@arm.com 4311527Sdavid.guillen@arm.com// Operator for helper class 4411527Sdavid.guillen@arm.combool 4511800Sbrandon.potter@amd.comoperator<(const LinkOrder& l1, const LinkOrder& l2) 4611527Sdavid.guillen@arm.com{ 4711527Sdavid.guillen@arm.com return (l1.m_value < l2.m_value); 4811527Sdavid.guillen@arm.com} 4911527Sdavid.guillen@arm.com 5011527Sdavid.guillen@arm.comPerfectSwitch::PerfectSwitch(SwitchID sid, SimpleNetwork* network_ptr) 5111527Sdavid.guillen@arm.com{ 5211527Sdavid.guillen@arm.com m_virtual_networks = network_ptr->getNumberOfVirtualNetworks(); 5311527Sdavid.guillen@arm.com m_switch_id = sid; 5411527Sdavid.guillen@arm.com m_round_robin_start = 0; 5511527Sdavid.guillen@arm.com m_network_ptr = network_ptr; 5611527Sdavid.guillen@arm.com m_wakeups_wo_switch = 0; 5711527Sdavid.guillen@arm.com 5811527Sdavid.guillen@arm.com for(int i = 0;i < m_virtual_networks;++i) 5911527Sdavid.guillen@arm.com { 6011527Sdavid.guillen@arm.com m_pending_message_count.push_back(0); 6111527Sdavid.guillen@arm.com } 6211527Sdavid.guillen@arm.com} 6311527Sdavid.guillen@arm.com 6411527Sdavid.guillen@arm.comvoid 6511527Sdavid.guillen@arm.comPerfectSwitch::addInPort(const vector<MessageBuffer*>& in) 6611527Sdavid.guillen@arm.com{ 6711527Sdavid.guillen@arm.com assert(in.size() == m_virtual_networks); 6811527Sdavid.guillen@arm.com NodeID port = m_in.size(); 6911527Sdavid.guillen@arm.com m_in.push_back(in); 7011527Sdavid.guillen@arm.com 7111527Sdavid.guillen@arm.com for (int j = 0; j < m_virtual_networks; j++) { 7211527Sdavid.guillen@arm.com m_in[port][j]->setConsumer(this); 7311527Sdavid.guillen@arm.com string desc = csprintf("[Queue from port %s %s %s to PerfectSwitch]", 7411527Sdavid.guillen@arm.com NodeIDToString(m_switch_id), NodeIDToString(port), 7511527Sdavid.guillen@arm.com NodeIDToString(j)); 7611527Sdavid.guillen@arm.com m_in[port][j]->setDescription(desc); 7711527Sdavid.guillen@arm.com m_in[port][j]->setIncomingLink(port); 7811527Sdavid.guillen@arm.com m_in[port][j]->setVnet(j); 7911527Sdavid.guillen@arm.com } 8011527Sdavid.guillen@arm.com} 8111527Sdavid.guillen@arm.com 8211527Sdavid.guillen@arm.comvoid 8311527Sdavid.guillen@arm.comPerfectSwitch::addOutPort(const vector<MessageBuffer*>& out, 8411527Sdavid.guillen@arm.com const NetDest& routing_table_entry) 8511527Sdavid.guillen@arm.com{ 8611527Sdavid.guillen@arm.com assert(out.size() == m_virtual_networks); 8711527Sdavid.guillen@arm.com 8811527Sdavid.guillen@arm.com // Setup link order 8911527Sdavid.guillen@arm.com LinkOrder l; 9011527Sdavid.guillen@arm.com l.m_value = 0; 9111527Sdavid.guillen@arm.com l.m_link = m_out.size(); 9211527Sdavid.guillen@arm.com m_link_order.push_back(l); 9311527Sdavid.guillen@arm.com 9411527Sdavid.guillen@arm.com // Add to routing table 9511527Sdavid.guillen@arm.com m_out.push_back(out); 9611527Sdavid.guillen@arm.com m_routing_table.push_back(routing_table_entry); 9711527Sdavid.guillen@arm.com} 9811527Sdavid.guillen@arm.com 9911527Sdavid.guillen@arm.comvoid 10011527Sdavid.guillen@arm.comPerfectSwitch::clearRoutingTables() 10111527Sdavid.guillen@arm.com{ 10211527Sdavid.guillen@arm.com m_routing_table.clear(); 10311527Sdavid.guillen@arm.com} 10411527Sdavid.guillen@arm.com 10511527Sdavid.guillen@arm.comvoid 10611527Sdavid.guillen@arm.comPerfectSwitch::clearBuffers() 10711527Sdavid.guillen@arm.com{ 10811527Sdavid.guillen@arm.com for (int i = 0; i < m_in.size(); i++){ 10911527Sdavid.guillen@arm.com for(int vnet = 0; vnet < m_virtual_networks; vnet++) { 11011527Sdavid.guillen@arm.com m_in[i][vnet]->clear(); 11111527Sdavid.guillen@arm.com } 11211527Sdavid.guillen@arm.com } 11311527Sdavid.guillen@arm.com 11411527Sdavid.guillen@arm.com for (int i = 0; i < m_out.size(); i++){ 11511527Sdavid.guillen@arm.com for(int vnet = 0; vnet < m_virtual_networks; vnet++) { 11611527Sdavid.guillen@arm.com m_out[i][vnet]->clear(); 11711527Sdavid.guillen@arm.com } 11811527Sdavid.guillen@arm.com } 11911527Sdavid.guillen@arm.com} 12011527Sdavid.guillen@arm.com 12111527Sdavid.guillen@arm.comvoid 12211527Sdavid.guillen@arm.comPerfectSwitch::reconfigureOutPort(const NetDest& routing_table_entry) 12311527Sdavid.guillen@arm.com{ 12411527Sdavid.guillen@arm.com m_routing_table.push_back(routing_table_entry); 12511527Sdavid.guillen@arm.com} 12611527Sdavid.guillen@arm.com 12711527Sdavid.guillen@arm.comPerfectSwitch::~PerfectSwitch() 12811527Sdavid.guillen@arm.com{ 12911527Sdavid.guillen@arm.com} 13011527Sdavid.guillen@arm.com 13111527Sdavid.guillen@arm.comvoid 13211527Sdavid.guillen@arm.comPerfectSwitch::wakeup() 13311527Sdavid.guillen@arm.com{ 13411527Sdavid.guillen@arm.com MsgPtr msg_ptr; 13511527Sdavid.guillen@arm.com 13611527Sdavid.guillen@arm.com // Give the highest numbered link priority most of the time 13711527Sdavid.guillen@arm.com m_wakeups_wo_switch++; 13811527Sdavid.guillen@arm.com int highest_prio_vnet = m_virtual_networks-1; 139 int lowest_prio_vnet = 0; 140 int decrementer = 1; 141 NetworkMessage* net_msg_ptr = NULL; 142 143 // invert priorities to avoid starvation seen in the component network 144 if (m_wakeups_wo_switch > PRIORITY_SWITCH_LIMIT) { 145 m_wakeups_wo_switch = 0; 146 highest_prio_vnet = 0; 147 lowest_prio_vnet = m_virtual_networks-1; 148 decrementer = -1; 149 } 150 151 // For all components incoming queues 152 for (int vnet = highest_prio_vnet; 153 (vnet * decrementer) >= (decrementer * lowest_prio_vnet); 154 vnet -= decrementer) { 155 156 // This is for round-robin scheduling 157 int incoming = m_round_robin_start; 158 m_round_robin_start++; 159 if (m_round_robin_start >= m_in.size()) { 160 m_round_robin_start = 0; 161 } 162 163 if(m_pending_message_count[vnet] > 0) { 164 // for all input ports, use round robin scheduling 165 for (int counter = 0; counter < m_in.size(); counter++) { 166 // Round robin scheduling 167 incoming++; 168 if (incoming >= m_in.size()) { 169 incoming = 0; 170 } 171 172 // temporary vectors to store the routing results 173 vector<LinkID> output_links; 174 vector<NetDest> output_link_destinations; 175 176 // Is there a message waiting? 177 while (m_in[incoming][vnet]->isReady()) { 178 DPRINTF(RubyNetwork, "incoming: %d\n", incoming); 179 180 // Peek at message 181 msg_ptr = m_in[incoming][vnet]->peekMsgPtr(); 182 net_msg_ptr = safe_cast<NetworkMessage*>(msg_ptr.get()); 183 DPRINTF(RubyNetwork, "Message: %s\n", (*net_msg_ptr)); 184 185 output_links.clear(); 186 output_link_destinations.clear(); 187 NetDest msg_dsts = 188 net_msg_ptr->getInternalDestination(); 189 190 // Unfortunately, the token-protocol sends some 191 // zero-destination messages, so this assert isn't valid 192 // assert(msg_dsts.count() > 0); 193 194 assert(m_link_order.size() == m_routing_table.size()); 195 assert(m_link_order.size() == m_out.size()); 196 197 if (m_network_ptr->getAdaptiveRouting()) { 198 if (m_network_ptr->isVNetOrdered(vnet)) { 199 // Don't adaptively route 200 for (int out = 0; out < m_out.size(); out++) { 201 m_link_order[out].m_link = out; 202 m_link_order[out].m_value = 0; 203 } 204 } else { 205 // Find how clogged each link is 206 for (int out = 0; out < m_out.size(); out++) { 207 int out_queue_length = 0; 208 for (int v = 0; v < m_virtual_networks; v++) { 209 out_queue_length += m_out[out][v]->getSize(); 210 } 211 int value = 212 (out_queue_length << 8) | (random() & 0xff); 213 m_link_order[out].m_link = out; 214 m_link_order[out].m_value = value; 215 } 216 217 // Look at the most empty link first 218 sort(m_link_order.begin(), m_link_order.end()); 219 } 220 } 221 222 for (int i = 0; i < m_routing_table.size(); i++) { 223 // pick the next link to look at 224 int link = m_link_order[i].m_link; 225 NetDest dst = m_routing_table[link]; 226 DPRINTF(RubyNetwork, "dst: %s\n", dst); 227 228 if (!msg_dsts.intersectionIsNotEmpty(dst)) 229 continue; 230 231 // Remember what link we're using 232 output_links.push_back(link); 233 234 // Need to remember which destinations need this 235 // message in another vector. This Set is the 236 // intersection of the routing_table entry and the 237 // current destination set. The intersection must 238 // not be empty, since we are inside "if" 239 output_link_destinations.push_back(msg_dsts.AND(dst)); 240 241 // Next, we update the msg_destination not to 242 // include those nodes that were already handled 243 // by this link 244 msg_dsts.removeNetDest(dst); 245 } 246 247 assert(msg_dsts.count() == 0); 248 //assert(output_links.size() > 0); 249 250 // Check for resources - for all outgoing queues 251 bool enough = true; 252 for (int i = 0; i < output_links.size(); i++) { 253 int outgoing = output_links[i]; 254 if (!m_out[outgoing][vnet]->areNSlotsAvailable(1)) 255 enough = false; 256 DPRINTF(RubyNetwork, "Checking if node is blocked ..." 257 "outgoing: %d, vnet: %d, enough: %d\n", 258 outgoing, vnet, enough); 259 } 260 261 // There were not enough resources 262 if (!enough) { 263 g_eventQueue_ptr->scheduleEvent(this, 1); 264 DPRINTF(RubyNetwork, "Can't deliver message since a node " 265 "is blocked\n"); 266 DPRINTF(RubyNetwork, "Message: %s\n", (*net_msg_ptr)); 267 break; // go to next incoming port 268 } 269 270 MsgPtr unmodified_msg_ptr; 271 272 if (output_links.size() > 1) { 273 // If we are sending this message down more than 274 // one link (size>1), we need to make a copy of 275 // the message so each branch can have a different 276 // internal destination we need to create an 277 // unmodified MsgPtr because the MessageBuffer 278 // enqueue func will modify the message 279 280 // This magic line creates a private copy of the 281 // message 282 unmodified_msg_ptr = msg_ptr->clone(); 283 } 284 285 // Enqueue it - for all outgoing queues 286 for (int i=0; i<output_links.size(); i++) { 287 int outgoing = output_links[i]; 288 289 if (i > 0) { 290 // create a private copy of the unmodified 291 // message 292 msg_ptr = unmodified_msg_ptr->clone(); 293 } 294 295 // Change the internal destination set of the 296 // message so it knows which destinations this 297 // link is responsible for. 298 net_msg_ptr = safe_cast<NetworkMessage*>(msg_ptr.get()); 299 net_msg_ptr->getInternalDestination() = 300 output_link_destinations[i]; 301 302 // Enqeue msg 303 DPRINTF(RubyNetwork, "Enqueuing net msg from " 304 "inport[%d][%d] to outport [%d][%d].\n", 305 incoming, vnet, outgoing, vnet); 306 307 m_out[outgoing][vnet]->enqueue(msg_ptr); 308 } 309 310 // Dequeue msg 311 m_in[incoming][vnet]->pop(); 312 m_pending_message_count[vnet]--; 313 } 314 } 315 } 316 } 317} 318 319void 320PerfectSwitch::storeEventInfo(int info) 321{ 322 m_pending_message_count[info]++; 323} 324 325void 326PerfectSwitch::printStats(std::ostream& out) const 327{ 328 out << "PerfectSwitch printStats" << endl; 329} 330 331void 332PerfectSwitch::clearStats() 333{ 334} 335 336void 337PerfectSwitch::printConfig(std::ostream& out) const 338{ 339} 340 341void 342PerfectSwitch::print(std::ostream& out) const 343{ 344 out << "[PerfectSwitch " << m_switch_id << "]"; 345} 346 347