-
Notifications
You must be signed in to change notification settings - Fork 5
/
voltspot.c
548 lines (469 loc) · 17.1 KB
/
voltspot.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
/*
* This is a trace-level thermal simulator. It reads power values
* from an input trace file and outputs the corresponding instantaneous
* voltage values to an output trace file. It also outputs the steady
* state voltage/current values to designated files.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "PDN_sim.h"
#include "PDN_analyze.h"
#include "flp.h"
#include "util.h"
#include "voltspot.h"
/* Lib for SuperLU */
#include "slu_ddefs.h"
void usage(int argc, char **argv)
{
fprintf(stdout, "Usage: %s -f <file> -p <file> [-c <file>] [options]\n", argv[0]);
fprintf(stdout, "Options:(may be specified in any order, within \"[]\" means optional)\n");
fprintf(stdout, " -f <file>\tfloorplan input file (e.g. example.flp) - overridden by the\n");
fprintf(stdout, " \tlayer configuration file (e.g. 3D.lcf) when the\n");
fprintf(stdout, " \tlatter is specified\n");
fprintf(stdout, " -p <file>\tpower trace input file (e.g. example.ptrace)\n");
fprintf(stdout, " [-c <file>]\tinput configuration parameters from file (e.g. pdn.config)\n");
fprintf(stdout, " [-v <file>]\ttransient PDN output file - will skip transient simulation\n");
fprintf(stdout, " \tif not provided\n");
fprintf(stdout, " [options]\tzero or more options of the form \"-<name> <value>\",\n");
fprintf(stdout, " \toverride the options from config file.\n");
}
/*
* parse a table of name-value string pairs and add the configuration
* parameters to 'config'
*/
void global_config_from_strs(global_config_t *config, str_pair *table, int size)
{
int idx;
if((idx = get_str_index(table, size, "f")) >= 0) {
if(sscanf(table[idx].value, "%s", config->flp_file) != 1)
fatal("invalid format for configuration parameter flp_file\n");
} else {
fatal("required parameter flp_file missing. check usage\n");
}
if((idx = get_str_index(table, size, "p")) >= 0) {
if(sscanf(table[idx].value, "%s", config->p_infile) != 1)
fatal("invalid format for configuration parameter p_infile\n");
} else {
fatal("required parameter p_infile missing. check usage\n");
}
if((idx = get_str_index(table, size, "c")) >= 0) {
if(sscanf(table[idx].value, "%s", config->config) != 1)
fatal("invalid format for configuration parameter config\n");
} else {
strcpy(config->config, NULLFILE);
}
if((idx = get_str_index(table, size, "d")) >= 0) {
if(sscanf(table[idx].value, "%s", config->dump_config) != 1)
fatal("invalid format for configuration parameter dump_config\n");
} else {
strcpy(config->dump_config, NULLFILE);
}
if((idx = get_str_index(table, size, "v")) >= 0) {
if(sscanf(table[idx].value, "%s", config->v_outfile) != 1)
fatal("invalid format for configuration parameter v_outfile\n");
} else {
strcpy(config->v_outfile, NULLFILE);
}
}
/*
* read a single line of trace file containing names
* of functional blocks
*/
int read_names(FILE *fp, char **names)
{
char line[LINE_SIZE], temp[LINE_SIZE], *src;
int i;
/* skip empty lines */
do {
/* read the entire line */
fgets(line, LINE_SIZE, fp);
if(feof(fp))
fatal("not enough names in trace file\n");
strcpy(temp, line);
src = strtok(temp, " \r\t\n");
} while (!src);
/* new line not read yet */
if(line[strlen(line)-1] != '\n')
fatal("line too long\n");
/* chop the names from the line read */
for(i=0,src=line; *src && i < MAX_UNITS; i++) {
if(!sscanf(src, "%s", names[i]))
fatal("invalid format of names\n");
src += strlen(names[i]);
while (isspace((int)*src))
src++;
}
if(*src && i == MAX_UNITS)
fatal("no. of units exceeded limit\n");
return i;
}
/* read a single line of power trace numbers */
int read_vals(FILE *fp, double *vals)
{
char line[LINE_SIZE], temp[LINE_SIZE], *src;
int i;
/* skip empty lines */
do {
/* read the entire line */
fgets(line, LINE_SIZE, fp);
if(feof(fp))
return 0;
strcpy(temp, line);
src = strtok(temp, " \r\t\n");
} while (!src);
/* new line not read yet */
if(line[strlen(line)-1] != '\n')
fatal("line too long\n");
/* chop the power values from the line read */
for(i=0,src=line; *src && i < MAX_UNITS; i++) {
if(!sscanf(src, "%s", temp) || !sscanf(src, "%lf", &vals[i]))
fatal("invalid format of values\n");
src += strlen(temp);
while (isspace((int)*src))
src++;
}
if(*src && i == MAX_UNITS)
fatal("no. of entries exceeded limit\n");
return i;
}
int compare_names(char **n1, char **n2, int size)
{
int i;
int flag = 0;
for(i=0; i < size-1; i++)
if(strcmp(n1[i], n2[i])){
flag = 1;
break;
}
return flag;
}
char **alloc_names(int nr, int nc)
{
int i;
char **m;
m = (char **) calloc (nr, sizeof(char *));
assert(m != NULL);
m[0] = (char *) calloc (nr * nc, sizeof(char));
assert(m[0] != NULL);
for (i = 1; i < nr; i++)
m[i] = m[0] + nc * i;
return m;
}
void free_names(char **m)
{
free(m[0]);
free(m);
}
int main(int argc, char **argv)
{
int i, j, m, idx, base = 0, count = 0, n = 0;
int num, size, lines = 0;
int do_transient = TRUE;
int intv, trans_intvl, switch_intvl;
int warmup_steps;
char **names; double *vals;
/* trace file pointers */
FILE *pin;
FILE *vout = NULL;
/* floorplan */
PDN_flp_t *flp;
/* PDN model */
model_t *model;
status_t *status;
/* instantaneous power values */
double *power;
double *cur_power, *delta_power;
double total_power = 0.0;
/* steady state power values */
double *overall_power;
/* PDN model configuration parameters */
PDN_config_t config;
/* global configuration parameters */
global_config_t global_config;
/* table to hold options and configuration */
str_pair table[MAX_ENTRIES];
/* SLU Matrix for Transient Solving */
SuperMatrix A, L, U;
int *perm_r; /* row permutations from partial pivoting */
int *perm_c; /* column permutation vector */
if(!(argc >= 6 && argc % 2)) {
usage(argc, argv);
return 1;
}
size = parse_cmdline(table, MAX_ENTRIES, argc, argv);
global_config_from_strs(&global_config, table, size);
/* no PDN transient simulation, only steady state */
if(!strcmp(global_config.v_outfile, NULLFILE))
do_transient = FALSE;
/* read configuration file */
if(strcmp(global_config.config, NULLFILE))
size += read_str_pairs(&table[size], MAX_ENTRIES, global_config.config);
/*
* earlier entries override later ones. so, command line options
* have priority over config file
*/
size = str_pairs_remove_duplicates(table, size);
/* get defaults PDN config*/
config = default_PDN_config();
/* modify according to command line / config file */
PDN_config_add_from_strs(&config, table, size);
/* initialization: the flp_file global configuration
* parameter is overridden by the layer configuration
* file in the layer_file_3D when the latter is specified.
*/
flp = PDN_read_flp(global_config.flp_file);
/* Manually set domain for PDN */
if(config.PDN_multi_dom)
set_flp_domain(flp, &config);
/* Simulation mode checking */
int is_3D = FALSE;
if(strcmp(config.layer_file_3D, NULLFILE))
is_3D = TRUE;
if((!is_3D) && config.v_stacking)
fatal("Please turn on 3D if you want to do voltage stacking.\n");
if(do_transient){
if(1 == config.run_PDN){
fatal("We do not allow running transient simulation along with\
steady state simulation.\n");
}
else if(2 == config.run_PDN){
fatal("Running transient simulation and per-line steady state simulation together \
does not make much sense. Modify voltspot.c to override this interruption.\n");
}
}
if(config.PDN_multi_dom && config.v_stacking)
fatal("Multi_dom and v_stacking do not work together yet.\n");
/* allocate and initialize the R model for PDN */
model = alloc_model(&config, flp);
populate_C4_PDN(model);
if(is_3D)
populate_TSV_PDN(model);
if(config.v_stacking)
populate_IVR(model);
populate_R_model_PDN(model);
if(do_transient){
populate_LC_model_PDN(model);
m = PDN_trans_matrix_dim(model);
model->trans_matrix_dim = m;
model->last_trans = dvector(m);
if(!is_3D){
if( !(perm_r = (int *) calloc(m, sizeof(int))) ) fatal("Malloc fails for perm_r[].\n");
if( !(perm_c = (int *) calloc(m, sizeof(int))) ) fatal("Malloc fails for perm_c[].\n");
}
}
status = alloc_status(model);
populate_status(model, status);
/* n is the sum total of the number of functional blocks
* of all the floorplans */
n = model->total_n_blocks;
/* allocate the power arrays */
power = dvector(n);
/* For trans PDN step input elimination */
cur_power = dvector(n);
delta_power = dvector(n);
overall_power = dvector(n);
if(!(pin = fopen(global_config.p_infile, "r")))
fatal("unable to open power trace input file\n");
if(do_transient && !(vout = fopen(global_config.v_outfile, "w")))
fatal("unable to open PDN trace file for output\n");
/* names of functional units */
names = alloc_names(MAX_UNITS, STR_SIZE);
if(read_names(pin, names) != n)
fatal("no. of units in floorplan and trace file differ\n");
vals = dvector(MAX_UNITS);
/* Initialize Matrix, power trace*/
if(do_transient){
status->trans_counter = 0;
status->draw_counter = 0;
/* Zero power before the first line */
zero_dvector(cur_power, n);
/* Create J matrix, LU decomposition */
if(!is_3D){
if(model->config.PDN_gridL){
trans_SLU_init(model, cur_power, &A, &L, &U, perm_c, perm_r);
}
else{
trans_SLU_init_nogridL(model, cur_power, &A, &L, &U, perm_c, perm_r);
}
}
/* Init vector */
PDN_init_trans_vector(model, model->last_trans);
if(model->config.PDN_sin_pattern)
trans_intvl = model->config.PDN_sin_totstep;
else
trans_intvl = model->config.ptrace_sampling_intvl * model->config.PDN_step_percycle;
warmup_steps = model->config.PDN_ptrace_warmup * trans_intvl;
if(model->config.v_stacking){
double Fsw_ratio = model->config.proc_clock_freq*model->config.PDN_step_percycle/model->sc_converter->freq;
if(fabs(floor(Fsw_ratio+0.5)-Fsw_ratio) < DELTA)
switch_intvl = (int) Fsw_ratio;
else
fatal("Please use an integer ratio between SC converters switching T and solver timestep!\n");
}
else{
switch_intvl = 0;
}
dump_PDN_config(model, vout);
print_trans_header(model, status, vout);
}
/* read the instantaneous power trace */
while ((num=read_vals(pin, vals)) != 0) {
if(num != n){
fatal("invalid trace file format\n");
}
/* permute the power numbers according to the floorplan order */
for(i=0, base=0, count=0; i<model->n_layers; i++) {
for(j=0; j<model->layers[i].flp->n_units; j++) {
idx = PDN_get_blk_index(model->layers[i].flp, names[count+j]);
power[base+idx] = vals[count+j];
}
count += model->layers[i].flp->n_units;
base += model->layers[i].flp->n_units;
}
/* compute PDN voltage */
if(do_transient){
if(is_3D){
if(model->config.PDN_sin_pattern){
for(intv=0; intv<trans_intvl; intv++){
status->trans_counter++;
sin_dvector(cur_power, power,
intv/(model->config.proc_clock_freq*model->config.PDN_step_percycle),
model->config.PDN_sin_freq, n);
trans_matrix_build_3D(model, cur_power, &A, &L, &U, &perm_c, &perm_r);
compute_PDN_SLU(model, cur_power, &A, &L, &U, perm_c, perm_r);
if(model->config.v_stacking)
switch_SCconverters(model, status->trans_counter, switch_intvl);
perstep_droop_3D(model, vout);
SUPERLU_FREE (perm_r);
SUPERLU_FREE (perm_c);
Destroy_CompCol_Matrix(&A);
Destroy_SuperNode_Matrix(&L);
Destroy_CompCol_Matrix(&U);
}
lines++;
break;
}
else{
trans_matrix_build_3D(model, power, &A, &L, &U, &perm_c, &perm_r);
for(intv=0; intv<trans_intvl; intv++){
status->trans_counter++;
compute_PDN_SLU(model, power, &A, &L, &U, perm_c, perm_r);
if(model->config.v_stacking)
switch_SCconverters(model, status->trans_counter, switch_intvl);
if(status->trans_counter > warmup_steps){ // ignore warm up
PDN_trans_analyze(model, status);
if(!(status->trans_counter % model->config.PDN_step_percycle))
print_trans_analyze(model, status, vout);
}
}
SUPERLU_FREE (perm_r);
SUPERLU_FREE (perm_c);
Destroy_CompCol_Matrix(&A);
Destroy_SuperNode_Matrix(&L);
Destroy_CompCol_Matrix(&U);
}
}
else{
if(model->config.PDN_sin_pattern){
for(intv=0; intv<trans_intvl; intv++){
sin_dvector(cur_power, power,
intv/(model->config.proc_clock_freq*model->config.PDN_step_percycle),
model->config.PDN_sin_freq, n);
compute_PDN_SLU(model, cur_power, &A, &L, &U, perm_c, perm_r);
print_step_singlenode(model, vout, 0, 0);
}
lines++;
break;
}
else{
sub_dvector(delta_power, power, cur_power, n);
mul_val_dvector(delta_power, ((double)1/trans_intvl), n);
for(intv=0; intv<trans_intvl; intv++){
status->trans_counter++;
add_dvector(cur_power, cur_power, delta_power, n);
compute_PDN_SLU(model, cur_power, &A, &L, &U, perm_c, perm_r);
if(status->trans_counter > warmup_steps){ // ignore warm up
PDN_trans_analyze(model, status);
if(!(status->trans_counter % model->config.PDN_step_percycle))
print_trans_analyze(model, status, vout);
}
}
}
}
printf("Done with ptrace line %d\n", lines+1);
}
/* per-line steady state */
if(2 == model->config.run_PDN){
steady_state_PDN(model, power);
PDN_steady_analyze(model, status);
if(model->config.animation){
draw_single_gif(model, status, status->draw_counter, STEADY);
status->draw_counter++;
}
printf("Done with ptrace line %d\n", lines+1);
}
/* for computing average */
for(i=0, base=0; i<model->n_layers; i++) {
for(j=0; j<model->layers[i].flp->n_units; j++)
overall_power[base+j] += power[base+j];
base += model->layers[i].flp->n_units;
}
lines++;
}
if(!lines)
fatal("no power numbers in trace file\n");
int tot_cycles;
if(do_transient){
/* Mark end in vtrace */
fprintf(vout, "#END\n");
// calculate per cycle average gridnode noise
tot_cycles = (lines - model->config.PDN_ptrace_warmup) * model->config.ptrace_sampling_intvl;
mul_val_dvector(status->gridstats.integral_2D[0], ((double)1/tot_cycles), model->rows*model->cols);
dump_files(model, status, TRANSIENT);
}
if(2 == model->config.run_PDN){
dump_files(model, status, STEADY);
}
if(model->config.animation)
create_animation(status->draw_counter, model->config.animation);
/* for computing average */
for(i=0, base=0; i<model->n_layers; i++) {
for(j=0; j<model->layers[i].flp->n_units; j++) {
overall_power[base+j] /= lines;
total_power += overall_power[base+j];
}
base += model->layers[i].flp->n_units;
}
if(1 == model->config.run_PDN){
/* steady state PDN */
steady_state_PDN(model, overall_power);
/* analyze and print PDN status */
PDN_steady_analyze(model, status);
print_steady_analyze(model, status);
dump_files(model, status, STEADY);
}
/* cleanup */
fclose(pin);
PDN_free_flp(flp);
free_dvector(power);
free_dvector(cur_power);
free_dvector(delta_power);
free_dvector(overall_power);
free_names(names);
free_dvector(vals);
if(do_transient && (!is_3D)){
fclose(vout);
SUPERLU_FREE (perm_r);
SUPERLU_FREE (perm_c);
Destroy_CompCol_Matrix(&A);
Destroy_SuperNode_Matrix(&L);
Destroy_CompCol_Matrix(&U);
}
free_status(model, status);
delete_model(model);
return 0;
}