forked from The-OpenROAD-Project/OpenSTA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bfs.cc
427 lines (386 loc) · 9.75 KB
/
Bfs.cc
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// OpenSTA, Static Timing Analyzer
// Copyright (c) 2023, Parallax Software, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "Bfs.hh"
#include "Report.hh"
#include "Debug.hh"
#include "Mutex.hh"
#include "DispatchQueue.hh"
#include "Network.hh"
#include "Graph.hh"
#include "Sdc.hh"
#include "Levelize.hh"
#include "SearchPred.hh"
namespace sta {
BfsIterator::BfsIterator(BfsIndex bfs_index,
Level level_min,
Level level_max,
SearchPred *search_pred,
StaState *sta) :
StaState(sta),
bfs_index_(bfs_index),
level_min_(level_min),
level_max_(level_max),
search_pred_(search_pred)
{
init();
}
void
BfsIterator::init()
{
first_level_ = level_max_;
last_level_ = level_min_;
ensureSize();
}
void
BfsIterator::ensureSize()
{
if (levelize_->levelized()) {
unsigned max_level_1 = levelize_->maxLevel() + 1;
if (queue_.size() < max_level_1)
queue_.resize(max_level_1);
}
}
BfsIterator::~BfsIterator()
{
}
void
BfsIterator::clear()
{
Level level = first_level_;
while (levelLessOrEqual(level, last_level_)) {
VertexSeq &level_vertices = queue_[level];
for (auto vertex : level_vertices) {
if (vertex)
vertex->setBfsInQueue(bfs_index_, false);
}
level_vertices.clear();
incrLevel(level);
}
init();
}
void
BfsIterator::reportEntries(const Network *network)
{
Level level = first_level_;
while (levelLessOrEqual(level, last_level_)) {
VertexSeq &level_vertices = queue_[level];
if (!level_vertices.empty()) {
printf("Level %d\n", level);
for (auto vertex : level_vertices) {
if (vertex)
printf(" %s\n", vertex->name(network));
}
}
incrLevel(level);
}
}
void
BfsIterator::deleteEntries(Level level)
{
VertexSeq &level_vertices = queue_[level];
for (auto vertex : level_vertices) {
if (vertex)
vertex->setBfsInQueue(bfs_index_, false);
}
level_vertices.clear();
}
bool
BfsIterator::empty() const
{
return levelLess(last_level_, first_level_);
}
void
BfsIterator::enqueueAdjacentVertices(Vertex *vertex)
{
enqueueAdjacentVertices(vertex, search_pred_, level_max_);
}
void
BfsIterator::enqueueAdjacentVertices(Vertex *vertex,
SearchPred *search_pred)
{
enqueueAdjacentVertices(vertex, search_pred, level_max_);
}
void
BfsIterator::enqueueAdjacentVertices(Vertex *vertex,
Level to_level)
{
enqueueAdjacentVertices(vertex, search_pred_, to_level);
}
int
BfsIterator::visit(Level to_level,
VertexVisitor *visitor)
{
int visit_count = 0;
while (levelLessOrEqual(first_level_, last_level_)
&& levelLessOrEqual(first_level_, to_level)) {
VertexSeq &level_vertices = queue_[first_level_];
incrLevel(first_level_);
if (!level_vertices.empty()) {
for (auto vertex : level_vertices) {
if (vertex) {
vertex->setBfsInQueue(bfs_index_, false);
visitor->visit(vertex);
visit_count++;
}
}
level_vertices.clear();
visitor->levelFinished();
}
}
return visit_count;
}
int
BfsIterator::visitParallel(Level to_level,
VertexVisitor *visitor)
{
size_t thread_count = thread_count_;
int visit_count = 0;
if (!empty()) {
if (thread_count == 1)
visit_count = visit(to_level, visitor);
else {
std::vector<VertexVisitor*> visitors;
for (int k = 0; k < thread_count_; k++)
visitors.push_back(visitor->copy());
while (levelLessOrEqual(first_level_, last_level_)
&& levelLessOrEqual(first_level_, to_level)) {
VertexSeq &level_vertices = queue_[first_level_];
incrLevel(first_level_);
if (!level_vertices.empty()) {
size_t vertex_count = level_vertices.size();
if (vertex_count < thread_count) {
for (Vertex *vertex : level_vertices) {
if (vertex) {
vertex->setBfsInQueue(bfs_index_, false);
visitor->visit(vertex);
}
}
}
else {
size_t from = 0;
size_t chunk_size = vertex_count / thread_count;
for (size_t k = 0; k < thread_count; k++) {
// Last thread gets the left overs.
size_t to = (k == thread_count - 1) ? vertex_count : from + chunk_size;
dispatch_queue_->dispatch( [=](int) {
for (size_t i = from; i < to; i++) {
Vertex *vertex = level_vertices[i];
if (vertex) {
vertex->setBfsInQueue(bfs_index_, false);
visitors[k]->visit(vertex);
}
}
});
from = to;
}
dispatch_queue_->finishTasks();
}
visitor->levelFinished();
level_vertices.clear();
}
}
for (VertexVisitor *visitor : visitors)
delete visitor;
}
}
return visit_count;
}
bool
BfsIterator::hasNext()
{
return hasNext(last_level_);
}
bool
BfsIterator::hasNext(Level to_level)
{
findNext(to_level);
return levelLessOrEqual(first_level_, last_level_)
&& !queue_[first_level_].empty();
}
Vertex *
BfsIterator::next()
{
VertexSeq &level_vertices = queue_[first_level_];
Vertex *vertex = level_vertices.back();
level_vertices.pop_back();
vertex->setBfsInQueue(bfs_index_, false);
return vertex;
}
void
BfsIterator::findNext(Level to_level)
{
while (levelLessOrEqual(first_level_, last_level_)
&& levelLessOrEqual(first_level_, to_level)
&& queue_[first_level_].empty())
incrLevel(first_level_);
}
void
BfsIterator::enqueue(Vertex *vertex)
{
debugPrint(debug_, "bfs", 2, "enqueue %s", vertex->name(sdc_network_));
if (!vertex->bfsInQueue(bfs_index_)) {
Level level = vertex->level();
UniqueLock lock(queue_lock_);
if (!vertex->bfsInQueue(bfs_index_)) {
vertex->setBfsInQueue(bfs_index_, true);
queue_[level].push_back(vertex);
if (levelLess(last_level_, level))
last_level_ = level;
if (levelLess(level, first_level_))
first_level_ = level;
}
}
}
bool
BfsIterator::inQueue(Vertex *vertex)
{
// checkInQueue(vertex);
return vertex->bfsInQueue(bfs_index_);
}
void
BfsIterator::checkInQueue(Vertex *vertex)
{
Level level = vertex->level();
if (static_cast<Level>(queue_.size()) > level) {
for (auto v : queue_[level]) {
if (v == vertex) {
if (vertex->bfsInQueue(bfs_index_))
return;
else
printf("extra %s\n", vertex->name(sdc_network_));
}
}
}
if (vertex->bfsInQueue(bfs_index_))
printf("missing %s\n", vertex->name(sdc_network_));
}
void
BfsIterator::deleteVertexBefore(Vertex *vertex)
{
remove(vertex);
}
// Remove by inserting null vertex pointer.
void
BfsIterator::remove(Vertex *vertex)
{
// If the iterator has not been inited the queue will be empty.
Level level = vertex->level();
if (vertex->bfsInQueue(bfs_index_)
&& static_cast<Level>(queue_.size()) > level) {
for (auto &v : queue_[level]) {
if (v == vertex) {
v = nullptr;
vertex->setBfsInQueue(bfs_index_, false);
break;
}
}
}
}
////////////////////////////////////////////////////////////////
BfsFwdIterator::BfsFwdIterator(BfsIndex bfs_index,
SearchPred *search_pred,
StaState *sta) :
BfsIterator(bfs_index, 0, level_max, search_pred, sta)
{
}
// clear() without saving lists to list_free_.
BfsFwdIterator::~BfsFwdIterator()
{
for (Level level = first_level_; level <= last_level_; level++)
deleteEntries(level);
}
void
BfsFwdIterator::incrLevel(Level &level)
{
level++;
}
bool
BfsFwdIterator::levelLessOrEqual(Level level1,
Level level2) const
{
return level1 <= level2;
}
bool
BfsFwdIterator::levelLess(Level level1,
Level level2) const
{
return level1 < level2;
}
void
BfsFwdIterator::enqueueAdjacentVertices(Vertex *vertex,
SearchPred *search_pred,
Level to_level)
{
if (search_pred->searchFrom(vertex)) {
VertexOutEdgeIterator edge_iter(vertex, graph_);
while (edge_iter.hasNext()) {
Edge *edge = edge_iter.next();
Vertex *to_vertex = edge->to(graph_);
if (to_vertex->level() <= to_level
&& search_pred->searchThru(edge)
&& search_pred->searchTo(to_vertex))
enqueue(to_vertex);
}
}
}
////////////////////////////////////////////////////////////////
BfsBkwdIterator::BfsBkwdIterator(BfsIndex bfs_index,
SearchPred *search_pred,
StaState *sta) :
BfsIterator(bfs_index, level_max, 0, search_pred, sta)
{
}
// clear() without saving lists to list_free_.
BfsBkwdIterator::~BfsBkwdIterator()
{
for (Level level = first_level_; level >= last_level_; level--)
deleteEntries(level);
}
void
BfsBkwdIterator::incrLevel(Level &level)
{
level--;
}
bool
BfsBkwdIterator::levelLessOrEqual(Level level1,
Level level2) const
{
return level1 >= level2;
}
bool
BfsBkwdIterator::levelLess(Level level1,
Level level2) const
{
return level1 > level2;
}
void
BfsBkwdIterator::enqueueAdjacentVertices(Vertex *vertex,
SearchPred *search_pred,
Level to_level)
{
if (search_pred->searchTo(vertex)) {
VertexInEdgeIterator edge_iter(vertex, graph_);
while (edge_iter.hasNext()) {
Edge *edge = edge_iter.next();
Vertex *from_vertex = edge->from(graph_);
if (from_vertex->level() >= to_level
&& search_pred->searchFrom(from_vertex)
&& search_pred->searchThru(edge))
enqueue(from_vertex);
}
}
}
} // namespace