forked from ChampSim/ChampSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch_comparison.py
More file actions
118 lines (89 loc) · 3.67 KB
/
branch_comparison.py
File metadata and controls
118 lines (89 loc) · 3.67 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
import argparse
import numpy as np
import os
import re
import matplotlib.pyplot as plt
from pathlib import Path
from scipy.stats import gmean
RUN_REGEXP=re.compile(r"run_(\d+)")
MPKI_REGEXP=re.compile(r"MPKI: (?P<mpki>\d+\.\d+) ")
IPC_REGEXP=re.compile(r"CPU 0 cumulative IPC: (?P<ipc>\d+\.\d+) instructions: (?P<instrs>\d+) cycles: (?P<cycles>\d+)")
INCORRECT_IPC=-1
INCORRECT_MPKI=-1
def extract_number(filename: str):
match = re.search(RUN_REGEXP, filename)
return int(match.group(1)) if match else float('inf')
def parse_mpki_and_ipc(filename: Path):
with open(filename, "r") as ifile:
ilines=ifile.readlines()
cur_ipc = INCORRECT_IPC
cur_mpki = INCORRECT_MPKI
for iline in ilines:
if (match:=re.search(IPC_REGEXP,iline)):
cur_ipc = float(match["ipc"])
continue
# mpki info is after ipc
if (match := re.search(MPKI_REGEXP, iline)):
cur_mpki=float(match["mpki"])
break
return cur_ipc, cur_mpki
def plot_results(all_results, labels):
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 10), sharex=True)
num_labels = len(labels)
file_idxs = sorted(list(set(el["file_idx"] for results in all_results for el in results)))
bar_width = 0.8 / num_labels
for i, (results, label) in enumerate(zip(all_results, labels)):
mpki_dict = {el["file_idx"]: el["mpki"] for el in results}
ipc_dict = {el["file_idx"]: el["ipc"] for el in results}
mpkis = [mpki_dict.get(idx, 0) for idx in file_idxs]
ipcs = [ipc_dict.get(idx, 0) for idx in file_idxs]
res_gmean_mpki = np.round(gmean([v for v in mpkis if v > 0]), 3)
res_gmean_ipc = np.round(gmean([v for v in ipcs if v > 0]), 3)
positions = np.arange(len(file_idxs)) + i * bar_width
ax1.bar(positions, mpkis, bar_width, label=f"{label} (gmean MPKI={res_gmean_mpki})")
ax2.bar(positions, ipcs, bar_width, label=f"{label} (gmean IPC={res_gmean_ipc})")
ax1.set_ylabel("MPKI")
ax2.set_ylabel("IPC")
ax2.set_xlabel("File Index")
ax1.set_title("MPKI Comparison")
ax2.set_title("IPC Comparison")
ax2.set_xticks(np.arange(len(file_idxs)) + bar_width * (num_labels - 1) / 2)
ax2.set_xticklabels(file_idxs)
ax1.legend()
ax2.legend()
ax1.grid(True)
ax2.grid(True)
plt.tight_layout()
plt.savefig("branch_comparison.png")
plt.close()
def run_analysis(input_dir: Path):
input_files = os.listdir(input_dir)
input_files = sorted(input_files, key=extract_number)
results = []
for ifile in input_files:
cur_ipc, cur_mpki = parse_mpki_and_ipc(Path(f"{input_dir}/{ifile}"))
if cur_ipc == INCORRECT_IPC or cur_mpki == INCORRECT_MPKI:
continue
if (file_idx := str(extract_number(ifile))) == float('inf'):
continue
results.append({
"file_idx": file_idx,
"ipc": cur_ipc,
"mpki": cur_mpki
})
return results
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--runs-dirs", type=str, help="comma-separated list of directories with results")
args = parser.parse_args()
runs_dirs = [Path(p.strip()) for p in args.runs_dirs.split(",")]
all_results = []
labels = []
labels_correct={"baseline_runs": "bimodal",
"markov_predictor_runs": "markov",
"markov_probable_runs": "markov with probability"}
for dir_path in runs_dirs:
results = run_analysis(dir_path)
all_results.append(results)
labels.append(labels_correct[dir_path.name] if dir_path.name in labels_correct.keys() else dir_path.name)
plot_results(all_results, labels)