#!/usr/bin/env python3 """Execute backend calls (CPU only). python3 mt_ground.py --designs designs.jsonl --calls calls.jsonl --out results.jsonl [--workers 12] calls.jsonl rows: {key, design_id, tool, parameters}. Output rows: {key, design_id, tool, parameters, result, seconds}. Resumable: keys already in --out are skipped. Used both for the reference results of the multi-tool set (gold calls) and for end-to-end evaluation (router-predicted calls). """ import argparse, json, os, sys, time from multiprocessing import Pool sys.path.insert(0, os.environ.get("CIRCUITQA_ROOT", os.path.dirname(os.path.abspath(__file__)))) import eda_tools CODE = {} def init(designs): global CODE for l in open(designs): d = json.loads(l); CODE[d["design_id"]] = d["verilog_code"] def run(c): t0 = time.time() try: res = eda_tools.execute_tool(c["tool"], CODE[c["design_id"]], **(c.get("parameters") or {})) except Exception as e: res = {"status": "error", "error": str(e)[:300]} return dict(c, result=res, seconds=round(time.time() - t0, 3)) def main(): ap = argparse.ArgumentParser() ap.add_argument("--designs", required=True); ap.add_argument("--calls", required=True) ap.add_argument("--out", required=True); ap.add_argument("--workers", type=int, default=12) a = ap.parse_args() done = set() if os.path.exists(a.out): for l in open(a.out): try: done.add(json.loads(l)["key"]) except Exception: pass todo = [json.loads(l) for l in open(a.calls)] todo = [c for c in todo if c["key"] not in done] print(f"{len(done)} done, {len(todo)} to run, {a.workers} workers", flush=True) t0 = time.time(); n = 0 with Pool(a.workers, initializer=init, initargs=(a.designs,)) as p, open(a.out, "a") as f: for r in p.imap_unordered(run, todo, chunksize=4): f.write(json.dumps(r) + "\n"); f.flush(); n += 1 if n % 200 == 0: print(f" {n}/{len(todo)} {time.time()-t0:.0f}s", flush=True) print(f"finished {n} in {time.time()-t0:.0f}s", flush=True) if __name__ == "__main__": main()