Upload gpu2.py
Browse files
gpu2.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import wandb
|
4 |
+
import nvidia_smi
|
5 |
+
import os
|
6 |
+
import time
|
7 |
+
import threading
|
8 |
+
import wandb
|
9 |
+
import wandb
|
10 |
+
from datetime import datetime
|
11 |
+
|
12 |
+
# 获取当前日期
|
13 |
+
current_date = datetime.now().strftime("%Y-%m-%d")
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
os.system("pip install nvidia-ml-py3")
|
18 |
+
# WandB登录
|
19 |
+
os.system('wandb login 6cdd066d56acd416b1c1680133e37589511d81f7')
|
20 |
+
nvidia_smi.nvmlInit()
|
21 |
+
# 初始化WandB项目
|
22 |
+
wandb.init(project="GPU-使用率-温度检测", name=f"{current_date}")
|
23 |
+
def monitor_gpu():
|
24 |
+
while True:
|
25 |
+
try:
|
26 |
+
# 获取 GPU 温度
|
27 |
+
handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0) # 0 表示第一个 GPU
|
28 |
+
gpu_temperature = nvidia_smi.nvmlDeviceGetTemperature(handle, nvidia_smi.NVML_TEMPERATURE_GPU)
|
29 |
+
|
30 |
+
# 获取 GPU 使用率
|
31 |
+
utilization = nvidia_smi.nvmlDeviceGetUtilizationRates(handle)
|
32 |
+
gpu_usage = utilization.gpu
|
33 |
+
|
34 |
+
# 使用 WandB 记录 GPU 温度和使用率
|
35 |
+
wandb.log({"GPU 温度": gpu_temperature, "GPU 使用率": gpu_usage})
|
36 |
+
|
37 |
+
except Exception as e:
|
38 |
+
print(f"Error: {e}")
|
39 |
+
|
40 |
+
time.sleep(60)
|
41 |
+
monitor_gpu()
|