-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathFD.py
1357 lines (1213 loc) · 55 KB
/
FD.py
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#######################################################################################
### Author: Zhiang Wang, zhw033@ucsd.edu
### Version: 0.1 (2022/11/20)
### Please make sure you have installed Circuit Training before running this script
######################################################################################
import sys
import os
import random
from math import log
from copy import deepcopy
from math import floor
from math import ceil
from math import exp
from math import sqrt
from math import pow
import time
import matplotlib.pyplot as plt
# utility function for visualization
sys.path.append('../VisualPlacement/')
from visual_placement import VisualPlacement
# Please replace this with your own circuit training directory
#
# Check Here !!!
#
sys.path.append('/home/zf4_projects/DREAMPlace/sakundu/GB/CT/circuit_training')
sys.path.append('/home/zf4_projects/DREAMPlace/sakundu/GB/CT/')
#sys.path.append('xxxxx/CT/circuit_training')
#sys.path.append('xxxxx/CT/')
from absl import flags
from circuit_training.grouping import grid_size_selection
from circuit_training.environment import plc_client
from circuit_training.grouping import grouper
# Define the class to handle netlist in Protocol Buffer Format
# Basic data structure
# OrientMap : map the orientation
# PlcObject : a superset of attributes for different types of plc objects
# PBFNetlist: the top-level class for handling netlist in protocol buffer netlist
# ***************************************************************
# Define basic classes
# ***************************************************************
# Define the orientation map
OrientMap = {
"N" : "R0",
"S" : "R180",
"W" : "R90",
"E" : "R270",
"FN" : "MY",
"FS" : "MX",
"FW" : "MX90",
"FE" : "MY90"
}
# String Helper for string and float value#
def print_placeholder(key, value):
line = " attr {\n"
line += f' key: "{key}"\n'
line += ' value {\n'
line += f' placeholder: {value}\n'
line += ' }\n'
line += ' }\n'
return line
def print_float(key, value):
value = round(value, 6)
line = " attr {\n"
line += f' key: "{key}"\n'
line += ' value {\n'
line += f' f: {value}\n'
line += ' }\n'
line += ' }\n'
return line
class Grid:
def __init__(self, grid_width, grid_height, num_grids_per_row, num_grids_per_col, x_idx, y_idx, smooth_factor = 0):
self.x_idx = x_idx
self.y_idx = y_idx
self.grid_width = grid_width
self.grid_height = grid_height
self.num_grids_per_row = num_grids_per_row
self.num_grids_per_col = num_grids_per_col
self.overlap_area = 0.0
self.hor_congestion = 0.0
self.ver_congestion = 0.0
self.id = self.GetId()
self.lx, self.ly, self.ux, self.uy = self.GetBBox()
self.x = (self.lx + self.ux) / 2.0
self.y = (self.ly + self.uy) / 2.0
self.smooth_hor_congestion = 0.0
self.smooth_ver_congestion = 0.0
self.smooth_factor = smooth_factor
self.macro_hor_congestion = 0.0
self.macro_ver_congestion = 0.0
self.available = True # if the grid has been occupied by a hard macro
# reset the status of grids
def Reset(self):
self.overlap_area = 0.0
self.hor_congestion = 0.0
self.ver_congestion = 0.0
self.smooth_hor_congestion = 0.0
self.smooth_ver_congestion = 0.0
self.macro_hor_congestion = 0.0
self.macro_ver_congestion = 0.0
# Smoothing the congestion
def UpdateSCongestion(self):
# smooth horizontal congestion
h_start = max(0, self.x_idx - self.smooth_factor)
h_end = min(self.x_idx + self.smooth_factor, self.num_grids_per_row - 1)
self.smooth_hor_congestion = self.hor_congestion
self.smooth_hor_congestion += self.hor_congestion / (h_end - h_start)
# smooth vertical congestion
v_start = max(0, self.y_idx - self.smooth_factor)
v_end = min(self.y_idx + self.smooth_factor, self.num_grids_per_col - 1)
self.smooth_ver_congestion = self.ver_congestion
self.smooth_ver_congestion += self.ver_congestion / (v_end - v_start)
def GetSCongH(self):
return self.smooth_hor_congestion
def GetSCongV(self):
return self.smooth_ver_congestion
# all grids are arranged in a row-based manner
def GetId(self):
return self.y_idx * self.num_grids_per_row + self.x_idx
# get bounding box
def GetBBox(self):
lx = self.x_idx * self.grid_width
ly = self.y_idx * self.grid_height
ux = lx + self.grid_width
uy = ly + self.grid_height
return lx, ly, ux, uy
# check overlap
def CalcOverlap(self, bbox):
lx, ly, ux, uy = bbox
x_overlap = min(ux, self.ux) - max(self.lx, lx)
y_overlap = min(uy, self.uy) - max(self.ly, ly)
if (x_overlap <= 0.0 or y_overlap <= 0.0):
return 0.0
else:
return x_overlap * y_overlap
def CalcHVOverlap(self, bbox):
lx, ly, ux, uy = bbox
x_overlap = min(ux, self.ux) - max(self.lx, lx)
y_overlap = min(uy, self.uy) - max(self.ly, ly)
x_overlap = max(0.0, x_overlap)
y_overlap = max(0.0, y_overlap)
return x_overlap, y_overlap
# True for Add
# False for Reduce
def UpdateOverlap(self, bbox, flag = False):
lx, ly, ux, uy = bbox
if (flag == True):
self.overlap_area += self.CalcOverlap(bbox)
else:
self.overlap_area -= self.CalcOverlap(bbox)
# calculate density
def GetDensity(self):
return self.overlap_area / (self.grid_width * self.grid_height)
# Update congestion
# True for Add, False for Reduce
def UpdateCongestionH(self, congestion, flag = False):
if (flag == True):
self.hor_congestion += congestion
else:
self.hor_congestion -= congestion
def UpdateCongestionV(self, congestion, flag = False):
if (flag == True):
self.ver_congestion += congestion
else:
self.ver_congestion -= congestion
# Get congestion
def GetCongestionH(self):
return self.hor_congestion
def GetCongestionV(self):
return self.ver_congestion
# Update Macro Congestion
def UpdateMacroCongH(self, congestion, flag = False):
if (flag == True):
self.macro_hor_congestion += congestion
else:
self.macro_hor_congestion -= congestion
def UpdateMacroCongV(self, congestion, flag = False):
if (flag == True):
self.macro_ver_congestion += congestion
else:
self.macro_ver_congestion -= congestion
# Get congestion
def GetMacroCongH(self):
return self.macro_hor_congestion
def GetMacroCongV(self):
return self.macro_ver_congestion
class Net:
def __init__(self, pins, grids, weight = 1.0):
# the first pin is always the source pin
self.pins = pins # pins of the net (each pin is a plc object)
self.weight = weight
self.grids = grids # references to the grid list
self.HPWL = 0.0
self.num_grids_per_row = 0
self.num_grids_per_col = 0
def Reset(self):
self.HPWL = 0.0
def GetHPWL(self, update_flag = False):
if (update_flag == True):
self.UpdateHPWL()
return self.HPWL
def UpdateHPWL(self):
if (len(self.pins) <= 1):
self.HPWL = 0.0
x_locs = []
y_locs = []
for pin in self.pins:
x_locs.append(pin.GetX())
y_locs.append(pin.GetY())
self.HPWL = self.weight * (max(x_locs) - min(x_locs) + max(y_locs) - min(y_locs))
x_min, y_min, x_max, y_max = 1e9, 1e9, -1e9, -1e9
for pin in self.pins:
x_min = min(x_min, pin.GetX())
y_min = min(y_min, pin.GetY())
x_max = max(x_max, pin.GetX())
y_max = max(y_max, pin.GetY())
self.HPWL = self.weight * (x_max - x_min + y_max - y_min)
# True for Add; False for Reduce
def UpdateRouting(self, flag = True):
if (len(self.pins) <= 1):
return
if (len(self.pins) == 2):
self.TwoPinNetRouting(self.pins[0], self.pins[1], flag)
elif (len(self.pins) == 3):
# sort pins based on column id
sorted(self.pins, key=lambda pin: pin.GetColId())
col_id_1, row_id_1 = self.pins[0].GetGridId()
col_id_2, row_id_2 = self.pins[1].GetGridId()
col_id_3, row_id_3 = self.pins[2].GetGridId()
# check for different cases
if (col_id_1 < col_id_2 and col_id_2 < col_id_3 and min(row_id_1, row_id_3) < row_id_2 and max(row_id_1, row_id_3) > row_id_2):
self.LRouting(flag)
elif (col_id_2 == col_id_3 and col_id_1 < col_id_2 and row_id_1 < min(row_id_2, row_id_3)):
# update horizontal congestion cost
for i in range(col_id_1, col_id_2):
self.grids[row_id_1 * self.num_grids_per_row + i].UpdateCongestionH(self.weight, flag)
# update vertical congestion cost
for j in range(row_id_1, max(row_id_2, row_id_3)):
self.grids[j * self.num_grids_per_row + col_id_2].UpdateCongestionV(self.weight, flag)
elif (row_id_2 == row_id_3):
# update horizontal congestion cost
for i in range(col_id_1, col_id_2):
self.grids[row_id_1 * self.num_grids_per_row + i].UpdateCongestionH(self.weight, flag)
for i in range(col_id_2, col_id_3):
self.grids[row_id_2 * self.num_grids_per_row + i].UpdateCongestionH(self.weight, flag)
# update vertical congestion cost
for j in range(min(row_id_2, row_id_3), max(row_id_2, row_id_3)):
self.grids[j * self.num_grids_per_row + col_id_2].UpdateCongestionV(self.weight, flag)
else:
self.TRouting(flag)
else:
# decompose the multiple-pin net into multiple two-pin nets
for i in range(1, len(self.pins)):
self.TwoPinNetRouting(self.pins[0], self.pins[i], flag)
# 2-pin net routing
# True for Add; False for Reduce
def TwoPinNetRouting(self, src_pin, sink_pin, flag = True):
src_col_idx, src_row_idx = src_pin.GetGridId()
sink_col_idx, sink_row_idx = sink_pin.GetGridId()
# horizontal congestion cost
min_col_idx = min(src_col_idx, sink_col_idx)
max_col_idx = min(src_col_idx, sink_col_idx)
for i in range(min_col_idx, max_col_idx):
self.grids[src_row_idx * self.num_grids_per_row + i].UpdateCongestionH(self.weight, flag)
# vertical congestion cost
min_row_idx = min(src_row_idx, sink_row_idx)
max_row_idx = max(src_row_idx, sink_row_idx)
for j in range(min_row_idx, max_row_idx):
self.grids[j * self.num_grids_per_row + sink_col_idx].UpdateCongestionV(self.weight, flag)
# L routig for 3-pin net
# True for Add; False for Reduce
def LRouting(self, flag = True):
col_id_1, row_id_1 = self.pins[0].GetGridId()
col_id_2, row_id_2 = self.pins[1].GetGridId()
col_id_3, row_id_3 = self.pins[2].GetGridId()
# add horizontal congestion cost
for i in range(col_id_1, col_id_2):
self.grids[row_id_1 * self.num_grids_per_row + i].UpdateCongestionH(self.weight, flag)
for i in range(col_id_2, col_id_3):
self.grids[row_id_2 * self.num_grids_per_row + i].UpdateCongestionH(self.weight, flag)
# add vertical congestion cost
for j in range(min(row_id_1, row_id_2), max(row_id_1, row_id_2)):
self.grids[j * self.num_grids_per_row + col_id_2].UpdateCongestionV(self.weight, flag)
for j in range(min(row_id_2, row_id_3), max(row_id_2, row_id_3)):
self.grids[j * self.num_grids_per_row + col_id_3].UpdateCongestionV(self.weight, flag)
# T routing for 3-pin net
# True for Add; False for reduce
def TRouting(self, flag = True):
sorted(self.pins, key=lambda pin: pin.GetRowId())
col_id_1, row_id_1 = self.pins[0].GetGridId()
col_id_2, row_id_2 = self.pins[1].GetGridId()
col_id_3, row_id_3 = self.pins[2].GetGridId()
col_id_min = min(col_id_1, col_id_2, col_id_3)
col_id_max = max(col_id_1, col_id_2, col_id_3)
# add horizontal congestion cost
for i in range(col_id_min, col_id_max):
self.grids[row_id_2 * self.num_grids_per_row + i].UpdateCongestionH(self.weight, flag)
# add vertical congestion cost
for j in range(min(row_id_1, row_id_2), max(row_id_1, row_id_2)):
self.grids[j * self.num_grids_per_row + col_id_1].UpdateCongestionV(self.weight, flag)
for j in range(min(row_id_2, row_id_3), max(row_id_2, row_id_3)):
self.grids[j * self.num_grids_per_row + col_id_3].UpdateCongestionV(self.weight, flag)
# Define the plc object
# This is a superset of attributes for different types of plc objects
# A plc object can only have some or all the attributes
# Please check Circuit Training repo (https://github.com/google-research/circuit_training/blob/main/docs/NETLIST_FORMAT.md) for detailed explanation
class PlcObject:
def __init__(self, id):
self.name = None
self.node_id = id
self.height = 0
self.width = 0
self.weight = 1
self.x = -1 # center of the object
self.x_offset = 0
self.y = -1 # center of the object
self.y_offset = 0
self.m_name = None # for macro name (only applied for pins)
self.m_node_id = -1 # the node id for macro (only applied for pins)
self.pb_type = None
self.side = None # only applied for IO ports
self.orientation = None
self.inputs = [] # Repeated field that lists all nodes driven by this pin
self.list_id = -1 # the attribute for placing plc objects in a list
self.nets = [] # nets (id) connected this plc_object
self.macro_object = None
self.n_cols = 0
self.n_rows = 0
self.grid_width = 0.0
self.grid_height = 0.0
self.f_x = 0.0
self.f_y = 0.0
def Move(self, x_disp, y_disp, canvas_width, canvas_height):
self.x += x_disp
self.y += y_disp
lx, ly, ux, uy = self.GetBBox()
if (lx <= 0.0 or ux >= canvas_width):
self.x -= x_disp
if (ly <= 0.0 or uy >= canvas_height):
self.y -= y_disp
def ResetForce(self):
self.f_x = 0.0
self.f_y = 0.0
def AddForce(self, f_x, f_y):
self.f_x += f_x
self.f_y += f_y
def GetForce(self):
return self.f_x, self.f_y
def IsHardMacro(self):
if (self.pb_type == '"MACRO"'):
return True
else:
return False
def IsSoftMacro(self):
if (self.pb_type == '"macro"'):
return True
else:
return False
def UpdateSquare(self):
area = self.width * self.height
self.width = sqrt(area)
self.height = sqrt(area)
def IsPort(self):
if (self.pb_type == '"PORT"'):
return True
else:
return False
def IsPin(self):
if (self.pb_type == '"MACRO_PIN"' or self.pb_type == '"macro_pin"'):
return True
else:
return False
def IsSoftMacroPin(self):
if (self.pb_type == '"macro_pin"'):
return True
else:
return False
# the center of object
def GetPos(self):
if (self.IsPin() == False):
return self.x, self.y
# check the orientation of macros
if (self.macro_object.orientation == "N"):
return self.macro_object.x + self.x_offset, self.macro_object.y + self.y_offset
elif (self.macro_object.orientation == "FN"):
return self.macro_object.x - self.x_offset, self.macro_object.y + self.y_offset
elif (self.macro_object.orientation == "S"):
return self.macro_object.x - self.x_offset, self.macro_object.y - self.y_offset
elif (self.macro_object.orientation == "FS"):
return self.macro_object.x + self.x_offset, self.macro_object.y - self.y_offset
elif (self.macro_object.orientation == "E"):
return self.macro_object.x + self.y_offset, self.macro_object.y - self.x_offset
elif (self.macro_object.orientation == "FE"):
return self.macro_object.x - self.y_offset, self.macro_object.y - self.x_offset
elif (self.macro_object.orientation == "FW"):
return self.macro_object.x - self.y_offset, self.macro_object.y + self.x_offset
elif (self.macro_object.orientation == "W"):
return self.macro_object.x + self.y_offset, self.macro_object.y + self.x_offset
else:
return self.macro_object.x + self.x_offset, self.macro_object.y + self.y_offset
def GetX(self):
x, y = self.GetPos()
return x
def GetY(self):
x, y = self.GetPos()
return y
def SetPos(self, x, y):
self.x = x
self.y = y
# get the bounding box of object
def GetBBox(self):
x, y = self.GetPos()
normal_orient_list = ["N", "FN", "S", "FS"]
reverse_orient_list = ["F", "FE", "W", "FW"]
width = self.width
height = self.height
if (self.orientation in reverse_orient_list):
width = self.height
height = self.width
lx = x - width / 2.0
ly = y - height / 2.0
ux = x + width / 2.0
uy = y + height / 2.0
return lx, ly, ux, uy
# get grid information
def GetColId(self):
col_id = floor(self.GetX() / self.grid_width)
col_id = max(col_id, 0)
col_id = min(col_id, self.n_cols - 1)
return col_id
def GetRowId(self):
row_id = floor(self.GetY() / self.grid_height)
row_id = max(row_id, 0)
row_id = min(row_id, self.n_rows - 1)
return row_id
def GetGridId(self):
return self.GetColId(), self.GetRowId()
# width respect to "N"
def GetWidth(self):
return self.width
# width respect to "N"
def GetHeight(self):
return self.height
def Flip(self, x_flag):
if (self.orientation == None):
return True
# flip across the x axis (x_flag = True)
if (x_flag == True):
if (self.orientation == "N"):
self.orientation = "FS"
elif (self.orientation == "FN"):
self.orientation = "S"
elif (self.orientation == "S"):
self.orientation = "FN"
elif (self.orientation == "FS"):
self.orientation = "N"
elif (self.orientation == "E"):
self.orientation = "FW"
elif (self.orientation == "FE"):
self.orientation = "W"
elif (self.orientation == "FW"):
self.orientation = "E"
elif (self.orientation == "W"):
self.orientation = "FE"
else:
self.orientation = None
else:
# flip across the y axis
if (self.orientation == "N"):
self.orientation = "FN"
elif (self.orientation == "FN"):
self.orientation = "N"
elif (self.orientation == "S"):
self.orientation = "FS"
elif (self.orientation == "FS"):
self.orientation = "S"
elif (self.orientation == "E"):
self.orientation = "FE"
elif (self.orientation == "FE"):
self.orientation = "E"
elif (self.orientation == "FW"):
self.orientation = "W"
elif (self.orientation == "W"):
self.orientation = "FW"
else:
self.orientation = None
# for protocol buffer netlist
def __str__(self):
self.str = ""
if (self.IsPort() == True):
self.str += "node {\n"
self.str += ' name: ' + self.name + '\n'
for sink in self.inputs:
self.str += ' input: ' + sink + '\n'
self.str += print_placeholder('side', self.side)
self.str += print_placeholder('type', self.pb_type)
self.str += print_float('x', self.x)
self.str += print_float('y', self.y)
self.str += "}\n"
elif (self.IsPin() == True):
self.str += "node {\n"
self.str += ' name: ' + self.name + '\n'
for sink in self.inputs:
self.str += ' input: ' + sink + '\n'
self.str += print_placeholder('macro_name', self.m_name)
self.str += print_placeholder('type', self.pb_type)
if (self.weight > 1):
self.str += print_float('weight', int(self.weight))
self.str += print_float('x', self.x)
#self.str += print_float('x_offset', self.GetX())
self.str += print_float('x_offset', self.x_offset)
self.str += print_float('y', self.y)
self.str += print_float('y_offset', self.y_offset)
#self.str += print_float('y_offset', self.GetY())
self.str += "}\n"
elif (self.IsHardMacro() == True):
self.str += "node {\n"
self.str += ' name: ' + self.name + '\n'
self.str += print_placeholder('type', self.pb_type)
self.str += print_float('height', self.height)
self.str += print_placeholder('orientation', '"' + str(self.orientation) + '"')
self.str += print_float('width', self.width)
self.str += print_float('x', self.x)
self.str += print_float('y', self.y)
self.str += "}\n"
else:
self.str += "node {\n"
self.str += ' name: ' + self.name + '\n'
self.str += print_float('height', self.height)
self.str += print_placeholder('type', self.pb_type)
self.str += print_float('width', self.width)
self.str += print_float('x', self.x)
self.str += print_float('y', self.y)
self.str += "}\n"
return self.str
# for plc file
def SimpleStr(self):
self.str = ""
self.str += str(self.node_id) + " "
self.str += str(round(self.x, 12)) + " "
self.str += str(round(self.y, 12)) + " "
if (self.IsPort() == True):
self.str += "- "
elif (self.orientation == None):
self.str += "N "
else:
self.str += str(self.orientation) + " "
self.str += "0\n"
return self.str
class PBFNetlist:
def __init__(self, netlist_pbf_file, plc_file):
self.netlist_pub_file = netlist_pbf_file
self.plc_file = plc_file
# information from plc file
self.plc_header = ""
self.n_cols = -1
self.n_rows = -1
self.canvas_width = 0.0
self.canvas_height = 0.0
self.plc_header = ""
self.grid_width = 0.0
self.grid_height = 0.0
# routing information
self.smooth_factor = 2
self.overlap_threshold = 0.0
self.vrouting_alloc = 0.0
self.hrouting_alloc = 0.0
self.hroute_per_micro = 0.0
self.vroute_per_micro = 0.0
# weight parameters for cost function
self.w_wirelength = 1.0
self.w_density = 0.5
self.w_congestion = 0.5
self.HPWL = 0.0
self.cost_wirelength = 0.0
self.cost_density = 0.0
self.cost_congestion = 0.0
# information from protocol buffer netlist
self.pb_netlist_header = ""
self.objects = []
self.macros = []
self.stdcell_clusters = []
self.ports = []
self.grids = []
self.nets = []
self.ParseNetlistFile()
self.ParsePlcFile()
def ParsePlcFile(self, plc_file = None):
if (plc_file == None):
plc_file = self.plc_file
# read plc file for all the plc objects
with open(plc_file) as f:
content = f.read().splitlines()
f.close()
self.plc_header = ""
# read the canvas and grid information
for line in content:
items = line.split()
if (len(items) > 2 and items[0] == "#" and items[1] == "Columns"):
self.n_cols = int(items[3])
self.n_rows = int(items[6])
elif (len(items) > 2 and items[0] == "#" and items[1] == "Width"):
self.canvas_width = float(items[3])
self.canvas_height = float(items[6])
elif (len(items) == 10 and items[0] == "#" and items[1] == "Routes"):
self.hroute_per_micro = float(items[-4])
self.vroute_per_micro = float(items[-1])
elif (len(items) == 11 and items[0] == "#" and items[1] == "Routes"):
self.hrouting_alloc = float(items[-4])
self.vrouting_alloc = float(items[-1])
elif (len(items) > 3 and items[0] == "#" and items[1] == "Smoothing"):
self.smooth_factor = floor(float(items[-1]))
elif (len(items) > 3 and items[0] == "#" and items[1] == "Smoothing"):
self.overlap_threshold = float(items[-1])
elif (len(items) == 5 and items[0] != '#'):
node_id = int(items[0])
self.objects[node_id].x = float(items[1])
self.objects[node_id].y = float(items[2])
self.objects[node_id].orientation = items[3]
if (len(items) > 0 and items[0] == "#"):
self.plc_header += line + "\n"
self.grid_width = self.canvas_width / self.n_cols
self.grid_height = self.canvas_height / self.n_rows
# create grids
for y_idx in range(self.n_rows):
for x_idx in range(self.n_cols):
self.grids.append(Grid(self.grid_width, self.grid_height, self.n_cols, self.n_rows, x_idx, y_idx, self.smooth_factor))
# add grid information to object
for plc_object in self.objects:
plc_object.n_rows = self.n_rows
plc_object.n_cols = self.n_cols
plc_object.grid_width = self.grid_width
plc_object.grid_height = self.grid_height
# add grid information to net
for net in self.nets:
net.num_grids_per_row = self.n_cols
net.num_grids_per_col = self.n_rows
# valid the col_id and row_id
def ValidGridId(self, col_id, row_id):
row_id = max(row_id, 0)
row_id = min(row_id, self.n_rows - 1)
col_id = max(col_id, 0)
col_id = min(col_id, self.n_cols - 1)
return col_id, row_id
# Express the location of macro in terms of grid id
def GetGridBBox(self, plc_object):
lx, ly, ux, uy = plc_object.GetBBox()
ll_col_id = floor(lx / self.grid_width)
ll_row_id = floor(ly / self.grid_height)
ur_col_id = ceil(ux / self.grid_width)
ur_row_id = ceil(uy / self.grid_height)
ll_col_id, ll_row_id = self.ValidGridId(ll_col_id, ll_row_id)
ur_col_id, ur_row_id = self.ValidGridId(ur_col_id, ur_row_id)
return ll_col_id, ll_row_id, ur_col_id, ur_row_id
# Update the congestion caused by macro
# True for Add and False for Reduce
def UpdateMacroCongestion(self, plc_object, flag = True):
ll_col_id, ll_row_id, ur_col_id, ur_row_id = self.GetGridBBox(plc_object)
IF_PARTIAL_OVERLAP_H = False
IF_PARTIAL_OVERLAP_V = False
# check the gridcells overlapped with plc_object
for col_id in range(ll_col_id, ur_col_id + 1):
for row_id in range(ll_row_id, ur_row_id + 1):
grid_id = row_id * self.n_cols + col_id
overlap_h, overlap_v = self.grids[grid_id].CalcHVOverlap(plc_object.GetBBox())
self.grids[grid_id].UpdateMacroCongV(overlap_v * self.vrouting_alloc, flag)
self.grids[grid_id].UpdateMacroCongH(overlap_h * self.hrouting_alloc, flag)
if (ll_row_id != ur_row_id):
if (row_id == ll_row_id and abs(self.grid_height - overlap_v) > 1e-5) \
or (row_id == ur_row_id and abs(self.grid_height - overlap_v) > 1e-5):
IF_PARTIAL_OVERLAP_H = True
if (ll_col_id != ll_row_id):
if (col_id == ll_col_id and abs(self.grid_width - overlap_h) > 1e-5) \
or (col_id == ur_col_id) and abs(self.grid_width - overlap_h):
IF_PARTIAL_OVERLAP_V = True
if (IF_PARTIAL_OVERLAP_V == True):
for col_id in range(ll_col_id, ur_col_id):
grid_id = ur_row_id * self.n_cols + col_id
overlap_h, overlap_v = self.grids[grid_id].CalcHVOverlap(plc_object.GetBBox())
self.grids[grid_id].UpdateMacroCongV(overlap_v * self.vrouting_alloc, not flag)
if (IF_PARTIAL_OVERLAP_H == True):
for row_id in range(ll_row_id, ur_row_id):
grid_id = row_id * self.n_cols + ur_col_id
overlap_h, overlap_v = self.grids[grid_id].CalcHVOverlap(plc_object.GetBBox())
self.grids[grid_id].UpdateMacroCongH(overlap_h * self.hrouting_alloc, not flag)
def ParseNetlistFile(self):
# read netlist file for all the plc objects
plc_object_id_map = { } # map name to node_id
# read protocol buffer netlist
float_values = ['"height"', '"weight"', '"width"', '"x"', '"x_offset"', '"y"', '"y_offset"']
placeholders = ['"macro_name"', '"orientation"', '"side"', '"type"']
with open(self.netlist_pub_file) as f:
content = f.read().splitlines()
f.close()
# reset all the variables
self.pb_netlist_header = ""
self.objects = []
self.macros = []
self.stdcell_clusters = []
self.ports = []
object_id = 0
key = ""
header = []
for line in content:
header.append(line)
words = line.split()
if words[0] == 'node':
if len(self.objects) > 0 and self.objects[-1].name == '"__metadata__"':
self.objects.pop(-1)
object_id -= 1
for i in range(len(header) - 1):
self.pb_netlist_header += header[i] + "\n"
self.objects.append(PlcObject(object_id)) # add object
object_id += 1
elif words[0] == 'name:':
self.objects[-1].name = words[1]
elif words[0] == 'input:':
self.objects[-1].inputs.append(words[1])
elif words[0] == 'key:' :
key = words[1] # the attribute name
elif words[0] == 'placeholder:' :
if key == placeholders[0]:
self.objects[-1].m_name = words[1]
elif key == placeholders[1]:
self.objects[-1].orientation = words[1]
elif key == placeholders[2]:
self.objects[-1].side = words[1]
elif key == placeholders[3]:
self.objects[-1].pb_type = words[1]
elif words[0] == 'f:' :
if key == float_values[0]:
self.objects[-1].height = round(float(words[1]), 6)
elif key == float_values[1]:
self.objects[-1].weight = round(float(words[1]), 6)
elif key == float_values[2]:
self.objects[-1].width = round(float(words[1]), 6)
elif key == float_values[3]:
self.objects[-1].x = round(float(words[1]),6)
elif key == float_values[4]:
self.objects[-1].x_offset = round(float(words[1]), 6)
elif key == float_values[5]:
self.objects[-1].y = round(float(words[1]),6)
elif key == float_values[6]:
self.objects[-1].y_offset = round(float(words[1]), 6)
# Get all the macros, standard-cell clusters and IO ports
for plc_object in self.objects:
plc_object_id_map[plc_object.name] = plc_object.node_id
if (plc_object.IsHardMacro() == True):
plc_object.list_id = len(self.macros)
self.macros.append(plc_object.node_id)
elif (plc_object.IsSoftMacro() == True):
self.stdcell_clusters.append(plc_object.node_id)
elif (plc_object.IsPort() == True):
self.ports.append(plc_object.node_id)
else:
pass
for plc_object in self.objects:
if (plc_object.IsSoftMacro() == True):
plc_object.UpdateSquare()
# Map macro pin with its macro
for plc_object in self.objects:
if (plc_object.IsPin() == True):
plc_object.m_node_id = plc_object_id_map[plc_object.m_name]
plc_object.macro_object = self.objects[plc_object.m_node_id]
else:
plc_object.m_node_id = plc_object.node_id
# create nets
for plc_object in self.objects:
if (plc_object.IsPin() == True or plc_object.IsPort() == True):
pins = [plc_object]
for input_pin in plc_object.inputs:
input_pin_id = plc_object_id_map[input_pin]
pins.append(self.objects[input_pin_id])
if (len(pins) > 1):
plc_object.nets.append(len(self.nets))
self.nets.append(Net(pins, self.grids, plc_object.weight))
# calculate the cost in an incremental order
def CalcCostIncremental(self, pre_objects, new_objects):
delta_HPWL = 0.0
# reduce the contribution caused by pre_objects
for plc_object in pre_objects:
self.objects[plc_object.node_id] = deepcopy(plc_object)
nets_id = []
for plc_object in pre_objects:
# update the density cost
ll_col_id, ll_row_id, ur_col_id, ur_row_id = self.GetGridBBox(plc_object)
for row_id in range(ll_row_id, ur_row_id + 1):
for col_id in range(ll_col_id, ur_col_id + 1):
bbox = plc_object.GetBBox()
self.grids[self.n_cols * row_id + col_id].UpdateOverlap(bbox, False)
# update the congestion cuased by macro
self.UpdateMacroCongestion(plc_object, False)
# update the nets connected to this object
for net_id in plc_object.nets:
if net_id not in nets_id:
nets_id.append(net_id)
# update the net routing
for net_id in nets_id:
self.nets[net_id].UpdateRouting(False)
delta_HPWL -= self.nets[net_id].GetHPWL(True)
# update the cost based on new objects
for plc_object in new_objects:
self.objects[plc_object.node_id] = deepcopy(plc_object)
for plc_object in new_objects:
# update the density cost
ll_col_id, ll_row_id, ur_col_id, ur_row_id = self.GetGridBBox(plc_object)
for row_id in range(ll_row_id, ur_row_id + 1):
for col_id in range(ll_col_id, ur_col_id + 1):
bbox = plc_object.GetBBox()
self.grids[self.n_cols * row_id + col_id].UpdateOverlap(bbox, True)
# update the congestion cuased by macro
self.UpdateMacroCongestion(plc_object, True)
# update the net routing
for net_id in nets_id:
self.nets[net_id].UpdateRouting(True)
delta_HPWL += self.nets[net_id].GetHPWL(True)
# wirelength cost
self.HPWL += delta_HPWL
cost_wirelength = self.HPWL / (len(self.nets) * (self.canvas_width + self.canvas_height))
self.cost_wirelength = cost_wirelength
# density cost
density_list = []
for grid in self.grids:
density_list.append(grid.GetDensity())
# sort density in a non-increasing order
density_list.sort(reverse = True)
# find top k
k = max(1, floor(self.n_cols * self.n_rows * 0.1))
cost_density = 0.0
for i in range(k):
cost_density += density_list[i]
cost_density = cost_density / k
self.cost_density = cost_density
# calculate congestion cost
# smooth the congestion caused by net
for grid in self.grids:
grid.UpdateSCongestion()
# update the congestion cost
congestion_list = []
for grid in self.grids:
congestion_list.append((grid.GetSCongV() + grid.GetMacroCongV()) / (self.grid_width * self.vroute_per_micro) +
(grid.GetSCongH() + grid.GetMacroCongH()) / (self.grid_height * self.hroute_per_micro))
# find top 5% congested grids
congestion_list.sort(reverse = True)
k = max(1, floor(self.n_cols * self.n_rows * 0.05))
cost_congestion = 0.0
for i in range(k):
cost_congestion += congestion_list[i]
cost_congestion = cost_congestion / k
self.cost_congestion = cost_congestion
return self.w_wirelength * cost_wirelength + self.w_density * cost_density + self.w_congestion * cost_congestion
# Calculate the cost from scratch
def CalcCost(self):
for grid in self.grids:
grid.Reset()
# calculate wirelength cost
cost_wirelength = 0.0
net_weight = 0.0
for net in self.nets:
cost_wirelength += net.GetHPWL(True)
net_weight += net.weight
if (net.weight <= 0.0):
print("weight : ", net.weight)
print("net_weight : ", net_weight, "len(net) : ", len(self.nets))
self.HPWL = cost_wirelength
cost_wirelength = cost_wirelength / (net_weight * (self.canvas_width + self.canvas_height))
self.cost_wirelength = cost_wirelength
print("cost_wirelength : ", cost_wirelength)
print("self.cost_wirelength : ", self.cost_wirelength)
# calculate density cost
for plc_object in self.objects:
if (plc_object.IsHardMacro() == True or plc_object.IsSoftMacro() == True):
ll_col_id, ll_row_id, ur_col_id, ur_row_id = self.GetGridBBox(plc_object)
for row_id in range(ll_row_id, ur_row_id + 1):
for col_id in range(ll_col_id, ur_col_id + 1):
bbox = plc_object.GetBBox()
self.grids[self.n_cols * row_id + col_id].UpdateOverlap(bbox, True)
density_list = []
for grid in self.grids:
density_list.append(grid.GetDensity())
# sort density in a non-increasing order
density_list.sort(reverse = True)
# find top k
k = max(1, floor(self.n_cols * self.n_rows * 0.1))
cost_density = 0.0
for i in range(k):
cost_density += density_list[i]
cost_density = cost_density / k
cost_density = cost_density / 2.0
self.cost_density = cost_density
# calculate congestion cost
# update the congestion caused by net
for net in self.nets:
net.UpdateRouting(True)
# update the congestion caused by macro
for macro in self.macros:
self.UpdateMacroCongestion(self.objects[macro], True)