Repository navigation
Expand file tree
/
Copy pathu64x64-decide.py
More file actions
120 lines (115 loc) · 6.99 KB
/
Copy pathu64x64-decide.py
File metadata and controls
120 lines (115 loc) · 6.99 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
import collections, statistics as st, sys, os
d, scen = sys.argv[1], sys.argv[2]
insp={}
for l in open(os.path.join(d,'inspect.txt')).readlines()[1:]:
f=l.split(); insp[f[0]]=(int(f[6]),int(f[7]))
rows=collections.defaultdict(list)
fixed={}
for p in (1,2,3):
for l in open(os.path.join(d,f'bench_{scen}_{p}.txt')):
f=l.split()
if len(f)==9 and f[0].startswith('O'): rows[(f[0],f[1],f[2])].append(float(f[4]))
elif len(f)==9 and f[3]=='Fixed': fixed.setdefault(f[0],[]).append(float(f[4]))
def cls(op,route,var):
if route!='d': return {'c':'Simd','f':'Simd','b':'Bitslice'}.get(route,'Scalar')
sym=f"u64x64_{'o6' if op=='O6c' else op.lower()}_d_{var}"
v,s=insp[sym]; return 'Simd' if v>s else 'Scalar'
for op in sorted({k[0] for k in rows}):
# A0 is an independent baseline (plan §3), never a candidate or reference.
ents={k:v for k,v in rows.items() if k[0]==op and k[1]!='a0'}
best=min(ents,key=lambda k:st.median(ents[k])); bc=cls(*best)
others=[k for k in ents if cls(*k)!=bc]
a0=rows.get((op,'a0','-'))
a0s=f" A0 {st.median(a0):.1f}" if a0 else ""
if not others: print(f"{op:5} only {bc}: best {best[1]}/{best[2]} {st.median(ents[best]):.1f} ns{a0s}"); continue
r=min(others,key=lambda k:st.median(ents[k])); W,R=ents[best],ents[r]
win=max(W)<min(R) and st.median(W)<=0.85*st.median(R)
tag=f"{bc.upper()}-WINS ({best[1]}/{best[2]})" if win else "NO-MEASURED-ADVANTAGE"
print(f"{op:5} W={best[1]}/{best[2]} [{bc}] {st.median(W):.1f} [{min(W):.1f}..{max(W):.1f}] R={r[1]}/{r[2]} [{cls(*r)}] {st.median(R):.1f} [{min(R):.1f}..{max(R):.1f}] {st.median(W)/st.median(R):.2f} {tag}{a0s}")
# Prepared reuse (plan §8): E's amortised cost per query over N reuses
# against the best unprepared entry for the same op; N* is the smallest N
# that passes max(W) < min(R) and median(W) <= 0.85 median(R).
def amort(prep, first, rest, n): return (prep + first + (n - 1) * rest) / n
if 'E_PREP_E2' in fixed:
P=fixed['E_PREP_E2']
for name, qrow, hit, op in (('O2 E2', 'E_O2_E2', 'E_O2_E2', 'O2'), ('O6 E2', 'E_O6_E2', 'E_O6_E2', 'O6'), ('O6 E2+memo (identical query)', 'E_O6_E2', 'E_O6_MEMO_HIT', 'O6')):
ents={k:v for k,v in rows.items() if k[0]==op and k[1]!='a0'}
r=min(ents,key=lambda k:st.median(ents[k])); R=ents[r]
out=[]; nstar=None
for n in (1,2,8,32,128,1024):
W=[amort(P[i],fixed[qrow][i],fixed[hit][i],n) for i in range(len(P))]
ok=max(W)<min(R) and st.median(W)<=0.85*st.median(R)
out.append(f"N={n}: {st.median(W):.1f}{' PASS' if ok else ''}")
if ok and nstar is None: nstar=n
print(f"{name:30} vs {r[1]}/{r[2]} {st.median(R):.1f}: " + " ".join(out) + f" -> {'PREPARED-REUSE-WINS from N=' + str(nstar) if nstar else 'never (N<=1024)'}")
# Route F against route C (plan section 13): both are Simd, so the rule above
# never compares them. ZERO-COST iff the ranges overlap or F's median is within
# 10% of C's; otherwise CONTRACT-COST (F slower) or F-FASTER.
for op in sorted({k[0] for k in rows}):
c = rows.get((op, 'c', '-'))
for (o, rt, var), F in sorted(rows.items()):
if o != op or rt != 'f' or not c:
continue
mf, mc = st.median(F), st.median(c)
overlap = min(F) <= max(c) and min(c) <= max(F)
if overlap or mf <= 1.10 * mc and mc <= 1.10 * mf:
v = 'ZERO-COST'
else:
v = 'CONTRACT-COST' if mf > mc else 'F-FASTER'
print(f"{op:5} F/{var} {mf:.1f} [{min(F):.1f}..{max(F):.1f}] vs C {mc:.1f} [{min(c):.1f}..{max(c):.1f}] {mf/mc:.2f} {v}")
# Isolation rows (plan section 13.3): F-coord against C on two fixed recipes,
# all powers of two (POW2) and none (GEN). WINS means max(F) < min(C) and
# median(F) <= 0.85 median(C), the same margin as the main rule.
for case in ('POW2', 'GEN'):
c, f = fixed.get(f'ISO_{case}_C'), fixed.get(f'ISO_{case}_FCOORD')
if not (c and f):
continue
mf, mc = st.median(f), st.median(c)
if max(f) < min(c) and mf <= 0.85 * mc:
v = 'FCOORD-WINS'
elif min(f) <= max(c) and min(c) <= max(f) or (mf <= 1.10 * mc and mc <= 1.10 * mf):
v = 'ZERO-COST'
else:
v = 'CONTRACT-COST' if mf > mc else 'F-FASTER'
print(f"ISO {case:4} F-coord {mf:.1f} [{min(f):.1f}..{max(f):.1f}] vs C {mc:.1f} [{min(c):.1f}..{max(c):.1f}] {mf/mc:.2f} {v}")
# Split O6 with a prepared cap LUT (plan section 14.4): the LUT is prepared
# once and the cell + reduction run per query; against the best unprepared O6.
if all(k in fixed for k in ('LUT_PREP', 'FS_O6_CELL', 'FS_O6_RED_LUT')):
P, Cc, Rr = fixed['LUT_PREP'], fixed['FS_O6_CELL'], fixed['FS_O6_RED_LUT']
ents = {k: v for k, v in rows.items() if k[0] == 'O6' and k[1] != 'a0'}
r = min(ents, key=lambda k: st.median(ents[k])); R = ents[r]
out = []; nstar = None
for n in (1, 2, 8, 32, 128, 1024):
W = [(P[i] + n * (Cc[i] + Rr[i])) / n for i in range(len(P))]
ok = max(W) < min(R) and st.median(W) <= 0.85 * st.median(R)
out.append(f"N={n}: {st.median(W):.1f}{' PASS' if ok else ''}")
if ok and nstar is None: nstar = n
print(f"O6 split+LUT (cell+red, LUT once) vs {r[1]}/{r[2]} {st.median(R):.1f}: " + " ".join(out) + f" -> {'WINS from N=' + str(nstar) if nstar else 'never (N<=1024)'}")
# Phase split for every op that has both rows: cell, reduction, sum.
for op in ('O1', 'O2', 'O3', 'O4', 'O6'):
c, r = fixed.get(f'FS_{op}_CELL'), fixed.get(f'FS_{op}_RED')
if c and r:
print(f"PHASES {op}: cell {st.median(c):.1f} reduce {st.median(r):.1f} sum {st.median(c)+st.median(r):.1f}")
# D-QUOT8-ADMISSION-0 (plan section 16): the power-of-two quotient in C and A.
# Same margins as the main rule: WIN = max(new) < min(old) and median(new) <=
# 0.85 median(old); SAME = ranges overlap or medians within 10%; else REGRESS
# (new slower) or FASTER (faster, below the win margin).
def verdict(new, old):
mn, mo = st.median(new), st.median(old)
if max(new) < min(old) and mn <= 0.85 * mo:
return 'WIN'
if (min(new) <= max(old) and min(old) <= max(new)) or (mn <= 1.10 * mo and mo <= 1.10 * mn):
return 'SAME'
return 'REGRESS' if mn > mo else 'FASTER'
for fam, keys, label in (('SENS', range(9), 'pow2 rows {}/8'), ('FND', range(8), 'Foundry item {}')):
tot = {r: 0.0 for r in ('C', 'CP', 'A', 'AP')}
for k in keys:
rr = {r: fixed.get(f'{fam}_{r}_{k}') for r in ('C', 'CP', 'A', 'AP')}
if not all(rr.values()):
continue
md = {r: st.median(v) for r, v in rr.items()}
for r in tot: tot[r] += md[r]
best = min(md, key=md.get)
print(f"{fam} {label.format(k):18} C {md['C']:.1f} CP {md['CP']:.1f} ({md['CP']/md['C']:.2f} {verdict(rr['CP'], rr['C'])}) A {md['A']:.1f} AP {md['AP']:.1f} ({md['AP']/md['A']:.2f} {verdict(rr['AP'], rr['A'])}) best {best} {md[best]:.1f}")
if fam == 'FND' and tot['C']:
print(f"FND menu sum (8 items, equal weight) C {tot['C']:.1f} CP {tot['CP']:.1f} ({tot['CP']/tot['C']:.2f}) A {tot['A']:.1f} AP {tot['AP']:.1f} ({tot['AP']/tot['A']:.2f})")