forked from SamAinsworth/reproduce-cgo2017-paper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_and_benchmark.py
executable file
·387 lines (306 loc) · 12.6 KB
/
test_and_benchmark.py
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env python3
import subprocess
import os.path
import time
import argparse
import pathlib
from datetime import datetime
import re
import shutil
import csv
import matplotlib.pyplot as plt
BENCHMARK_OUTPUT_DIR = pathlib.Path("./benchmark_output")
BENCHMARK_TEMP_OUTPUT_DIR = pathlib.Path("./temp_output")
TEST_REPITITIONS = 10
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--save", help="Save benchmark output in benchmark_output/", action="store_true")
return parser.parse_args()
def do_cmd(command, dir, use_shell=True):
p = subprocess.Popen(command, cwd=dir, shell=use_shell)
p.wait()
def build_benchmarks():
do_cmd(["./compile_x86.sh"], "./program/graph500")
do_cmd(["./compile_x86.sh"], "./program/hashjoin-ph-2")
do_cmd(["./compile_x86.sh"], "./program/hashjoin-ph-8")
do_cmd(["./compile_x86.sh"], "./program/nas-cg")
do_cmd(["./compile_x86.sh"], "./program/randacc")
def generate_output_dir(workdir):
global timestamp
global args
out_dir = BENCHMARK_TEMP_OUTPUT_DIR
if (args.save):
out_dir = BENCHMARK_OUTPUT_DIR
output_dir = out_dir / workdir.strip("/").split("/")[-1] / timestamp
return output_dir
def run_benchmark(commands, workdir):
global args
global timestamp
try:
output_dir = generate_output_dir(workdir)
output_dir.mkdir(parents=True)
output_dir = output_dir.resolve()
output_commands = []
for command in commands:
executable_name = command[0].split('/')[-1]
command.extend(["|", f"tee {output_dir / executable_name}.txt"])
output_commands.append(' '.join(command))
commands = output_commands
except:
pass
for command in commands:
do_cmd(command, workdir)
def get_files_to_parse(commands, workdir):
global args
files = []
output_dir = generate_output_dir(workdir)
for command in commands:
file = output_dir / (command[0].split('/')[-1] + ".txt")
files.append(file)
return files
def create_pretty_print_text(benchmark, times):
s = '\n'
if (len(times) > 1):
s += '*********************************************************\n'
s += 'Benchmark: ' + benchmark + '\n'
s += 'Normal: ' + str(times[0]) + '\n'
s += 'Prefetch: ' + str(times[1]) + '\n'
s += 'New Prefetch: ' + str(times[2]) + '\n'
s += '*********************************************************\n'
s += '\n'
return s
def try_parse_results(commands, workdir, regex):
global args
files = get_files_to_parse(commands, workdir)
times = []
for file in files:
with open(file, 'r') as f:
data = f.read()
match = re.search(regex, data)
if match:
times.append(float(match.group(1)))
if not args.save:
# delete the temp out director
dir = generate_output_dir(workdir)
shutil.rmtree(dir)
return times
def repeat_benchmark(commands, workdir, regex):
total_times = [0] * len(commands)
test_name = workdir.split('/')[-1]
for i in range(TEST_REPITITIONS):
print("")
print(test_name + " Benchmark Run #" + str(i + 1))
print("")
run_benchmark(commands, workdir)
temp = try_parse_results(commands, workdir, regex)
for i in range(len(total_times)):
total_times[i] += temp[i]
for i in range(len(total_times)):
total_times[i] /= TEST_REPITITIONS
return create_pretty_print_text(test_name, total_times), total_times
def run_graph500_benchmark(run_only_standard_prefetch = False):
if run_only_standard_prefetch:
commands = [["bin/x86/g500-auto"]]
else:
commands = [["bin/x86/g500-no"], ["bin/x86/g500-auto"], ["bin/x86/g500-auto-new"]]
workdir = "./program/graph500"
return repeat_benchmark(commands, workdir, r"median_time: (\d+\.\d+e[+-]\d+)")
def run_hj2_benchmark(run_only_standard_prefetch = False):
if run_only_standard_prefetch:
commands = [["src/bin/x86/hj2-auto"]]
else:
commands = [["src/bin/x86/hj2-no"], ["src/bin/x86/hj2-auto"], ["src/bin/x86/hj2-auto-new"]]
workdir = "./program/hashjoin-ph-2"
return repeat_benchmark(commands, workdir, r"TOTAL-TIME-USECS, TOTAL-TUPLES, CYCLES-PER-TUPLE:\s+(\d+\.\d+)\s+")
def run_hj8_benchmark(run_only_standard_prefetch = False):
if run_only_standard_prefetch:
commands = [["src/bin/x86/hj2-auto"]]
else:
commands = [["src/bin/x86/hj2-no"], ["src/bin/x86/hj2-auto"], ["src/bin/x86/hj2-auto-new"]]
workdir = "./program/hashjoin-ph-8"
return repeat_benchmark(commands, workdir, r"TOTAL-TIME-USECS, TOTAL-TUPLES, CYCLES-PER-TUPLE:\s+(\d+\.\d+)\s+")
def run_nas_cg_benchmark(run_only_standard_prefetch = False):
if run_only_standard_prefetch:
commands = [["bin/x86/cg-auto"]]
else:
commands = [["bin/x86/cg-no"], ["bin/x86/cg-auto"], ["bin/x86/cg-auto-new"]]
workdir = "./program/nas-cg"
return repeat_benchmark(commands, workdir, r"Time in seconds\s*=\s*(\d+\.\d+)")
def run_randacc_benchmark(run_only_standard_prefetch = False):
if run_only_standard_prefetch:
commands = [["bin/x86/randacc-auto", "100000000"]]
else:
commands = [["bin/x86/randacc-no", "100000000"], ["bin/x86/randacc-auto", "100000000"], ["bin/x86/randacc-auto-new", "100000000"]]
workdir = "./program/randacc"
return repeat_benchmark(commands, workdir, r"Time in milliseconds:\s*(\d+\.\d+)")
def modify_cpp_constant(new_value):
pattern = re.compile(r'^#define C_CONSTANT \(\d+\)$')
file_path = "./freshAttempt/swPrefetchPass/swPrefetchPass.cpp"
lines = []
with open(file_path, 'r') as file:
lines = file.readlines()
with open(file_path, 'w') as file:
for line in lines:
if pattern.match(line.strip()):
# Replace the existing value with the new value
line = f'#define C_CONSTANT ({new_value})\n'
file.write(line)
def plot_optimal_c_value_data(file_path):
with open(file_path, 'r') as file:
csv_reader = csv.reader(file)
headers = next(csv_reader) # Read the header row
x_labels = headers[1:] # Column headers except the first 'test' column
plt.figure(constrained_layout=True)
for row in csv_reader:
test = row[0]
y_values = list(map(float, row[1:]))
# Normalize by dividing by the first data point in the row
base_value = y_values[0]
normalized_values = [y / base_value for y in y_values]
plt.plot(x_labels, normalized_values, marker='o', label=test)
plt.title("Optimal C Constant Value for benchmarks")
plt.xlabel("C Constant Value")
plt.ylabel("Time Relative to Performance for C=32")
plt.grid(True)
plt.legend() # Add a legend to identify each line
plt.savefig('optimal_c_const_value.png')
def find_optimal_c_value():
current = 0
g500 = []
hj2 = []
hj8 = []
cg = []
rand = []
for i in range(8):
modify_cpp_constant(current)
rebuild_pass()
build_benchmarks()
ignore, times = run_all_benchmarks(True)
g500.append(times[0][0])
hj2.append(times[1][0])
hj8.append(times[2][0])
cg.append(times[3][0])
rand.append(times[4][0])
current = current + 32
out_file = "./optimal_c_value_out.csv"
with open(out_file, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['test', '32', '64', '96','128','160','192','224', '256'])
temp = g500
writer.writerow(['g500', temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7]])
temp = hj2
writer.writerow(['hj2', temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7]])
temp = hj8
writer.writerow(['hj8', temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7]])
temp = cg
writer.writerow(['cg', temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7]])
temp = rand
writer.writerow(['rand', temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7]])
# restore original value in cpp file, pass, and benchmarks
modify_cpp_constant(256)
build_everything()
plot_optimal_c_value_data(out_file)
def rebuild_pass():
do_cmd(['rm', '-rf', 'build'], "./freshAttempt", False)
do_cmd(['mkdir', 'build'], "./freshAttempt", False)
do_cmd(['cmake', '..'], "./freshAttempt/build", False)
do_cmd(['make'], "./freshAttempt/build", False)
def run_all_benchmarks(run_only_standard_prefetch = False):
results = ''
times = []
s, t = run_graph500_benchmark(run_only_standard_prefetch)
results += s
times.append(t)
s, t = run_hj2_benchmark(run_only_standard_prefetch)
results += s
times.append(t)
s, t = run_hj8_benchmark(run_only_standard_prefetch)
results += s
times.append(t)
s, t = run_nas_cg_benchmark(run_only_standard_prefetch)
results += s
times.append(t)
s, t = run_randacc_benchmark(run_only_standard_prefetch)
results += s
times.append(t)
return results, times
def generate_all_ipc():
if os.path.exists("values.txt"):
os.remove("values.txt")
do_cmd(["./generate_ipc.sh"], "./program/graph500")
do_cmd(["./generate_ipc.sh"], "./program/hashjoin-ph-2")
do_cmd(["./generate_ipc.sh"], "./program/hashjoin-ph-8")
do_cmd(["./generate_ipc.sh"], "./program/nas-cg")
do_cmd(["./generate_ipc.sh"], "./program/randacc")
def get_computed_c_val(commands, workdir, regex):
run_benchmark(commands, workdir)
c_val = try_parse_results(commands, workdir, regex)
test_name = workdir.split('/')[-1]
s = '\n\nBenchmark ' + test_name + ' computed c value = ' + str(c_val) + '\n\n'
return s
def print_computed_c_vals():
regex = r"Calculated C Const Value:\s*([0-9]*\.?[0-9]+)"
s = '\n*********************************************************'
s += get_computed_c_val([["./print_computed_c_val.sh"]], "./program/graph500", regex)
s += get_computed_c_val([["./print_computed_c_val.sh"]], "./program/hashjoin-ph-2", regex)
s += get_computed_c_val([["./print_computed_c_val.sh"]], "./program/hashjoin-ph-8", regex)
s += get_computed_c_val([["./print_computed_c_val.sh"]], "./program/nas-cg", regex)
s += get_computed_c_val([["./print_computed_c_val.sh"]], "./program/randacc", regex)
s += '*********************************************************\n'
return s
def build_everything():
generate_all_ipc()
rebuild_pass()
build_benchmarks()
if __name__ == "__main__":
global args
global timestamp
args = parse_args()
libs_exists = os.path.isfile("./freshAttempt/build/swPrefetchPass/SwPrefetchPass.so")
if not libs_exists:
print("!!! Required libs/binaries not found... building them now !!!")
time.sleep(1)
build_everything()
execute = True
while execute:
timestamp = datetime.now().strftime("%m-%d-%y_%I:%M%p")
print("a - run all benchmarks")
print("b - rebuild the pass and all benchmarks")
print("f - find optimal c value")
print("c - print computed c values for benchmarks")
print("1 - run graph500 benchmark")
print("2 - run hashjoin2 benchmark")
print("3 - run hashjoin8 benchmark")
print("4 - run nas-cg benchmark")
print("5 - run randacc benchmark")
print(f"s - toggle benchmark output save (currently {'ON' if args.save else 'OFF'})")
print("e - exit")
user_in = input(">> ")
print("")
results = ''
match user_in:
case 'a':
results, ignore = run_all_benchmarks()
case 'b':
build_everything()
case 'f':
find_optimal_c_value()
case 'c':
results = print_computed_c_vals()
case '1':
results, ignore = run_graph500_benchmark()
case '2':
results, ignore = run_hj2_benchmark()
case '3':
results, ignore = run_hj8_benchmark()
case '4':
results, ignore = run_nas_cg_benchmark()
case '5':
results, ignore = run_randacc_benchmark()
case 's':
args.save = not args.save
case 'e':
execute = False
case _:
print("invalid option!")
print(results)