ContentsFoundations to FrontierBezhig

frontier_code/verify_f19.py

All lab code. Download this file.

"""Numeric checks behind F19's worked examples and solutions. Run from frontier_code/."""
import math
import random
from solutions import perf_lab as lab

checks = []

def check(name, value, expected):
    checks.append((name, value, expected, value == expected))

ex = [lab.Op("la", "load"), lab.Op("lb", "load"), lab.Op("lc", "load"), lab.Op("ld", "load"),
      lab.Op("le", "load"), lab.Op("lf", "load"), lab.Op("s1", "alu", ("la", "lb")),
      lab.Op("s2", "alu", ("lc", "ld")), lab.Op("m1", "alu", ("s1", "s2")), lab.Op("m2", "alu", ("le", "lf")),
      lab.Op("y", "alu", ("m1", "m2")), lab.Op("st", "store", ("y",))]
S = lab.TOY_SLOTS
check("F19.01 critical path", lab.critical_path(ex), 5)
check("F19.01 resource bound", lab.resource_bound(ex, S), 6)
check("F19.02 best schedule length", len(lab.list_schedule(ex, S)), 9)
for chains, expected in [(1, 6), (2, 6), (8, 24)]:
    ops = lab.independent_chains(chains, 6)
    check(f"F19.03 {chains} chains cycles", len(lab.list_schedule(ops, S)), expected)
check("F19.04 vector ops for 1000", math.ceil(1000 / 4), 250)
check("F19.04 vector cycles", math.ceil(250 / 2), 125)
check("F19.04 scalar cycles", math.ceil(1000 / 4), 250)
check("F19.04 vectors for 1002", math.ceil(1002 / 4), 251)
tree = lab.eytzinger(list(range(1, 16)))
check("F19.05 layout", tree, [None, 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15])
check("F19.05 rank of 11", lab.search_one(tree, 11), 10)
rng = random.Random(1)
keys = sorted(rng.sample(range(100_000), 2 ** 10 - 1))
t = lab.eytzinger(keys)
queries = [rng.randrange(-10, 100_010) for _ in range(500)]
check("F19.06 distinct nodes per level", lab.distinct_nodes_per_level(t, queries[:256]),
      [1, 2, 4, 8, 16, 32, 63, 106, 146, 172])
c0 = lab.traversal_cost(256, 10, 0)
c5 = lab.traversal_cost(256, 10, 5)
c6 = lab.traversal_cost(256, 10, 6)
check("F19.07 no-cache loads/valu/bound", (c0.loads, c0.valu, c0.bound), (2560, 1280, 2560))
check("F19.07 5 levels loads/valu/bound", (c5.loads, c5.valu, c5.bound), (1288, 2944, 1472))
check("F19.07 6 levels bound", c6.bound, 2464)
check("F19.07 best", lab.best_cached_levels(256, 10)[0], 5)

bad = 0
for name, value, expected, ok in checks:
    print(f"{'PASS' if ok else 'FAIL'}  {name}: {value}")
    bad += not ok
raise SystemExit(1 if bad else 0)