-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.cpp
383 lines (323 loc) · 11.7 KB
/
common.cpp
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
// common routines and functions
//
#include "common.h"
#include <cstdlib>
#ifdef EPIC_MPI
#include "mpi.h"
#endif
bool Configuration::is_feasible()
{
return ( (L1D_size + L1I_size <= L2U_size) &&
(L1D_size >= L1D_block * L1D_assoc) &&
(L1I_size >= L1I_block * L1I_assoc) &&
(L2U_size >= L2U_block * L2U_assoc) &&
(L1I_block <= L2U_block) &&
(L1D_block <= L2U_block) );
}
void Configuration::invalidate()
{
num_clusters = -1;
integer_units = -1;
float_units = -1;
branch_units = -1;
memory_units = -1;
gpr_static_size = -1;
fpr_static_size = -1;
pr_static_size = -1;
cr_static_size = -1;
btr_static_size = -1;
L1D_size = -1;
L1D_block = -1;
L1D_assoc = -1;
L1I_size = -1;
L1I_block = -1;
L1I_assoc = -1;
L2U_size = -1;
L2U_block = -1;
L2U_assoc = -1;
tcc_region = -1; //db
max_unroll_allowed = -1; //db
regroup_only = -1; //db
do_classic_opti = -1; //db
do_prepass_scalar_scheduling = -1; //db
do_postpass_scalar_scheduling = -1; //db
do_modulo_scheduling = -1; //db
memvr_profiled = -1; //db
}
bool Configuration::check_difference(const Configuration& conf, Space_mask mask)
{
if (mask.integer_units && (integer_units!= conf.integer_units)) return true;
if (mask.float_units && (float_units!= conf.float_units)) return true;
if (mask.branch_units && (branch_units!=conf.branch_units)) return true;
if (mask.memory_units && (memory_units!=conf.memory_units)) return true;
if (mask.gpr_static_size && (gpr_static_size!=conf.gpr_static_size)) return true;
if (mask.fpr_static_size && (fpr_static_size!=conf.fpr_static_size)) return true;
if (mask.pr_static_size && (pr_static_size!= conf.pr_static_size)) return true;
if (mask.cr_static_size && (cr_static_size!= conf.cr_static_size)) return true;
if (mask.btr_static_size && (btr_static_size!=conf.btr_static_size)) return true;
if (mask.num_clusters && (num_clusters!=conf.num_clusters)) return true;
if (mask.L1D_size && (L1D_size!=conf.L1D_size)) return true;
if (mask.L1D_block && (L1D_block!=conf.L1D_block)) return true;
if (mask.L1D_assoc && (L1D_assoc!=conf.L1D_assoc)) return true;
if (mask.L1I_size && (L1I_size!=conf.L1I_size)) return true;
if (mask.L1I_block && (L1I_block!=conf.L1I_block)) return true;
if (mask.L1I_assoc && (L1I_assoc!=conf.L1I_assoc)) return true;
if (mask.L2U_size && (L2U_size!=conf.L2U_size)) return true;
if (mask.L2U_block && (L2U_block!=conf.L2U_block)) return true;
if (mask.L2U_assoc && (L2U_assoc!=conf.L2U_assoc)) return true;
if (mask.tcc_region && (tcc_region!=conf.tcc_region)) return true; //db
if (mask.max_unroll_allowed && (max_unroll_allowed!=conf.max_unroll_allowed)) return true; //db
if (mask.regroup_only && (regroup_only|=conf.regroup_only)) return true; //db
if (mask.do_classic_opti && (do_classic_opti!=conf.do_classic_opti)) return true; //db
if (mask.do_prepass_scalar_scheduling && (do_prepass_scalar_scheduling!=conf.do_prepass_scalar_scheduling)) return true; //db
if (mask.do_postpass_scalar_scheduling && (do_postpass_scalar_scheduling!=conf.do_postpass_scalar_scheduling)) return true; //db
if (mask.do_modulo_scheduling && (do_modulo_scheduling!=conf.do_modulo_scheduling)) return true; //db
if (mask.memvr_profiled && (memvr_profiled!=conf.memvr_profiled)) return true; //db
return false;
}
string Configuration::get_header() const
{
char s[100];
sprintf(s,"%u / %u %u %u %u / %u %u %u %u %u / %u %u %u / %u %u %u / %u %u %u / %u %u %u %u %u %u %u %u",
num_clusters, integer_units, float_units, branch_units,memory_units,
gpr_static_size, fpr_static_size, pr_static_size, cr_static_size, btr_static_size,
L1D_size, L1D_block, L1D_assoc, L1I_size, L1I_block, L1I_assoc, L2U_size, L2U_block, L2U_assoc,
tcc_region, max_unroll_allowed, regroup_only, do_classic_opti, do_prepass_scalar_scheduling, do_postpass_scalar_scheduling, do_modulo_scheduling, memvr_profiled); //db
return string(s);
}
string Configuration::get_executable_dir() const
{
char s[100];
sprintf(s,"%u_%u%u%u%u_%u%u%u%u%u_%u%u%u%u%u%u%u%u",
num_clusters, integer_units, float_units, branch_units, memory_units,
gpr_static_size, fpr_static_size, pr_static_size, cr_static_size, btr_static_size,
tcc_region, max_unroll_allowed, regroup_only, do_classic_opti, do_prepass_scalar_scheduling, do_postpass_scalar_scheduling, do_modulo_scheduling, memvr_profiled); //db
return string(s);
}
string Configuration::get_mem_dir() const
{
char s[100];
sprintf(s,"%u%u%u_%u%u%u_%u%u%u",L1D_size, L1D_block, L1D_assoc, L1I_size, L1I_block, L1I_assoc, L2U_size, L2U_block, L2U_assoc);
return string(s);
}
//added by [email protected]
void Configuration::fix_parameter(EParameterType pt, int value)
{
switch (pt)
{
case ke_gpr_static_size: gpr_static_size=value; return;
case ke_fpr_static_size: fpr_static_size=value; return;
case ke_pr_static_size: pr_static_size=value; return;
case ke_cr_static_size: cr_static_size=value; return;
case ke_btr_static_size: btr_static_size=value; return;
case ke_num_clusters: num_clusters=value; return;
case ke_integer_units: integer_units=value; return;
case ke_float_units: float_units=value; return;
case ke_branch_units: branch_units=value; return;
case ke_memory_units: memory_units=value; return;
case ke_L1D_size: L1D_size=value; return;
case ke_L1D_block_size: L1D_block=value; return;
case ke_L1D_associativity: L1D_assoc=value; return;
case ke_L1I_size: L1I_size=value; return;
case ke_L1I_block_size: L1I_block=value; return;
case ke_L1I_associativity: L1I_assoc=value; return;
case ke_L2U_size: L2U_size=value; return;
case ke_L2U_block_size: L2U_block=value; return;
case ke_L2U_associativity: L2U_assoc=value; return;
case ke_tcc_region: tcc_region=value; return;
case ke_max_unroll_allowed: max_unroll_allowed=value; return;
case ke_regroup_only: regroup_only=value; return;
case ke_do_classic_opti: do_classic_opti=value; return;
case ke_do_prepass_scalar_scheduling:
do_prepass_scalar_scheduling=value; return;
case ke_do_postpass_scalar_scheduling:
do_postpass_scalar_scheduling=value; return;
case ke_do_modulo_scheduling:
do_modulo_scheduling=value; return;
case ke_memvr_profiled: memvr_profiled=value; return;
default:
string message = "explorer.cpp: ERROR: parameter "+to_string(pt)+
" is not valid";
exit(-124);
}
}
//added by [email protected]
int Configuration::getParameterValue(EParameterType pt) const
{
switch (pt)
{
case ke_gpr_static_size: return gpr_static_size;
case ke_fpr_static_size: return fpr_static_size;
case ke_pr_static_size: return pr_static_size;
case ke_cr_static_size: return cr_static_size;
case ke_btr_static_size: return btr_static_size;
case ke_num_clusters: return num_clusters;
case ke_integer_units: return integer_units;
case ke_float_units: return float_units;
case ke_branch_units: return branch_units;
case ke_memory_units: return memory_units;
case ke_L1D_size: return L1D_size;
case ke_L1D_block_size: return L1D_block;
case ke_L1D_associativity: return L1D_assoc;
case ke_L1I_size: return L1I_size;
case ke_L1I_block_size: return L1I_block;
case ke_L1I_associativity: return L1I_assoc;
case ke_L2U_size: return L2U_size;
case ke_L2U_block_size: return L2U_block;
case ke_L2U_associativity: return L2U_assoc;
case ke_tcc_region: return tcc_region;
case ke_max_unroll_allowed: return max_unroll_allowed;
case ke_regroup_only: return regroup_only;
case ke_do_classic_opti: return do_classic_opti;
case ke_do_prepass_scalar_scheduling:
return do_prepass_scalar_scheduling;
case ke_do_postpass_scalar_scheduling:
return do_postpass_scalar_scheduling;
case ke_do_modulo_scheduling:
return do_modulo_scheduling;
case ke_memvr_profiled: return memvr_profiled;
default:
printf("explorer.cpp %d: ERROR: parameter is not valid",__LINE__);
exit(-124);
}
}
//added by [email protected]
string Configuration::configuration_to_string() const
{
string s="[";
for (int i=0; i<N_PARAMS; i++)
s = s+ to_string(getParameterValue( (EParameterType)i ) ) +"; ";
s = s+"]";
return s;
}
//G
void Simulation::add_simulation(const Simulation& other)
{
//assert(config == other.config); // same configuration
if(energy_v.size() == 0 && area_v.size() == 0 && exec_time_v.size() == 0){ // single to multi-valued transform
energy_v.push_back(energy);
area_v.push_back(area);
exec_time_v.push_back(exec_time);
}
if(other.energy_v.size()>0 && other.area_v.size()>0 && exec_time_v.size()>0) { // multiple valued simulation
energy_v.insert(energy_v.end(), other.energy_v.begin(), other.energy_v.end());
area_v.insert(area_v.end(), other.area_v.begin(), other.area_v.end());
exec_time_v.insert(exec_time_v.end(), other.exec_time_v.begin(), other.exec_time_v.end());
} else { // single valued simulation
energy_v.push_back(other.energy);
area_v.push_back(other.area);
exec_time_v.push_back(other.exec_time);
}
// update mean values
vector<double>::iterator it;
double sum = 0;
for(it = energy_v.begin(); it != energy_v.end(); it++)
sum += *it;
//cout<<"\n ENERGY: "<< energy <<" sum/size: "<< sum << "/" << energy_v.size() << endl;
energy = sum / energy_v.size();
sum = 0;
for(it = area_v.begin(); it != area_v.end(); it++)
sum += *it;
area = sum / area_v.size();
sum = 0;
for(it = exec_time_v.begin(); it != exec_time_v.end(); it++)
sum += *it;
exec_time = sum / exec_time_v.size();
}
// File access utilities - mainly to improve readability
void go_until(const string& dest,ifstream& ifs)
{
string word;
while ( (ifs>>word) && (word.find(dest)==string::npos));
}
string skip_to(ifstream& ifs,int n)
{
string word;
for (int i=0;i<n;i++) ifs>>word;
return word;
}
string skip_to(ifstream& ifs,const string& target)
{
string word;
while ( (ifs>>word) && (word.find(target)==string::npos));
return word;
}
void wait_key()
{
cout << "\n\n Press RETURN to continue..." << endl;
getchar();
getchar();
}
int count_word(const string& w, ifstream& ifs)
{
string word;
int n = 0;
while ( ifs>>word ) if (word==w) n++;
return n;
}
bool file_exists(const string& filename)
{
FILE *fp;
fp=fopen(filename.c_str(),"r");
if (fp!=NULL)
{
fclose(fp);
return true;
}
return false;
}
// redefinition of some commonly used C-style functions
extern "C" int atoi(const char*);
int atoi(const string& s)
{
return atoi(s.c_str());
}
extern "C" long long atoll(const char*);
long long atoll(const string& s)
{
return atoll(s.c_str());
}
extern "C" double atof(const char*);
double atof(const string& s)
{
return atof(s.c_str());
}
double max(const double& a,const double& b)
{
if (a>b) return a;
return b;
}
string noyes(int x)
{
if (x==1) return string("no");
if (x==2) return string("yes");
return string("NOT_VALID noyes");
}
void write_to_log(int pid,const string& logfile,const string& message)
{
time_t now = time(NULL);
string uora = string(asctime(localtime(&now)));
uora[24]=' '; // to avoid newline after date
string cmd = "echo \""+uora+" [P"+to_string(pid)+"]: "+message+"\" >> "+logfile;
system(cmd.c_str());
}
int get_mpi_rank()
{
#ifdef EPIC_MPI
int myrank;
MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
return myrank;
#else
return 0;
#endif
}
int get_mpi_size()
{
#ifdef EPIC_MPI
int mysize;
MPI_Comm_size(MPI_COMM_WORLD,&mysize);
return mysize;
#else
return 1;
#endif
}