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