Files
FemLite/tools/testing/check_perf_contact_pair_omp.py
shenw ee18a9b757
FemLite CI / build-test (push) Has been cancelled
Release FemLite 0.6.2 stability hardening
2026-07-02 08:26:46 +08:00

195 lines
6.1 KiB
Python

#!/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",
"path_fric_work",
"force_sum",
"tan_force_sum",
"gap_sum",
"slip_sum",
"fric_work",
)
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["path_fric_work"] < 0.0 or values["fric_work"] < 0.0:
raise SystemExit(f"negative friction work 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 compare_cases(
label: str,
reference: dict[str, float],
actual: dict[str, float],
) -> list[tuple[str, str]]:
mismatches: list[tuple[str, str]] = []
for metric in INT_METRICS:
if reference[metric] != actual[metric]:
mismatches.append((label, metric))
for metric in FLOAT_METRICS:
if not close_enough(reference[metric], actual[metric]):
mismatches.append((label, metric))
return mismatches
def main(argv: list[str]) -> int:
if len(argv) != 3:
print(
"usage: check_perf_contact_pair_omp.py <source-root> "
"<perf_contact_pair-exe>"
)
return 2
source_root = Path(argv[1]).resolve()
executable = Path(argv[2]).resolve()
runs = {
threads: [
run_case(source_root, executable, threads),
run_case(source_root, executable, threads),
]
for threads in (1, 2, 4)
}
repeat_mismatches = [
mismatch
for threads, values in runs.items()
for mismatch in compare_cases(f"OMP={threads} repeat", values[0], values[1])
]
cases = {threads: values[0] for threads, values in runs.items()}
serial = cases[1]
cross_mismatches = [
mismatch
for threads, values in cases.items()
if threads != 1
for mismatch in compare_cases(f"OMP={threads}", serial, values)
]
if repeat_mismatches or cross_mismatches:
print("OpenMP contact performance statistics differ:")
for label, metric in repeat_mismatches:
print(f" {label}: repeat mismatch in {metric}")
for label, metric in cross_mismatches:
print(f" {label}: mismatch versus OMP=1 in {metric}")
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))