-
Notifications
You must be signed in to change notification settings - Fork 0
/
applyDVFS.py
288 lines (246 loc) · 8.93 KB
/
applyDVFS.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
import subprocess
import sys
import json
CoreLevel = int(sys.argv[1])
MemoryLevel = int(sys.argv[2])
configFile = None
try:
configFile = sys.argv[3]
except:
pass
CoreFreq = []
CoreVoltage = []
MemoryFreq = []
MemoryVoltage = []
def runBashCommand(bashCommand):
"""Runs a bash command
Args:
bashCommand: Command to be run
Returns:
The resulting process
"""
# print("Running %s" % (bashCommand))
seconds = 10
try:
if "3.6" in sys.version:
process = subprocess.run(bashCommand.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
check=True,
timeout=seconds)
return process.stdout.decode("utf-8")
else:
process = subprocess.run(bashCommand.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
text=True,
timeout=seconds)
return process.stdout
except subprocess.CalledProcessError as e:
print()
print("\tERROR: Execution warmup ", str(e.returncode))
print()
except subprocess.TimeoutExpired as t:
print()
print("\tERROR: Timeout warmup")
print()
sys.exit(-1)
def preDVFSconfig():
# Enables Overdrive
result = runBashCommand("rocm-smi --setfan 255")
if "Successfully" not in result:
print("Not able to reset GPU")
sys.exit(-1)
# Enables Overdrive
result = runBashCommand("rocm-smi --autorespond y --setoverdrive 20")
if "Successfully" not in result:
print("Not able to reset GPU")
sys.exit(-1)
result = runBashCommand("rocm-smi --autorespond y --setpoweroverdrive 300")
if "Successfully" not in result:
print("Not able to reset GPU")
sys.exit(-1)
# Set Core performance level to the one to be tested
if setPerformanceLevel("core", 0) == False:
print(" Not able to select core level.")
return False
# Set Memory performance level to the highest
if setPerformanceLevel("mem", 0) == False:
print(" Not able to select memory level.")
return False
return True
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 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
if configFile == None:
with open('configFile.txt') as fp:
line = fp.readline()
i = 0
while line:
line = line.replace("\n", "").split(',')
if i < 8:
CoreFreq.append(int(line[0]))
CoreVoltage.append(int(line[1]))
else:
MemoryFreq.append(int(line[0]))
MemoryVoltage.append(int(line[1]))
line = fp.readline()
i += 1
else:
with open(configFile) as fp:
line = fp.readline()
line = fp.readline()
line = fp.readline()
i = 0
while line:
line = line.replace("\n", "").split(',')
if i < 8:
CoreFreq.append(int(line[0]))
CoreVoltage.append(int(line[1]))
elif i >= 10:
MemoryFreq.append(int(line[0]))
MemoryVoltage.append(int(line[1]))
line = fp.readline()
i += 1
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", '')
print(core + "," + mem)
return CoreFreq.index(int(core)), MemoryFreq.index(int(mem))
def currentVoltageIsRespected(currentVoltCore, currentVoltMemory):
"""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(currentVoltCore)) <= 5:
return True, volt
if abs(int(volt) - int(currentVoltMemory)) <= 5:
return True, volt
return False, volt
if preDVFSconfig() == False:
print("not able to update table for current run")
sys.exit(-1)
if editAllPerformanceLevels() == False:
print("not able to update table for current run")
sys.exit(-1)
# Set Core performance level to the one to be tested
if setPerformanceLevel("core", CoreLevel) == False:
print(" Not able to select core level.")
sys.exit(-1)
# Set Memory performance level to the highest
if setPerformanceLevel("mem", MemoryLevel) == False:
print(" Not able to select memory level.")
sys.exit(-1)
# Run warm up DVFS program
runBashCommand("./warmup")
# Get current DVFS settings - to make sure it was correctly applyed
cur = currentPerfLevel()
print(cur)
print("%s, %s" % (CoreVoltage[cur[0]], MemoryVoltage[cur[1]]))
if cur != (CoreLevel, MemoryLevel):
print(" Selected Performance Levels don't match current ones. %s != (%d, %d)" % (cur, CoreLevel, MemoryLevel))
sys.exit(-1)
# Checks if the intended voltage is correctly applied to the GPU
result, volt = currentVoltageIsRespected(CoreVoltage[CoreLevel], MemoryVoltage[MemoryLevel])
print(result, volt)
if result == False:
print("Current voltage is %d != Core: %d | Memory: %d" % (int(volt), int(CoreVoltage[CoreLevel]), int(MemoryVoltage[MemoryLevel])))
sys.exit(-1)
sys.exit(0)