-
Notifications
You must be signed in to change notification settings - Fork 13
/
sim-template.c
221 lines (197 loc) · 7.73 KB
/
sim-template.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
/*
* A dummy simulator template file to illustrate the use of
* HotSpot in a cycle-accurate simulator like Simplescalar.
* This file contains the following sample routines:
* a) Model initialization (sim_init)
* b) Model use in a cycle-by-cycle power model (sim_main)
* c) Model uninitialization (sim_exit)
* Please note that all of these routines are just instructional
* templates and not full-fledged code. Hence they are not used
* anywhere else in the distribution.
*/
#include <string.h>
#include "temperature.h"
#include "temperature_grid.h" /* for dump_steady_temp_grid */
#include "flp.h"
#include "util.h"
/* input and output files */
static char *flp_file; /* has the floorplan configuration */
static char *init_file; /* initial temperatures from file */
static char *steady_file; /* steady state temperatures to file */
/* floorplan */
static flp_t *flp;
/* hotspot temperature model */
static RC_model_t *model;
/* instantaneous temperature and power values */
static double *temp, *power;
/* steady state temperature and power values */
static double *overall_power, *steady_temp;
/* sample model initialization */
void sim_init()
{
/* initialize flp, get adjacency matrix */
flp = read_flp(flp_file, FALSE);
/*
* configure thermal model parameters. default_thermal_config
* returns a set of default parameters. only those configuration
* parameters (config.*) that need to be changed are set explicitly.
*/
thermal_config_t config = default_thermal_config();
strcpy(config.init_file, init_file);
strcpy(config.steady_file, steady_file);
/* default_thermal_config selects block model as the default.
* in case grid model is needed, select it explicitly and
* set the grid model parameters (grid_rows, grid_cols,
* grid_steady_file etc.) appropriately. for e.g., in the
* following commented line, we just choose the grid model
* and let everything else to be the default.
* NOTE: for modeling 3-D chips, it is essential to set
* the layer configuration file (grid_layer_file) parameter.
*/
/* strcpy(config->model_type, GRID_MODEL_STR); */
/* allocate and initialize the RC model */
model = alloc_RC_model(&config, flp);
populate_R_model(model, flp);
populate_C_model(model, flp);
/* allocate the temp and power arrays */
/* using hotspot_vector to internally allocate any extra nodes needed */
temp = hotspot_vector(model);
power = hotspot_vector(model);
steady_temp = hotspot_vector(model);
overall_power = hotspot_vector(model);
/* set up initial instantaneous temperatures */
if (strcmp(model->config->init_file, NULLFILE)) {
if (!model->config->dtm_used) /* initial T = steady T for no DTM */
read_temp(model, temp, model->config->init_file, FALSE);
else /* initial T = clipped steady T with DTM */
read_temp(model, temp, model->config->init_file, TRUE);
}
else /* no input file - use init_temp as the common temperature */
set_temp(model, temp, model->config->init_temp);
}
/*
* sample routine to illustrate the possible use of hotspot in a
* cycle-by-cycle power model. note that this is just a stub
* function and is not called anywhere in this file
*/
void sim_main()
{
static double cur_time, prev_time;
static int first_call = TRUE;
int i, j, base, idx;
/* the main simulator loop */
while (1) {
/* set the per cycle power values as returned by Wattch/power simulator */
if (model->type == BLOCK_MODEL) {
power[get_blk_index(flp, "Icache")] += 0; /* set the power numbers instead of '0' */
power[get_blk_index(flp, "Dcache")] += 0;
power[get_blk_index(flp, "Bpred")] += 0;
/* ... more functional units ... */
/* for the grid model, set the power numbers for all power dissipating layers */
} else
for(i=0, base=0; i < model->grid->n_layers; i++) {
if(model->grid->layers[i].has_power) {
idx = get_blk_index(model->grid->layers[i].flp, "Icache");
power[base+idx] += 0; /* set the power numbers instead of '0' */
idx = get_blk_index(model->grid->layers[i].flp, "Dcache");
power[base+idx] += 0;
idx = get_blk_index(model->grid->layers[i].flp, "Bpred");
power[base+idx] += 0;
/* ... more functional units ... */
}
base += model->grid->layers[i].flp->n_units;
}
/* call compute_temp at regular intervals */
if ((cur_time - prev_time) >= model->config->sampling_intvl) {
double elapsed_time = (cur_time - prev_time);
prev_time = cur_time;
/* find the average power dissipated in the elapsed time */
if (model->type == BLOCK_MODEL) {
for (i = 0; i < flp->n_units; i++) {
/* for steady state temperature calculation */
overall_power[i] += power[i];
/*
* 'power' array is an aggregate of per cycle numbers over
* the sampling_intvl. so, compute the average power
*/
power[i] /= (elapsed_time * model->config->base_proc_freq);
}
/* for the grid model, account for all the power dissipating layers */
} else
for(i=0, base=0; i < model->grid->n_layers; i++) {
if(model->grid->layers[i].has_power)
for(j=0; j < model->grid->layers[i].flp->n_units; j++) {
/* for steady state temperature calculation */
overall_power[base+j] += power[base+j];
/* compute average power */
power[base+j] /= (elapsed_time * model->config->base_proc_freq);
}
base += model->grid->layers[i].flp->n_units;
}
/* calculate the current temp given the previous temp, time
* elapsed since then, and the average power dissipated during
* that interval. for the grid model, only the first call to
* compute_temp passes a non-null 'temp' array. if 'temp' is NULL,
* compute_temp remembers it from the last non-null call.
* this is used to maintain the internal grid temperatures
* across multiple calls of compute_temp
*/
if (model->type == BLOCK_MODEL || first_call)
compute_temp(model, power, temp, elapsed_time);
else
compute_temp(model, power, NULL, elapsed_time);
/* make sure to record the first call */
first_call = FALSE;
/* reset the power array */
if (model->type == BLOCK_MODEL)
for (i = 0; i < flp->n_units; i++)
power[i] = 0;
else
for(i=0, base=0; i < model->grid->n_layers; i++) {
if(model->grid->layers[i].has_power)
for(j=0; j < model->grid->layers[i].flp->n_units; j++)
power[base+j] = 0;
base += model->grid->layers[i].flp->n_units;
}
}
}
}
/*
* sample uninitialization routine to illustrate the possible use of hotspot in a
* cycle-by-cycle power model. note that this is just a stub
* function and is not called anywhere in this file
*/
void sim_exit()
{
double total_elapsed_cycles = 0; /* set this to be the correct time elapsed (in cycles) */
int i, j, base;
/* find the average power dissipated in the elapsed time */
if (model->type == BLOCK_MODEL)
for (i = 0; i < flp->n_units; i++)
overall_power[i] /= total_elapsed_cycles;
else
for(i=0, base=0; i < model->grid->n_layers; i++) {
if(model->grid->layers[i].has_power)
for(j=0; j < model->grid->layers[i].flp->n_units; j++)
overall_power[base+j] /= total_elapsed_cycles;
base += model->grid->layers[i].flp->n_units;
}
/* get steady state temperatures */
steady_state_temp(model, overall_power, steady_temp);
/* dump temperatures if needed */
if (strcmp(model->config->steady_file, NULLFILE))
dump_temp(model, steady_temp, model->config->steady_file);
/* for the grid model, optionally dump the internal
* temperatures of the grid cells
*/
if (model->type == GRID_MODEL &&
strcmp(model->config->grid_steady_file, NULLFILE))
dump_steady_temp_grid(model->grid, model->config->grid_steady_file);
/* cleanup */
delete_RC_model(model);
free_flp(flp, FALSE);
free_dvector(temp);
free_dvector(power);
free_dvector(steady_temp);
free_dvector(overall_power);
}