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