python查看GPU显存占用情况
python查看GPU显存占用情况
from pynvml import nvmlDeviceGetHandleByIndex, nvmlInit, nvmlDeviceGetCount, nvmlDeviceGetMemoryInfo, nvmlDeviceGetName, \
nvmlDeviceGetTemperature, nvmlShutdown
# 初始化
nvmlInit()
# 获取GPU个数
deviceCount = nvmlDeviceGetCount()
# 总显存
total_memory = 0
# 未用总显存
total_free = 0
# 已用总显存
total_used = 0
# 遍历查看每一个GPU的情况
for i in range(deviceCount):
# 创建句柄
handle = nvmlDeviceGetHandleByIndex(i)
# 获取信息
info = nvmlDeviceGetMemoryInfo(handle)
# 获取gpu名称
gpu_name = nvmlDeviceGetName(handle)
# 查看型号、显存、温度、电源
print("[ GPU{}: {}".format(i, gpu_name), end=" ")
print("总共显存: {}G".format((info.total // 1048576) / 1024), end=" ")
print("空余显存: {}G".format((info.free // 1048576) / 1024), end=" ")
print("已用显存: {}G".format((info.used // 1048576) / 1024), end=" ")
print("显存占用率: {:.2%}".format( info.used / info.total), end=" ")
print("运行温度: {}摄氏度 ]".format(nvmlDeviceGetTemperature(handle, 0)))
total_memory = (info.total // 1048576) / 1024
total_free = (info.free // 1048576) / 1024
total_used = (info.used // 1048576) / 1024
# 打印所有GPU信息
print("显卡名称:[{}],显卡数量:[{}],总共显存;[{}G],空余显存:[{}G],已用显存:[{}G],显存占用率:[{:.2%}]。".format(gpu_name, deviceCount, total_memory,
total_free, total_used,
(total_used / total_memory)))
# 关闭管理工具
nvmlShutdown()
打印为字典格式
from pynvml import nvmlDeviceGetHandleByIndex, nvmlInit, nvmlDeviceGetCount, nvmlDeviceGetMemoryInfo, nvmlDeviceGetName, \
nvmlDeviceGetTemperature, nvmlShutdown
# 初始化
nvmlInit()
# 获取GPU个数
deviceCount = nvmlDeviceGetCount()
all_info = []
# 遍历查看每一个GPU的情况
for i in range(deviceCount):
# 创建句柄
handle = nvmlDeviceGetHandleByIndex(i)
# 获取信息
info = nvmlDeviceGetMemoryInfo(handle)
# 获取gpu名称
gpu_name = nvmlDeviceGetName(handle)
gpu_name_series = gpu_name.split(" ")[-1]
# 查看型号、显存、温度、电源
# print("[ GPU-{}: {}".format(i, gpu_name_series), end=" ")
# print("空余显存: {:.1f}".format((info.free // 1048576) / 1024), end=" ")
# print("已用显存: {:.1f}".format((info.used // 1048576) / 1024), end=" ")
# print("显存占用率: {:.2%}".format( info.used / info.total), end=" ")
# print("温度: {}度 ]".format(nvmlDeviceGetTemperature(handle, 0)))
single_gpu_info ="[ GPU-{}: {}".format(i, gpu_name_series) " " "空余显存: {:.1f}".format((info.free // 1048576) / 1024) " " "已用显存: {:.1f}".format((info.used // 1048576) / 1024) " " "显存占用率: {:.2%}".format( info.used / info.total) " " "温度: {}度 ]".format(nvmlDeviceGetTemperature(handle, 0))
single_gpu_dict = {'index': i,'info':single_gpu_info}
all_info.append(single_gpu_dict)
print(all_info)
# 关闭管理工具
nvmlShutdown()
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgcagjf
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13