-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstart_server_gpu_monitor.py
More file actions
38 lines (31 loc) · 1.05 KB
/
Copy pathstart_server_gpu_monitor.py
File metadata and controls
38 lines (31 loc) · 1.05 KB
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
from flask import Flask, jsonify
import pynvml
app = Flask(__name__)
def get_gpu_info():
pynvml.nvmlInit()
device_count = pynvml.nvmlDeviceGetCount()
gpu_info = []
for i in range(device_count):
handle = pynvml.nvmlDeviceGetHandleByIndex(i)
name = pynvml.nvmlDeviceGetName(handle)
if isinstance(name, bytes):
name = name.decode('utf-8')
utilization = pynvml.nvmlDeviceGetUtilizationRates(handle)
memory_info = pynvml.nvmlDeviceGetMemoryInfo(handle)
gpu_info.append(
{
"gpu_id": i,
"name": name,
"gpu_utilization": utilization.gpu,
"memory_utilization": utilization.memory,
"memory_used": memory_info.used // 1024 ** 2,
"memory_total": memory_info.total // 1024 ** 2
}
)
pynvml.nvmlShutdown()
return gpu_info
@app.route('/gpu_info', methods=['GET'])
def gpu():
return jsonify(get_gpu_info())
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)