#!/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 INT_METRICS = ( "path_steps", "path_active", "path_activated", "path_released", "path_bvh_nodes", "path_cache_used", "path_cache_over", "active_count", "state_open", "state_activated", "state_maintained", "state_released", "activation_count", "release_count", "search_facets", "search_masters", "search_broad", "search_active", "bvh_nodes", "cache_used", "cache_overflow", ) FLOAT_METRICS = ( "path_force_sum", "path_tan_mat_sum", "path_tan_f_sum", "path_gap_sum", "path_slip_sum", "force_sum", "tan_force_sum", "gap_sum", "slip_sum", ) def run_case(source_root: Path, executable: Path, threads: int) -> dict[str, float]: 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, float]: values: dict[str, float] = {} for line in output.splitlines(): match = re.match( r"\s*([A-Za-z_]+):\s+([-+]?\d+(?:\.\d*)?(?:[Ee][-+]?\d+)?)\s*$", line, ) if not match: continue name = match.group(1) if name in INT_METRICS: values[name] = int(float(match.group(2))) elif name in FLOAT_METRICS: values[name] = float(match.group(2)) expected = INT_METRICS + FLOAT_METRICS missing = [metric for metric in expected 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}") if values["cache_overflow"] != 0 or values["path_cache_over"] != 0: raise SystemExit(f"unexpected cache overflow for OMP_NUM_THREADS={threads}") if values["path_force_sum"] <= 0.0 or values["force_sum"] <= 0.0: raise SystemExit(f"missing force checksum for OMP_NUM_THREADS={threads}") if values["path_tan_mat_sum"] <= 0.0: raise SystemExit(f"missing tangent checksum for OMP_NUM_THREADS={threads}") if values["path_gap_sum"] <= 0.0 or values["gap_sum"] <= 0.0: raise SystemExit(f"missing gap checksum for OMP_NUM_THREADS={threads}") if values["state_maintained"] <= 0: raise SystemExit(f"missing maintained state for OMP_NUM_THREADS={threads}") if values["activation_count"] <= 0: raise SystemExit(f"missing activation count for OMP_NUM_THREADS={threads}") return values def close_enough(left: float, right: float) -> bool: scale = max(1.0, abs(left), abs(right)) return abs(left - right) <= 1.0e-9 * scale 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() cases = { threads: run_case(source_root, executable, threads) for threads in (1, 2, 4) } serial = cases[1] mismatches = [ (threads, metric) for threads, values in cases.items() if threads != 1 for metric in INT_METRICS if serial[metric] != values[metric] ] float_mismatches = [ (threads, metric) for threads, values in cases.items() if threads != 1 for metric in FLOAT_METRICS if not close_enough(serial[metric], values[metric]) ] if mismatches or float_mismatches: print("OpenMP contact performance statistics differ:") for threads, metric in mismatches: print( f" {metric}: OMP=1 {serial[metric]}, " f"OMP={threads} {cases[threads][metric]}" ) for threads, metric in float_mismatches: print( f" {metric}: OMP=1 {serial[metric]:.12e}, " f"OMP={threads} {cases[threads][metric]:.12e}" ) return 1 print("perf_contact_pair path OpenMP consistency: PASS") for metric in INT_METRICS: print(f" {metric}: {serial[metric]}") for metric in FLOAT_METRICS: print(f" {metric}: {serial[metric]:.12e}") return 0 if __name__ == "__main__": raise SystemExit(main(sys.argv))