-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDimOrderAdaptiveRoutingAlgorithm.cc
More file actions
175 lines (150 loc) · 5.44 KB
/
DimOrderAdaptiveRoutingAlgorithm.cc
File metadata and controls
175 lines (150 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright 2016 Ashish Chaudhari, Franky Romero, Nehal Bhandari, Wasam Altoyan
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "network/hyperx/DimOrderAdaptiveRoutingAlgorithm.h"
#include <cassert>
#include <unordered_set>
#include "types/Message.h"
#include "types/Packet.h"
namespace HyperX {
DimOrderAdaptiveRoutingAlgorithm::DimOrderAdaptiveRoutingAlgorithm(
const std::string& _name, const Component* _parent, Router* _router,
u64 _latency, u32 _numVcs, const std::vector<u32>& _dimensionWidths,
const std::vector<u32>& _dimensionWeights, u32 _concentration)
: RoutingAlgorithm(_name, _parent, _router, _latency),
numVcs_(router_->numVcs()), dimensionWidths_(_dimensionWidths),
dimensionWeights_(_dimensionWeights),
concentration_(_concentration) {
assert(numVcs_ >= 2);
}
DimOrderAdaptiveRoutingAlgorithm::~DimOrderAdaptiveRoutingAlgorithm() {}
void DimOrderAdaptiveRoutingAlgorithm::processRequest(
Flit* _flit, RoutingAlgorithm::Response* _response) {
std::unordered_set<u32> outputPorts;
Packet* packet = _flit->getPacket();
// ex: [x,y,z]
const std::vector<u32>& routerAddress = router_->getAddress();
// ex: [c,x,y,z]
const std::vector<u32>* destinationAddress =
_flit->getPacket()->getMessage()->getDestinationAddress();
assert(routerAddress.size() == (destinationAddress->size() - 1));
// first, test if already at destination router
bool arrived = true;
for (u32 mydim = 0; mydim < routerAddress.size(); mydim++) {
if (routerAddress.at(mydim) != destinationAddress->at(mydim+1)) {
arrived = false;
break;
}
}
if (arrived) {
bool res = outputPorts.insert(destinationAddress->at(0)).second;
(void)res;
assert(res);
} else {
// determine which dimension to work on
u32 dim;
u32 portBase = concentration_;
u32 numPorts = 0; // excludes terminal ports
std::vector<u32> portBases;
portBases.resize(routerAddress.size());
// get number of ports
for (dim = 0; dim < routerAddress.size(); dim++) {
numPorts += dimensionWeights_[dim];
if (routerAddress.at(dim) == destinationAddress->at(dim+1)) {
portBases[dim] = 0;
} else {
portBases[dim] = portBase;
}
portBase += ((dimensionWidths_.at(dim) - 1) * dimensionWeights_.at(dim));
}
// else get list of valid ports
u32 prt = 0;
std::vector<u32> valid_ports;
valid_ports.resize(numPorts);
for (dim = 0; dim < routerAddress.size(); dim++) {
if (portBases[dim] != 0) {
// more router-to-router hops needed
u32 src = routerAddress.at(dim);
u32 dst = destinationAddress->at(dim+1);
if (dst < src) {
dst += dimensionWidths_.at(dim);
}
u32 offset = (dst - src - 1) * dimensionWeights_.at(dim);
// add all ports where the two routers are connecting
for (u32 weight = 0; weight < dimensionWeights_.at(dim); weight++) {
valid_ports[prt] = portBases[dim] + offset + weight;
prt++;
}
}
}
valid_ports.resize(prt);
// calculate max availability for each port
u32 vcIdx;
u32 outputPort;
f64 max = 0;
f64 availability;
f64 total_availability;
f64 avg_availability;
std::unordered_map<u32, f64> port_availability =
std::unordered_map<u32, f64>(numPorts);
// for each output port
for (u32 port : valid_ports) {
total_availability = 0;
avg_availability = 0;
// for each VC
for (u32 vc = 0; vc < numVcs_; vc++) {
vcIdx = router_->vcIndex(port, vc);
availability = router_->congestionStatus(vcIdx);
total_availability += availability;
}
avg_availability = total_availability / ((f64)numVcs_);
// add to vector of ports availability
port_availability.insert({port, avg_availability});
if ( avg_availability > max ) {
max = avg_availability;
}
}
// get ports with max availability
std::vector<u32> max_ports = std::vector<u32>(numPorts);
u32 max_counter = 0;
for ( auto it = port_availability.begin();
it != port_availability.end(); ++it ) {
if (it->second == max) {
max_ports.at(max_counter) = it->first;
max_counter++;
}
}
// randomly choose an output port with max availability
u32 portIdx = gSim->rnd.nextU64(0, max_counter - 1);
outputPort = max_ports.at(portIdx);
bool res = outputPorts.insert(outputPort).second;
(void)res;
assert(res);
}
u32 vcSet = (_flit->getVc()+1) % 2;
assert(outputPorts.size() > 0);
for (auto it = outputPorts.cbegin(); it != outputPorts.cend(); ++it) {
u32 outputPort = *it;
// if arrived, select all VCs in the output port
for (u32 vc = 0; vc < numVcs_; vc++) {
_response->add(outputPort, vc);
}
// else, pump up the VC set by one
for (u32 vc = vcSet; vc < numVcs_; vc+=2) {
_response->add(outputPort, vc);
}
}
}
} // namespace HyperX