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