Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
uvahotspot
GitHub Repository: uvahotspot/HotSpot
Path: blob/master/sim-template.c
612 views
1
/*
2
* A dummy simulator template file to illustrate the use of
3
* HotSpot in a cycle-accurate simulator like Simplescalar.
4
* This file contains the following sample routines:
5
* a) Model initialization (sim_init)
6
* b) Model use in a cycle-by-cycle power model (sim_main)
7
* c) Model uninitialization (sim_exit)
8
* Please note that all of these routines are just instructional
9
* templates and not full-fledged code. Hence they are not used
10
* anywhere else in the distribution.
11
*/
12
#include <string.h>
13
14
#include "temperature.h"
15
#include "temperature_grid.h" /* for dump_steady_temp_grid */
16
#include "flp.h"
17
#include "util.h"
18
19
/* input and output files */
20
static char *flp_file; /* has the floorplan configuration */
21
static char *init_file; /* initial temperatures from file */
22
static char *steady_file; /* steady state temperatures to file */
23
24
/* floorplan */
25
static flp_t *flp;
26
/* hotspot temperature model */
27
static RC_model_t *model;
28
/* instantaneous temperature and power values */
29
static double *temp, *power;
30
/* steady state temperature and power values */
31
static double *overall_power, *steady_temp;
32
33
/* sample model initialization */
34
void sim_init()
35
{
36
/* initialize flp, get adjacency matrix */
37
flp = read_flp(flp_file, FALSE, FALSE);
38
39
/*
40
* configure thermal model parameters. default_thermal_config
41
* returns a set of default parameters. only those configuration
42
* parameters (config.*) that need to be changed are set explicitly.
43
*/
44
thermal_config_t config = default_thermal_config();
45
strcpy(config.init_file, init_file);
46
strcpy(config.steady_file, steady_file);
47
48
/* default_thermal_config selects block model as the default.
49
* in case grid model is needed, select it explicitly and
50
* set the grid model parameters (grid_rows, grid_cols,
51
* grid_steady_file etc.) appropriately. for e.g., in the
52
* following commented line, we just choose the grid model
53
* and let everything else to be the default.
54
* NOTE: for modeling 3-D chips, it is essential to set
55
* the layer configuration file (grid_layer_file) parameter.
56
*/
57
/* strcpy(config->model_type, GRID_MODEL_STR); */
58
59
/* allocate and initialize the RC model */
60
alloc_RC_model(&config, flp, NULL, NULL, FALSE, FALSE);
61
populate_R_model(model, flp);
62
populate_C_model(model, flp);
63
64
/* allocate the temp and power arrays */
65
/* using hotspot_vector to internally allocate any extra nodes needed */
66
temp = hotspot_vector(model);
67
power = hotspot_vector(model);
68
steady_temp = hotspot_vector(model);
69
overall_power = hotspot_vector(model);
70
71
/* set up initial instantaneous temperatures */
72
if (strcmp(model->config->init_file, NULLFILE)) {
73
if (!model->config->dtm_used) /* initial T = steady T for no DTM */
74
read_temp(model, temp, model->config->init_file, FALSE);
75
else /* initial T = clipped steady T with DTM */
76
read_temp(model, temp, model->config->init_file, TRUE);
77
}
78
else /* no input file - use init_temp as the common temperature */
79
set_temp(model, temp, model->config->init_temp);
80
}
81
82
/*
83
* sample routine to illustrate the possible use of hotspot in a
84
* cycle-by-cycle power model. note that this is just a stub
85
* function and is not called anywhere in this file
86
*/
87
void sim_main()
88
{
89
static double cur_time, prev_time;
90
static int first_call = TRUE;
91
int i, j, base, idx;
92
93
/* the main simulator loop */
94
while (TRUE) {
95
/* set the per cycle power values as returned by Wattch/power simulator */
96
if (model->type == BLOCK_MODEL) {
97
power[get_blk_index(flp, "Icache")] += 0; /* set the power numbers instead of '0' */
98
power[get_blk_index(flp, "Dcache")] += 0;
99
power[get_blk_index(flp, "Bpred")] += 0;
100
/* ... more functional units ... */
101
102
/* for the grid model, set the power numbers for all power dissipating layers */
103
} else
104
for(i=0, base=0; i < model->grid->n_layers; i++) {
105
if(model->grid->layers[i].has_power) {
106
idx = get_blk_index(model->grid->layers[i].flp, "Icache");
107
power[base+idx] += 0; /* set the power numbers instead of '0' */
108
idx = get_blk_index(model->grid->layers[i].flp, "Dcache");
109
power[base+idx] += 0;
110
idx = get_blk_index(model->grid->layers[i].flp, "Bpred");
111
power[base+idx] += 0;
112
/* ... more functional units ... */
113
114
}
115
base += model->grid->layers[i].flp->n_units;
116
}
117
118
/* call compute_temp at regular intervals */
119
if ((cur_time - prev_time) >= model->config->sampling_intvl) {
120
double elapsed_time = (cur_time - prev_time);
121
prev_time = cur_time;
122
123
/* find the average power dissipated in the elapsed time */
124
if (model->type == BLOCK_MODEL) {
125
for (i = 0; i < flp->n_units; i++) {
126
/* for steady state temperature calculation */
127
overall_power[i] += power[i];
128
/*
129
* 'power' array is an aggregate of per cycle numbers over
130
* the sampling_intvl. so, compute the average power
131
*/
132
power[i] /= (elapsed_time * model->config->base_proc_freq);
133
}
134
/* for the grid model, account for all the power dissipating layers */
135
} else
136
for(i=0, base=0; i < model->grid->n_layers; i++) {
137
if(model->grid->layers[i].has_power)
138
for(j=0; j < model->grid->layers[i].flp->n_units; j++) {
139
/* for steady state temperature calculation */
140
overall_power[base+j] += power[base+j];
141
/* compute average power */
142
power[base+j] /= (elapsed_time * model->config->base_proc_freq);
143
}
144
base += model->grid->layers[i].flp->n_units;
145
}
146
147
/* calculate the current temp given the previous temp, time
148
* elapsed since then, and the average power dissipated during
149
* that interval. for the grid model, only the first call to
150
* compute_temp passes a non-null 'temp' array. if 'temp' is NULL,
151
* compute_temp remembers it from the last non-null call.
152
* this is used to maintain the internal grid temperatures
153
* across multiple calls of compute_temp
154
*/
155
if (model->type == BLOCK_MODEL || first_call)
156
compute_temp(model, power, temp, elapsed_time);
157
else
158
compute_temp(model, power, NULL, elapsed_time);
159
160
/* make sure to record the first call */
161
first_call = FALSE;
162
163
/* reset the power array */
164
if (model->type == BLOCK_MODEL)
165
for (i = 0; i < flp->n_units; i++)
166
power[i] = 0;
167
else
168
for(i=0, base=0; i < model->grid->n_layers; i++) {
169
if(model->grid->layers[i].has_power)
170
for(j=0; j < model->grid->layers[i].flp->n_units; j++)
171
power[base+j] = 0;
172
base += model->grid->layers[i].flp->n_units;
173
}
174
}
175
}
176
}
177
178
/*
179
* sample uninitialization routine to illustrate the possible use of hotspot in a
180
* cycle-by-cycle power model. note that this is just a stub
181
* function and is not called anywhere in this file
182
*/
183
void sim_exit()
184
{
185
double total_elapsed_cycles = 0; /* set this to be the correct time elapsed (in cycles) */
186
int i, j, base;
187
188
/* find the average power dissipated in the elapsed time */
189
if (model->type == BLOCK_MODEL)
190
for (i = 0; i < flp->n_units; i++)
191
overall_power[i] /= total_elapsed_cycles;
192
else
193
for(i=0, base=0; i < model->grid->n_layers; i++) {
194
if(model->grid->layers[i].has_power)
195
for(j=0; j < model->grid->layers[i].flp->n_units; j++)
196
overall_power[base+j] /= total_elapsed_cycles;
197
base += model->grid->layers[i].flp->n_units;
198
}
199
200
/* get steady state temperatures */
201
steady_state_temp(model, overall_power, steady_temp);
202
203
/* dump temperatures if needed */
204
if (strcmp(model->config->steady_file, NULLFILE))
205
dump_temp(model, steady_temp, model->config->steady_file);
206
207
/* for the grid model, optionally dump the internal
208
* temperatures of the grid cells
209
*/
210
if (model->type == GRID_MODEL &&
211
strcmp(model->config->grid_steady_file, NULLFILE))
212
dump_steady_temp_grid(model->grid, model->config->grid_steady_file);
213
214
/* cleanup */
215
delete_RC_model(model);
216
free_flp(flp, FALSE, FALSE);
217
free_dvector(temp);
218
free_dvector(power);
219
free_dvector(steady_temp);
220
free_dvector(overall_power);
221
}
222
223