pip install nvidia-ml-py -i https://pypi.douban.com/simple
使用pynvml相关函数获取gpu资源状态,数量、型号、显存、温度,并存储为txt文件
def get_gpu(simlpe=True):
# Init
nvmlInit()
# get the number of GPU
deviceCount = nvmlDeviceGetCount()
total_memory = 0
total_free = 0
total_used = 0
gpu_name = ""
gpu_num = deviceCount
for i in range(deviceCount):
handle = nvmlDeviceGetHandleByIndex(i)
info = nvmlDeviceGetMemoryInfo(handle)
gpu_name = nvmlDeviceGetName(handle).decode('utf-8')
# 查看型号、显存、温度、电源
if not simlpe:
logging.info(
"GPU{}:{} total memory:{}G free memory:{:.1f}G used memory:{:.1f}G Used Percentage:{:.1f}% Temperature:{}'C".format(
i,
gpu_name,
(info.total // 1048576) / 1024,
(info.free // 1048576) / 1024,
(info.used // 1048576) / 1024,
info.used / info.total *100,
nvmlDeviceGetTemperature(handle, 0),
)
)
total_memory += (info.total // 1048576) / 1024
total_free += (info.free // 1048576) / 1024
total_used += (info.used // 1048576) / 1024
logging.info(
"GPU name:{} number:{} total memory:{:.1f}G free memory:{:.1f}G used memory:{:.1f}G Used Percentage:{:.1f}%".format(
gpu_name,
deviceCount,
total_memory,
total_free,
total_used,
total_used/total_memory*100,
)
)
# shutdown
nvmlShutdown()
GPU0:NVIDIA GeForce RTX 3090 total memory:24.0G free memory:20.0G used memory:4.0G Used Percentage:16.7% Temperature:32'C GPU1:NVIDIA GeForce RTX 3090 total memory:24.0G free memory:23.7G used memory:0.3G Used Percentage:1.3% Temperature:32'C GPU2:NVIDIA GeForce RTX 3090 total memory:24.0G free memory:18.6G used memory:5.4G Used Percentage:22.3% Temperature:47'C GPU3:NVIDIA GeForce RTX 3090 total memory:24.0G free memory:23.7G used memory:0.3G Used Percentage:1.3% Temperature:33'C GPU4:NVIDIA GeForce RTX 3090 total memory:24.0G free memory:23.7G used memory:0.3G Used Percentage:1.3% Temperature:31'C GPU5:NVIDIA GeForce RTX 3090 total memory:24.0G free memory:21.0G used memory:3.0G Used Percentage:12.3% Temperature:31'C GPU6:NVIDIA GeForce RTX 3090 total memory:24.0G free memory:23.7G used memory:0.3G Used Percentage:1.3% Temperature:32'C GPU7:NVIDIA GeForce RTX 3090 total memory:24.0G free memory:23.7G used memory:0.3G Used Percentage:1.3% Temperature:31'C GPU name:NVIDIA GeForce RTX 3090 number:8 total memory:192.0G free memory:178.1G used memory:13.9G Used Percentage:7.2%
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!
