|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +E3 — N=4 d=1 1/r cancel-vs-together speedup bench. |
| 4 | +
|
| 5 | +Two passes: |
| 6 | + Pass 1: head-to-head at L=2 (both cancel and together, same engine |
| 7 | + freshly instantiated; per-level wall time + per-bracket counts) |
| 8 | + Pass 2: together-only at L=3 (compared against the pinned cancel time |
| 9 | + recorded in results/symbolic_rank/rank_N4_d1_1r.json: |
| 10 | + computation_time_seconds = 3137.6s) |
| 11 | +
|
| 12 | +Generation phase only (no extract_monomial_matrix, no rank). The patch only |
| 13 | +touches generation; the rank phase is identical for both engines. |
| 14 | +
|
| 15 | +Output: bench_flint/symrank_n4_timing.json |
| 16 | +""" |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import json |
| 20 | +import os |
| 21 | +import sys |
| 22 | +import time |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 26 | +sys.path.insert(0, str(REPO_ROOT / "nbody")) |
| 27 | + |
| 28 | +import sympy as sp # noqa: E402 |
| 29 | +from sympy import cancel, expand, together # noqa: E402 |
| 30 | + |
| 31 | +import symbolic_rank_nbody as srn # noqa: E402 |
| 32 | +from symbolic_rank_nbody import NBodySymbolicRank # noqa: E402 |
| 33 | + |
| 34 | + |
| 35 | +def _make_simplify(mode): |
| 36 | + """Return a _simplify implementation that uses the given mode.""" |
| 37 | + if mode == "cancel": |
| 38 | + def _simplify(self, expr): |
| 39 | + if self.uses_u: |
| 40 | + return cancel(expr) |
| 41 | + return expand(expr) |
| 42 | + elif mode == "together": |
| 43 | + def _simplify(self, expr): |
| 44 | + if self.uses_u: |
| 45 | + return together(expr) |
| 46 | + return expand(expr) |
| 47 | + else: |
| 48 | + raise ValueError(mode) |
| 49 | + return _simplify |
| 50 | + |
| 51 | + |
| 52 | +def run_one(mode, n_bodies, d_spatial, potential, max_level): |
| 53 | + print(f"\n{'='*68}") |
| 54 | + print(f" N={n_bodies} d={d_spatial} potential={potential} " |
| 55 | + f"max_level={max_level} mode={mode}") |
| 56 | + print(f"{'='*68}") |
| 57 | + # Monkeypatch _simplify on the class, then build engine afresh. |
| 58 | + NBodySymbolicRank._simplify = _make_simplify(mode) |
| 59 | + engine = NBodySymbolicRank( |
| 60 | + n_bodies=n_bodies, d_spatial=d_spatial, potential=potential) |
| 61 | + |
| 62 | + t0 = time.time() |
| 63 | + exprs, names, levels = engine.build_generators( |
| 64 | + max_level, checkpoint_dir=None, n_workers=1) |
| 65 | + elapsed = time.time() - t0 |
| 66 | + |
| 67 | + # Per-level counts |
| 68 | + by_level = {} |
| 69 | + for lv in levels: |
| 70 | + by_level[lv] = by_level.get(lv, 0) + 1 |
| 71 | + |
| 72 | + # Average term count of generated brackets at the top level |
| 73 | + from sympy import Add |
| 74 | + top_lv = max(levels) |
| 75 | + top_exprs = [e for e, lv in zip(exprs, levels) if lv == top_lv] |
| 76 | + top_nterms = [len(Add.make_args(e)) for e in top_exprs] |
| 77 | + avg_nterms = (sum(top_nterms) / len(top_nterms)) if top_nterms else 0.0 |
| 78 | + |
| 79 | + return { |
| 80 | + "mode": mode, |
| 81 | + "n_bodies": n_bodies, |
| 82 | + "d_spatial": d_spatial, |
| 83 | + "potential": potential, |
| 84 | + "max_level": max_level, |
| 85 | + "elapsed_s": round(elapsed, 2), |
| 86 | + "n_generators": len(exprs), |
| 87 | + "by_level": by_level, |
| 88 | + "top_level_avg_nterms": round(avg_nterms, 1), |
| 89 | + "top_level_max_nterms": max(top_nterms) if top_nterms else 0, |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | +def main(): |
| 94 | + print(f"E3 — N=4 d=1 1/r cancel-vs-together (sympy {sp.__version__})") |
| 95 | + t_global = time.time() |
| 96 | + |
| 97 | + # Pass 1: head-to-head at L=2 (small enough that cancel won't hang) |
| 98 | + pass1 = [] |
| 99 | + pass1.append(run_one("cancel", n_bodies=4, d_spatial=1, |
| 100 | + potential="1/r", max_level=2)) |
| 101 | + pass1.append(run_one("together", n_bodies=4, d_spatial=1, |
| 102 | + potential="1/r", max_level=2)) |
| 103 | + |
| 104 | + if pass1[0]["elapsed_s"] > 0: |
| 105 | + l2_speedup = pass1[0]["elapsed_s"] / max(pass1[1]["elapsed_s"], 1e-6) |
| 106 | + else: |
| 107 | + l2_speedup = None |
| 108 | + |
| 109 | + # Pass 2: together-only at L=3, compared against pin |
| 110 | + pin_path = REPO_ROOT / "results" / "symbolic_rank" / "rank_N4_d1_1r.json" |
| 111 | + with open(pin_path, "r", encoding="utf-8") as f: |
| 112 | + pin = json.load(f) |
| 113 | + pin_t_l3 = pin["computation_time_seconds"] |
| 114 | + |
| 115 | + pass2 = run_one("together", n_bodies=4, d_spatial=1, |
| 116 | + potential="1/r", max_level=3) |
| 117 | + l3_speedup = pin_t_l3 / max(pass2["elapsed_s"], 1e-6) |
| 118 | + |
| 119 | + summary = { |
| 120 | + "sympy_version": sp.__version__, |
| 121 | + "pass1_l2_head_to_head": pass1, |
| 122 | + "pass1_l2_speedup_cancel_over_together": round(l2_speedup, 2) |
| 123 | + if l2_speedup is not None else None, |
| 124 | + "pass2_l3_together_only": pass2, |
| 125 | + "pass2_l3_pin_cancel_elapsed_s": pin_t_l3, |
| 126 | + "pass2_l3_speedup_pin_over_together": round(l3_speedup, 2), |
| 127 | + "elapsed_total_s": round(time.time() - t_global, 2), |
| 128 | + "completed_at": time.strftime("%Y-%m-%dT%H:%M:%S"), |
| 129 | + } |
| 130 | + |
| 131 | + out = REPO_ROOT / "bench_flint" / "symrank_n4_timing.json" |
| 132 | + out.parent.mkdir(parents=True, exist_ok=True) |
| 133 | + tmp = out.with_suffix(".tmp.json") |
| 134 | + with open(tmp, "w", encoding="utf-8") as f: |
| 135 | + json.dump(summary, f, indent=2) |
| 136 | + os.replace(tmp, out) |
| 137 | + |
| 138 | + print("\n" + "=" * 68) |
| 139 | + print(" E3 SUMMARY") |
| 140 | + print("=" * 68) |
| 141 | + print(f" L=2 cancel: {pass1[0]['elapsed_s']}s") |
| 142 | + print(f" L=2 together: {pass1[1]['elapsed_s']}s") |
| 143 | + print(f" L=2 speedup: x{summary['pass1_l2_speedup_cancel_over_together']}") |
| 144 | + print(f" L=3 pin (cancel, 3137.6s reference)") |
| 145 | + print(f" L=3 together: {pass2['elapsed_s']}s") |
| 146 | + print(f" L=3 speedup: x{summary['pass2_l3_speedup_pin_over_together']}") |
| 147 | + print(f" Output: {out.relative_to(REPO_ROOT)}") |
| 148 | + return 0 |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + sys.exit(main()) |
0 commit comments