NetDest.cc revision 9138
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 "mem/ruby/common/NetDest.hh" 32 33NetDest::NetDest() 34{ 35 resize(); 36} 37 38void 39NetDest::add(MachineID newElement) 40{ 41 assert(bitIndex(newElement.num) < m_bits[vecIndex(newElement)].getSize()); 42 m_bits[vecIndex(newElement)].add(bitIndex(newElement.num)); 43} 44 45void 46NetDest::addNetDest(const NetDest& netDest) 47{ 48 assert(m_bits.size() == netDest.getSize()); 49 for (int i = 0; i < m_bits.size(); i++) { 50 m_bits[i].addSet(netDest.m_bits[i]); 51 } 52} 53 54void 55NetDest::addRandom() 56{ 57 int i = random()%m_bits.size(); 58 m_bits[i].addRandom(); 59} 60 61void 62NetDest::setNetDest(MachineType machine, const Set& set) 63{ 64 // assure that there is only one set of destinations for this machine 65 assert(MachineType_base_level((MachineType)(machine + 1)) - 66 MachineType_base_level(machine) == 1); 67 m_bits[MachineType_base_level(machine)] = set; 68} 69 70void 71NetDest::remove(MachineID oldElement) 72{ 73 m_bits[vecIndex(oldElement)].remove(bitIndex(oldElement.num)); 74} 75 76void 77NetDest::removeNetDest(const NetDest& netDest) 78{ 79 assert(m_bits.size() == netDest.getSize()); 80 for (int i = 0; i < m_bits.size(); i++) { 81 m_bits[i].removeSet(netDest.m_bits[i]); 82 } 83} 84 85void 86NetDest::clear() 87{ 88 for (int i = 0; i < m_bits.size(); i++) { 89 m_bits[i].clear(); 90 } 91} 92 93void 94NetDest::broadcast() 95{ 96 for (MachineType machine = MachineType_FIRST; 97 machine < MachineType_NUM; ++machine) { 98 broadcast(machine); 99 } 100} 101 102void 103NetDest::broadcast(MachineType machineType) 104{ 105 for (int i = 0; i < MachineType_base_count(machineType); i++) { 106 MachineID mach = {machineType, i}; 107 add(mach); 108 } 109} 110 111//For Princeton Network 112std::vector<NodeID> 113NetDest::getAllDest() 114{ 115 std::vector<NodeID> dest; 116 dest.clear(); 117 for (int i = 0; i < m_bits.size(); i++) { 118 for (int j = 0; j < m_bits[i].getSize(); j++) { 119 if (m_bits[i].isElement(j)) { 120 int id = MachineType_base_number((MachineType)i) + j; 121 dest.push_back((NodeID)id); 122 } 123 } 124 } 125 return dest; 126} 127 128int 129NetDest::count() const 130{ 131 int counter = 0; 132 for (int i = 0; i < m_bits.size(); i++) { 133 counter += m_bits[i].count(); 134 } 135 return counter; 136} 137 138NodeID 139NetDest::elementAt(MachineID index) 140{ 141 return m_bits[vecIndex(index)].elementAt(bitIndex(index.num)); 142} 143 144MachineID 145NetDest::smallestElement() const 146{ 147 assert(count() > 0); 148 for (int i = 0; i < m_bits.size(); i++) { 149 for (int j = 0; j < m_bits[i].getSize(); j++) { 150 if (m_bits[i].isElement(j)) { 151 MachineID mach = {MachineType_from_base_level(i), j}; 152 return mach; 153 } 154 } 155 } 156 panic("No smallest element of an empty set."); 157} 158 159MachineID 160NetDest::smallestElement(MachineType machine) const 161{ 162 int size = m_bits[MachineType_base_level(machine)].getSize(); 163 for (int j = 0; j < size; j++) { 164 if (m_bits[MachineType_base_level(machine)].isElement(j)) { 165 MachineID mach = {machine, j}; 166 return mach; 167 } 168 } 169 170 panic("No smallest element of given MachineType."); 171} 172 173// Returns true iff all bits are set 174bool 175NetDest::isBroadcast() const 176{ 177 for (int i = 0; i < m_bits.size(); i++) { 178 if (!m_bits[i].isBroadcast()) { 179 return false; 180 } 181 } 182 return true; 183} 184 185// Returns true iff no bits are set 186bool 187NetDest::isEmpty() const 188{ 189 for (int i = 0; i < m_bits.size(); i++) { 190 if (!m_bits[i].isEmpty()) { 191 return false; 192 } 193 } 194 return true; 195} 196 197// returns the logical OR of "this" set and orNetDest 198NetDest 199NetDest::OR(const NetDest& orNetDest) const 200{ 201 assert(m_bits.size() == orNetDest.getSize()); 202 NetDest result; 203 for (int i = 0; i < m_bits.size(); i++) { 204 result.m_bits[i] = m_bits[i].OR(orNetDest.m_bits[i]); 205 } 206 return result; 207} 208 209// returns the logical AND of "this" set and andNetDest 210NetDest 211NetDest::AND(const NetDest& andNetDest) const 212{ 213 assert(m_bits.size() == andNetDest.getSize()); 214 NetDest result; 215 for (int i = 0; i < m_bits.size(); i++) { 216 result.m_bits[i] = m_bits[i].AND(andNetDest.m_bits[i]); 217 } 218 return result; 219} 220 221// Returns true if the intersection of the two sets is non-empty 222bool 223NetDest::intersectionIsNotEmpty(const NetDest& other_netDest) const 224{ 225 assert(m_bits.size() == other_netDest.getSize()); 226 for (int i = 0; i < m_bits.size(); i++) { 227 if (!m_bits[i].intersectionIsEmpty(other_netDest.m_bits[i])) { 228 return true; 229 } 230 } 231 return false; 232} 233 234bool 235NetDest::isSuperset(const NetDest& test) const 236{ 237 assert(m_bits.size() == test.getSize()); 238 239 for (int i = 0; i < m_bits.size(); i++) { 240 if (!m_bits[i].isSuperset(test.m_bits[i])) { 241 return false; 242 } 243 } 244 return true; 245} 246 247bool 248NetDest::isElement(MachineID element) const 249{ 250 return ((m_bits[vecIndex(element)])).isElement(bitIndex(element.num)); 251} 252 253void 254NetDest::resize() 255{ 256 m_bits.resize(MachineType_base_level(MachineType_NUM)); 257 assert(m_bits.size() == MachineType_NUM); 258 259 for (int i = 0; i < m_bits.size(); i++) { 260 m_bits[i].setSize(MachineType_base_count((MachineType)i)); 261 } 262} 263 264void 265NetDest::print(std::ostream& out) const 266{ 267 out << "[NetDest (" << m_bits.size() << ") "; 268 269 for (int i = 0; i < m_bits.size(); i++) { 270 for (int j = 0; j < m_bits[i].getSize(); j++) { 271 out << (bool) m_bits[i].isElement(j) << " "; 272 } 273 out << " - "; 274 } 275 out << "]"; 276} 277 278