|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""python-flint vs default ground-types benchmark on the actual project workload. |
| 3 | +
|
| 4 | +Run with:: |
| 5 | +
|
| 6 | + # Default Python ground types (in either venv) |
| 7 | + python bench_flint/bench.py |
| 8 | +
|
| 9 | + # FLINT ground types (in bench_flint/venv with python-flint installed) |
| 10 | + SYMPY_GROUND_TYPES=flint python bench_flint/bench.py |
| 11 | +
|
| 12 | +The script does three things: |
| 13 | +
|
| 14 | +1. Micro: time a single hard ``cancel()`` on a polynomial expression |
| 15 | + structurally similar to the Schwarzschild L3 brackets that take |
| 16 | + 100s+ in the live run. |
| 17 | +
|
| 18 | +2. End-to-end (small): run the Schwarzschild composite at L<=2 and time |
| 19 | + the full ``compute_growth(max_level=2)`` call. Repeats N times for |
| 20 | + noise reduction. |
| 21 | +
|
| 22 | +3. End-to-end (medium): same at L<=3 with N=3, d=2, low samples. This is |
| 23 | + the most representative single-run benchmark; takes a few minutes |
| 24 | + under stock Python and should be much faster under FLINT. |
| 25 | +
|
| 26 | +Output is plain text plus a JSON line at the end suitable for |
| 27 | +diffing across runs. |
| 28 | +""" |
| 29 | + |
| 30 | +from __future__ import annotations |
| 31 | + |
| 32 | +import json |
| 33 | +import os |
| 34 | +import platform |
| 35 | +import sys |
| 36 | +import time |
| 37 | +from pathlib import Path |
| 38 | + |
| 39 | +# Identify backend BEFORE importing sympy heavy bits. |
| 40 | +import sympy # noqa: E402 |
| 41 | +from sympy.polys.domains import GROUND_TYPES # noqa: E402 |
| 42 | + |
| 43 | +import sympy as sp # noqa: E402 |
| 44 | + |
| 45 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 46 | +sys.path.insert(0, str(REPO_ROOT / "nbody")) |
| 47 | + |
| 48 | + |
| 49 | +def banner(title: str) -> None: |
| 50 | + print() |
| 51 | + print("=" * 72) |
| 52 | + print(f" {title}") |
| 53 | + print("=" * 72) |
| 54 | + |
| 55 | + |
| 56 | +def print_env() -> dict: |
| 57 | + env = { |
| 58 | + "python": sys.version.split()[0], |
| 59 | + "platform": platform.platform(), |
| 60 | + "sympy": sympy.__version__, |
| 61 | + "ground_types": GROUND_TYPES, |
| 62 | + "env_var": os.environ.get("SYMPY_GROUND_TYPES", "<unset>"), |
| 63 | + } |
| 64 | + try: |
| 65 | + import flint # type: ignore |
| 66 | + env["python_flint"] = flint.__version__ |
| 67 | + except ImportError: |
| 68 | + env["python_flint"] = "<not installed>" |
| 69 | + banner("Environment") |
| 70 | + for k, v in env.items(): |
| 71 | + print(f" {k:18s} {v}") |
| 72 | + return env |
| 73 | + |
| 74 | + |
| 75 | +# --------------------------------------------------------------------------- # |
| 76 | +# Bench 1: hard cancel() on a structurally-realistic polynomial |
| 77 | +# --------------------------------------------------------------------------- # |
| 78 | + |
| 79 | +def make_hard_polynomial() -> sp.Expr: |
| 80 | + """Build a multivariate rational polynomial sized like a typical |
| 81 | + Schwarzschild L3 intermediate. 15 variables, lots of cross products, |
| 82 | + nontrivial denominator. |
| 83 | + """ |
| 84 | + syms = sp.symbols("x1 y1 x2 y2 x3 y3 px1 py1 px2 py2 px3 py3 u12 u13 u23") |
| 85 | + x1, y1, x2, y2, x3, y3, px1, py1, px2, py2, px3, py3, u12, u13, u23 = syms |
| 86 | + |
| 87 | + # Numerator: a deliberately ugly polynomial in 15 variables. |
| 88 | + num = ( |
| 89 | + (px1 ** 2 + py1 ** 2) * u12 ** 3 * u13 |
| 90 | + + (px2 ** 2 + py2 ** 2) * u12 ** 3 * u23 |
| 91 | + + (px3 ** 2 + py3 ** 2) * u13 ** 3 * u23 |
| 92 | + + (x1 - x2) ** 2 * (y2 - y3) ** 2 * u12 ** 2 * u23 ** 2 |
| 93 | + + (x1 - x3) ** 2 * (y1 - y3) ** 2 * u13 ** 2 * u23 ** 2 |
| 94 | + + (px1 * px2 + py1 * py2) * (x1 - x3) * (y2 - y3) * u12 ** 2 * u13 ** 2 |
| 95 | + + (px2 * px3 + py2 * py3) * (x2 - x1) * (y3 - y1) * u13 ** 2 * u23 ** 2 |
| 96 | + - sp.Rational(7, 4) * u12 ** 4 * u13 ** 2 * u23 |
| 97 | + + sp.Rational(11, 8) * u12 ** 2 * u13 ** 4 * u23 ** 2 |
| 98 | + - sp.Rational(3, 16) * u12 * u13 * u23 ** 5 |
| 99 | + ) |
| 100 | + |
| 101 | + # Denominator: small but nontrivial - exercises GCD path. |
| 102 | + den = u12 ** 2 + u13 ** 2 + u23 ** 2 |
| 103 | + |
| 104 | + return num / den |
| 105 | + |
| 106 | + |
| 107 | +def bench_micro(n_iter: int = 5) -> dict: |
| 108 | + banner(f"Bench 1: cancel() on a hard rational polynomial (n_iter={n_iter})") |
| 109 | + expr = make_hard_polynomial() |
| 110 | + print(f" num/den built: {len(sp.Add.make_args(sp.expand(expr.as_numer_denom()[0])))} terms in numerator") |
| 111 | + |
| 112 | + times = [] |
| 113 | + for i in range(n_iter): |
| 114 | + # Re-create from scratch each iteration to defeat any caches. |
| 115 | + e = make_hard_polynomial() |
| 116 | + t0 = time.perf_counter() |
| 117 | + result = sp.cancel(e) |
| 118 | + elapsed = time.perf_counter() - t0 |
| 119 | + nterms = len(sp.Add.make_args(result.as_numer_denom()[0])) |
| 120 | + times.append(elapsed) |
| 121 | + print(f" iter {i+1}/{n_iter}: {elapsed:8.3f}s ({nterms} terms in numerator)") |
| 122 | + |
| 123 | + avg = sum(times) / len(times) |
| 124 | + best = min(times) |
| 125 | + print(f" -> mean: {avg:.3f}s, best: {best:.3f}s") |
| 126 | + return {"name": "micro_cancel", "iters": n_iter, "times_s": times, |
| 127 | + "mean_s": avg, "best_s": best} |
| 128 | + |
| 129 | + |
| 130 | +# --------------------------------------------------------------------------- # |
| 131 | +# Bench 2: end-to-end Schwarzschild L2 (small, fast, noise-free) |
| 132 | +# --------------------------------------------------------------------------- # |
| 133 | + |
| 134 | +def bench_schwarzschild_l2(n_iter: int = 3) -> dict: |
| 135 | + from exact_growth_nbody import NBodyAlgebra |
| 136 | + |
| 137 | + banner(f"Bench 2: Schwarzschild composite L<=2 dimseq (n_iter={n_iter})") |
| 138 | + times = [] |
| 139 | + for i in range(n_iter): |
| 140 | + # Use a unique cache dir per iter to avoid loading prior pickle. |
| 141 | + ckpt = REPO_ROOT / "bench_flint" / f"_cache_l2_iter{i}_{GROUND_TYPES}" |
| 142 | + params = [ |
| 143 | + (-sp.Integer(1), 1), |
| 144 | + (sp.Rational(1, 2), 2), |
| 145 | + (-sp.Integer(1), 3), |
| 146 | + ] |
| 147 | + alg = NBodyAlgebra( |
| 148 | + n_bodies=3, d_spatial=2, potential="composite", |
| 149 | + potential_params=params, checkpoint_dir=str(ckpt), |
| 150 | + ) |
| 151 | + t0 = time.perf_counter() |
| 152 | + dims = alg.compute_growth(max_level=2, n_samples=50, seed=42) |
| 153 | + elapsed = time.perf_counter() - t0 |
| 154 | + seq = [int(dims[lv]) for lv in range(3)] |
| 155 | + times.append(elapsed) |
| 156 | + print(f" iter {i+1}/{n_iter}: {elapsed:8.3f}s sequence = {seq}") |
| 157 | + # Cleanup |
| 158 | + import shutil |
| 159 | + if ckpt.exists(): |
| 160 | + shutil.rmtree(ckpt, ignore_errors=True) |
| 161 | + |
| 162 | + avg = sum(times) / len(times) |
| 163 | + best = min(times) |
| 164 | + print(f" -> mean: {avg:.3f}s, best: {best:.3f}s") |
| 165 | + return {"name": "schwarzschild_l2", "iters": n_iter, "times_s": times, |
| 166 | + "mean_s": avg, "best_s": best} |
| 167 | + |
| 168 | + |
| 169 | +# --------------------------------------------------------------------------- # |
| 170 | +# Bench 3: end-to-end Schwarzschild L3 (slow, single iter) |
| 171 | +# --------------------------------------------------------------------------- # |
| 172 | + |
| 173 | +def bench_schwarzschild_l3() -> dict: |
| 174 | + """Time one full Schwarzschild L<=3 run. Single iter (this takes |
| 175 | + minutes-to-hours).""" |
| 176 | + from exact_growth_nbody import NBodyAlgebra |
| 177 | + |
| 178 | + banner("Bench 3: Schwarzschild composite L<=3 dimseq (1 iter, can take minutes)") |
| 179 | + ckpt = REPO_ROOT / "bench_flint" / f"_cache_l3_{GROUND_TYPES}" |
| 180 | + params = [ |
| 181 | + (-sp.Integer(1), 1), |
| 182 | + (sp.Rational(1, 2), 2), |
| 183 | + (-sp.Integer(1), 3), |
| 184 | + ] |
| 185 | + alg = NBodyAlgebra( |
| 186 | + n_bodies=3, d_spatial=2, potential="composite", |
| 187 | + potential_params=params, checkpoint_dir=str(ckpt), |
| 188 | + ) |
| 189 | + t0 = time.perf_counter() |
| 190 | + dims = alg.compute_growth(max_level=3, n_samples=100, seed=42) |
| 191 | + elapsed = time.perf_counter() - t0 |
| 192 | + seq = [int(dims[lv]) for lv in range(4)] |
| 193 | + print(f"\n L<=3 elapsed: {elapsed:.1f}s sequence = {seq}") |
| 194 | + |
| 195 | + # Don't auto-clean cache - caller may want to inspect it |
| 196 | + return {"name": "schwarzschild_l3", "iters": 1, "times_s": [elapsed], |
| 197 | + "mean_s": elapsed, "best_s": elapsed, "sequence": seq} |
| 198 | + |
| 199 | + |
| 200 | +# --------------------------------------------------------------------------- # |
| 201 | +# Main |
| 202 | +# --------------------------------------------------------------------------- # |
| 203 | + |
| 204 | +def main() -> int: |
| 205 | + import argparse |
| 206 | + ap = argparse.ArgumentParser(description=__doc__) |
| 207 | + ap.add_argument("--skip-l3", action="store_true", |
| 208 | + help="Skip the slow L3 benchmark") |
| 209 | + ap.add_argument("--micro-iter", type=int, default=5) |
| 210 | + ap.add_argument("--l2-iter", type=int, default=3) |
| 211 | + ap.add_argument("--out", default=None, |
| 212 | + help="Append JSON results line to this file") |
| 213 | + args = ap.parse_args() |
| 214 | + |
| 215 | + env = print_env() |
| 216 | + results = {"env": env, "benches": []} |
| 217 | + |
| 218 | + results["benches"].append(bench_micro(args.micro_iter)) |
| 219 | + results["benches"].append(bench_schwarzschild_l2(args.l2_iter)) |
| 220 | + if not args.skip_l3: |
| 221 | + results["benches"].append(bench_schwarzschild_l3()) |
| 222 | + |
| 223 | + banner("SUMMARY") |
| 224 | + print(f" ground_types: {env['ground_types']}") |
| 225 | + for b in results["benches"]: |
| 226 | + print(f" {b['name']:30s} mean={b['mean_s']:8.2f}s best={b['best_s']:8.2f}s") |
| 227 | + |
| 228 | + if args.out: |
| 229 | + with open(args.out, "a", encoding="utf-8") as f: |
| 230 | + f.write(json.dumps(results) + "\n") |
| 231 | + print(f"\n Appended to: {args.out}") |
| 232 | + |
| 233 | + return 0 |
| 234 | + |
| 235 | + |
| 236 | +if __name__ == "__main__": |
| 237 | + sys.exit(main()) |
0 commit comments