-
Notifications
You must be signed in to change notification settings - Fork 13
/
temperature_mobile.c
1643 lines (1440 loc) · 48.3 KB
/
temperature_mobile.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 <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#else
#include <strings.h>
#endif
#include <math.h>
#include "temperature_mobile.h"
#include "flp.h"
#include "util.h"
/* constructors */
blist_t *new_blist(int idx, double occupancy)
{
blist_t *ptr = (blist_t *) calloc (1, sizeof(blist_t));
if (!ptr)
fatal("memory allocation error\n");
ptr->idx = idx;
ptr->occupancy = occupancy;
ptr->next = NULL;
return ptr;
}
blist_t ***new_b2gmap(int rows, int cols)
{
int i;
blist_t ***b2gmap;
b2gmap = (blist_t ***) calloc (rows, sizeof(blist_t **));
b2gmap[0] = (blist_t **) calloc (rows * cols, sizeof(blist_t *));
if (!b2gmap || !b2gmap[0])
fatal("memory allocation error\n");
for(i=1; i < rows; i++)
b2gmap[i] = b2gmap[0] + cols * i;
return b2gmap;
}
/* destructor */
void delete_b2gmap(blist_t ***b2gmap, int rows, int cols)
{
int i, j;
blist_t *ptr, *temp;
/* free the linked list */
for(i=0; i < rows; i++)
for(j=0; j < cols; j++) {
ptr = b2gmap[i][j];
while(ptr) {
temp = ptr->next;
free(ptr);
ptr = temp;
}
}
/* free the array space */
free(b2gmap[0]);
free(b2gmap);
}
/* re-initialize */
void reset_b2gmap(grid_model_t *model, layer_t *layer)
{
int i, j;
blist_t *ptr, *temp;
/* free the linked list */
for(i=0; i < model->rows; i++)
for(j=0; j < model->cols; j++) {
ptr = layer->b2gmap[i][j];
while(ptr) {
temp = ptr->next;
free(ptr);
ptr = temp;
}
layer->b2gmap[i][j] = NULL;
}
}
/* create a linked list node and append it at the end */
void blist_append(blist_t *head, int idx, double occupancy)
{
blist_t *tail = NULL;
if(!head)
fatal("blist_append called with empty list\n");
/* traverse till the end */
for(; head; head = head->next)
tail = head;
/* append */
tail->next = new_blist(idx, occupancy);
}
/* compute the power/temperature average weighted by occupancies */
double blist_avg(blist_t *ptr, flp_t *flp, double *v, int type)
{
double val = 0.0;
for(; ptr; ptr = ptr->next) {
if (type == V_POWER)
val += ptr->occupancy * v[ptr->idx] / (flp->units[ptr->idx].width *
flp->units[ptr->idx].height);
else if (type == V_TEMP)
val += ptr->occupancy * v[ptr->idx];
else
fatal("unknown vector type\n");
}
return val;
}
void debug_print_blist(blist_t *head, flp_t *flp);
/* test the block-grid map data structure */
void test_b2gmap(grid_model_t *model, layer_t *layer)
{
int i, j;
blist_t *ptr;
double sum;
/* a correctly formed b2gmap should have the
* sum of occupancies in each linked list
* to be equal to 1.0
*/
for (i=0; i < model->rows; i++)
for(j=0; j < model->cols; j++) {
sum = 0.0;
for(ptr = layer->b2gmap[i][j]; ptr; ptr = ptr->next)
sum += ptr->occupancy;
if (!eq(floor(sum*1e5 + 0.5)/1e5, 1.0)) {
fprintf(stdout, "i: %d\tj: %d\n", i, j);
debug_print_blist(layer->b2gmap[i][j], layer->flp);
fatal("erroneous b2gmap data structure. invalid floorplan?\n");
}
}
}
/* setup the block and grid mapping data structures */
void set_bgmap(grid_model_t *model, layer_t *layer)
{
/* i1, i2, j1 and j2 are indices of the boundary grid cells */
int i, j, u, i1, i2, j1, j2;
/* shortcuts for cell width(cw) and cell height(ch) */
double cw = model->width / model->cols;
double ch = model->height / model->rows;
/* initialize */
reset_b2gmap(model, layer);
/* for each functional unit */
for(u=0; u < layer->flp->n_units; u++) {
/* shortcuts for unit boundaries */
double lu = layer->flp->units[u].leftx;
double ru = lu + layer->flp->units[u].width;
double bu = layer->flp->units[u].bottomy;
double tu = bu + layer->flp->units[u].height;
/* top index (lesser row) = rows - ceil (topy / cell height) */
i1 = model->rows - tolerant_ceil(tu/ch);
/* bottom index (greater row) = rows - floor (bottomy / cell height) */
i2 = model->rows - tolerant_floor(bu/ch);
/* left index = floor (leftx / cell width) */
j1 = tolerant_floor(lu/cw);
/* right index = ceil (rightx / cell width) */
j2 = tolerant_ceil(ru/cw);
/* sanity check */
if((i1 < 0) || (j1 < 0))
fatal("negative grid cell start index!\n");
if((i2 > model->rows) || (j2 > model->cols))
fatal("grid cell end index out of bounds!\n");
if((i1 >= i2) || (j1 >= j2))
fatal("invalid floorplan spec or grid resolution\n");
/* setup g2bmap */
layer->g2bmap[u].i1 = i1;
layer->g2bmap[u].i2 = i2;
layer->g2bmap[u].j1 = j1;
layer->g2bmap[u].j2 = j2;
/* setup b2gmap */
/* for each grid cell in this unit */
for(i=i1; i < i2; i++)
for(j=j1; j < j2; j++)
/* grid cells fully overlapped by this unit */
if ((i > i1) && (i < i2-1) && (j > j1) && (j < j2-1)) {
/* first unit in the list */
if (!layer->b2gmap[i][j])
layer->b2gmap[i][j] = new_blist(u, 1.0);
else {
/* this should not occur since the grid cell is
* fully covered and hence, no other unit should
* be sharing it
*/
blist_append(layer->b2gmap[i][j], u, 1.0);
warning("overlap of functional blocks?\n");
}
/* boundary grid cells partially overlapped by this unit */
} else {
/* shortcuts for cell boundaries */
double lc = j * cw, rc = (j+1) * cw;
double tc = model->height - i * ch;
double bc = model->height - (i+1) * ch;
/* shortcuts for overlap width and height */
double oh = (MIN(tu, tc) - MAX(bu, bc));
double ow = (MIN(ru, rc) - MAX(lu, lc));
double occupancy;
/* overlap tolerance */
if (eq(oh/ch, 0))
oh = 0;
else if (eq(oh/ch, 1))
oh = ch;
if (eq(ow/cw, 0))
ow = 0;
else if (eq(ow/cw, 1))
ow = cw;
occupancy = (oh * ow) / (ch * cw);
if (oh < 0 || ow < 0)
fatal("negative overlap!\n");
/* first unit in the list */
if (!layer->b2gmap[i][j])
layer->b2gmap[i][j] = new_blist(u, occupancy);
else
/* append at the end */
blist_append(layer->b2gmap[i][j], u, occupancy);
}
}
/*
* sanity check
test_b2gmap(model, layer);
*/
}
/* populate default set of layers */
void populate_default_layers(grid_model_t *model, flp_t *flp_default)
{
/* silicon */
model->layers[LAYER_SI].no = LAYER_SI;
model->layers[LAYER_SI].has_lateral = TRUE;
model->layers[LAYER_SI].has_power = TRUE;
model->layers[LAYER_SI].k = model->config.k_chip;
model->layers[LAYER_SI].thickness = model->config.t_chip;
model->layers[LAYER_SI].sp = model->config.p_chip;
model->layers[LAYER_SI].flp = flp_default;
model->layers[LAYER_SI].b2gmap = new_b2gmap(model->rows, model->cols);
model->layers[LAYER_SI].g2bmap = (glist_t *) calloc(flp_default->n_units, sizeof(glist_t));
if (!model->layers[LAYER_SI].g2bmap)
fatal("memory allocation error\n");
/* interface material */
model->layers[LAYER_INT].no = LAYER_INT;
model->layers[LAYER_INT].has_lateral = TRUE;
model->layers[LAYER_INT].has_power = FALSE;
model->layers[LAYER_INT].k = model->config.k_interface;
model->layers[LAYER_INT].thickness = model->config.t_interface;
model->layers[LAYER_INT].sp = model->config.p_interface;
model->layers[LAYER_INT].flp = flp_default;
model->layers[LAYER_INT].b2gmap = model->layers[LAYER_SI].b2gmap;
model->layers[LAYER_INT].g2bmap = model->layers[LAYER_SI].g2bmap;
}
/* parse the layer file open for reading */
void parse_layer_file(grid_model_t *model, FILE *fp)
{
char line[LINE_SIZE], *ptr, cval;
int count, i = 0, field = LCF_SNO, ival;
double dval;
fseek(fp, 0, SEEK_SET);
count = 0;
while (!feof(fp) && count < (model->n_layers * LCF_NPARAMS)) {
fgets(line, LINE_SIZE, fp);
if (feof(fp))
break;
/* ignore comments and empty lines */
ptr = strtok(line, " \r\t\n");
if (!ptr || ptr[0] == '#')
continue;
switch (field)
{
case LCF_SNO:
if (sscanf(ptr, "%d", &ival) != 1)
fatal("invalid layer number\n");
if(ival >= model->n_layers || ival < 0)
fatal("layer number must be >= 0 and < no. of layers\n");
if (model->layers[ival].no != 0)
fatal("layer numbers must be unique\n");
i = ival;
model->layers[i].no = ival;
break;
case LCF_LATERAL:
if (sscanf(ptr, "%c", &cval) != 1)
fatal("invalid layer heat flow indicator\n");
if (cval == 'Y' || cval == 'y')
model->layers[i].has_lateral = TRUE;
else if (cval == 'N' || cval == 'n')
model->layers[i].has_lateral = FALSE;
else
fatal("invalid layer heat flow indicator\n");
break;
case LCF_POWER:
if (sscanf(ptr, "%c", &cval) != 1)
fatal("invalid layer power dissipation indicator\n");
if (cval == 'Y' || cval == 'y')
model->layers[i].has_power = TRUE;
else if (cval == 'N' || cval == 'n')
model->layers[i].has_power = FALSE;
else
fatal("invalid layer power dissipation indicator\n");
break;
case LCF_SP:
if (sscanf(ptr, "%lf", &dval) != 1)
fatal("invalid specific heat\n");
model->layers[i].sp = dval;
break;
case LCF_RHO:
if (sscanf(ptr, "%lf", &dval) != 1)
fatal("invalid resistivity\n");
model->layers[i].k = 1.0 / dval;
break;
case LCF_THICK:
if (sscanf(ptr, "%lf", &dval) != 1)
fatal("invalid thickness\n");
model->layers[i].thickness = dval;
break;
case LCF_FLP:
model->layers[i].flp = read_flp(ptr, FALSE);
/* first layer */
if (count < LCF_NPARAMS) {
model->width = get_total_width(model->layers[i].flp);
model->height = get_total_height(model->layers[i].flp);
} else if(!eq(model->width, get_total_width(model->layers[i].flp)) ||
!eq(model->height, get_total_height(model->layers[i].flp)))
fatal("width and height differ across layers\n");
break;
default:
fatal("invalid field id\n");
break;
}
field = (field + 1) % LCF_NPARAMS;
count++;
}
/* allocate the block-grid maps */
for(i=0; i < model->n_layers; i++) {
model->layers[i].b2gmap = new_b2gmap(model->rows, model->cols);
model->layers[i].g2bmap = (glist_t *) calloc(model->layers[i].flp->n_units,
sizeof(glist_t));
if (!model->layers[i].g2bmap)
fatal("memory allocation error\n");
}
}
/* populate layer info either from the default floorplan or from
* the layer configuration file (lcf)
*/
void populate_layers_grid(grid_model_t *model, flp_t *flp_default)
{
char str[STR_SIZE];
FILE *fp = NULL;
/* lcf file specified */
if (model->has_lcf) {
if (!strcasecmp(model->config.grid_layer_file, "stdin"))
fp = stdin;
else
fp = fopen (model->config.grid_layer_file, "r");
if (!fp) {
sprintf(str, "error opening file %s\n", model->config.grid_layer_file);
fatal(str);
}
}
/* compute the no. of layers */
if (model->has_lcf) {
model->n_layers = count_significant_lines(fp);
if (model->n_layers % LCF_NPARAMS)
fatal("wrong no. of lines in layer file\n");
model->n_layers /= LCF_NPARAMS;
/* default no. of layers when lcf file is not specified */
} else
model->n_layers = DEFAULT_CHIP_LAYERS;
/* allocate initial memory */
model->layers = (layer_t *) calloc (model->n_layers, sizeof(layer_t));
if (!model->layers)
fatal("memory allocation error\n");
/* read in values from the lcf when specified */
if (model->has_lcf) {
parse_layer_file(model, fp);
warning("layer configuration file specified. overriding default floorplan with those in lcf file\n");
/* default set of layers */
} else
populate_default_layers(model, flp_default);
if (model->has_lcf && fp != stdin)
fclose(fp);
}
/* constructor */
grid_model_t *alloc_grid_model(thermal_config_t *config, flp_t *flp_default)
{
int i;
grid_model_t *model;
if (config->grid_rows & (config->grid_rows-1) ||
config->grid_cols & (config->grid_cols-1))
fatal("grid rows and columns should both be powers of two\n");
model = (grid_model_t *) calloc (1, sizeof(grid_model_t));
if (!model)
fatal("memory allocation error\n");
model->config = *config;
model->rows = config->grid_rows;
model->cols = config->grid_cols;
if(!strcasecmp(model->config.grid_map_mode, GRID_AVG_STR))
model->map_mode = GRID_AVG;
else if(!strcasecmp(model->config.grid_map_mode, GRID_MIN_STR))
model->map_mode = GRID_MIN;
else if(!strcasecmp(model->config.grid_map_mode, GRID_MAX_STR))
model->map_mode = GRID_MAX;
else if(!strcasecmp(model->config.grid_map_mode, GRID_CENTER_STR))
model->map_mode = GRID_CENTER;
else
fatal("unknown mapping mode\n");
/* layer configuration file specified? */
if(strcmp(model->config.grid_layer_file, NULLFILE))
model->has_lcf = TRUE;
else {
model->has_lcf = FALSE;
model->base_n_units = flp_default->n_units;
}
/* get layer information */
populate_layers_grid(model, flp_default);
/* count the total no. of blocks */
model->total_n_blocks = 0;
for(i=0; i < model->n_layers; i++)
model->total_n_blocks += model->layers[i].flp->n_units;
/* allocate internal state */
model->last_steady = new_grid_model_vector(model);
model->last_trans = new_grid_model_vector(model);
return model;
}
void populate_R_model_grid(grid_model_t *model, flp_t *flp)
{
int i;
double cw, ch;
/* setup the block-grid maps; flp parameter is ignored */
if (model->has_lcf)
for(i=0; i < model->n_layers; i++)
set_bgmap(model, &model->layers[i]);
/* only the silicon layer has allocated space for the maps.
* all the rest just point to it. so it is sufficient to
* setup the block-grid map for the silicon layer alone.
* further, for default layer configuration, the `flp'
* parameter should be the same as that of the silicon
* layer. finally, the chip width and height information
* need to be populated for default layer configuration
*/
else {
if (flp != model->layers[LAYER_SI].flp)
fatal("mismatch between the floorplan and the thermal model\n");
model->width = get_total_width(flp);
model->height = get_total_height(flp);
set_bgmap(model, &model->layers[LAYER_SI]);
}
/* shortcuts for cell width(cw) and cell height(ch) */
cw = model->width / model->cols;
ch = model->height / model->rows;
/* layer specific resistances */
for(i=0; i < model->n_layers; i++) {
if (model->layers[i].has_lateral) {
model->layers[i].rx = getr(model->layers[i].k, cw, ch * model->layers[i].thickness);
model->layers[i].ry = getr(model->layers[i].k, ch, cw * model->layers[i].thickness);
} else {
/* positive infinity */
model->layers[i].rx = LARGENUM;
model->layers[i].ry = LARGENUM;
}
model->layers[i].rz = getr(model->layers[i].k, model->layers[i].thickness, cw * ch);
/* last layer is connected to ambient. divide r_convec proportional to cell area */
if (i == model->n_layers - 1)
model->layers[i].rz += model->config.r_convec *
(model->width * model->height) / (cw * ch);
}
/* done */
model->r_ready = TRUE;
}
void populate_C_model_grid(grid_model_t *model, flp_t *flp)
{
int i;
/* shortcuts for cell width(cw) and cell height(ch) */
double cw = model->width / model->cols;
double ch = model->height / model->rows;
if (!model->r_ready)
fatal("R model not ready\n");
if (!model->has_lcf && flp != model->layers[LAYER_SI].flp)
fatal("different floorplans for R and C models!\n");
/* layer specific capacitances */
for(i=0; i < model->n_layers; i++)
model->layers[i].c = getcap(model->layers[i].sp, model->layers[i].thickness, cw * ch);
/* last layer is connected to the ambient.
* divide c_convec proportional to cell area
*/
model->layers[model->n_layers-1].c += C_FACTOR * model->config.c_convec * (cw * ch) /
(model->width * model->height);
/* done */
model->c_ready = TRUE;
}
/* destructor */
void delete_grid_model(grid_model_t *model)
{
int i;
if (model->has_lcf)
for(i=0; i < model->n_layers; i++) {
delete_b2gmap(model->layers[i].b2gmap, model->rows, model->cols);
free(model->layers[i].g2bmap);
free_flp(model->layers[i].flp, FALSE);
}
/* only the silicon layer has allocated space for the maps.
* all the rest just point to it. also, its floorplan was
* allocated elsewhere. so, we don't need to deallocate those.
*/
else {
delete_b2gmap(model->layers[LAYER_SI].b2gmap, model->rows, model->cols);
free(model->layers[LAYER_SI].g2bmap);
}
free_grid_model_vector(model->last_steady);
free_grid_model_vector(model->last_trans);
free(model->layers);
free(model);
}
/* differs from 'dvector()' in that memory for internal nodes is also allocated */
double *hotspot_vector_grid(grid_model_t *model)
{
if (model->total_n_blocks <= 0)
fatal("total_n_blocks is not greater than zero\n");
return dvector(model->total_n_blocks);
}
/* copy 'src' to 'dst' except for a window of 'size'
* elements starting at 'at'. useful in floorplan
* compaction. can be used only with default layer
* configuration as all layers should have the same
* floorplan. incompatible with layer configuration
* file.
*/
void trim_hotspot_vector_grid(grid_model_t *model, double *dst, double *src,
int at, int size)
{
int i;
if (model->has_lcf)
fatal("trim_hotspot_vector_grid called with lcf file\n");
for (i=0; i < at && i < model->total_n_blocks; i++)
dst[i] = src[i];
for(i=at+size; i < model->total_n_blocks; i++)
dst[i-size] = src[i];
}
/* update the model corresponding to floorplan compaction */
void resize_thermal_model_grid(grid_model_t *model, int n_units)
{
int i;
if (model->has_lcf)
fatal("resize_thermal_model_grid called with lcf file\n");
if (n_units > model->base_n_units)
fatal("resizing grid model to more than the allocated space\n");
/* count the total no. of blocks again */
model->total_n_blocks = 0;
for(i=0; i < model->n_layers; i++)
model->total_n_blocks += model->layers[i].flp->n_units;
/* nothing more needs to be done because the only data structure
* that is dependent on flp->n_units is g2bmap (others are
* dependent on 'grid size' which does not change because
* of resizing). g2bmap is a 1-d array and needs no reallocation
*/
}
/* sets the temperature of a vector 'temp' allocated using 'hotspot_vector' */
void set_temp_grid(grid_model_t *model, double *temp, double val)
{
int i;
if (model->total_n_blocks <= 0)
fatal("total_n_blocks is not greater than zero\n");
for(i=0; i < model->total_n_blocks; i++)
temp[i] = val;
}
/* dump the top layer grid temperatures of 'temp' onto 'file' */
void dump_top_layer_temp_grid(grid_model_t *model, char *file,
grid_model_vector_t *temp)
{
int i, j;
char str[STR_SIZE];
FILE *fp;
if (!model->r_ready)
fatal("R model not ready\n");
if (!strcasecmp(file, "stdout"))
fp = stdout;
else if (!strcasecmp(file, "stderr"))
fp = stderr;
else
fp = fopen (file, "w");
if (!fp) {
sprintf (str,"error: %s could not be opened for writing\n", file);
fatal(str);
}
for(i=0; i < model->rows; i++)
for(j=0; j < model->cols; j++)
fprintf(fp, "%d\t%.2f\n", i*model->cols+j, temp->cuboid[0][i][j]);
if(fp != stdout && fp != stderr)
fclose(fp);
}
/* dump the steady state grid temperatures of the top layer onto 'file' */
void dump_steady_temp_grid (grid_model_t *model, char *file)
{
/* top layer of the most-recently computed steady state temperature */
dump_top_layer_temp_grid(model, file, model->last_steady);
}
/* dump temperature vector alloced using 'hotspot_vector' to 'file' */
void dump_temp_grid(grid_model_t *model, double *temp, char *file)
{
int i, n, base = 0;
char str[STR_SIZE];
FILE *fp;
if (!strcasecmp(file, "stdout"))
fp = stdout;
else if (!strcasecmp(file, "stderr"))
fp = stderr;
else
fp = fopen (file, "w");
if (!fp) {
sprintf (str,"error: %s could not be opened for writing\n", file);
fatal(str);
}
/* layer temperatures */
for(n=0; n < model->n_layers; n++) {
/* default set of layers */
if (!model->has_lcf) {
switch(n)
{
case LAYER_SI:
strcpy(str,"");
break;
case LAYER_INT:
strcpy(str,"iface_");
break;
default:
fatal("unknown layer\n");
break;
}
/* layer configuration file */
} else {
sprintf(str,"layer_%d_", n);
}
for(i=0; i < model->layers[n].flp->n_units; i++)
fprintf(fp, "%s%s\t%.2f\n", str,
model->layers[n].flp->units[i].name, temp[base+i]);
base += model->layers[n].flp->n_units;
}
if (base != model->total_n_blocks)
fatal("total_n_blocks failed to tally\n");
if(fp != stdout && fp != stderr)
fclose(fp);
}
void copy_temp_grid(grid_model_t *model, double *dst, double *src)
{
copy_dvector(dst, src, model->total_n_blocks);
}
/*
* read temperature vector alloced using 'hotspot_vector' from 'file'
* which was dumped using 'dump_temp'. values are clipped to thermal
* threshold based on 'clip'
*/
void read_temp_grid(grid_model_t *model, double *temp, char *file, int clip)
{
int i, n, idx, base = 0;
double max=0, val;
char *ptr, str1[LINE_SIZE], str2[LINE_SIZE];
char name[STR_SIZE], format[STR_SIZE];
FILE *fp;
if (!strcasecmp(file, "stdin"))
fp = stdin;
else
fp = fopen (file, "r");
if (!fp) {
sprintf (str1,"error: %s could not be opened for reading\n", file);
fatal(str1);
}
/* temperatures of the different layers */
for (n=0; n < model->n_layers; n++) {
/* default set of layers */
if (!model->has_lcf) {
switch(n)
{
case LAYER_SI:
strcpy(format,"%s%lf");
break;
case LAYER_INT:
strcpy(format,"iface_%s%lf");
break;
default:
fatal("unknown layer\n");
break;
}
/* layer configuration file */
} else {
sprintf(format,"layer_%d_%%s%%lf", n);
}
for (i=0; i < model->layers[n].flp->n_units; i++) {
fgets(str1, LINE_SIZE, fp);
if (feof(fp))
fatal("not enough lines in temperature file\n");
strcpy(str2, str1);
/* ignore comments and empty lines */
ptr = strtok(str1, " \r\t\n");
if (!ptr || ptr[0] == '#') {
i--;
continue;
}
if (sscanf(str2, format, name, &val) != 2)
fatal("invalid temperature file format\n");
idx = get_blk_index(model->layers[n].flp, name);
if (idx >= 0)
temp[base+idx] = val;
else /* since get_blk_index calls fatal, the line below cannot be reached */
fatal ("unit in temperature file not found in floorplan\n");
/* find max temp on the top layer
* (silicon for the default set of layers)
*/
if (n == 0 && temp[idx] > max)
max = temp[idx];
}
base += model->layers[n].flp->n_units;
}
if (base != model->total_n_blocks)
fatal("total_n_blocks failed to tally\n");
fgets(str1, LINE_SIZE, fp);
if (!feof(fp))
fatal("too many lines in temperature file\n");
if(fp != stdin)
fclose(fp);
/* clipping */
if (clip && (max > model->config.thermal_threshold)) {
/* if max has to be brought down to thermal_threshold,
* (w.r.t the ambient) what is the scale down factor?
*/
double factor = (model->config.thermal_threshold - model->config.ambient) /
(max - model->config.ambient);
/* scale down all temperature differences (from ambient) by the same factor */
for (i=0; i < model->total_n_blocks; i++)
temp[i] = (temp[i]-model->config.ambient)*factor + model->config.ambient;
}
}
/* dump power numbers to file */
void dump_power_grid(grid_model_t *model, double *power, char *file)
{
int i, n, base = 0;
char str[STR_SIZE];
FILE *fp;
if (!strcasecmp(file, "stdout"))
fp = stdout;
else if (!strcasecmp(file, "stderr"))
fp = stderr;
else
fp = fopen (file, "w");
if (!fp) {
sprintf (str,"error: %s could not be opened for writing\n", file);
fatal(str);
}
/* dump values only for the layers dissipating power */
for(n=0; n < model->n_layers; n++) {
if (model->layers[n].has_power) {
for(i=0; i < model->layers[n].flp->n_units; i++)
if (model->has_lcf)
fprintf(fp, "layer_%d_%s\t%.3f\n", n,
model->layers[n].flp->units[i].name, power[base+i]);
else
fprintf(fp, "%s\t%.3f\n",
model->layers[n].flp->units[i].name, power[base+i]);
}
base += model->layers[n].flp->n_units;
}
if(fp != stdout && fp != stderr)
fclose(fp);
}
/*
* read power vector alloced using 'hotspot_vector' from 'file'
* which was dumped using 'dump_power'.
*/
void read_power_grid (grid_model_t *model, double *power, char *file)
{
int i, idx, n, base = 0;
double val;
char *ptr, str1[LINE_SIZE], str2[LINE_SIZE];
char name[STR_SIZE], format[STR_SIZE];
FILE *fp;
if (!strcasecmp(file, "stdin"))
fp = stdin;
else
fp = fopen (file, "r");
if (!fp) {
sprintf (str1,"error: %s could not be opened for reading\n", file);
fatal(str1);
}
/* lcf file could potentially specify more than one power dissipating
* layer. hence, units with zero power within a layer cannot be left
* out in the power file.
*/
if (model->has_lcf) {
for(n=0; n < model->n_layers; n++) {
if (model->layers[n].has_power)
for(i=0; i < model->layers[n].flp->n_units; i++) {
fgets(str1, LINE_SIZE, fp);
if (feof(fp))
fatal("not enough lines in power file\n");
strcpy(str2, str1);
/* ignore comments and empty lines */
ptr = strtok(str1, " \r\t\n");
if (!ptr || ptr[0] == '#') {
i--;
continue;
}
sprintf(format, "layer_%d_%%s%%lf", n);
if (sscanf(str2, format, name, &val) != 2)
fatal("invalid power file format\n");
idx = get_blk_index(model->layers[n].flp, name);
if (idx >= 0)
power[base+idx] = val;
/* since get_blk_index calls fatal, the line below cannot be reached */
else
fatal ("unit in power file not found in floorplan\n");
}
base += model->layers[n].flp->n_units;
}
fgets(str1, LINE_SIZE, fp);
if (!feof(fp))
fatal("too many lines in power file\n");
/* default layer configuration. so only one layer
* has power dissipation. units with zero power
* can be omitted in the power file
*/
} else {
while(!feof(fp)) {
fgets(str1, LINE_SIZE, fp);
if (feof(fp))
break;
strcpy(str2, str1);
/* ignore comments and empty lines */
ptr = strtok(str1, " \r\t\n");
if (!ptr || ptr[0] == '#')
continue;
if (sscanf(str2, "%s%lf", name, &val) != 2)
fatal("invalid power file format\n");
idx = get_blk_index(model->layers[LAYER_SI].flp, name);
if (idx >= 0)
power[idx] = val;
else /* since get_blk_index calls fatal, the line below cannot be reached */
fatal ("unit in power file not found in floorplan\n");
}
}
if(fp != stdin)
fclose(fp);
}
double find_max_temp_grid(grid_model_t *model, double *temp)
{
int i;
double max = 0.0;
/* max temperature occurs on the top-most layer */
for(i=0; i < model->layers[0].flp->n_units; i++) {
if (temp[i] < 0)
fatal("negative temperature!\n");
else if (max < temp[i])
max = temp[i];
}
return max;
}
double find_avg_temp_grid(grid_model_t *model, double *temp)
{
int i, n, base = 0, count = 0;
double sum = 0.0;
/* average temperature of all the power dissipating blocks */
for(n=0; n < model->n_layers; n++) {
if (model->layers[n].has_power) {
for(i=0; i < model->layers[n].flp->n_units; i++) {
if (temp[base+i] < 0)
fatal("negative temperature!\n");
else
sum += temp[base+i];
}
count += model->layers[n].flp->n_units;
}
base += model->layers[n].flp->n_units;
}
if (!count)
fatal("no power dissipating units?!\n");
return (sum / count);
}
/* grid_model_vector routines */
/* constructor */
grid_model_vector_t *new_grid_model_vector(grid_model_t *model)
{
grid_model_vector_t *v;
v = (grid_model_vector_t *) calloc (1, sizeof(grid_model_vector_t));
if (!v)
fatal("memory allocation error\n");
v->cuboid = dcuboid_tail(model->rows, model->cols, model->n_layers, 0);
return v;
}
/* destructor */
void free_grid_model_vector(grid_model_vector_t *v)
{
free_dcuboid(v->cuboid);
free(v);
}