-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
175 lines (139 loc) · 4.96 KB
/
benchmark.py
File metadata and controls
175 lines (139 loc) · 4.96 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
import curses
import subprocess
import sys
import os
import time
def resource_path(relative_path):
if hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
QPESeq = resource_path("QPESeq")
QPEOMP = resource_path("QPEOMP")
QPEMPI = resource_path("QPEMPI")
def run_command(command, silent=False, dataset=None):
"""Runs a shell command."""
try:
if silent:
subprocess.run(command, shell=True, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
else:
subprocess.run(command, shell=True, check=True)
except subprocess.CalledProcessError as e:
if not silent:
print(f"Error running command: {command}")
print(f"Exit code: {e.returncode}")
def draw_menu(stdscr, selected_row_idx, options, title):
stdscr.clear()
h, w = stdscr.getmaxyx()
stdscr.addstr(h//2 - len(options)//2 - 2, w//2 - len(title)//2, title)
for idx, row in enumerate(options):
x = w//2 - len(row)//2
y = h//2 - len(options)//2 + idx
if idx == selected_row_idx:
stdscr.attron(curses.color_pair(1))
stdscr.addstr(y, x, row)
stdscr.attroff(curses.color_pair(1))
else:
stdscr.addstr(y, x, row)
stdscr.refresh()
def select_option(stdscr, title, options):
selected_row_idx = 0
while True:
draw_menu(stdscr, selected_row_idx, options, title)
key = stdscr.getch()
if key == curses.KEY_UP and selected_row_idx > 0:
selected_row_idx -= 1
elif key == curses.KEY_DOWN and selected_row_idx < len(options) - 1:
selected_row_idx += 1
elif key == curses.KEY_ENTER or key in [10, 13]:
return options[selected_row_idx]
def get_datasets():
data_dir = resource_path("data-generation")
if not os.path.exists(data_dir):
return []
files = [f for f in os.listdir(data_dir) if f.endswith(".csv")]
files.sort()
# Return absolute paths
return [os.path.join(data_dir, f) for f in files]
def main(stdscr):
# Setup colors
curses.start_color()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.curs_set(0)
# Select Dataset
datasets = get_datasets()
if not datasets:
return None, "No CSV files found in data-generation/", None
datasets.append("Exit")
dataset = select_option(stdscr, "Select Dataset (Use Arrow Keys + Enter)", datasets)
if dataset == "Exit":
return "Exit", None, None
# Select Benchmark Version
benchmarks = ["Serial", "OMP", "MPI", "ALL", "Exit"]
benchmark = select_option(stdscr, "Select Benchmark to Run", benchmarks)
if benchmark == "Exit":
return "Exit", None, None
# Select Cores
cores = None
if benchmark in ["OMP", "MPI", "ALL"]:
try:
num_cpus = os.cpu_count()
if num_cpus is None: num_cpus = 4
except:
num_cpus = 4
core_options = ["ALL"] + [str(i) for i in range(1, num_cpus + 1)]
cores = select_option(stdscr, "Select Number of Cores", core_options)
return benchmark, dataset, cores
def run_benchmark():
# Run make silently
print("Building project... (this may take a moment)")
run_command("make", silent=True)
# Show Menu
try:
result = curses.wrapper(main)
if not result: return
selection, dataset, cores = result
except Exception as e:
print(f"Error in UI: {e}")
return
if selection is None:
if dataset:
print(dataset) # Print error message
return
if selection == "Exit":
print("Exiting...")
return
print(f"\nRunning {selection} Benchmark with dataset: {dataset}")
if cores:
print(f"Cores: {cores}")
print("="*60 + "\n")
# Determine core count
omp_prefix = ""
mpi_prefix = ""
if cores:
count = 0
if cores == "ALL":
count = os.cpu_count() or 4
else:
count = int(cores)
omp_prefix = f"OMP_NUM_THREADS={count} "
mpi_prefix = f"mpirun -np {count} "
# Run Selected
start_time = time.time()
if selection == "Serial":
run_command(f"{QPESeq} {dataset}")
elif selection == "OMP":
run_command(f"{omp_prefix}{QPEOMP} {dataset}")
elif selection == "MPI":
run_command(f"{mpi_prefix}{QPEMPI} {dataset}")
elif selection == "ALL":
print("--- Running Serial ---")
run_command(f"{QPESeq} {dataset}")
print("\n--- Running OMP ---")
run_command(f"{omp_prefix}{QPEOMP} {dataset}")
print("\n--- Running MPI ---")
run_command(f"{mpi_prefix}{QPEMPI} {dataset}")
end_time = time.time()
print("\n" + "="*60)
print(f"Total Benchmark Time: {end_time - start_time:.4f} seconds")
if __name__ == "__main__":
run_benchmark()