-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathparse_refactor_logs.py
More file actions
175 lines (134 loc) · 4.3 KB
/
Copy pathparse_refactor_logs.py
File metadata and controls
175 lines (134 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
"""
Parse refactor benchmark logs for ReSolve issue 326.
Expected timing row format:
TIMING,example,backend,ir_enabled,system,time_ms
The script combines timing rows with residuals already printed by
gpuRefactor, kluRefactor, gluRefactor, and sysRefactor then writes one CSV file for plotting.
"""
import argparse
import csv
import re
from pathlib import Path
FIELDNAMES = [
"source_log",
"N",
"example",
"backend",
"method",
"ir_enabled",
"system",
"time_ms",
"residual",
]
METHOD_LABELS = {
"kluRefactor": "klu",
"gpuRefactor": "gpu_refactor",
"gluRefactor": "glu_refactor",
"sysRefactor": "sys_refactor",
}
SYSTEM_RE = re.compile(r"^System\s+(\d+):")
TIMING_RE = re.compile(
r"^TIMING,"
r"(?P<example>[^,]+),"
r"(?P<backend>[^,]+),"
r"(?P<ir_enabled>[01]),"
r"(?P<system>\d+),"
r"(?P<time_ms>[-+0-9.eE]+)"
)
RESIDUAL_RE = re.compile(
r"2-Norm of the residual(?: \(before IR\))?:\s*([-+0-9.eE]+)"
)
FGMRES_RE = re.compile(
r"FGMRES:\s+init nrm:\s*[-+0-9.eE]+\s+final nrm:\s*([-+0-9.eE]+)"
)
N_RE = re.compile(
r"(?:^|[_/-])N(?P<N>125|250|500|1000|1k)(?:[_./-]|$)",
re.IGNORECASE,
)
def infer_problem_size(path: Path) -> str:
"""Infer the GridKit N value from a log filename when possible."""
match = N_RE.search(str(path))
if not match:
return ""
value = match.group("N")
if value.lower() == "1k":
return "1000"
return value
def method_name(example: str, ir_enabled: str) -> str:
"""Create a compact plotting label from the example and IR flag."""
method = METHOD_LABELS.get(example, example)
if ir_enabled == "1":
return f"{method}_ir"
return method
def parse_log(path: Path, problem_size: str = "") -> list[dict[str, str]]:
"""Parse one benchmark log into CSV-ready rows."""
rows = []
residual_by_system = {}
current_system = ""
if not problem_size:
problem_size = infer_problem_size(path)
with path.open("r", encoding="utf-8", errors="replace") as log_file:
for line in log_file:
line = line.strip()
system_match = SYSTEM_RE.match(line)
if system_match:
current_system = system_match.group(1)
continue
residual_match = RESIDUAL_RE.search(line)
if residual_match and current_system:
residual_by_system[current_system] = residual_match.group(1)
continue
fgmres_match = FGMRES_RE.search(line)
if fgmres_match and current_system:
residual_by_system[current_system] = fgmres_match.group(1)
continue
timing_match = TIMING_RE.match(line)
if not timing_match:
continue
row = timing_match.groupdict()
row["source_log"] = str(path)
row["N"] = problem_size
row["method"] = method_name(row["example"], row["ir_enabled"])
row["residual"] = residual_by_system.get(row["system"], "")
rows.append(row)
return rows
def parse_args() -> argparse.Namespace:
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(
description="Parse ReSolve refactor timing logs into CSV."
)
parser.add_argument(
"logs",
nargs="+",
type=Path,
help="Log files produced by refactor examples with -t.",
)
parser.add_argument(
"-o",
"--output",
type=Path,
default=Path("refactor_timings.csv"),
help="Output CSV path.",
)
parser.add_argument(
"--N",
dest="problem_size",
default="",
help="GridKit N value to use when it cannot be inferred from log names.",
)
return parser.parse_args()
def main() -> int:
"""Parse input logs and write one combined CSV file."""
args = parse_args()
rows = []
for log_path in args.logs:
rows.extend(parse_log(log_path, args.problem_size))
with args.output.open("w", newline="", encoding="utf-8") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=FIELDNAMES)
writer.writeheader()
writer.writerows(rows)
print(f"Wrote {len(rows)} rows to {args.output}")
return 0
if __name__ == "__main__":
raise SystemExit(main())