-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
334 lines (283 loc) · 8.34 KB
/
Copy pathmain.cpp
File metadata and controls
334 lines (283 loc) · 8.34 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
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
#include <iostream>
#include <fstream>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <chrono>
#include <set>
#include "main.h"
#include "router.h"
//
using namespace std;
#define SFML_STATIC
int main(int argc, char **argv)
{
int gridx, gridy; //Grid sizes
int numCells;
int numWires;
int maxPins = 0;
char *filename;
ifstream infile;
if(argc < 2)
{
cout << "Invalid number of arguments: using default" << endl;
filename = "benchmarks/stdcell.txt";
}
else
{
filename = argv[1];
}
infile.open(filename);
if(!infile)
{
printf("Unable to open file\n");
exit(1);
}
infile >> gridx;
infile >> gridy;
infile >> numCells;
//Read in cell information
int **cells = new int *[numCells];
for (int i = 0; i < numCells; i++)
{
cells[i] = new int[2];
infile >> cells[i][0];
infile >> cells[i][1];
}
//Read in net/wire information
infile >> numWires;
Wire *W = new Wire[numWires];
Point **points = new Point *[gridx];
for (int i = 0; i < numWires; i++)
{
infile >> W[i].numPins;
maxPins = maxPins < W[i].numPins ? W[i].numPins : maxPins;
W[i].r = rand() % 255;
W[i].g = rand() % 255;
W[i].b = rand() % 255;
W[i].pins = new int *[W[i].numPins];
W[i].found = vector<int>(W[i].numPins);
for (int j = 0; j < W[i].numPins; j++)
{
W[i].pins[j] = new int[2];
infile >> W[i].pins[j][0];
infile >> W[i].pins[j][1];
}
}
init(points, W, gridx, gridy, numWires, numCells, cells); //Initialise search space
vector<vector<pair<int, int>>>edges(numWires);
int numEdges = spanningTree(W, numWires, edges); //Calculate the spanning tree for each net
priority_queue<pair<int, int>>PQ;
//Calculate the bounding box for each net and store in the priority queue (highest first)
vector<BoundingBox> BB(numWires);
for(int i = 0; i < numWires; i++)
{
BB[i] = boundingBox(W[i]);
PQ.push(make_pair(BB[i].area, i));
}
vector<vector<int>> dependencyList(numWires);
vector<bool> done(numWires);
BoundingBox B;
while(!PQ.empty())
{
B = BB[PQ.top().second];
done[PQ.top().second] = true;
for(int i = 0; i < numWires; i++)
{
//Check each other net
if(i != PQ.top().second && !done[i])
{
//Any that have overlap are dependent on this net
if(hasOverlap(B, BB[i]))
{
dependencyList[i].push_back(PQ.top().second);
}
}
}
PQ.pop();
}
int count = 0;
vector<int> routeList;
//Put any with no overlap on the route list
for(int i = 0; i < numWires; i++)
{
if(dependencyList[i].empty())
{
routeList.push_back(i);
count ++;
}
}
auto start = std::chrono::high_resolution_clock::now();
for(int i = 0; i < 50; i++)
{
schedule(points, W, edges, dependencyList, routeList, BB, gridx, gridy, numEdges, numWires);
}
auto finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = finish - start;
cout << "CUDA Time: " << elapsed.count() << "s\n";
init(points, W, gridx, gridy, numWires, numCells, cells);
for (int i = 0; i < numWires; i++)
{
PQ.push(make_pair(BB[i].area, i));
}
routeList.clear();
while(!PQ.empty())
{
routeList.push_back(PQ.top().second);
PQ.pop();
}
start = std::chrono::high_resolution_clock::now();
for(int i = 0; i < 50; i++)
{
sequentialSchedule(points, W, edges, dependencyList, routeList, BB, gridx, gridy, numEdges, numWires);
}
finish = std::chrono::high_resolution_clock::now();
elapsed = finish - start;
cout << "Sequential Time: " << elapsed.count() << "s\n";
return 0;
}
//Determines if 2 bounding boxes overlap by comparing the edges
bool hasOverlap(BoundingBox a, BoundingBox b)
{
if((a.minx - 5) < (b.maxx + 5) && (a.maxx + 5) > (b.minx - 5)
&& (a.miny - 5) < (b.maxy + 5) && (a.maxy + 5) > (b.miny - 5))
{
return true;
}
return false;
}
//Calculate the bounding for for every pin in this net
BoundingBox boundingBox(Wire W)
{
int maxX = -1E06;
int minX = 1E06;
int maxY = -1E06;
int minY = 1E06;
int mxxi = -1;
int mnxi = -1;
int mxyi = -1;
int mnyi = -1;
for(int i = 0; i < W.numPins; i++)
{
mnxi = W.pins[i][0] < minX ? i : mnxi;
minX = W.pins[i][0] < minX ? W.pins[i][0] : minX;
mxxi = W.pins[i][0] > maxX ? i : mxxi;
maxX = W.pins[i][0] > maxX ? W.pins[i][0] : maxX;
mnyi = W.pins[i][1] < minY ? i : mnyi;
minY = W.pins[i][1] < minY ? W.pins[i][1] : minY;
mxyi = W.pins[i][1] > maxY ? i : mxyi;
maxY = W.pins[i][1] > maxY ? W.pins[i][1] : maxY;
}
BoundingBox BB;
BB.maxx = maxX;
BB.maxy = maxY;
BB.minx = minX;
BB.miny = minY;
BB.area = (maxX - minX) * (maxY - minY);
return BB;
}
//Calculate the spanning tree using Prim's algorithm
int spanningTree(Wire *W, int numWires, vector<vector<pair<int, int>>> &edges)
{
int count = 0; //Counts the total number of edges
set<int> pins;
vector<vector<vector<int>>> adj(numWires);
//Convert to adjacency matrix
for (int i = 0; i < numWires; i++)
{
adj[i] = vector<vector<int>>(W[i].numPins);
for (int j = 0; j < W[i].numPins; j++)
{
adj[i][j] = vector<int>(W[i].numPins);
for (int k = 0; k < W[i].numPins; k++)
{
adj[i][j][k] = abs(W[i].pins[j][0] - W[i].pins[k][0]) + abs(W[i].pins[j][1] - W[i].pins[k][1]);
}
}
}
for (int i = 0; i < numWires; i++)
{
vector<vector<int>> Adj = adj[i];
int numEdge = 0;
vector<bool> sel(W[i].numPins);
sel[0] = true;
int x, y;
while (numEdge < W[i].numPins - 1) //Tree always has N - 1 edges
{
int min = 1E09;
x = 0;
y = 0;
for (int j = 0; j < W[i].numPins; j++)
{
if (sel[j])
{
for (int k = 0; k < W[i].numPins; k++)
{
//find the minimum cost to any pins that haven't already been selected
if (!sel[k] && Adj[j][k])
{
if (min > Adj[j][k])
{
min = Adj[j][k];
x = j;
y = k;
}
}
}
}
}
edges[i].push_back(make_pair(x, y)); //Push the minimum cost edge
sel[y] = true;
numEdge++;
count++;
}
}
return count;
}
void init(Point **points, Wire *W, int gridx, int gridy, int numWires, int numCells, int **cells)
{
for (int i = 0; i < gridx; i++)
{
points[i] = new Point[gridy];
for (int j = 0; j < gridy; j++)
{
points[i][j].x = i;
points[i][j].y = j;
points[i][j].dist = 0;
points[i][j].target = false;
points[i][j].expanded = false;
points[i][j].obstructed = false;
points[i][j].obstructedBy = -2;
points[i][j].prev = NULL;
points[i][j].f = 1E06;
}
}
int x, y, np;
//Set sinks and pins as obstructions
for (int i = 0; i < numWires; i++)
{
np = W[i].numPins;
W[i].counter = new int[numWires];
for (int j = 0; j < np; j++)
{
x = W[i].pins[j][0];
y = W[i].pins[j][1];
points[x][y].obstructed = true;
points[x][y].obstructedBy = i;
}
for (int j = 0; j < numWires; j++)
{
W[i].counter[j] = 0;
}
}
//Set cells as obstructions
for (int i = 0; i < numCells; i++)
{
x = cells[i][0];
y = cells[i][1];
points[x][y].obstructed = true;
points[x][y].obstructedBy = -1;
}
}