This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvidia-ml.py
executable file
·135 lines (115 loc) · 4.67 KB
/
nvidia-ml.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
#!/usr/local/bin python
# coding=utf-8
import optparse, sys, string
from pynvml import *
class OptionClass:
def __init__(self):
self.id = None
self.properties = None
self.number = None
self.summary = None
self.avgGpuUtil = None
self.helpProperties = None
self.parser = None
def parse(self):
option_list = [
optparse.make_option("-i", "--id",
action="store", type="string", dest="id",
help="Specific GPU unit id"),
optparse.make_option("-p", "--properties",
action="store", type="string", dest="properties",
help="Query GPU properties"),
optparse.make_option("--number",
action="store_true", dest="number", help="Number of GPUs"),
optparse.make_option("--summary",
action="store_true", dest="summary", help="Summary list GPUs"),
optparse.make_option("--avg-gpu-util",
action="store_true", dest="avgGpuUtil", help="Average GPU utilization"),
optparse.make_option("--help-properties",
action="store_true", dest="helpProperties", help="Help properties of GPUs"),
]
self.parser = optparse.OptionParser(option_list=option_list)
(options, args) = self.parser.parse_args()
if options.id is not None:
self.id = int(options.id)
if options.properties is not None:
self.properties = options.properties
if options.number is not None:
self.number = options.number
if options.summary is not None:
self.summary = options.summary
if options.avgGpuUtil is not None:
self.avgGpuUtil = options.avgGpuUtil
if options.helpProperties is not None:
self.helpProperties = options.helpProperties
def printHelpProperties(self):
print("--properties=utilization.gpu", "Percent of executing on the GPU")
print("--properties=memory.used", "Percent of used memory on the GPU")
def validate(self):
if self.helpProperties:
self.printHelpProperties()
sys.exit(0)
if self.number or self.summary or self.avgGpuUtil:
pass
return
if self.id is None or self.properties is None:
self.parser.print_help()
sys.exit(1)
class NvmlClass:
def __init__(self):
nvmlInit()
def __del__(self):
nvmlShutdown()
def getDeviceNumber(self):
deviceCount = nvmlDeviceGetCount()
return deviceCount
def getDeviceSummary(self):
summaryList = []
deviceCount = nvmlDeviceGetCount()
for i in range(deviceCount):
handle = nvmlDeviceGetHandleByIndex(i)
name = nvmlDeviceGetName(handle)
uuid = nvmlDeviceGetUUID(handle)
info = {"id":i, "name":name, "uuid": uuid}
summaryList.append(info)
return summaryList
def getDeviceUtilizationGPU(self, id):
handle = nvmlDeviceGetHandleByIndex(int(id))
util = nvmlDeviceGetUtilizationRates(handle)
return int(util.gpu)
def getDeviceUtilizationGPUAvg(self):
deviceCount = nvmlDeviceGetCount()
util_gpu = 0.0
for i in range(deviceCount):
handle = nvmlDeviceGetHandleByIndex(i)
util = nvmlDeviceGetUtilizationRates(handle)
util_gpu += util.gpu
return int(util_gpu / deviceCount)
def getDeviceMemoryUsed(self, id):
handle = nvmlDeviceGetHandleByIndex(int(id))
mem_info = nvmlDeviceGetMemoryInfo(handle)
return int(float(mem_info.used) / float(mem_info.total) * 100)
def main():
option = OptionClass()
option.parse()
option.validate()
nvml = NvmlClass()
if option.number:
deviceCount = nvml.getDeviceNumber()
print("GPU number:%d" % deviceCount)
elif option.summary:
for summary in nvml.getDeviceSummary():
print("GPU %d: %s (UUID: %s)") % \
(summary['id'], summary['name'], summary['uuid'])
elif option.avgGpuUtil:
print("GPU avg util:%d" % nvml.getDeviceUtilizationGPUAvg())
elif option.properties == "utilization.gpu":
print("GPU %d util:%d") % (option.id, nvml.getDeviceUtilizationGPU(option.id))
elif option.properties == "memory.used":
print("GPU %d mem used:%d") % (option.id, nvml.getDeviceMemoryUsed(option.id))
else:
print("Invalid properties:", option.properties)
option.printHelpProperties()
sys.exit(1)
if __name__ == "__main__":
main()