-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpower.py
71 lines (55 loc) · 2.35 KB
/
gpower.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
import sys
import os
import time
import subprocess
import matplotlib.pyplot as plt
def measure_power_or_temp(executable_with_args):
start_time = time.time()
process = subprocess.Popen(executable_with_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
time_points = []
measurements = []
power_supported = False
label = 'Temperature (C)'
try:
while process.poll() is None:
result = subprocess.run(['nvidia-smi', '--query-gpu=power.draw,temperature.gpu', '--format=csv,noheader,nounits'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = result.stdout.decode('utf-8').strip().split(', ')
power_draw = float(output[0])
temperature = float(output[1])
current_time = time.time() - start_time
if not power_supported and power_draw > 0:
power_supported = True
label = 'Power Draw (W)'
value = power_draw if power_supported else temperature
time_points.append(current_time)
measurements.append(value)
print(f'Time: {current_time:.2f}s, {label}: {value:.2f}')
time.sleep(1)
except KeyboardInterrupt:
process.terminate()
return time_points, measurements, label
def plot_measurements(time_points, measurements, label, output_file):
plt.figure(figsize=(10, 6))
plt.plot(time_points, measurements, label=label)
plt.xlabel('Time (seconds)')
plt.ylabel(label)
plt.title(f'GPU {label} during CUDA Execution')
plt.legend()
plt.grid(True)
plt.savefig(output_file)
plt.close()
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python gpower.py <path_to_cuda_executable> [executable_flags]")
sys.exit(1)
cuda_executable = sys.argv[1]
executable_flags = sys.argv[2:]
if not os.path.isfile(cuda_executable):
print(f"CUDA executable file not found: {cuda_executable}")
sys.exit(1)
executable_with_args = [cuda_executable] + executable_flags
time_points, measurements, label = measure_power_or_temp(executable_with_args)
output_filename = os.path.basename(cuda_executable).replace('.exe', '.jpeg')
plot_measurements(time_points, measurements, label, output_filename)
print(f"GPU {label.lower()} graph saved as: {output_filename}")