ACCA225 commited on
Commit
b6b010a
1 Parent(s): 84e7582

Create gpu.py

Browse files
Files changed (1) hide show
  1. gpu.py +34 -0
gpu.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import wandb
4
+ import nvidia_smi
5
+ import os
6
+ import time
7
+ import wandb
8
+ os.system("pip install nvidia-ml-py3")
9
+ # WandB登录
10
+ os.system('wandb login 6cdd066d56acd416b1c1680133e37589511d81f7')
11
+ nvidia_smi.nvmlInit()
12
+ # 初始化WandB项目
13
+ wandb.init(project="GPU-使用率-温度检测")
14
+ def monitor_gpu():
15
+ start_thread = threading.Thread(target=start)
16
+ start_thread.start()
17
+ while True:
18
+ try:
19
+ # 获取 GPU 温度
20
+ handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0) # 0 表示第一个 GPU
21
+ gpu_temperature = nvidia_smi.nvmlDeviceGetTemperature(handle, nvidia_smi.NVML_TEMPERATURE_GPU)
22
+
23
+ # 获取 GPU 使用率
24
+ utilization = nvidia_smi.nvmlDeviceGetUtilizationRates(handle)
25
+ gpu_usage = utilization.gpu
26
+
27
+ # 使用 WandB 记录 GPU 温度和使用率
28
+ wandb.log({"GPU 温度": gpu_temperature, "GPU 使用率": gpu_usage})
29
+
30
+ except Exception as e:
31
+ print(f"Error: {e}")
32
+
33
+ time.sleep(60)
34
+ monitor_gpu()