"""Compact small-row table: per cell, ours-best/upstream-best us (and ours % of DRAM roofline) at rows 1..8."""
import json, statistics, sys
PEAK = 1008e9
OURS = ("mma", "mma_p3", "simt", "i8", "mma_group", "simt_group")
UP = ("served", "auto", "up_gemv", "members_served", "members_up_gemv")
acc = {}
for p in sys.argv[1:]:
    for r in json.load(open(p)):
        key = (r["in_f"], r.get("out_f") or tuple(r["widths"]), r["bits"], r["w_bytes"])
        for arm, us in r["us"].items():
            acc.setdefault(key, {}).setdefault(r["M"], {}).setdefault(arm, []).append(us)
rows = [1, 2, 3, 4, 5, 6, 7, 8]
print("| cell | roof µs | " + " | ".join(str(m) for m in rows) + " |")
print("|---|---|" + "---|" * len(rows))
for (k, n, bits, wb), by_m in acc.items():
    roof = wb / PEAK * 1e6
    name = f"{k}x{n}" if isinstance(n, int) else str(k) + "x" + "+".join(map(str, n))
    cells = []
    for m in rows:
        a = {arm: statistics.median(v) for arm, v in by_m[m].items()}
        o = min((a[x], x) for x in OURS if x in a); u = min((a[x], x) for x in UP if x in a)
        tag = {"mma": "m", "mma_p3": "m3", "simt": "s", "i8": "i", "mma_group": "mg", "simt_group": "sg"}[o[1]]
        s = f"{o[0]:.1f}{tag}/{u[0]:.1f} ({100 * roof / o[0]:.0f}%)"
        cells.append(f"**{s}**" if o[0] > u[0] else s)
    print(f"| {name} K{bits} | {roof:.1f} | " + " | ".join(cells) + " |")
