-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path431projectUtils.cpp
More file actions
377 lines (352 loc) · 11.4 KB
/
431projectUtils.cpp
File metadata and controls
377 lines (352 loc) · 11.4 KB
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
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sys/stat.h>
#include <unistd.h>
#include <algorithm>
#include <fstream>
#include <map>
#include <math.h>
#include <fcntl.h>
#include <sys/file.h>
#include "431project.h"
/*
Runs experiments for a given configuration, if they have not already been run. Stores raw data in globally accessible location
*/
int runexperiments(std::string configuration, unsigned int iteration) {
std::cout << "Iter # " << iteration << " config: " << configuration;
struct stat buffer;
std::string dotconfig = configuration; // filename version
std::replace(dotconfig.begin(), dotconfig.end(), ' ', '.'); // generate filename version
std::string endfile = GLOB_outputpath + "DONE." + dotconfig + ".DONE"; // post-run file
if (!isNumDimConfiguration(configuration)) { // Configuration in incorrect format!!!
std::cerr << "ATTEMPTING TO RUN INCORRECTLY FORMATTED CONFIGURATION!\n"
"ABORTING EXECUTION IMMEDIATELY!\n"
"Configuration in question: " << configuration << std::endl;
exit(-1);
}
if (0 == (stat(endfile.c_str(), &buffer))) { // already generated for this configuration
std::cout << " : found in file";
return 0;
} else {
// run experiments;
std::string bashcmdline = GLOB_script + configuration + " > /dev/null";
std::cout << " : running simulation";
int retval = system(bashcmdline.c_str());
return retval;
}
}
/*
* Retrieves data from raw result files and places it in an in-memory data structure
*/
void populate(std::string configuration) {
std::map<std::string, double>* curmap = GLOB_extracted_values[configuration];
if (!curmap) {
curmap = new std::map<std::string, double>;
GLOB_extracted_values[configuration] = curmap;
}
std::string cmdtail = " | sed -re 's:[ ]+: :g' | cut -d\\ -f2 >";
std::string cmdhead = "grep ";
std::string dotconfig = configuration;
std::replace(dotconfig.begin(), dotconfig.end(), ' ', '.'); // generate filename version
dotconfig += ".simout";
for (int i = 0; i < 5; ++i) { // for each benchmark
std::string querybody = GLOB_outputpath + GLOB_prefixes[i] + dotconfig
+ cmdtail + "summaryfiles/" + GLOB_prefixes[i] + dotconfig
+ ".summary";
std::string curquery = cmdhead + "sim_num_insn " + querybody;
system(curquery.c_str());
// switch to append
querybody = GLOB_outputpath + GLOB_prefixes[i] + dotconfig + cmdtail
+ "> summaryfiles/" + GLOB_prefixes[i] + dotconfig + ".summary";
for (int j = 1; j < 7; ++j) { //skip first field
curquery = cmdhead + GLOB_fields[j] + querybody;
system(curquery.c_str());
}
// summary file generated, retrieve values
// !does not perform error checking!
std::fstream summaryfile(
("summaryfiles/" + GLOB_prefixes[i] + dotconfig + ".summary").c_str(),
std::ios_base::in);
for (int j = 0; j < 7; ++j) {
double curval;
summaryfile >> curval;
(*curmap)[GLOB_prefixes[i] + GLOB_fields[j]] = curval;
}
}
GLOB_seen_configurations[configuration] = 1;
}
//Geomean of execution times across benchmarks in a configuration
double calculategeomeanExecutionTime(std::string configuration) {
double geomean = 1.0;
for (int i = 0; i < 5; ++i) {
geomean *= calculateExecutionTime(configuration, GLOB_prefixes[i]);
}
geomean = pow(geomean, 1.0 / 5.0);
return geomean;
}
//Geomean of EDP across benchmarks in a configuration
double calculategeomeanEDP(std::string configuration) {
double geomean = 1.0;
for (int i = 0; i < 5; ++i) {
geomean *= calculateEDP(configuration, GLOB_prefixes[i]);
}
geomean = pow(geomean, 1.0 / 5.0);
return geomean;
}
// Trivial sanity check
int isNumDimConfiguration(std::string configuration) {
if ((NUM_DIMS * 2 - 1) != configuration.size()) {
std::cerr << "Wrong length for configuration\n";
return 0; // wrong length
}
for (int fieldnum = 0; fieldnum < NUM_DIMS; ++fieldnum) {
int j = 2 * fieldnum;
std::string field = configuration.substr(j, 1);
if ('0' <= field[0] && field[0] <= '9') {
// is a digit - convert to unteger value
int fieldvalue = atoi(field.c_str());
if (fieldvalue >= GLOB_dimensioncardinality[fieldnum]) {
std::cerr << "Field " << fieldnum << " is out of range. Value: "
<< fieldvalue << std::endl;
return 0; // dimension value out of range
}
} else {
std::cerr << "Field not a digit: " << fieldnum << std::endl;
return 0; // field not a digit
}
if ((NUM_DIMS - 1) != fieldnum
&& " " != configuration.substr(j + 1, 1)) {
std::cerr << "Odd characters not spaces for field: " << fieldnum
<< std::endl;
return 0; // not single digits separated by spaces
}
}
return 1; //Passed tests
}
double cycleTime(std::string configuration) {
bool inorder = (0 == extractConfigPararm(configuration, 1));
int width = 1 << extractConfigPararm(configuration, 0);
int fpwidth = 1 << extractConfigPararm(configuration, 11);
double fpCycleTime = fpwidth*5e-12;
if (inorder) {
switch (width) {
case 1:
return 100e-12 + fpCycleTime;
case 2:
return 120e-12 + fpCycleTime;
case 4:
return 140e-12 + fpCycleTime;
case 8:
return 165e-12 + fpCycleTime;
}
} else {
switch (width) {
case 1:
return 115e-12 + fpCycleTime;
case 2:
return 125e-12 + fpCycleTime;
case 4:
return 150e-12 + fpCycleTime;
case 8:
return 175e-12 + fpCycleTime;
}
}
std::cerr << "invalid width or inorder setting" << std::endl;
exit(-1); // invalid width/inorder
return 1e-12; // should never get here
}
double EPCI(std::string configuration) {
bool inorder = (0 == extractConfigPararm(configuration, 1));
int width = 1 << extractConfigPararm(configuration, 0);
if (inorder) {
switch (width) {
case 1:
return 8e-12;
case 2:
return 10e-12;
case 4:
return 14e-12;
case 8:
return 20e-12;
}
} else {
switch (width) {
case 1:
return 10e-12;
case 2:
return 12e-12;
case 4:
return 18e-12;
case 8:
return 27e-12;
}
}
std::cerr << "invalid width or inorder setting" << std::endl;
exit(-1); // invalid width/inorder
return 1e-12; // should never get here
}
double PipelineLeakage(std::string configuration) {
bool inorder = (0 == extractConfigPararm(configuration, 1));
int width = 1 << extractConfigPararm(configuration, 0);
int fpwidth = 1 << extractConfigPararm(configuration, 11);
double fpLeakage = 0.25e-3 * fpwidth;
if (inorder) {
switch (width) {
case 1:
return 1e-3 + fpLeakage;
case 2:
return 1.5e-3 + fpLeakage;
case 4:
return 7e-3 + fpLeakage;
case 8:
return 30e-3 + fpLeakage;
}
} else {
switch (width) {
case 1:
return 1.5e-3 + fpLeakage;
case 2:
return 2e-3 + fpLeakage;
case 4:
return 8e-3 + fpLeakage;
case 8:
return 32e-3 + fpLeakage;
}
}
std::cerr << "invalid width or inorder setting" << std::endl;
exit(-1); // invalid width/inorder
return 1e-12; // should never get here
}
// leakage in watts
double getcacheleak(unsigned int size) {
if (size <= 8192) {
return 125e-6;
} else if (size <= 16384) {
return 250e-6;
} else if (size <= 32768) {
return 500e-6;
} else if (size <= 65536) {
return 1e-3;
} else if (size <= 131072) {
return 2e-3;
} else if (size <= 262144) {
return 4e-3;
} else if (size <= 524288) {
return 8e-3;
} else if (size <= 1048576) {
return 16e-3;
} else if (size <= 2097152) {
return 32e-3;
}
// std::cerr << "USED BAD CACHE SIZE: "<< size <<std::endl;
return 40e-3;
}
// all sizes in bytes
unsigned int getdl1size(std::string configuration) {
unsigned int dl1sets = 32 << extractConfigPararm(configuration, 3);
unsigned int dl1assoc = 1 << extractConfigPararm(configuration, 4);
unsigned int dl1blocksize = 8
* (1 << extractConfigPararm(configuration, 2));
return dl1assoc * dl1sets * dl1blocksize;
}
unsigned int getil1size(std::string configuration) {
unsigned int il1sets = 32 << extractConfigPararm(configuration, 5);
unsigned int il1assoc = 1 << extractConfigPararm(configuration, 6);
unsigned int il1blocksize = 8
* (1 << extractConfigPararm(configuration, 2));
return il1assoc * il1sets * il1blocksize;
}
unsigned int getl2size(std::string configuration) {
unsigned int l2sets = 256 << extractConfigPararm(configuration, 7);
unsigned int l2blocksize = 16 << extractConfigPararm(configuration, 8);
unsigned int l2assoc = 1 << extractConfigPararm(configuration, 9);
return l2assoc * l2sets * l2blocksize;
}
// leakage in watts
double cacheleak(std::string configuration) {
double sum = 0;
sum += getcacheleak(getdl1size(configuration));
sum += getcacheleak(getil1size(configuration));
sum += getcacheleak(getl2size(configuration));
return sum;
}
//energy per access in joules
double getaccessenergy(unsigned int size) {
if (size <= 8192) {
return 20e-12;
} else if (size <= 16384) {
return 28e-12;
} else if (size <= 32768) {
return 40e-12;
} else if (size <= 65536) {
return 56e-12;
} else if (size <= 131072) {
return 80e-12;
} else if (size <= 262144) {
return 112e-12;
} else if (size <= 524288) {
return 160e-12;
} else if (size <= 1048576) {
return 224e-12;
} else if (size <= 2097152) {
return 360e-12;
}
// std::cerr << "USED BAD CACHE SIZE: "<< cachesize <<std::endl;
return 400 - 12;
}
/*
* Use data in GLOB_extracted_values to calculate execution time, in seconds, of 1 benchmark, on one configuration
*/
double calculateExecutionTime(std::string configuration,
std::string benchmarkprefix) {
double secondspercycle = cycleTime(configuration);
double cycleCount =
(*(GLOB_extracted_values[configuration]))[benchmarkprefix
+ GLOB_fields[1]]; // field 1 is sim_cycle
return secondspercycle * cycleCount;
}
/*
* Use data in GLOB_extracted_values to calculate energy delay product, in Joule-seconds, of 1 benchmark, on one configuration. EDP will = Time in seconds * ((sum of all leakage in W) * Time in seconds) + (sum over
*/
double calculateEDP(std::string configuration, std::string benchmarkprefix) {
//recall that GLOB_fields[7]={"sim_num_insn ", "sim_cycle ","il1.accesses ","dl1.accesses ","ul2.accesses ","ul2.misses ","ul2.writebacks "};
double executiontime = calculateExecutionTime(configuration,
benchmarkprefix);
double leakageEnergy = executiontime
* (PipelineLeakage(configuration) + cacheleak(configuration)
+ /*Main memory refresh*/512e-3);
double executionEnergy = EPCI(configuration)
* (*(GLOB_extracted_values[configuration]))[benchmarkprefix
+ GLOB_fields[0]];
double instaccessEnergy = getaccessenergy(getil1size(configuration))
* (*(GLOB_extracted_values[configuration]))[benchmarkprefix
+ GLOB_fields[2]];
double d1accessEnergy = getaccessenergy(getdl1size(configuration))
* (*(GLOB_extracted_values[configuration]))[benchmarkprefix
+ GLOB_fields[3]];
double l2accessEnergy = getaccessenergy(getl2size(configuration))
* (*(GLOB_extracted_values[configuration]))[benchmarkprefix
+ GLOB_fields[4]];
double memoryaccessEnergy = 2e-9 * 1
* (((*(GLOB_extracted_values[configuration]))[benchmarkprefix
+ GLOB_fields[5]])
+ (*(GLOB_extracted_values[configuration]))[benchmarkprefix
+ GLOB_fields[6]]);
executionEnergy = executionEnergy + instaccessEnergy + d1accessEnergy
+ l2accessEnergy + memoryaccessEnergy;
return executiontime * (leakageEnergy + executionEnergy);
}
/*
* Helper function
*/
int extractConfigPararm(std::string config, int paramIndex) {
if (paramIndex >= NUM_DIMS) {
std::cerr << "Trying to extract param index " << paramIndex
<< " but total number of params are " << NUM_DIMS << std::endl;
exit(1);
}
return atoi((config.substr(2 * paramIndex, 1)).c_str());
}