-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconnectionmanager.cpp
396 lines (328 loc) · 10.5 KB
/
connectionmanager.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include <stdlib.h>
#include "connectionmanager.h"
#include "fileformats/generaldefines.h"
#include "convertcoordinate.h"
/**
* Constructor
* @param param the heuristic settings
*/
connectionManager::connectionManager(heuristicparameters* heur)
{
heuristic = heur;
for(int i = 0; i < heuristic->connPoolStart; i++)
{
//pool.available.push_back(i);
pool.increaseAvailable(0 /*ATYPE*/, heuristic->connPoolStart);
pool.increaseAvailable(1 /*YTYPE*/, heuristic->connPoolStart);
}
}
/**
* Add a specific connection to the manager
* @param boxType type of connection
* @param boxPins a pair of pins representing the connection
* @return the number of the connection
*/
int connectionManager::addConnection(/*is this still used?*/int boxType2, pinpair boxPins)
{
int boxType = boxPins.getType();
if(!pool.sufficientAvailable(boxType))
{
if(!pool.increaseAvailable(boxType, heuristic->maxConnections))
{
return -1;
}
}
//19.05.2017
// size_t connNr = pool.reserveConnection(boxType);
/*
* NEEDED TO SIM ASAP
* 21.02.2017
* considering that connection number zero is at width coordinate zero
*/
long cw = boxPins.getPinDetail(SOURCEPIN).coord[CIRCUITWIDTH];
if(cw < 0)
{
cw = 0;
}
size_t approxConnectionNumber = cw / DELTA;
size_t connNr = pool.reserveConnectionPreferred(boxType, approxConnectionNumber);
/*
* Increase the length of the vector managing the connection pins
* if the connection number returned is higher than it size
*/
if(connectionPins.size() <= connNr)
{
connectionPins.resize(connNr + 1);
}
connectionPins[connNr] = boxPins;
return connNr;
}
bool connectionManager::assignOperationToConnection(int operationId, int boxType)
{
if(pool.sufficientToConsume(boxType))
{
int connNr = pool.consumeConnection(boxType);
assignedIdToConnection[operationId] = connNr;
printf("3.11.2017: assigned conn %d to %d\n", connNr, operationId);
return true;
}
return false;
}
void connectionManager::removeConnections(int boxType, std::vector<int>& operationIds)
{
for(size_t i=0; i<operationIds.size(); i++)
{
int operationId = operationIds[i];
int connNr = assignedIdToConnection[operationId];
assignedIdToConnection.erase(operationId);
pool.preReleaseConnection(boxType, connNr);
}
}
void connectionManager::releaseIfNotUsed(long inputTimeCoordinate, long connectionPinHeight, Pathfinder& pathfinder, bool checkInPoints)
{
for(int boxType=0; boxType<2; boxType++)
{
std::set<size_t> toRelease;
for(std::set<size_t>::iterator it = pool.toBeAvailable[boxType].begin();
it != pool.toBeAvailable[boxType].end(); it++)
{
/*compute connection pin coordinates based on heuristic and depth*/
int connNr = *it;
pinpair connPins = getConnectionPinPair(connNr, inputTimeCoordinate, connectionPinHeight);
printf("712: check conn %d @ coord %d\n", connNr, inputTimeCoordinate);
bool isReleased = true;
if(checkInPoints)
{
for(int i = 0; i < 2; i++)
{
Point* point = pathfinder.getOrCreatePoint(connPins.getPinDetail(i).coord[CIRCUITWIDTH],
connPins.getPinDetail(i).coord[CIRCUITDEPTH],
connPins.getPinDetail(i).coord[CIRCUITHEIGHT], false /*no boxes should exist in future*/);
printf("712: %s\n", connPins.getPinDetail(i).coord.toString(',').c_str());
isReleased = isReleased && point->isFree();
}
}
if(isReleased)
{
toRelease.insert(connNr);
}
}
for(std::set<size_t>::iterator it = toRelease.begin();
it != toRelease.end(); it++)
{
int connNr = *it;
pool.releaseConnection(boxType, connNr);
}
}
}
pinpair connectionManager::getConnectionPinPair(int connNr, long time, long height)
{
/*
* TODO: A class for coordinate system conversions
*/
//19.05.2017
// int blockHeight = 100;
// int heightPositionParity = (connNr/blockHeight)%2;
// int heightAdd = heightPositionParity * blockHeight + (1 - heightPositionParity*2) * connNr%blockHeight;
int heightAdd = 0;
height = height + heightAdd;
// pindetails pin1;
// pin1.coord[CIRCUITWIDTH] = connNr * DELTA + 1;
// pin1.coord[CIRCUITDEPTH] = (time + 0) * DELTA + 1;
// pin1.coord[CIRCUITHEIGHT] = height * DELTA + 1;
//
// pindetails pin2;
// pin2.coord[CIRCUITWIDTH] = connNr * DELTA + 1;
// pin2.coord[CIRCUITDEPTH] = (time + 1) * DELTA + 1;
// pin2.coord[CIRCUITHEIGHT] = height * DELTA + DELTA + 1;
/**
* 3 NOV RESOURCE Estimator
* Dual coords
*/
// pindetails pin1;
// pin1.coord[CIRCUITWIDTH] = 2*connNr * DELTA + 1;
// pin1.coord[CIRCUITDEPTH] = (time + 0) * DELTA + 1;
// pin1.coord[CIRCUITHEIGHT] = height * DELTA + 1;
//
// pindetails pin2;
// pin2.coord[CIRCUITWIDTH] = (2*connNr + 1)* DELTA + 1;
// pin2.coord[CIRCUITDEPTH] = (time + 0) * DELTA + 1;
// pin2.coord[CIRCUITHEIGHT] = height * DELTA + 1;
pindetails pin1;
pin1.coord[CIRCUITWIDTH] = 2*connNr * DELTA + 4;
pin1.coord[CIRCUITDEPTH] = (time + 0) * DELTA + 4;
pin1.coord[CIRCUITHEIGHT] = height * DELTA + 4;
pindetails pin2;
pin2.coord[CIRCUITWIDTH] = (2*connNr + 1) * DELTA + 4;
pin2.coord[CIRCUITDEPTH] = (time + 0) * DELTA + 4;
pin2.coord[CIRCUITHEIGHT] = height * DELTA + 4;
pinpair newPins;
newPins.setPinDetail(0, pin1);
newPins.setPinDetail(1, pin2);
return newPins;
}
void connectionManager::updateConnections(int typeOfConnection, long timeWhenPoolEnds, long heightIncrement, std::vector<pinpair>& toconn)
{
std::set<size_t> usedConnNrs;
switch(typeOfConnection)
{
case EXTENDCONNECTION:
usedConnNrs = pool.getAssigned();
break;
case BOXCONNECTION:
usedConnNrs = pool.getReservedButNotAssigned();
break;
case CIRCUITCONNECTION:
break;
}
long timeCoordinate = timeWhenPoolEnds;
// long heightIncrement = heuristic->connectionHeight;
for(std::set<size_t>::iterator it = usedConnNrs.begin();
it != usedConnNrs.end(); it++)
{
size_t connNr = *it;
/*
* The new coordinates of the connection.
*/
pinpair newPins = getConnectionPinPair(connNr, timeCoordinate, heightIncrement);
/*
* Form two pairs between the new pins and the old pins
* Save the pairs in the toConn list
*/
pinpair oldPins = connectionPins[connNr];
//TODO: a second parallel layer of connection pool
// //22.05.2017
// if(oldPins.getPinDetail(SOURCEPIN).coord[CIRCUITHEIGHT] < 0)
// {
// newPins.getPinDetail(DESTPIN).coord[CIRCUITHEIGHT] -= 60 * DELTA;
// newPins.getPinDetail(SOURCEPIN).coord[CIRCUITHEIGHT] -= 60 * DELTA;
// }
/*
* form pairs
*/
std::vector<pinpair> pairs = formPairs(oldPins, newPins);
if(typeOfConnection == BOXCONNECTION
&& oldPins.getPinDetail(0).coord[CIRCUITWIDTH] < newPins.getPinDetail(0).coord[CIRCUITWIDTH])
{
/*
* 3 NOV
* the connection pool rail has a circuitwidth coordinate higher than the box
* and the connections will intersect
* swap the pins
*/
pindetails dettmp = pairs[0].getPinDetail(0);
pairs[0].setPinDetail(0, pairs[1].getPinDetail(0));
pairs[1].setPinDetail(0, dettmp);
}
//22.05.2017
//do insertion sort?
/*
* insertion sort depending on distance
* use distance between a single pin pair
* the other distance should be similar
*/
int distIns = pairs[0].minDistBetweenPins();
/* distIns is allowed to be zero
* will be filtered when the connections are computed
* in pinConnector (connectpins)
*/
std::vector<pinpair>::iterator iti = toconn.begin();// + startInsPos;
for( ; iti != toconn.end(); iti++)
{
int distCurr = iti->minDistBetweenPins();
if(distIns < distCurr)
{
// printf("%d < %d\n", distIns, distCurr);
toconn.insert(iti, pairs.begin(), pairs.end());
break;
}
}
if(iti == toconn.end())
{
toconn.insert(iti, pairs.begin(), pairs.end());
}
/*
* Update the pins of the connection
*/
connectionPins[connNr] = newPins;
printf("update connection connect %d %s %s\n", connNr,
pairs[0].getPinDetail(DESTPIN).coord.toString(',').c_str(),
pairs[1].getPinDetail(DESTPIN).coord.toString(',').c_str());
}
}
bool connectionManager::finaliseAssignedConnections(bfsState& state, std::vector<pinpair>& circPins,
std::vector<pinpair>& constructedListOfConnections)
{
std::vector<int> toRemOperationIds[2];
for(std::map<int, int>::iterator it = state.operationIdToCircuitPinIndex.begin();
it != state.operationIdToCircuitPinIndex.end(); it++)
{
int operationId = it->first;
int circPinIndex = it->second;
if(assignedIdToConnection.find(operationId) == assignedIdToConnection.end())
{
//for some buggy reason, an operation was not assigned a connection
// toRemOperationIds.clear();
printf(" fAILED %d\n", operationId);
return false;
}
//get connection of operationid
int connNr = assignedIdToConnection[operationId];
printf("PAIR id%d circ%d conn%d\n", operationId, circPinIndex, connNr);
pinpair dist = connectionPins[connNr];
pinpair circ = circPins[circPinIndex];
printf("712 circ coord %d\n", circ.getPinDetail(0).coord[CIRCUITDEPTH]);
int boxType = circ.getType();
std::vector<pinpair> pairs = formPairs(dist, circ);
//01.11.2017
pairs[0].allowConnectionThroughChannel = true;
pairs[1].allowConnectionThroughChannel = true;
constructedListOfConnections.insert(constructedListOfConnections.end(), pairs.begin(), pairs.end());
/*
* Free the connection
*/
//removeConnection(operationId);
toRemOperationIds[boxType].push_back(operationId);
}
//for these operation ids, the connections are finished
state.operationIdToCircuitPinIndex.clear();
removeConnections(0, toRemOperationIds[0]);
toRemOperationIds[0].clear();
removeConnections(1, toRemOperationIds[1]);
toRemOperationIds[1].clear();
return true;
}
std::vector<pinpair> connectionManager::formPairs(pinpair& source, pinpair& destination)
{
std::vector<pinpair> ret;
// pinpair pair1;
// pair1.setPinDetail(SOURCEPIN, source.getPinDetail(0));
// pair1.setPinDetail(DESTPIN, destination.getPinDetail(0));
//
// pinpair pair2;
// pair2.setPinDetail(SOURCEPIN, source.getPinDetail(1));
// pair2.setPinDetail(DESTPIN, destination.getPinDetail(1));
/*
* 3 NOV RESOURCE ESTIM
*/
pinpair pair1;
pair1.setPinDetail(SOURCEPIN, source.getPinDetail(1));
pair1.setPinDetail(DESTPIN, destination.getPinDetail(1));
pinpair pair2;
pair2.setPinDetail(SOURCEPIN, source.getPinDetail(0));
pair2.setPinDetail(DESTPIN, destination.getPinDetail(0));
ret.push_back(pair1);
ret.push_back(pair2);
return ret;
}
int connectionManager::getUnusedReservedNr(int boxType)
{
return pool.reservedButNotAssigned[boxType].size();
}
size_t connectionManager::getCurrentNumberOfConnections()
{
/*
* Will constantly increase. Never decrease. See how to use with pool together
*/
return connectionPins.size();
}