#!/usr/bin/env python3 """ Check whether AMSS_NCKU_Input.py is suitable for the current GPU branch. Usage: python3 AMSS_NCKU_GPUCheck.py python3 AMSS_NCKU_GPUCheck.py -f /path/to/AMSS_NCKU_Input.py """ from __future__ import annotations import argparse import importlib.util import os import sys from dataclasses import dataclass, field from pathlib import Path from typing import Any, List @dataclass class CheckResult: ok: bool = True warnings: List[str] = field(default_factory=list) risks: List[str] = field(default_factory=list) notes: List[str] = field(default_factory=list) def add_warning(self, msg: str) -> None: self.warnings.append(msg) def add_risk(self, msg: str) -> None: self.ok = False self.risks.append(msg) def add_note(self, msg: str) -> None: self.notes.append(msg) def load_input_module(path: Path): spec = importlib.util.spec_from_file_location("amss_ncku_input", str(path)) if spec is None or spec.loader is None: raise RuntimeError(f"cannot load input module from {path}") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) # type: ignore[union-attr] return module def get_attr(mod: Any, name: str, default: Any = None) -> Any: return getattr(mod, name, default) def as_text(value: Any) -> str: if isinstance(value, str): return value.strip() return str(value).strip() def check_input(mod: Any) -> CheckResult: r = CheckResult() gpu = as_text(get_attr(mod, "GPU_Calculation", "no")).lower() == "yes" eq = as_text(get_attr(mod, "Equation_Class", "")) init = as_text(get_attr(mod, "Initial_Data_Method", "")) grid = as_text(get_attr(mod, "basic_grid_set", "")) center = as_text(get_attr(mod, "grid_center_set", "")) fd = as_text(get_attr(mod, "Finite_Diffenence_Method", "")) gauge = get_attr(mod, "gauge_choice", None) ahf = as_text(get_attr(mod, "AHF_Find", "no")).lower() boundary = as_text(get_attr(mod, "boundary_choice", "")) cpu_part = get_attr(mod, "CPU_Part", None) gpu_part = get_attr(mod, "GPU_Part", None) if not gpu: r.add_note("GPU_Calculation=no; this check only targets the GPU branch.") return r r.add_note("GPU_Calculation=yes detected.") if eq not in {"BSSN", "BSSN-EScalar", "BSSN-EM", "Z4C"}: r.add_risk(f"Unsupported Equation_Class for GPU branch: {eq!r}") elif eq == "BSSN": r.add_note("Equation_Class=BSSN is supported.") if init != "Ansorg-TwoPuncture": r.add_risk(f"Initial_Data_Method={init!r} is not the safe default for this GPU path.") else: r.add_note("Initial_Data_Method=Ansorg-TwoPuncture is supported.") if grid not in {"Patch", "Shell-Patch"}: r.add_risk(f"Unsupported basic_grid_set: {grid!r}") elif grid == "Patch": r.add_note("basic_grid_set=Patch is supported.") else: r.add_note("basic_grid_set=Shell-Patch is supported, but GPU runtime uses extra shell-specific switches.") if center not in {"Cell", "Vertex"}: r.add_risk(f"Unsupported grid_center_set: {center!r}") else: r.add_note(f"grid_center_set={center} is supported.") if fd not in {"2nd-order", "4th-order", "6th-order", "8th-order"}: r.add_risk(f"Unsupported Finite_Diffenence_Method: {fd!r}") else: r.add_note(f"Finite_Diffenence_Method={fd} is supported.") if gauge not in {0, 1, 2, 3, 4, 5, 6, 7}: r.add_risk(f"Unsupported gauge_choice: {gauge!r}") else: r.add_note(f"gauge_choice={gauge} is supported.") if ahf not in {"yes", "no"}: r.add_risk(f"Unsupported AHF_Find value: {ahf!r}") elif ahf == "yes": r.add_note("AHF_Find=yes is supported, but it increases sensitivity to state sync.") if boundary not in {"BAM-choice", "Shibata-choice"}: r.add_risk(f"Unsupported boundary_choice: {boundary!r}") elif boundary == "Shibata-choice": r.add_risk("Shibata-choice is not faithfully distinguished in the current macro generator; it maps to the BAM branch.") else: r.add_note("boundary_choice=BAM-choice is supported.") if cpu_part is not None or gpu_part is not None: r.add_warning("CPU_Part/GPU_Part are printed and propagated, but they do not control a real mixed CPU/GPU split in this branch.") if eq == "BSSN" and grid == "Patch" and init == "Ansorg-TwoPuncture": if get_attr(mod, "GPU_Calculation", "no") == "yes": r.add_note("This configuration is generally runnable on the GPU branch.") if eq == "BSSN" and get_attr(mod, "GPU_Calculation", "no") == "yes": r.add_warning("Default BH resident interpolation was previously enabled and could cause trajectory drift; the current code now defaults it off unless AMSS_CUDA_BH_INTERP_RESIDENT=1 is set.") return r def main() -> int: parser = argparse.ArgumentParser() parser.add_argument( "-f", "--file", "--input", dest="input_file", default="AMSS_NCKU_Input.py", help="path to AMSS_NCKU_Input.py", ) args = parser.parse_args() path = Path(args.input_file).resolve() if not path.exists(): print(f"ERROR: input file not found: {path}") return 2 try: mod = load_input_module(path) except Exception as exc: print(f"ERROR: failed to load input file: {exc}") return 2 result = check_input(mod) print(f"Input: {path}") print(f"GPU_Calculation: {get_attr(mod, 'GPU_Calculation', 'no')}") print(f"Equation_Class: {get_attr(mod, 'Equation_Class', '')}") print(f"Initial_Data_Method: {get_attr(mod, 'Initial_Data_Method', '')}") print(f"basic_grid_set: {get_attr(mod, 'basic_grid_set', '')}") print(f"grid_center_set: {get_attr(mod, 'grid_center_set', '')}") print(f"Finite_Diffenence_Method: {get_attr(mod, 'Finite_Diffenence_Method', '')}") print(f"gauge_choice: {get_attr(mod, 'gauge_choice', '')}") print(f"AHF_Find: {get_attr(mod, 'AHF_Find', '')}") print("") for msg in result.notes: print(f"NOTE: {msg}") for msg in result.warnings: print(f"WARNING: {msg}") for msg in result.risks: print(f"RISK: {msg}") print("") if result.risks: print("Verdict: review the risks above before running.") return 1 if result.warnings: print("Verdict: runnable on the current GPU branch, but keep the warnings in mind.") return 0 print("Verdict: OK to run on the current GPU branch.") return 0 if __name__ == "__main__": raise SystemExit(main())