-
Notifications
You must be signed in to change notification settings - Fork 46
/
flp.c
1659 lines (1477 loc) · 48.3 KB
/
flp.c
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
#include <stdio.h>
#include <string.h>
#include <string.h>
#ifdef _MSC_VER
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#else
#include <strings.h>
#endif
#include <stdlib.h>
#include <math.h>
#include "flp.h"
#include "npe.h"
#include "shape.h"
#include "util.h"
#include "temperature.h"
#include "temperature_block.h"
/*
* this is the metric function used for the floorplanning.
* in order to enable a different metric, just change the
* return statement of this function to return an appropriate
* metric. The current metric used is a linear function of
* area (A), temperature (T) and wire length (W):
* lambdaA * A + lambdaT * T + lambdaW * W
* thermal model and power density are passed as parameters
* since temperature is used in the metric.
*/
double flp_evaluate_metric(flp_t *flp, RC_model_t *model, double *power,
double lambdaA, double lambdaT, double lambdaW)
{
double tmax, area, wire_length;
double *temp;
temp = hotspot_vector(model);
populate_R_model(model, flp);
steady_state_temp(model, power, temp);
tmax = find_max_temp(model, temp);
area = get_total_area(flp);
wire_length = get_wire_metric(flp);
free_dvector(temp);
/* can return any arbitrary function of area, tmax and wire_length */
return (lambdaA * area + lambdaT * tmax + lambdaW * wire_length);
}
/* default flp_config */
flp_config_t default_flp_config(void)
{
flp_config_t config;
/* wrap around L2? */
config.wrap_l2 = TRUE;
strcpy(config.l2_label, "L2");
/* model dead space around the rim of the chip? */
config.model_rim = FALSE;
config.rim_thickness = 50e-6;
/* area ratio below which to ignore dead space */
config.compact_ratio = 0.005;
/*
* no. of discrete orientations for a shape curve.
* should be an even number greater than 1
*/
config.n_orients = 300;
/* annealing parameters */
config.P0 = 0.99; /* initial acceptance probability */
/*
* average change (delta) in cost. varies according to
* the metric. need not be very accurate. just the right
* order of magnitude is enough. for instance, if the
* metric is flp area, this Davg is the average difference
* in the area of successive slicing floorplan attempts.
* since the areas are in the order of mm^2, the delta
* is also in the same ball park.
*/
config.Davg = 1.0; /* for our a*A + b*T + c*W metric */
config.Kmoves = 7.0; /* no. of moves to try in each step */
config.Rcool = 0.99; /* ratio for the cooling schedule */
config.Rreject = 0.99; /* ratio of rejects at which to stop annealing */
config.Nmax = 1000; /* absolute max no. of annealing steps */
/* weights for the metric: lambdaA * A + lambdaT * T + lambdaW * W
* the weights incorporate two things:
* 1) the conversion of A to mm^2, T to K and W to mm.
* 2) weighing the relative importance of A, T and K
*/
config.lambdaA = 5.0e+6;
config.lambdaT = 1.0;
config.lambdaW = 350;
return config;
}
/*
* parse a table of name-value string pairs and add the configuration
* parameters to 'config'
*/
void flp_config_add_from_strs(flp_config_t *config, str_pair *table, int size)
{
int idx;
if ((idx = get_str_index(table, size, "wrap_l2")) >= 0)
if(sscanf(table[idx].value, "%d", &config->wrap_l2) != 1)
fatal("invalid format for configuration parameter wrap_l2\n");
if ((idx = get_str_index(table, size, "l2_label")) >= 0)
if(sscanf(table[idx].value, "%s", config->l2_label) != 1)
fatal("invalid format for configuration parameter l2_label\n");
if ((idx = get_str_index(table, size, "model_rim")) >= 0)
if(sscanf(table[idx].value, "%d", &config->model_rim) != 1)
fatal("invalid format for configuration parameter model_rim\n");
if ((idx = get_str_index(table, size, "rim_thickness")) >= 0)
if(sscanf(table[idx].value, "%lf", &config->rim_thickness) != 1)
fatal("invalid format for configuration parameter rim_thickness\n");
if ((idx = get_str_index(table, size, "compact_ratio")) >= 0)
if(sscanf(table[idx].value, "%lf", &config->compact_ratio) != 1)
fatal("invalid format for configuration parameter compact_ratio\n");
if ((idx = get_str_index(table, size, "n_orients")) >= 0)
if(sscanf(table[idx].value, "%d", &config->n_orients) != 1)
fatal("invalid format for configuration parameter n_orients\n");
if ((idx = get_str_index(table, size, "P0")) >= 0)
if(sscanf(table[idx].value, "%lf", &config->P0) != 1)
fatal("invalid format for configuration parameter P0\n");
if ((idx = get_str_index(table, size, "Davg")) >= 0)
if(sscanf(table[idx].value, "%lf", &config->Davg) != 1)
fatal("invalid format for configuration parameter Davg\n");
if ((idx = get_str_index(table, size, "Kmoves")) >= 0)
if(sscanf(table[idx].value, "%lf", &config->Kmoves) != 1)
fatal("invalid format for configuration parameter Kmoves\n");
if ((idx = get_str_index(table, size, "Rcool")) >= 0)
if(sscanf(table[idx].value, "%lf", &config->Rcool) != 1)
fatal("invalid format for configuration parameter Rcool\n");
if ((idx = get_str_index(table, size, "Rreject")) >= 0)
if(sscanf(table[idx].value, "%lf", &config->Rreject) != 1)
fatal("invalid format for configuration parameter Rreject\n");
if ((idx = get_str_index(table, size, "Nmax")) >= 0)
if(sscanf(table[idx].value, "%d", &config->Nmax) != 1)
fatal("invalid format for configuration parameter Nmax\n");
if ((idx = get_str_index(table, size, "lambdaA")) >= 0)
if(sscanf(table[idx].value, "%lf", &config->lambdaA) != 1)
fatal("invalid format for configuration parameter lambdaA\n");
if ((idx = get_str_index(table, size, "lambdaT")) >= 0)
if(sscanf(table[idx].value, "%lf", &config->lambdaT) != 1)
fatal("invalid format for configuration parameter lambdaT\n");
if ((idx = get_str_index(table, size, "lambdaW")) >= 0)
if(sscanf(table[idx].value, "%lf", &config->lambdaW) != 1)
fatal("invalid format for configuration parameter lambdaW\n");
if (config->rim_thickness <= 0)
fatal("rim thickness should be greater than zero\n");
if ((config->compact_ratio < 0) || (config->compact_ratio > 1))
fatal("compact_ratio should be between 0 and 1\n");
if ((config->n_orients <= 1) || (config->n_orients & 1))
fatal("n_orients should be an even number greater than 1\n");
if (config->Kmoves < 0)
fatal("Kmoves should be non-negative\n");
if ((config->P0 < 0) || (config->P0 > 1))
fatal("P0 should be between 0 and 1\n");
if ((config->Rcool < 0) || (config->Rcool > 1))
fatal("Rcool should be between 0 and 1\n");
if ((config->Rreject < 0) || (config->Rreject > 1))
fatal("Rreject should be between 0 and 1\n");
if (config->Nmax < 0)
fatal("Nmax should be non-negative\n");
}
/*
* convert config into a table of name-value pairs. returns the no.
* of parameters converted
*/
int flp_config_to_strs(flp_config_t *config, str_pair *table, int max_entries)
{
if (max_entries < 15)
fatal("not enough entries in table\n");
sprintf(table[0].name, "wrap_l2");
sprintf(table[1].name, "l2_label");
sprintf(table[2].name, "model_rim");
sprintf(table[3].name, "rim_thickness");
sprintf(table[4].name, "compact_ratio");
sprintf(table[5].name, "n_orients");
sprintf(table[6].name, "P0");
sprintf(table[7].name, "Davg");
sprintf(table[8].name, "Kmoves");
sprintf(table[9].name, "Rcool");
sprintf(table[10].name, "Rreject");
sprintf(table[11].name, "Nmax");
sprintf(table[12].name, "lambdaA");
sprintf(table[13].name, "lambdaT");
sprintf(table[14].name, "lambdaW");
sprintf(table[0].value, "%d", config->wrap_l2);
sprintf(table[1].value, "%s", config->l2_label);
sprintf(table[2].value, "%d", config->model_rim);
sprintf(table[3].value, "%lg", config->rim_thickness);
sprintf(table[4].value, "%lg", config->compact_ratio);
sprintf(table[5].value, "%d", config->n_orients);
sprintf(table[6].value, "%lg", config->P0);
sprintf(table[7].value, "%lg", config->Davg);
sprintf(table[8].value, "%lg", config->Kmoves);
sprintf(table[9].value, "%lg", config->Rcool);
sprintf(table[10].value, "%lg", config->Rreject);
sprintf(table[11].value, "%d", config->Nmax);
sprintf(table[12].value, "%lg", config->lambdaA);
sprintf(table[13].value, "%lg", config->lambdaT);
sprintf(table[14].value, "%lg", config->lambdaW);
return 15;
}
/*
* copy L2 connectivity from 'from' of flp_desc to 'to'
* of flp. 'size' elements are copied. the arms are not
* connected amidst themselves or with L2 base block
*/
void copy_l2_info (flp_t *flp, int to, flp_desc_t *flp_desc, int from, int size)
{
int j, count;
for(count=0; count < L2_ARMS + 1; count++, to++) {
/* copy names */
strcpy(flp->units[to].name, flp_desc->units[from].name);
for(j=0; j < size; j++) {
/* rows */
flp->wire_density[to][j] = flp_desc->wire_density[from][j];
/* columns */
flp->wire_density[j][to] = flp_desc->wire_density[j][from];
}
}
/* fix the names of the arms */
strcat(flp->units[to-L2_ARMS+L2_LEFT].name, L2_LEFT_STR);
strcat(flp->units[to-L2_ARMS+L2_RIGHT].name, L2_RIGHT_STR);
}
/* create a floorplan placeholder from description */
flp_t *flp_placeholder(flp_desc_t *flp_desc)
{
int i, j, count, n_dead;
flp_t *flp;
/* wrap L2 around? */
int wrap_l2 = FALSE;
if (flp_desc->config.wrap_l2 &&
!strcasecmp(flp_desc->units[flp_desc->n_units-1].name, flp_desc->config.l2_label))
wrap_l2 = TRUE;
flp = (flp_t *) calloc (1, sizeof(flp_t));
if(!flp)
fatal("memory allocation error\n");
/*
* number of dead blocks = no. of core blocks - 1.
* (one per vertical or horizontal cut). if L2 is
* wrapped around, core blocks = flp_desc->n_units-1
*/
n_dead = flp_desc->n_units - !!(wrap_l2) - 1;
flp->n_units = flp_desc->n_units + n_dead;
/* wrap L2 around - extra arms are added */
if (wrap_l2)
flp->n_units += L2_ARMS;
/*
* model the dead space in the edge. let us make
* one dead block per corner edge of a block. so,
* no. of rim blocks could be at most 2*n+2 where
* n is the total no. of blocks (the worst case
* is just all blocks lined up side-by-side)
*/
if (flp_desc->config.model_rim)
flp->n_units += (2*flp->n_units + 2);
flp->units = (unit_t *) calloc (flp->n_units, sizeof(unit_t));
flp->wire_density = (double **) calloc(flp->n_units, sizeof(double *));
if (!flp->units || !flp->wire_density)
fatal("memory allocation error\n");
for (i=0; i < flp->n_units; i++) {
flp->wire_density[i] = (double *) calloc(flp->n_units, sizeof(double));
if (!flp->wire_density[i])
fatal("memory allocation error\n");
}
/* copy connectivity (only for non-dead core blocks) */
for(i=0; i < flp_desc->n_units-!!(wrap_l2); i++) {
strcpy(flp->units[i].name, flp_desc->units[i].name);
for (j=0; j < flp_desc->n_units-!!(wrap_l2); j++) {
flp->wire_density[i][j] = flp_desc->wire_density[i][j];
}
}
/* name the dead blocks */
for(count=0; count < n_dead; count++, i++)
sprintf(flp->units[i].name, DEAD_PREFIX"%d", count);
/* L2 connectivity info */
if (wrap_l2)
copy_l2_info(flp, i, flp_desc, flp_desc->n_units-1, flp_desc->n_units-1);
return flp;
}
/*
* note that if wrap_l2 is true, L2 is beyond the boundary in flp_desc
* but flp contains it within its boundaries.
*/
void restore_dead_blocks(flp_t *flp, flp_desc_t *flp_desc,
int compacted, int wrap_l2,
int model_rim, int rim_blocks)
{
int i, j, idx=0;
/* remove L2 and rim blocks and restore the compacted blocks */
if(model_rim)
flp->n_units -= rim_blocks;
if (wrap_l2)
flp->n_units -= (L2_ARMS+1);
flp->n_units += compacted;
/* reinitialize the dead blocks */
for(i=0; i < flp_desc->n_units-1; i++) {
idx = flp_desc->n_units + i;
sprintf(flp->units[idx].name, DEAD_PREFIX"%d", i);
flp->units[idx].leftx = flp->units[idx].bottomy = 0;
flp->units[idx].width = flp->units[idx].height = 0;
for(j=0; j < flp->n_units; j++)
flp->wire_density[idx][j] = flp->wire_density[j][idx] = 0;
}
}
/* translate the floorplan to new origin (x,y) */
void flp_translate(flp_t *flp, double x, double y)
{
int i;
double minx = flp->units[0].leftx;
double miny = flp->units[0].bottomy;
for (i=1; i < flp->n_units; i++) {
if (minx > flp->units[i].leftx)
minx = flp->units[i].leftx;
if (miny > flp->units[i].bottomy)
miny = flp->units[i].bottomy;
}
for (i=0; i < flp->n_units; i++) {
flp->units[i].leftx += (x - minx);
flp->units[i].bottomy += (y - miny);
}
}
/* scale the floorplan by a factor 'factor' */
void flp_scale(flp_t *flp, double factor)
{
int i;
double minx = flp->units[0].leftx;
double miny = flp->units[0].bottomy;
for (i=1; i < flp->n_units; i++) {
if (minx > flp->units[i].leftx)
minx = flp->units[i].leftx;
if (miny > flp->units[i].bottomy)
miny = flp->units[i].bottomy;
}
for(i=0; i < flp->n_units; i++) {
flp->units[i].leftx = (flp->units[i].leftx - minx) * factor + minx;
flp->units[i].bottomy = (flp->units[i].bottomy - miny) * factor + miny;
flp->units[i].width *= factor;
flp->units[i].height *= factor;
}
}
/*
* change the orientation of the floorplan by
* rotating and/or flipping. the target orientation
* is specified in 'target'. 'width', 'height', 'xorig'
* and 'yorig' are those of 'flp' respectively.
*/
void flp_change_orient(flp_t *flp, double xorig, double yorig,
double width, double height, orient_t target)
{
int i;
for(i=0; i < flp->n_units; i++) {
double leftx, bottomy, rightx, topy;
/* all co-ordinate calculations are
* done assuming (0,0) as the center.
* so, shift accordingly
*/
leftx = flp->units[i].leftx - (xorig + width / 2.0);
bottomy = flp->units[i].bottomy - (yorig + height / 2.0);
rightx = leftx + flp->units[i].width;
topy = bottomy + flp->units[i].height;
/* when changing orientation, leftx and
* bottomy of a rectangle could change
* to one of the other three corners.
* also, signs of the co-ordinates
* change according to the rotation
* or reflection. Further x & y are
* swapped for rotations that are
* odd multiples of 90 degrees
*/
switch(target) {
case ROT_0:
flp->units[i].leftx = leftx;
flp->units[i].bottomy = bottomy;
break;
case ROT_90:
flp->units[i].leftx = -topy;
flp->units[i].bottomy = leftx;
swap_dval(&(flp->units[i].width), &(flp->units[i].height));
break;
case ROT_180:
flp->units[i].leftx = -rightx;
flp->units[i].bottomy = -topy;
break;
case ROT_270:
flp->units[i].leftx = bottomy;
flp->units[i].bottomy = -rightx;
swap_dval(&(flp->units[i].width), &(flp->units[i].height));
break;
case FLIP_0:
flp->units[i].leftx = -rightx;
flp->units[i].bottomy = bottomy;
break;
case FLIP_90:
flp->units[i].leftx = bottomy;
flp->units[i].bottomy = leftx;
swap_dval(&(flp->units[i].width), &(flp->units[i].height));
break;
case FLIP_180:
flp->units[i].leftx = leftx;
flp->units[i].bottomy = -topy;
break;
case FLIP_270:
flp->units[i].leftx = -topy;
flp->units[i].bottomy = -rightx;
swap_dval(&(flp->units[i].width), &(flp->units[i].height));
break;
default:
fatal("unknown orientation\n");
break;
}
/* translate back to original origin */
flp->units[i].leftx += (xorig + width / 2.0);
flp->units[i].bottomy += (yorig + height / 2.0);
}
}
/*
* create a non-uniform grid-like floorplan equivalent to this.
* this function is mainly useful when using the HotSpot block
* model to model floorplans of drastically differing aspect
* ratios and granularity. an example for such a floorplan
* would be the standard ev6 floorplan that comes with HotSpot,
* where the register file is subdivided into say 128 entries.
* the HotSpot block model could result in inaccuracies while
* trying to model such floorplans of differing granularity.
* if such inaccuracies occur, use this function to create an
* equivalent floorplan that can be modeled accurately in
* HotSpot. 'map', if non-NULL, is an output parameter to store
* the 2-d array allocated by the function.
*/
flp_t *flp_create_grid(flp_t *flp, int ***map)
{
double x[MAX_UNITS], y[MAX_UNITS];
int i, j, n, xsize=0, ysize=0, count=0, found, **ptr;
flp_t *grid;
/* sort the units' boundary co-ordinates */
for(i=0; i < flp->n_units; i++) {
double r, t;
r = flp->units[i].leftx + flp->units[i].width;
t = flp->units[i].bottomy + flp->units[i].height;
if(bsearch_insert_double(x, xsize, flp->units[i].leftx))
xsize++;
if(bsearch_insert_double(y, ysize, flp->units[i].bottomy))
ysize++;
if(bsearch_insert_double(x, xsize, r))
xsize++;
if(bsearch_insert_double(y, ysize, t))
ysize++;
}
/*
* the grid formed by the lines from x and y arrays
* is our desired floorplan. allocate memory for it
*/
grid = (flp_t *) calloc (1, sizeof(flp_t));
if(!grid)
fatal("memory allocation error\n");
grid->n_units = (xsize-1) * (ysize-1);
grid->units = (unit_t *) calloc (grid->n_units, sizeof(unit_t));
grid->wire_density = (double **) calloc(grid->n_units, sizeof(double *));
if (!grid->units || !grid->wire_density)
fatal("memory allocation error\n");
for (i=0; i < grid->n_units; i++) {
grid->wire_density[i] = (double *) calloc(grid->n_units, sizeof(double));
if (!grid->wire_density[i])
fatal("memory allocation error\n");
}
/* mapping between blocks of 'flp' to those of 'grid' */
ptr = (int **) calloc(flp->n_units, sizeof(int *));
if (!ptr)
fatal("memory allocation error\n");
/*
* ptr is a 2-d array with each row of possibly different
* length. the size of each row is stored in its first element.
* here, it is basically the mapping between 'flp' to 'grid'
* i.e., for each flp->unit, it stores the set of grid->units
* it maps to.
*/
for(i=0; i < flp->n_units; i++) {
ptr[i] = (int *) calloc(grid->n_units+1, sizeof(int));
if(!ptr[i])
fatal("memory allocation error\n");
}
/*
* now populate the 'grid' blocks and map the blocks
* from 'flp' to 'grid'. for each block, identify the
* intervening lines that chop it into grid cells and
* assign the names of those cells from that of the
* block
*/
for(i=0; i < flp->n_units; i++) {
double *xstart, *xend, *ystart, *yend;
double *ptr1, *ptr2;
int grid_num=0;
if (!bsearch_double(x, xsize, flp->units[i].leftx, &xstart))
fatal("invalid sorted arrays\n");
if (!bsearch_double(x, xsize, flp->units[i].leftx+flp->units[i].width, &xend))
fatal("invalid sorted arrays\n");
if (!bsearch_double(y, ysize, flp->units[i].bottomy, &ystart))
fatal("invalid sorted arrays\n");
if (!bsearch_double(y, ysize, flp->units[i].bottomy+flp->units[i].height, ¥d))
fatal("invalid sorted arrays\n");
for(ptr1 = xstart; ptr1 < xend; ptr1++)
for(ptr2 = ystart; ptr2 < yend; ptr2++) {
/* add this grid block if it has not been added already */
for(n=0, found=FALSE; n < count; n++) {
if (grid->units[n].leftx == ptr1[0] && grid->units[n].bottomy == ptr2[0]) {
found = TRUE;
break;
}
}
if(!found) {
sprintf(grid->units[count].name, "%s_%d", flp->units[i].name, grid_num);
grid->units[count].leftx = ptr1[0];
grid->units[count].bottomy = ptr2[0];
grid->units[count].width = ptr1[1]-ptr1[0];
grid->units[count].height = ptr2[1]-ptr2[0];
/* map between position in 'flp' to that in 'grid' */
ptr[i][++ptr[i][0]] = count;
grid_num++;
count++;
}
}
}
/* sanity check */
if(count != (xsize-1) * (ysize-1))
fatal("mismatch in the no. of units\n");
/* fill-in the wire densities */
for(i=0; i < flp->n_units; i++)
for(j=0; j < flp->n_units; j++) {
int p, q;
for(p=1; p <= ptr[i][0]; p++)
for(q=1; q <= ptr[j][0]; q++)
grid->wire_density[ptr[i][p]][ptr[j][q]] = flp->wire_density[i][j];
}
/* output the map */
if (map)
(*map) = ptr;
else
free_blkgrid_map(flp, ptr);
return grid;
}
/* free the map allocated by flp_create_grid */
void free_blkgrid_map(flp_t *flp, int **map)
{
int i;
for(i=0; i < flp->n_units; i++)
free(map[i]);
free(map);
}
/* translate power numbers to the grid created by flp_create_grid */
void xlate_power_blkgrid(flp_t *flp, flp_t *grid, \
double *bpower, double *gpower, int **map)
{
int i, p;
for(i=0; i < flp->n_units; i++)
for(p=1; p <= map[i][0]; p++)
/* retain the power density */
gpower[map[i][p]] = bpower[i] / (flp->units[i].width * flp->units[i].height) *\
grid->units[map[i][p]].width * grid->units[map[i][p]].height;
}
/*
* wrap the L2 around this floorplan. L2's area information
* is obtained from flp_desc. memory for L2 and its arms has
* already been allocated in the flp. note that flp & flp_desc
* have L2 hidden beyond the boundary at this point
*/
void flp_wrap_l2(flp_t *flp, flp_desc_t *flp_desc)
{
/*
* x is the width of the L2 arms
* y is the height of the bottom portion
*/
double x, y, core_width, core_height, total_side, core_area, l2_area;
unit_t *l2, *l2_left, *l2_right;
/* find L2 dimensions so that the total chip becomes a square */
core_area = get_total_area(flp);
core_width = get_total_width(flp);
core_height = get_total_height(flp);
/* flp_desc has L2 hidden beyond the boundary */
l2_area = flp_desc->units[flp_desc->n_units].area;
total_side = sqrt(core_area + l2_area);
/*
* width of the total chip after L2 wrapping is equal to
* the width of the core plus the width of the two arms
*/
x = (total_side - core_width) / 2.0;
y = total_side - core_height;
/*
* we are trying to solve the equation
* (2*x+core_width) * (y+core_height)
* = l2_area + core_area
* for x and y. it is possible that the values
* turnout to be negative if we restrict the
* total chip to be a square. in that case,
* theoretically, any value of x in the range
* (0, l2_area/(2*core_height)) and the
* corresponding value of y or any value of y
* in the range (0, l2_area/core_width) and the
* corresponding value of x would be a solution
* we look for a solution with a reasonable
* aspect ratio. i.e., we constrain kx = y (or
* ky = x depending on the aspect ratio of the
* core) where k = WRAP_L2_RATIO. solving the equation
* with this constraint, we get the following
*/
if ( x <= 0 || y <= 0.0) {
double sum;
if (core_width >= core_height) {
sum = WRAP_L2_RATIO * core_width + 2 * core_height;
x = (sqrt(sum*sum + 8*WRAP_L2_RATIO*l2_area) - sum) / (4*WRAP_L2_RATIO);
y = WRAP_L2_RATIO * x;
} else {
sum = core_width + 2 * WRAP_L2_RATIO * core_height;
y = (sqrt(sum*sum + 8*WRAP_L2_RATIO*l2_area) - sum) / (4*WRAP_L2_RATIO);
x = WRAP_L2_RATIO * y;
}
total_side = 2 * x + core_width;
}
/* fix the positions of core blocks */
flp_translate(flp, x, y);
/* restore the L2 blocks */
flp->n_units += (L2_ARMS+1);
/* copy L2 info again from flp_desc but from beyond the boundary */
copy_l2_info(flp, flp->n_units-L2_ARMS-1, flp_desc,
flp_desc->n_units, flp_desc->n_units);
/* fix the positions of the L2 blocks. connectivity
* information has already been fixed (in flp_placeholder).
* bottom L2 block - (leftx, bottomy) is already (0,0)
*/
l2 = &flp->units[flp->n_units-1-L2_ARMS];
l2->width = total_side;
l2->height = y;
l2->leftx = l2->bottomy = 0;
/* left L2 arm */
l2_left = &flp->units[flp->n_units-L2_ARMS+L2_LEFT];
l2_left->width = x;
l2_left->height = core_height;
l2_left->leftx = 0;
l2_left->bottomy = y;
/* right L2 arm */
l2_right = &flp->units[flp->n_units-L2_ARMS+L2_RIGHT];
l2_right->width = x;
l2_right->height = core_height;
l2_right->leftx = x + core_width;
l2_right->bottomy = y;
}
/*
* wrap the rim strips around. each edge has rim blocks
* equal to the number of blocks abutting that edge. at
* the four corners, the rim blocks are extended by the
* rim thickness in a clockwise fashion
*/
int flp_wrap_rim(flp_t *flp, double rim_thickness)
{
double width, height;
int i, j = 0, k, n = flp->n_units;
unit_t *unit;
width = get_total_width(flp) + 2 * rim_thickness;
height = get_total_height(flp) + 2 * rim_thickness;
flp_translate(flp, rim_thickness, rim_thickness);
for (i = 0; i < n; i++) {
/* shortcut */
unit = &flp->units[i];
/* block is on the western border */
if (eq(unit->leftx, rim_thickness)) {
sprintf(flp->units[n+j].name, "%s_%s",
RIM_LEFT_STR, unit->name);
flp->units[n+j].width = rim_thickness;
flp->units[n+j].height = unit->height;
flp->units[n+j].leftx = 0;
flp->units[n+j].bottomy = unit->bottomy;
/* northwest corner */
if (eq(unit->bottomy + unit->height, height-rim_thickness))
flp->units[n+j].height += rim_thickness;
j++;
}
/* block is on the eastern border */
if (eq(unit->leftx + unit->width, width-rim_thickness)) {
sprintf(flp->units[n+j].name, "%s_%s",
RIM_RIGHT_STR, unit->name);
flp->units[n+j].width = rim_thickness;
flp->units[n+j].height = unit->height;
flp->units[n+j].leftx = unit->leftx + unit->width;
flp->units[n+j].bottomy = unit->bottomy;
/* southeast corner */
if (eq(unit->bottomy, rim_thickness)) {
flp->units[n+j].height += rim_thickness;
flp->units[n+j].bottomy = 0;
}
j++;
}
/* block is on the northern border */
if (eq(unit->bottomy + unit->height, height-rim_thickness)) {
sprintf(flp->units[n+j].name, "%s_%s",
RIM_TOP_STR, unit->name);
flp->units[n+j].width = unit->width;
flp->units[n+j].height = rim_thickness;
flp->units[n+j].leftx = unit->leftx;
flp->units[n+j].bottomy = unit->bottomy + unit->height;
/* northeast corner */
if (eq(unit->leftx + unit->width, width-rim_thickness))
flp->units[n+j].width += rim_thickness;
j++;
}
/* block is on the southern border */
if (eq(unit->bottomy, rim_thickness)) {
sprintf(flp->units[n+j].name, "%s_%s",
RIM_BOTTOM_STR, unit->name);
flp->units[n+j].width = unit->width;
flp->units[n+j].height = rim_thickness;
flp->units[n+j].leftx = unit->leftx;
flp->units[n+j].bottomy = 0;
/* southwest corner */
if (eq(unit->leftx, rim_thickness)) {
flp->units[n+j].width += rim_thickness;
flp->units[n+j].leftx = 0;
}
j++;
}
}
flp->n_units += j;
/* update all the rim wire densities */
for(i=n; i < n+j; i++)
for(k=0; k <= i; k++)
flp->wire_density[i][k] = flp->wire_density[k][i] = 0;
return j;
}
/*
* floorplanning using simulated annealing.
* precondition: flp is a pre-allocated placeholder.
* returns the number of compacted blocks in the selected
* floorplan
*/
int floorplan(flp_t *flp, flp_desc_t *flp_desc,
RC_model_t *model, double *power)
{
NPE_t *expr, *next, *best; /* Normalized Polish Expressions */
tree_node_stack_t *stack; /* for NPE evaluation */
tree_node_t *root; /* shape curve tree */
double cost, new_cost, best_cost, sum_cost, T, Tcold;
int i, steps, downs, n, rejects, compacted, rim_blocks = 0;
int original_n = flp->n_units;
int wrap_l2;
/* to maintain the order of power values during
* the compaction/shifting around of blocks
*/
double *tpower = hotspot_vector(model);
/* shortcut */
flp_config_t cfg = flp_desc->config;
/*
* make the rim strips disappear for slicing tree
* purposes. can be restored at the end
*/
if (cfg.model_rim)
flp->n_units = (flp->n_units - 2) / 3;
/* wrap L2 around? */
wrap_l2 = FALSE;
if (cfg.wrap_l2 &&
!strcasecmp(flp_desc->units[flp_desc->n_units-1].name, cfg.l2_label)) {
wrap_l2 = TRUE;
/* make L2 disappear too */
flp_desc->n_units--;
flp->n_units -= (L2_ARMS+1);
}
/* initialization */
expr = NPE_get_initial(flp_desc);
stack = new_tree_node_stack();
init_rand();
/* convert NPE to flp */
root = tree_from_NPE(flp_desc, stack, expr);
/* compacts too small dead blocks */
compacted = tree_to_flp(root, flp, TRUE, cfg.compact_ratio);
/* update the tpower vector according to the compaction */
trim_hotspot_vector(model, tpower, power, flp->n_units, compacted);
free_tree(root);
if(wrap_l2)
flp_wrap_l2(flp, flp_desc);
if(cfg.model_rim)
rim_blocks = flp_wrap_rim(flp, cfg.rim_thickness);
resize_thermal_model(model, flp->n_units);
#if VERBOSE > 2
print_flp(flp, TRUE);
#endif
cost = flp_evaluate_metric(flp, model, tpower, cfg.lambdaA, cfg.lambdaT, cfg.lambdaW);
/* restore the compacted blocks */
restore_dead_blocks(flp, flp_desc, compacted, wrap_l2, cfg.model_rim, rim_blocks);
best = NPE_duplicate(expr); /* best till now */
best_cost = cost;
/* simulated annealing */
steps = 0;
/* initial annealing temperature */
T = -cfg.Davg / log(cfg.P0);
/*
* final annealing temperature - we stop when there
* are fewer than (1-cfg.Rreject) accepts.
* of those accepts, assuming half are uphill moves,
* we want the temperature so that the probability
* of accepting uphill moves is as low as
* (1-cfg.Rreject)/2.
*/
Tcold = -cfg.Davg / log ((1.0 - cfg.Rreject) / 2.0);
#if VERBOSE > 0
fprintf(stdout, "initial cost: %g\tinitial T: %g\tfinal T: %g\n", cost, T, Tcold);
#endif
/*
* stop annealing if temperature has cooled down enough or
* max no. of iterations have been tried
*/
while (T >= Tcold && steps < cfg.Nmax) {
/* shortcut */
n = cfg.Kmoves * flp->n_units;
i = downs = rejects = 0;
sum_cost = 0;
/* try enough total or downhill moves per T */
while ((i < 2 * n) && (downs < n)) {
next = make_random_move(expr);
/* convert NPE to flp */
root = tree_from_NPE(flp_desc, stack, next);
compacted = tree_to_flp(root, flp, TRUE, cfg.compact_ratio);
/* update the tpower vector according to the compaction */
trim_hotspot_vector(model, tpower, power, flp->n_units, compacted);
free_tree(root);
if(wrap_l2)
flp_wrap_l2(flp, flp_desc);
if(cfg.model_rim)
rim_blocks = flp_wrap_rim(flp, cfg.rim_thickness);
resize_thermal_model(model, flp->n_units);
#if VERBOSE > 2
print_flp(flp, TRUE);
#endif
new_cost = flp_evaluate_metric(flp, model, tpower, cfg.lambdaA, cfg.lambdaT, cfg.lambdaW);
restore_dead_blocks(flp, flp_desc, compacted, wrap_l2, cfg.model_rim, rim_blocks);
#if VERBOSE > 1
fprintf(stdout, "count: %d\tdowns: %d\tcost: %g\t",
i, downs, new_cost);
#endif
/* move accepted? */
if (new_cost < cost || /* downhill always accepted */
/* boltzmann probability function */
rand_fraction() < exp(-(new_cost-cost)/T)) {
free_NPE(expr);
expr = next;
/* downhill move */
if (new_cost < cost) {
downs++;
/* found new best */
if (new_cost < best_cost) {
free_NPE(best);
best = NPE_duplicate(expr);
best_cost = new_cost;
}
}
#if VERBOSE > 1
fprintf(stdout, "accepted\n");
#endif
cost = new_cost;
sum_cost += cost;
} else { /* rejected move */
rejects++;
free_NPE(next);
#if VERBOSE > 1
fprintf(stdout, "rejected\n");
#endif
}
i++;
}
#if VERBOSE > 0
fprintf(stdout, "step: %d\tT: %g\ttries: %d\taccepts: %d\trejects: %d\t",
steps, T, i, (i-rejects), rejects);
fprintf(stdout, "avg. cost: %g\tbest cost: %g\n",
(i-rejects)?(sum_cost / (i-rejects)):sum_cost, best_cost);
#endif
/* stop annealing if there are too little accepts */
if(((double)rejects/i) > cfg.Rreject)
break;
/* annealing schedule */
T *= cfg.Rcool;
steps++;
}
/* best floorplan found */
root = tree_from_NPE(flp_desc, stack, best);
#if VERBOSE > 0
{
int pos = min_area_pos(root->curve);
print_tree_relevant(root, pos, flp_desc);
}
#endif
compacted = tree_to_flp(root, flp, TRUE, cfg.compact_ratio);
/* update the power vector according to the compaction */
trim_hotspot_vector(model, power, power, flp->n_units, compacted);
free_tree(root);
/* restore L2 and rim */
if(wrap_l2) {
flp_wrap_l2(flp, flp_desc);
flp_desc->n_units++;
}
if(cfg.model_rim)
rim_blocks = flp_wrap_rim(flp, cfg.rim_thickness);
resize_thermal_model(model, flp->n_units);
#if VERBOSE > 2
print_flp(flp, TRUE);
#endif
free_NPE(expr);
free_NPE(best);
free_tree_node_stack(stack);
free_dvector(tpower);
/*
* return the number of blocks compacted finally
* so that any deallocator can take care of memory
* accordingly.
*/
return (original_n - flp->n_units);
}
/* functions duplicated from flp_desc.c */
/*