-
Notifications
You must be signed in to change notification settings - Fork 0
/
runBenchmark.py
638 lines (552 loc) · 22.2 KB
/
runBenchmark.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
import argparse
import subprocess
from pathlib import Path
import os
import re
import sys
import json
CoreFreq = []
CoreVoltage = []
MemoryFreq = []
MemoryVoltage = []
def benchmarkCommand(benchmark, folder, levelCore, levelMem, typeEx, explore):
"""Constructs the bash command with default naming scheme
Args:
benchmark: benchmark command.
folder: folder where benchmark is.
Returns:
command - command generated
file - output file name and path
"""
global CoreFreq
global CoreVoltage
global MemoryFreq
global MemoryVoltage
name = benchmark.copy()
name[0] = str(benchmark[0]).replace(str(folder) + '/', '')
name = '_'.join(name)
benchmark = ' '.join(benchmark)
command = str(benchmark)
file = str(folder) + "/Results/" + name + "-" + str(typeEx) + "-" + str(
explore) + "-" + "Core-" + str(levelCore) + "-" + str(
CoreFreq[levelCore]) + '-' + str(
CoreVoltage[levelCore]) + "-Memory-" + str(
levelMem) + '-' + str(MemoryFreq[levelMem]) + '-' + str(
MemoryVoltage[levelMem]) + ".txt"
return command, file
def runBashCommandOutputToFile(bashCommand, filePath, execution):
"""Runs a bash command and outputs the process stdout and stderr to file
Args:
bashCommand: Command to be run
filePath: Path of the output file
Returns:
The resulting process
"""
print("Running %s" % (bashCommand))
output_file = open(filePath, 'a')
output_file.write("\n")
output_file.write("#################################################\n")
output_file.write("Execution: " + str(execution) + "\n")
output_file.write("#################################################\n")
output_file.write("\n")
if "3.6" in sys.version:
process = subprocess.run(bashCommand.split(),
stdout=output_file,
stderr=output_file,
stdin=subprocess.PIPE,
check=True)
return process.stdout.decode("utf-8")
else:
process = subprocess.run(bashCommand.split(),
stdout=output_file,
stderr=output_file,
check=True,
text=True)
return process.stdout
def runBashCommand(bashCommand):
"""Runs a bash command
Args:
bashCommand: Command to be run
Returns:
The resulting process
"""
print("Running %s" % (bashCommand))
if "3.6" in sys.version:
process = subprocess.run(bashCommand.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
check=True)
return process.stdout.decode("utf-8")
else:
process = subprocess.run(bashCommand.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
text=True)
return process.stdout
def runDVFSscript(command):
"""Runs the DVFS script
Args:
command: Command to be run
Returns:
The resulting process
"""
script = "./DVFS " + str(command)
process = runBashCommand(script)
return process
def setPerformanceLevel(source, level):
"""Sets a given performance level for the GPU Core and Memory.
Args:
source: string containing word "core" or "mem"
level: an integer between 0-7 for core and 0-3 memory
Returns:
True - if action is sucessful.
False - not possible to apply configuration.
"""
if source == "core":
assert level in list(range(
0, 8)), "Core Performance Level betwen 0 and 7."
result = runDVFSscript("-P " + str(level))
if "ERROR" in result:
return False
elif source == "mem":
assert level in list(range(
0, 4)), "Core Performance Level betwen 0 and 3."
result = runDVFSscript("-p " + str(level))
if "ERROR" in result:
return False
else:
print("Not valid source used - core or mem")
return False
return True
def setCoreAndMemPerformanceLevel(coreLevel, memoryLevel):
assert coreLevel in list(range(0, 8)), "Core Performance Level betwen 0 and 7."
assert memoryLevel in list(range(0, 4)), "Core Performance Level betwen 0 and 3."
result = runDVFSscript("-P " + str(level))
if "ERROR" in result:
return False
return True
def editPerformanceLevel(source, level, frequency, voltage):
"""Edits a given performance level for the GPU Core and Memory.
Args:
source: string containing word "core" or "mem"
level: an integer between 0-7 for core and 0-3 memory
frequency: an integer indicating the frequency
voltage: an integer indicating the voltage
Returns:
True - if action is sucessful.
False - not possible to apply configuration.
"""
if source == "core":
assert level in list(range(
0, 8)), "Core Performance Level betwen 0 and 7."
result = runDVFSscript("-L " + str(level) + " -F " + str(frequency) +
" -V " + str(voltage))
if "ERROR" in result:
return False
elif source == "mem":
assert level in list(range(
0, 4)), "Core Performance Level betwen 0 and 3."
result = runDVFSscript("-l " + str(level) + " -f " + str(frequency) +
" -v " + str(voltage))
if "ERROR" in result:
return False
else:
print("Not valid source used - core or mem")
return False
return True
def editAllPerformanceLevels():
for level in range(0, 4):
result = editPerformanceLevel("mem", level, MemoryFreq[level], MemoryVoltage[level])
if result == False:
return False
for level in range(0, 8):
result = editPerformanceLevel("core", level, CoreFreq[level], CoreVoltage[level])
if result == False:
return False
return True
def currentPerfLevel():
"""Gets the current applied performance level for the Core and Memory
Returns:
Tuple on the form (core, memory) indicating the current
performance level of the two domains
"""
global CoreFreq
global MemoryFreq
result = runBashCommand("rocm-smi")
core = -1
mem = -1
line = result.split('\n')
line = line[5].split(" ")
# Find indices of Core and Mem frequency
indices = [i for i, s in enumerate(line) if 'Mhz' in s]
core = line[indices[0]].replace("Mhz", '')
mem = line[indices[1]].replace("Mhz", '')
return CoreFreq.index(core), MemoryFreq.index(mem)
def appendCurrentTemp(file):
"""Writes the current GPU Temperature to an file
Args:
file - a path to a filepath.
Returns:
temp - current GPU temperature.
"""
result = runBashCommand("rocm-smi")
line = result.split('\n')[5]
# Find temperature
temp = re.search(r"..\..c", line).group()
print("File ", file)
with open(file, "a+") as benchFile:
benchFile.write("Temperature: " + str(temp.replace("c", " c")) + "\n")
return temp
def currentVoltageIsRespected(currentVolt):
"""Gets the current voltage applied to the GPU Core
Args:
currentVolt - a path to a filepath.
Returns:
True if the current applied voltage respects the
intended one.
"""
result = runBashCommand("rocm-smi --showvoltage --json")
# Find core voltage
try:
volt= json.loads(result)["card1"]["Voltage (mV)"]
except:
print("Not able to get voltage")
return False, -1
if abs(int(volt) - int(currentVolt)) <= 5:
return True, volt
return False, volt
# Parser to collect user given arguments
parser = argparse.ArgumentParser(
prog="exploreDVFS",
description='Run Benchmark and Perform DVFS Exploration')
group = parser.add_mutually_exclusive_group(required=True)
parser.add_argument('-b',
'--benchmark',
metavar='path',
type=str,
help="Name of the benchmark",
required=True,
nargs='+')
parser.add_argument('-r', '--reset',
const=1,
default=0,
action='store_const',
help="Reset the DVFS table between runs")
parser.add_argument('-c',
const=1,
default=0,
action='store_const',
help="Performs exploration on GPU Core")
parser.add_argument('-m',
const=1,
default=0,
action='store_const',
help="Performs exploration on GPU Memory")
group.add_argument('-v',
const=1,
default=0,
help="Performs exploration of voltage",
action='store_const')
group.add_argument('-f',
const=1,
default=0,
help="Performs exploration of frequency",
action='store_const')
parser.add_argument('-lc',
'--levelscore',
help="Performance Levels to be explored on Core",
nargs='+',
type=int,
choices=range(0, 8))
parser.add_argument('-lm',
'--levelsmemory',
help="Performance Levels to be explored on Memory",
nargs='+',
type=int,
choices=range(0, 4))
parser.add_argument('-t',
'--tries',
default=10,
help="Number of times to perform the benchmark",
type=int,
choices=range(0, 51))
parser.add_argument('--config',
metavar='path',
type=str,
help="Config file",
nargs='+')
args = parser.parse_args()
if args.levelscore == None:
args.levelscore = list(range(0, 8))
if args.levelsmemory == None:
args.levelsmemory = list(range(0, 4))
if args.c == 1:
print("Exploration of Core -", end=" ")
if args.m == 1:
print("Exploration of Memory -", end=" ")
if args.v == 1:
print("volt", end=" ")
if args.f == 1:
print("frequency")
print()
# Reset GPU Power Table
result = runBashCommand("rocm-smi -r")
if "Successfully" not in result:
print("Not able to reset GPU")
exit()
# Disable DVFS
result = runBashCommand("rocm-smi --setperflevel manual")
if "Successfully" not in result:
print("Not able to set manual performance level")
exit()
# Set GPU fan to 100%
result = runBashCommand("rocm-smi --setfan 255")
if "Successfully" not in result:
print("Not able to set fan")
exit()
# Enables Overdrive
result = runBashCommand("rocm-smi --autorespond y --setoverdrive 20")
if "Successfully" not in result:
print("Not able to reset GPU")
exit()
result = runBashCommand("rocm-smi --autorespond y --setpoweroverdrive 320")
if "Successfully" not in result:
print("Not able to reset GPU")
exit()
if args.config is not None:
Core = []
Mem = []
# Parse config file
config_file = open(" ".join(args.config), "r")
while True:
line = config_file.readline()
if not line:
break
# Check the type of content of the output file
parseType = re.search(r"(.*?)\=", line)
if parseType is not None:
# Get the information written on the name of the file
if parseType.group(0) == "defaultCore=":
for i in range(0, 8):
outputLine = config_file.readline()
if not outputLine:
break
Core.append([int(n) for n in outputLine.replace('\n', '').split(",")])
# Get the expected output lines
if parseType.group(0) == "defaultMemory=":
for i in range(0, 4):
outputLine = config_file.readline()
if not outputLine:
break
Mem.append([int(n) for n in outputLine.replace('\n', '').split(",")])
for pair in Core:
a, b = pair
CoreFreq.append(str(a))
CoreVoltage.append(str(b))
for pair in Mem:
a, b = pair
MemoryFreq.append(str(a))
MemoryVoltage.append(str(b))
# Set Core performance level to the one to be tested
if setPerformanceLevel("core", 0) == False:
print(" Not able to select core level.")
exit()
# Set Memory performance level to the highest
if setPerformanceLevel("mem", 0) == False:
print(" Not able to select memory level.")
exit()
if editAllPerformanceLevels() == False:
print("not able to update table for current run")
exit()
else:
# Get the current power table
process = runBashCommand("rocm-smi -S")
i = 0
for line in process.split('\n'):
if i > 4 and i < 13:
CoreFreq.append(line.replace(':', '').split()[2].replace("Mhz", ''))
CoreVoltage.append(line.replace(':', '').split()[3].replace("mV", ''))
if i > 13 and i < 18:
MemoryFreq.append(line.replace(':', '').split()[2].replace("Mhz", ''))
MemoryVoltage.append(
line.replace(':', '').split()[3].replace("mV", ''))
i = i + 1
# Checks if the benchmark exists and create a Results folder
folder = str(args.benchmark[0][:args.benchmark[0].rfind('/')])
if not os.path.isdir(folder) or not os.path.isfile(args.benchmark[0]):
print("Benchmark doesn't exist!")
exit()
Path(folder + "/Results").mkdir(parents=True, exist_ok=True)
# Exploration of Core and Memory
if args.c == 1 and args.m == 1:
# Activates intended performance levels
working_core = [0] * 8
for i in args.levelscore:
working_core[i] = [0] * 4
for j in args.levelscore:
working_core[i][j] = 1
while 1 in working:
# Run the benchmark for the proposed levels
for levels in args.levelscore:
# Check if level is still giving valid results
if working[levels] == 0:
continue
# Set Core performance level to the one to be tested
if setPerformanceLevel("core", int(levels)) == False:
working[levels] = 0
continue
# Set Memory performance level to the highest
if setPerformanceLevel("mem", 3) == False:
working[levels] = 0
continue
# Get current DVFS settings - to make sure it was correctly applyed
if currentPerfLevel() != (int(levels), 3):
working[levels] = 0
continue
# Run the benchmark multiple times
for i in range(0, args.tries):
# Command to be launch
if args.v == 1:
commandBenchmark, fileBenchmark = benchmarkCommand(
args.benchmark, folder, levels, 3, "CoreExploration",
"Voltage")
else:
commandBenchmark, fileBenchmark = benchmarkCommand(
args.benchmark, folder, levels, 3, "CoreExploration",
"Frequency")
# Run the benchmark
runBashCommandOutputToFile(commandBenchmark, fileBenchmark, i)
# Write GPU Temp to end of output file
appendCurrentTemp(fileBenchmark)
if args.v == 1:
# Undervolt Core by 10mV
CoreVoltage = [int(volt) - 10 for volt in CoreVoltage]
else:
# Overclock all levels Core by 10Hz
CoreFreq = [int(volt) + 10 for volt in CoreFreq]
# Apply new Power Table Settings
for levels in range(0, 8):
if setPerformanceLevel("core", levels) == False:
working[levels] = 0
# Exploration of Core
elif args.c == 1:
# Activates intended performance levels
working = [0] * 8
last = [0] * 8
for i in args.levelscore:
working[i] = 1
# Run the benchmark for the proposed levels
for levels in args.levelscore:
# Run the benchmark multiple times
for i in range(0, args.tries):
# Places PowerPlay Table to current values
if args.reset == 1:
if editAllPerformanceLevels() == False:
print("not able to update table for current run")
continue
# Set Core performance level to the one to be tested
if setPerformanceLevel("core", int(levels)) == False:
print(" Not able to select core level.")
continue
# Set Memory performance level to the highest
if setPerformanceLevel("mem", 3) == False:
print(" Not able to select memory level.")
continue
runBashCommand("./warm_up_GPU 2")
# Get current DVFS settings - to make sure it was correctly applyed
cur = currentPerfLevel()
if cur != (int(levels), 3):
print(" Selected Performance Levels don't match current ones. %s != (%d, 3)" % (cur, int(levels)))
continue
# Command to be launch
if args.v == 1:
commandBenchmark, fileBenchmark = benchmarkCommand(
args.benchmark, folder, levels, 3, "CoreExploration",
"Voltage")
# Checks if the intended voltage is correctly applied to the GPU
result, volt = currentVoltageIsRespected(CoreVoltage[int(levels)])
print(result, volt)
if result == False:
print("Current voltage is %d != %d" % (int(volt), int(levels)))
continue
else:
commandBenchmark, fileBenchmark = benchmarkCommand(
args.benchmark, folder, levels, 3, "CoreExploration",
"Frequency")
# Run the benchmark
runBashCommandOutputToFile(commandBenchmark, fileBenchmark, i)
# Write GPU Temp to end of output file
appendCurrentTemp(fileBenchmark)
# Exploration of Memory
elif args.m == 1:
# Activates intended performance levels
working = [0] * 4
last = [0] * 4
for i in args.levelsmemory:
working[i] = 1
while 1 in working:
# Run the benchmark for the proposed levels
for levels in args.levelsmemory:
# Check if level is still giving valid results
if working[levels] == 0:
continue
# Run the benchmark multiple times
for i in range(0, args.tries):
# Places PowerPlay Table to current values
if args.reset == 1:
if editAllPerformanceLevels() == False:
print("not able to update table for current run")
continue
# Set Core performance level to the one to be tested
if setPerformanceLevel("core", 7) == False:
print(" Not able to select core level.")
continue
# Set Memory performance level to the highest
if setPerformanceLevel("mem", int(levels)) == False:
print(" Not able to select memory level.")
continue
# Get current DVFS settings - to make sure it was correctly applyed
cur = currentPerfLevel()
if cur != (7, int(levels)):
print(" Selected Performance Levels don't match current ones. %s != (7, %d)" % (cur, int(levels)))
continue
# Command to be launch
if args.v == 1:
commandBenchmark, fileBenchmark = benchmarkCommand(
args.benchmark, folder, 7, levels, "MemoryExploration",
"Voltage")
else:
commandBenchmark, fileBenchmark = benchmarkCommand(
args.benchmark, folder, 7, levels, "MemoryExploration",
"Frequency")
# Run the benchmark
runBashCommandOutputToFile(commandBenchmark, fileBenchmark, i)
# Write GPU Temp to end of output file
appendCurrentTemp(fileBenchmark)
if args.v == 1:
# Undervolt Memory by 10mV
for editLevel in range(0,4):
if int(MemoryVoltage[editLevel]) - 10 > 810:
MemoryVoltage[editLevel] = int(MemoryVoltage[editLevel]) - 10
else:
MemoryVoltage[editLevel] = 800 + editLevel * 2
if last[editLevel] == 1:
working[editLevel] = 0
last[editLevel] = 1
else:
# Overclock Memory by 10Hz
MemoryFreq = [int(freq) + 10 for freq in MemoryFreq]
# Apply new Power Table Settings
for levels in range(0, 4):
if editPerformanceLevel("mem", levels, MemoryFreq[levels], MemoryVoltage[levels]) == False:
print("Failed to update level %d" % (levels))
working[levels] = 0
else:
print("No indication of exploration given [v:voltage, f:frequency]")
# GPU fan automatic
result = runBashCommand("rocm-smi --resetfans")
if "Successfully" not in result:
print("Not able to set fan")
exit()