#!/usr/bin/env python3 """Check contact pair performance smoke stats across OpenMP thread counts.""" from __future__ import annotations import os import re import subprocess import sys from pathlib import Path METRICS = ( "path_steps", "path_active", "path_bvh_nodes", "path_cache_used", "active_count", "search_facets", "search_masters", "search_broad", "search_active", "bvh_nodes", "cache_used", ) def run_case(source_root: Path, executable: Path, threads: int) -> dict[str, int]: env = os.environ.copy() env["OMP_NUM_THREADS"] = str(threads) proc = subprocess.run( [str(executable), "64", "1", "4"], cwd=source_root, env=env, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False, ) if proc.returncode != 0: print(proc.stdout, end="") raise SystemExit( f"perf_contact_pair failed for OMP_NUM_THREADS={threads}: " f"exit {proc.returncode}" ) return parse_metrics(proc.stdout, threads) def parse_metrics(output: str, threads: int) -> dict[str, int]: values: dict[str, int] = {} for line in output.splitlines(): match = re.match(r"\s*([A-Za-z_]+):\s+([-+]?\d+)\s*$", line) if match and match.group(1) in METRICS: values[match.group(1)] = int(match.group(2)) missing = [metric for metric in METRICS if metric not in values] if missing: print(output, end="") raise SystemExit( f"missing metrics for OMP_NUM_THREADS={threads}: {', '.join(missing)}" ) if values["active_count"] <= 0: raise SystemExit(f"no active contact for OMP_NUM_THREADS={threads}") if values["path_active"] <= 0: raise SystemExit(f"no active contact path for OMP_NUM_THREADS={threads}") if values["path_bvh_nodes"] <= 0: raise SystemExit(f"no BVH build path for OMP_NUM_THREADS={threads}") if values["path_cache_used"] <= 0: raise SystemExit(f"no BVH cache path for OMP_NUM_THREADS={threads}") if values["path_steps"] != 4: raise SystemExit( f"unexpected path step count for OMP_NUM_THREADS={threads}: " f"{values['path_steps']}" ) if values["cache_used"] <= 0: raise SystemExit(f"no BVH cache use for OMP_NUM_THREADS={threads}") return values def main(argv: list[str]) -> int: if len(argv) != 3: print( "usage: check_perf_contact_pair_omp.py " "" ) return 2 source_root = Path(argv[1]).resolve() executable = Path(argv[2]).resolve() serial = run_case(source_root, executable, 1) parallel = run_case(source_root, executable, 4) mismatches = [ metric for metric in METRICS if serial[metric] != parallel[metric] ] if mismatches: print("OpenMP contact performance statistics differ:") for metric in mismatches: print( f" {metric}: OMP=1 {serial[metric]}, " f"OMP=4 {parallel[metric]}" ) return 1 print("perf_contact_pair path OpenMP consistency: PASS") for metric in METRICS: print(f" {metric}: {serial[metric]}") return 0 if __name__ == "__main__": raise SystemExit(main(sys.argv))