| 1 |
|
| 2 |
import sys, re, csv |
| 3 |
|
| 4 |
SILENCE_MISSING_REAL_USER_WARNING = True |
| 5 |
|
| 6 |
def warn(text): |
| 7 |
sys.stderr.write(text + '\n') |
| 8 |
|
| 9 |
def get_input(fname): |
| 10 |
if fname == '-': |
| 11 |
return sys.stdin.readlines() |
| 12 |
else: |
| 13 |
with open(fname, 'r') as f: |
| 14 |
return f.readlines() |
| 15 |
|
| 16 |
def postformat(lines): |
| 17 |
i = 0 |
| 18 |
while i < len(lines): |
| 19 |
if i + 1 < len(lines): |
| 20 |
if lines[i].startswith('Testing ') and lines[i+1].startswith('Tactic call ran for'): |
| 21 |
yield lines[i] + lines[i+1] |
| 22 |
i += 2 |
| 23 |
continue |
| 24 |
if lines[i].startswith('Testing '): |
| 25 |
yield lines[i] |
| 26 |
i += 1 |
| 27 |
elif lines[i].strip() in ('', 'No such index') or lines[i].startswith('No params'): |
| 28 |
i += 1 |
| 29 |
else: |
| 30 |
warn('Could not recognize: %s' % lines[i]) |
| 31 |
i += 1 |
| 32 |
|
| 33 |
def eval_numexpr(numexpr): |
| 34 |
|
| 35 |
numexpr = re.sub(r"\.(?![0-9])", "", numexpr) |
| 36 |
return eval(numexpr, {'__builtins__':None}) |
| 37 |
|
| 38 |
REGS = {'kind': r'(UnsaturatedSolinas|WordByWordMontgomery)', |
| 39 |
'bitwidth': r'bitwidth = ([0-9]+)', |
| 40 |
'prime': r'(?:UnsaturatedSolinas|WordByWordMontgomery)[\s"]*([^" ]+)', |
| 41 |
'method': r'method = ([a-zA-Z0-9_-]+)', |
| 42 |
'descr': r'\|}\s*\)[\s"]*([^:]+)', |
| 43 |
'real': r'(?:ran for |real:\s*)([0-9\.]*) s', |
| 44 |
'user': r'(?:secs \(|user:\s*)([0-9\.]*)(?:u,| s)', |
| 45 |
'index': r'(?:index = ([0-9]+)|WordByWordMontgomery)', |
| 46 |
'nlimbs': r'(?:[ \.]n := ([0-9]+)|WordByWordMontgomery)'} |
| 47 |
def get_data(line): |
| 48 |
ret = {} |
| 49 |
bad = False |
| 50 |
for key, reg in REGS.items(): |
| 51 |
res = re.search(reg, line) |
| 52 |
if res is None: |
| 53 |
if key in ('real', 'user'): |
| 54 |
if SILENCE_MISSING_REAL_USER_WARNING: return None |
| 55 |
else: bad = True |
| 56 |
warn('Could not find %s (%s) in %s' % (key, reg, line)) |
| 57 |
else: |
| 58 |
ret[key] = res.groups()[0] |
| 59 |
if ret[key] is None: ret[key] = '' |
| 60 |
ret[key] = ret[key].replace('"', '').strip() |
| 61 |
if ret['index'] != '': ret['index'] = '(%s)' % ret['index'] |
| 62 |
ret['prime_str'] = ret['prime'] |
| 63 |
ret['prime'] = eval_numexpr(ret['prime'].replace('^', '**')) |
| 64 |
if ' with ' in ret['descr']: |
| 65 |
ret['descr1'], ret['descr2'] = ret['descr'].split(' with ') |
| 66 |
else: |
| 67 |
ret['descr1'], ret['descr2'] = ret['descr'], '' |
| 68 |
del ret['descr'] |
| 69 |
if bad: return None |
| 70 |
return ret |
| 71 |
|
| 72 |
def get_data_for_graph(lines, key_is_good): |
| 73 |
def key_for_sort(key): |
| 74 |
return ('Gallina' in key, 'cbv' in key, 'vm_compute' in key, 'extraction' in key, key) |
| 75 |
data = {} |
| 76 |
for line in map(get_data, lines): |
| 77 |
if line is None: continue |
| 78 |
prime = line['prime'] |
| 79 |
if not prime in data.keys(): data[prime] = {} |
| 80 |
key = ('%(kind)s x%(bitwidth)s %(index)s | %(descr1)s with %(descr2)s using %(method)s' % line).strip() |
| 81 |
new_data = {'real': line['real'], 'user': line['user']} |
| 82 |
if key in data[prime]: warn('Overwriting (%s) data[%s][%s] = %s with %s' % (line['prime_str'], prime, key, str(data[prime][key]), str(new_data))) |
| 83 |
data[prime][key] = new_data |
| 84 |
pre_all_keys = list(sorted(set(key for dat in data.values() for key in dat.keys() if key_is_good(key)), key=key_for_sort)) |
| 85 |
all_keys = list(sorted(set(key.split(' | ')[1] for key in pre_all_keys), key=key_for_sort)) |
| 86 |
extra_key_parts = list(sorted(set(key.split(' | ')[0] for key in pre_all_keys))) |
| 87 |
return data, all_keys, extra_key_parts |
| 88 |
|
| 89 |
|
| 90 |
POSSIBLE_COLUMNS = ('perf_old_vm_times', |
| 91 |
'perf_new_vm_times', |
| 92 |
'perf_new_extraction_times', |
| 93 |
'perf_old_cbv_times', |
| 94 |
'perf_new_extraction_over_old_vm', |
| 95 |
'perf_new_vm_over_old_vm', |
| 96 |
'perf_old_vm_over_old_vm', |
| 97 |
'perf_new_extraction_over_new_extraction', |
| 98 |
'perf_new_vm_over_new_extraction', |
| 99 |
'perf_old_vm_over_new_extraction') |
| 100 |
|
| 101 |
def lines_to_rows(lines, for_graph=False, real_or_user='real', only=None, **kwargs): |
| 102 |
def key_is_good(key): |
| 103 |
if only is not None and only.replace('-', ' ').replace('_', ' ') not in key: return False |
| 104 |
if 'lazy' in key or '(1)' in key or 'native_compute' in key: return False |
| 105 |
if 'Pipeline' in key and 'NBE' not in key: return False |
| 106 |
if 'Pipeline' in key and 'cbv' in key: return False |
| 107 |
if 'Pipeline' not in key and 'GallinaAxOf' not in key: return False |
| 108 |
return True |
| 109 |
if for_graph: |
| 110 |
data, all_keys, extra_key_parts = get_data_for_graph(lines, key_is_good) |
| 111 |
yield ['prime'] + [key + (' (%s, s)' % real_or_user) for key in all_keys] + ['extra info'] |
| 112 |
defaultv = None |
| 113 |
default = {'real':defaultv, 'user':defaultv} |
| 114 |
for prime in sorted(data.keys(), key=int): |
| 115 |
for extra_key in extra_key_parts: |
| 116 |
yield [prime] + [data[prime].get(extra_key + ' | ' + short_key, default)[real_or_user] for short_key in all_keys] + [extra_key] |
| 117 |
elif any(kwargs[col] for col in POSSIBLE_COLUMNS): |
| 118 |
data, all_keys, extra_key_parts = get_data_for_graph(lines, key_is_good) |
| 119 |
defaultv = None |
| 120 |
default = {'real':defaultv, 'user':defaultv} |
| 121 |
def format_prime(p): return p |
| 122 |
def format_time(t): return t |
| 123 |
def format_time_ratio(n, d): return float(n) / float(d) |
| 124 |
def get_div_row(p, n=None, d=None): |
| 125 |
if p is None or n is None or d is None or '' in (p, n, d): return None |
| 126 |
return [format_prime(p), format_time_ratio(n, d)] |
| 127 |
def get_row(p, t=None): |
| 128 |
if p is None or t is None or '' in (p, t): return |
| 129 |
return [format_prime(p), format_time(t)] |
| 130 |
arg_to_key_filter = { |
| 131 |
'perf_old_vm_times': (lambda short_key: 'Pipeline' not in short_key and 'vm_compute' in short_key and 'precomputed_decision_tree' in short_key), |
| 132 |
'perf_new_vm_times': (lambda short_key: 'Pipeline' in short_key and 'vm_compute' in short_key and 'precomputed_decision_tree' in short_key), |
| 133 |
'perf_new_extraction_times': (lambda short_key: 'Pipeline' in short_key and 'extraction' in short_key and 'precomputed_decision_tree' in short_key), |
| 134 |
'perf_old_cbv_times': (lambda short_key: 'Pipeline' not in short_key and 'cbv' in short_key and 'precomputed_decision_tree' in short_key) |
| 135 |
} |
| 136 |
div_arg_to_key_filters = dict( |
| 137 |
(key, (arg_to_key_filter['perf_' + n_key + '_times'], arg_to_key_filter['perf_' + d_key + '_times'])) |
| 138 |
for key, (n_key, d_key) in |
| 139 |
[(key, key.replace('perf_', '').split('_over_')) for key in POSSIBLE_COLUMNS if '_over_' in key] |
| 140 |
) |
| 141 |
|
| 142 |
for prime in sorted(data.keys(), key=int): |
| 143 |
for extra_key in extra_key_parts: |
| 144 |
extra_args = [(short_key, data[prime].get(extra_key + ' | ' + short_key, default)[real_or_user]) for short_key in all_keys] |
| 145 |
for arg, key_filter in arg_to_key_filter.items(): |
| 146 |
if kwargs[arg]: |
| 147 |
row = get_row(prime, *[v for short_key, v in extra_args if key_filter(short_key)]) |
| 148 |
if row is not None: yield row |
| 149 |
for arg, (n_key_filter, d_key_filter) in div_arg_to_key_filters.items(): |
| 150 |
if kwargs[arg]: |
| 151 |
row = get_div_row(prime, |
| 152 |
*[v for short_key, v in extra_args if n_key_filter(short_key)], |
| 153 |
*[v for short_key, v in extra_args if d_key_filter(short_key)]) |
| 154 |
if row is not None: yield row |
| 155 |
else: |
| 156 |
keys = ['prime', 'user', 'real', 'kind', 'bitwidth', 'descr1', 'descr2', 'method', 'index', 'nlimbs', 'prime_str'] |
| 157 |
yield list(keys) |
| 158 |
for data in map(get_data, lines): |
| 159 |
if data is None: continue |
| 160 |
yield [data[k] for k in keys] |
| 161 |
|
| 162 |
def writecsv(outfname, lines, **kwargs): |
| 163 |
rows = lines_to_rows(lines, **kwargs) |
| 164 |
def do_write(csvfile): |
| 165 |
writer = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL, lineterminator='\n') |
| 166 |
writer.writerows(rows) |
| 167 |
if outfname == '-': do_write(sys.stdout) |
| 168 |
else: |
| 169 |
with open(outfnname, 'w', newline='') as csvfile: |
| 170 |
do_write(csvfile) |
| 171 |
|
| 172 |
def writetxt(outfname, lines, **kwargs): |
| 173 |
rows = lines_to_rows(lines, **kwargs) |
| 174 |
def do_write(txtfile): |
| 175 |
txtfile.write('\n'.join(' '.join(map(str, row)) for row in rows)) |
| 176 |
if outfname == '-': do_write(sys.stdout) |
| 177 |
else: |
| 178 |
with open(outfnname, 'w', newline='') as txtfile: |
| 179 |
do_write(txtfile) |
| 180 |
|
| 181 |
if __name__ == '__main__': |
| 182 |
fnames = sys.argv[1:] |
| 183 |
outfname = '-' |
| 184 |
if '-o' in fnames: |
| 185 |
i = fnames.index('-o') |
| 186 |
outfname = fnames[i+1] |
| 187 |
del fnames[i+1] |
| 188 |
del fnames[i] |
| 189 |
only = None |
| 190 |
for i, fname in enumerate(fnames): |
| 191 |
if '--only-' in fname and fname.startswith('--'): |
| 192 |
if only is not None: |
| 193 |
raise Exception('Only one argument can start with -- and contain --only-') |
| 194 |
fnames[i], only = fname.split('--only-') |
| 195 |
kwargs = dict([('for_graph', False)] + [(i, False) for i in POSSIBLE_COLUMNS]) |
| 196 |
for key in kwargs.keys(): |
| 197 |
arg = '--' + key.replace('_', '-') |
| 198 |
if arg in fnames: |
| 199 |
kwargs[key] = True |
| 200 |
del fnames[fnames.index(arg)] |
| 201 |
kwargs['only'] = only |
| 202 |
txt = False |
| 203 |
if '--txt' in fnames: |
| 204 |
txt = True |
| 205 |
del fnames[fnames.index('--txt')] |
| 206 |
while '--file-list' in fnames: |
| 207 |
file_list_file = fnames[fnames.index('--file-list')+1] |
| 208 |
del fnames[fnames.index('--file-list')+1] |
| 209 |
del fnames[fnames.index('--file-list')] |
| 210 |
with open(file_list_file, 'r') as f: |
| 211 |
file_list = [i.strip() for i in f.readlines() if i.strip()] |
| 212 |
fnames.extend(file_list) |
| 213 |
lines = [line |
| 214 |
for fname in fnames |
| 215 |
for line in postformat(get_input(fname))] |
| 216 |
if txt: |
| 217 |
writetxt(outfname, lines, **kwargs) |
| 218 |
else: |
| 219 |
writecsv(outfname, lines, **kwargs) |