forked from sampsyo/enerj-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect.py
executable file
·407 lines (355 loc) · 12.8 KB
/
collect.py
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/env python
from __future__ import division
import os
import subprocess
from collections import namedtuple
import logging
import math
import json
import sys
import re
Bmark = namedtuple('Benchmark', 'name runargs output regex')
BMARKS = {
'scimark2': [
Bmark('SciMark2: FFT', 'fft', 'absolute', None),
Bmark('SciMark2: SOR', 'sor', 'absolute', None),
Bmark('SciMark2: MonteCarlo', 'mc', 'proportion', None),
Bmark('SciMark2: SMM', 'smm', 'proportion', None),
Bmark('SciMark2: LU', 'lu', 'absolute', None),
],
'zxing': [
Bmark('ZXing', '', 'string', r'Parsed result:\n(\S+)'),
],
'jmeint': [
Bmark('jME', '', 'boolean', None),
],
'imagefill': [
Bmark('ImageJ', '', 10, None),
],
'simpleRaytracer': [
Bmark('Plane', '' , 255, None)
]
}
COMMANDS = {
'run_precise': './run.sh -nonoise %s',
'build_sim': './build.sh',
'run_approx': './run.sh %s',
}
REPLICATIONS = 1
STATS_FILENAME = 'enerjstats.json'
NOISE = {
'INVPROB_SRAM_WRITE_FAILURE': (int(10**5.59), int(10**4.94), 10**3),
'INVPROB_SRAM_READ_UPSET': (int(10**16.7), int(10**7.4), 10**5),
'MB_FLOAT_APPROX': (16, 8, 4),
'MB_DOUBLE_APPROX': (32, 16, 8),
'INVPROB_DRAM_FLIP_PER_SECOND': (10**9, 10**5, 10**3),
'TIMING_ERROR_PROB_PERCENT': (0.0001, 0.01, 1.0),
}
MODES_NOISE_KEY = 'TIMING_ERROR_PROB_PERCENT'
MODE_KEY = 'TIMING_ERROR_MODE'
MODES = 1, 2, 3
MODE_DEFAULT = 2
NOISE_FILE = 'enerjnoiseconsts.json'
LOW = 0
MED = 1
HIGH = 2
DISABLED = 0
JSON_OUT = 'results.json'
class Result(object):
def __init__(self, bmark):
self.bmark = bmark
self.precise_output = None
self.output_individual = {}
self.output_collective = [None, None, None]
self.stats = None
def _level_error(self, level, outputs):
if not outputs:
return 'level %i failed!' % level
error = calc_error(self.bmark, self.precise_output, outputs)
return 'level %i: %f' % (level, error)
def _frac(self, a, b):
total = a + b
if total == 0.0:
return 0.0
else:
return a / total
def __str__(self):
out = []
out.append('%s\n' % self.bmark.name)
if self.output_collective != [None, None, None]:
out.append(' mean error for collective noise variation:\n')
for level in (LOW, MED, HIGH):
outputs = self.output_collective[level]
out.append(' %s\n' % self._level_error(level, outputs))
if self.output_individual:
out.append(' mean error for individual noise variation:\n')
for const, level_outputs in self.output_individual.iteritems():
out.append(' %s:\n' % const)
for level in (LOW, MED, HIGH):
outputs = level_outputs[level]
out.append(' %s\n' % self._level_error(level, outputs))
out.append(' approximateness:\n')
for stat, (precise, approx) in self.stats.iteritems():
out.append(' %s: %.1f%% (%i/%i)\n' % \
(stat, self._frac(approx, precise)*100, approx, approx+precise))
return ''.join(out)
def flatten(self):
obj = {}
if self.output_collective != [None, None, None]:
obj['collective'] = [None, None, None]
for level in (LOW, MED, HIGH):
obj['collective'][level] = calc_error(self.bmark,
self.precise_output, self.output_collective[level])
if self.output_individual:
obj['individual'] = {}
for const, level_outputs in self.output_individual.iteritems():
out = [None, None, None]
obj['individual'][const] = out
for level in (LOW, MED, HIGH):
out[level] = calc_error(self.bmark,
self.precise_output, level_outputs[level])
obj['approximateness'] = self.stats
return obj
class CommandError(Exception):
def __init__(self, command, code, stderr):
self.command = command
self.code = code
self.stderr = stderr
def __str__(self):
return 'command %s exited wth code %i:\n%s' % \
(self.command, self.code, self.stderr)
def shell(line):
logging.debug('+ ' + line);
proc = subprocess.Popen(line, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
if proc.returncode != 0:
raise CommandError(line, proc.returncode, stderr)
return stdout
def run(bmark, noise):
logging.info('running "%s" %s' % \
(bmark.name, 'approx' if noise else 'precise'))
# Write out noise dictionary.
if noise is not None:
logging.debug('writing noise dictionary: %s' % noise)
with open(NOISE_FILE, 'w') as f:
json.dump(noise, f)
# Get the stdout from running the benchmark.
if noise is not None:
cmd = COMMANDS['run_approx']
else:
cmd = COMMANDS['run_precise']
out = shell(cmd % bmark.runargs)
out = re.sub(r'Loading Precision.+', '', out)
out = out.strip()
# Extract output.
if bmark.regex:
match = re.search(bmark.regex, out)
if match:
out = match.group(1)
else:
out = ''
if bmark.output != 'string':
if ':' in out:
tag, out = out.split(': ', 1)
else:
out = out
# Parse output.
if bmark.output in ('absolute', 'proportion', 'boolean'):
return [float(value.strip()) for value in out.split()]
if isinstance(bmark.output, int):
return [int(value.strip()) for value in out.split()]
elif bmark.output == 'string':
return out
else:
assert False
def replicated_run(bmark, noise):
out = []
for i in xrange(REPLICATIONS):
try:
res = run(bmark, noise)
logging.debug('output: ' + repr(res))
out.append(res)
except CommandError:
logging.warn('execution failed!')
# TODO! Log number of failed executions.
return out
def collect_outputs(path, bmarks, apronly=False, colonly=False):
curdir = os.getcwd()
os.chdir(path)
# Now rebuild with simulation.
logging.info('building %s with simulation' % path)
shell(COMMANDS['build_sim'])
# Run each benchmark precisely and gather precise outputs.
results = {}
for bmark in bmarks:
result = Result(bmark)
result.precise_output = run(bmark, None)
logging.debug('output: ' + repr(result.precise_output))
results[bmark] = result
# Load statistics from precise run.
results[bmark].stats = summarize_stats(json.load(open(STATS_FILENAME)))
# Abort if we're just getting the approximateness.
if apronly:
os.chdir(curdir)
return results
# Get approximate outputs.
approx_outputs = {}
stats = {}
for bmark in bmarks:
# Collective variation.
for level in (LOW, MED, HIGH):
logging.info('collective noise level: %i' % level)
noise = {}
for name, vals in NOISE.iteritems():
noise[name] = vals[level]
noise[MODE_KEY] = MODE_DEFAULT
results[bmark].output_collective[level] = \
replicated_run(bmark, noise)
if colonly:
# Skip individual variation.
continue
# Individual variation.
for const in NOISE:
if const == MODES_NOISE_KEY:
continue
logging.info('individual variation for %s' % const)
outputs = [None, None, None]
results[bmark].output_individual[const] = outputs
for level in (LOW, MED, HIGH):
logging.info('indiv. noise level for %s: %i' % (const, level))
noise = {}
for g_const, g_values in NOISE.iteritems():
if g_const == const:
noise[g_const] = g_values[level]
else:
noise[g_const] = DISABLED
noise[MODE_KEY] = DISABLED
outputs[level] = replicated_run(bmark, noise)
# Individual variation for timing error modes.
#FIXME Terrible copy-and-paste!
for mode in MODES:
logging.info('individual variation for %s mode %i' %
(MODES_NOISE_KEY, mode))
name = '%s-%i' % (MODES_NOISE_KEY, mode)
outputs = [None, None, None]
results[bmark].output_individual[name] = outputs
for level in (LOW, MED, HIGH):
logging.info('mode noise level for %s: %i' % (name, level))
noise = {}
for g_const, g_values in NOISE.iteritems():
if g_const == MODES_NOISE_KEY:
noise[g_const] = g_values[level]
else:
noise[g_const] = DISABLED
noise[MODE_KEY] = mode
outputs[level] = replicated_run(bmark, noise)
os.chdir(curdir)
return results
def mean(vals):
return sum(vals) / len(vals)
def err(x, y, proportion, norm=None):
errs = []
for a, b in zip(x, y):
if math.isnan(a):
e = 0.0 if math.isnan(b) else 1.0
elif math.isnan(b):
e = 1.0
else:
e = abs(a - b)
if proportion:
e = e / a
e = min(e, norm if norm else 1.0) # Bound the error at unity.
errs.append(e)
avg = mean(errs)
if norm:
avg = min(avg / norm, 1.0)
return avg
def calc_error(bmark, precise_output, approx_outputs):
errors = []
for approx_output in approx_outputs:
if bmark.output == 'string':
# String matching on output.
if approx_output == precise_output:
errors.append(0.0)
else:
errors.append(1.0)
elif bmark.output in ('absolute', 'boolean', 'proportion'):
# Output is a list of floats.
errors.append(err(
precise_output,
approx_output,
bmark.output == 'proportion',
0.5 if bmark.output == 'boolean' else None
))
elif isinstance(bmark.output, int):
# Output is a list of integers. The value is the normalization.
norm = float(bmark.output)
errors.append(err(precise_output, approx_output, False, norm))
else:
assert false
if not errors:
return float('nan')
else:
return mean(errors)
def summarize_stats(stats):
out = {
'fpu': [0, 0],
'alu': [0, 0],
'heap': [0, 0],
'stack': [0, 0],
'loads': [0, 0],
'stores': [0, 0],
}
for name, (precise, approx) in stats['operations'].iteritems():
if name.startswith('FLOAT') or name.startswith('DOUBLE'):
out['fpu'][0] += precise
out['fpu'][1] += approx
elif name.startswith('INT') or name.startswith('LONG') or \
name.startswith('SHORT'):
out['alu'][0] += precise
out['alu'][1] += approx
elif name.startswith('load'):
out['loads'][0] += precise
out['loads'][1] += approx
elif name.startswith('store'):
out['stores'][0] += precise
out['stores'][1] += approx
for name, (precise, approx) in stats['footprint'].iteritems():
section, kind = name.split('-')
if kind != 'bytes':
continue
out[section][0] += precise
out[section][1] += approx
return out
def dump_json(results, path):
obj = {}
for bmark, result in results.iteritems():
obj[bmark.name] = result.flatten()
with open(path, 'w') as f:
json.dump(obj, f)
if __name__ == '__main__':
logging.getLogger('').setLevel(logging.INFO)
args = sys.argv[1:]
apronly = False
colonly = False
if args and args[0] == '-d':
logging.getLogger('').setLevel(logging.DEBUG)
args.pop(0)
elif args and args[0] == '-a':
apronly = True
args.pop(0)
elif args and args[0] == '-c':
colonly = True
args.pop(0)
if args:
paths = args
else:
paths = BMARKS.keys()
total = {}
for path in paths:
results = collect_outputs(path, BMARKS[path], apronly, colonly)
print '\n'.join(str(res) for res in results.itervalues())
total.update(results)
dump_json(total, JSON_OUT)