/*1* A dummy simulator template file to illustrate the use of2* HotSpot in a cycle-accurate simulator like Simplescalar.3* This file contains the following sample routines:4* a) Model initialization (sim_init)5* b) Model use in a cycle-by-cycle power model (sim_main)6* c) Model uninitialization (sim_exit)7* Please note that all of these routines are just instructional8* templates and not full-fledged code. Hence they are not used9* anywhere else in the distribution.10*/11#include <string.h>1213#include "temperature.h"14#include "temperature_grid.h" /* for dump_steady_temp_grid */15#include "flp.h"16#include "util.h"1718/* input and output files */19static char *flp_file; /* has the floorplan configuration */20static char *init_file; /* initial temperatures from file */21static char *steady_file; /* steady state temperatures to file */2223/* floorplan */24static flp_t *flp;25/* hotspot temperature model */26static RC_model_t *model;27/* instantaneous temperature and power values */28static double *temp, *power;29/* steady state temperature and power values */30static double *overall_power, *steady_temp;3132/* sample model initialization */33void sim_init()34{35/* initialize flp, get adjacency matrix */36flp = read_flp(flp_file, FALSE, FALSE);3738/*39* configure thermal model parameters. default_thermal_config40* returns a set of default parameters. only those configuration41* parameters (config.*) that need to be changed are set explicitly.42*/43thermal_config_t config = default_thermal_config();44strcpy(config.init_file, init_file);45strcpy(config.steady_file, steady_file);4647/* default_thermal_config selects block model as the default.48* in case grid model is needed, select it explicitly and49* set the grid model parameters (grid_rows, grid_cols,50* grid_steady_file etc.) appropriately. for e.g., in the51* following commented line, we just choose the grid model52* and let everything else to be the default.53* NOTE: for modeling 3-D chips, it is essential to set54* the layer configuration file (grid_layer_file) parameter.55*/56/* strcpy(config->model_type, GRID_MODEL_STR); */5758/* allocate and initialize the RC model */59alloc_RC_model(&config, flp, NULL, NULL, FALSE, FALSE);60populate_R_model(model, flp);61populate_C_model(model, flp);6263/* allocate the temp and power arrays */64/* using hotspot_vector to internally allocate any extra nodes needed */65temp = hotspot_vector(model);66power = hotspot_vector(model);67steady_temp = hotspot_vector(model);68overall_power = hotspot_vector(model);6970/* set up initial instantaneous temperatures */71if (strcmp(model->config->init_file, NULLFILE)) {72if (!model->config->dtm_used) /* initial T = steady T for no DTM */73read_temp(model, temp, model->config->init_file, FALSE);74else /* initial T = clipped steady T with DTM */75read_temp(model, temp, model->config->init_file, TRUE);76}77else /* no input file - use init_temp as the common temperature */78set_temp(model, temp, model->config->init_temp);79}8081/*82* sample routine to illustrate the possible use of hotspot in a83* cycle-by-cycle power model. note that this is just a stub84* function and is not called anywhere in this file85*/86void sim_main()87{88static double cur_time, prev_time;89static int first_call = TRUE;90int i, j, base, idx;9192/* the main simulator loop */93while (TRUE) {94/* set the per cycle power values as returned by Wattch/power simulator */95if (model->type == BLOCK_MODEL) {96power[get_blk_index(flp, "Icache")] += 0; /* set the power numbers instead of '0' */97power[get_blk_index(flp, "Dcache")] += 0;98power[get_blk_index(flp, "Bpred")] += 0;99/* ... more functional units ... */100101/* for the grid model, set the power numbers for all power dissipating layers */102} else103for(i=0, base=0; i < model->grid->n_layers; i++) {104if(model->grid->layers[i].has_power) {105idx = get_blk_index(model->grid->layers[i].flp, "Icache");106power[base+idx] += 0; /* set the power numbers instead of '0' */107idx = get_blk_index(model->grid->layers[i].flp, "Dcache");108power[base+idx] += 0;109idx = get_blk_index(model->grid->layers[i].flp, "Bpred");110power[base+idx] += 0;111/* ... more functional units ... */112113}114base += model->grid->layers[i].flp->n_units;115}116117/* call compute_temp at regular intervals */118if ((cur_time - prev_time) >= model->config->sampling_intvl) {119double elapsed_time = (cur_time - prev_time);120prev_time = cur_time;121122/* find the average power dissipated in the elapsed time */123if (model->type == BLOCK_MODEL) {124for (i = 0; i < flp->n_units; i++) {125/* for steady state temperature calculation */126overall_power[i] += power[i];127/*128* 'power' array is an aggregate of per cycle numbers over129* the sampling_intvl. so, compute the average power130*/131power[i] /= (elapsed_time * model->config->base_proc_freq);132}133/* for the grid model, account for all the power dissipating layers */134} else135for(i=0, base=0; i < model->grid->n_layers; i++) {136if(model->grid->layers[i].has_power)137for(j=0; j < model->grid->layers[i].flp->n_units; j++) {138/* for steady state temperature calculation */139overall_power[base+j] += power[base+j];140/* compute average power */141power[base+j] /= (elapsed_time * model->config->base_proc_freq);142}143base += model->grid->layers[i].flp->n_units;144}145146/* calculate the current temp given the previous temp, time147* elapsed since then, and the average power dissipated during148* that interval. for the grid model, only the first call to149* compute_temp passes a non-null 'temp' array. if 'temp' is NULL,150* compute_temp remembers it from the last non-null call.151* this is used to maintain the internal grid temperatures152* across multiple calls of compute_temp153*/154if (model->type == BLOCK_MODEL || first_call)155compute_temp(model, power, temp, elapsed_time);156else157compute_temp(model, power, NULL, elapsed_time);158159/* make sure to record the first call */160first_call = FALSE;161162/* reset the power array */163if (model->type == BLOCK_MODEL)164for (i = 0; i < flp->n_units; i++)165power[i] = 0;166else167for(i=0, base=0; i < model->grid->n_layers; i++) {168if(model->grid->layers[i].has_power)169for(j=0; j < model->grid->layers[i].flp->n_units; j++)170power[base+j] = 0;171base += model->grid->layers[i].flp->n_units;172}173}174}175}176177/*178* sample uninitialization routine to illustrate the possible use of hotspot in a179* cycle-by-cycle power model. note that this is just a stub180* function and is not called anywhere in this file181*/182void sim_exit()183{184double total_elapsed_cycles = 0; /* set this to be the correct time elapsed (in cycles) */185int i, j, base;186187/* find the average power dissipated in the elapsed time */188if (model->type == BLOCK_MODEL)189for (i = 0; i < flp->n_units; i++)190overall_power[i] /= total_elapsed_cycles;191else192for(i=0, base=0; i < model->grid->n_layers; i++) {193if(model->grid->layers[i].has_power)194for(j=0; j < model->grid->layers[i].flp->n_units; j++)195overall_power[base+j] /= total_elapsed_cycles;196base += model->grid->layers[i].flp->n_units;197}198199/* get steady state temperatures */200steady_state_temp(model, overall_power, steady_temp);201202/* dump temperatures if needed */203if (strcmp(model->config->steady_file, NULLFILE))204dump_temp(model, steady_temp, model->config->steady_file);205206/* for the grid model, optionally dump the internal207* temperatures of the grid cells208*/209if (model->type == GRID_MODEL &&210strcmp(model->config->grid_steady_file, NULLFILE))211dump_steady_temp_grid(model->grid, model->config->grid_steady_file);212213/* cleanup */214delete_RC_model(model);215free_flp(flp, FALSE, FALSE);216free_dvector(temp);217free_dvector(power);218free_dvector(steady_temp);219free_dvector(overall_power);220}221222223