本文共 1759 字,大约阅读时间需要 5 分钟。
可以利用python工具来实现显卡信息的读取与管理
Nvidia的显卡提供了(英伟达显卡管理库)以及构建在其上的(显卡系统管理界面),可以方便的查询显卡的信息和工作状况。在python中同样可以利用pynvml库来实现显卡信息的获取。
可以使用pip
方便的安装:
pip install nvidia-ml-py#也可以根据python版本制定2/3#python2pip install nvidia-ml-py2#python3pip install nvidia-ml-py3#或者使用源码安装#下载链接:http://pypi.python.org/pypi/nvidia-ml-py/sudo python setup.py install#e.g~$ pip install nvidia-ml-py3>>>Collecting nvidia-ml-py3>>> Downloading >>> https://files.pythonhosted.org/packages/6d/64/cce82bddb80c0b0f5c703bbdafa94bfb69a1c5ad7a79cff00b482468f0d3/nvidia-ml-py3-7.352.0.tar.gz>>> Building wheels for collected packages: nvidia-ml-py3>>> Running setup.py bdist_wheel for nvidia-ml-py3 ... done>>> Stored in directory: xxxxxxx/xxxxxx/xxxxxSuccessfully built nvidia-ml-py3Installing collected packages: nvidia-ml-py3Successfully installed nvidia-ml-py3-7.352.0
#简单使用from pynvml import *nvmlInit() #初始化print("Driver: "nvmlSystemGetDriverVersion()) #显示驱动信息#>>> Driver: 384.xxx#查看设备deviceCount = nvmlDeviceGetCount()for i in range(deviceCount): handle = nvmlDeviceGetHandleByIndex(i) print("GPU", i, ":", nvmlDeviceGetName(handle))#>>>#GPU 0 : b'GeForce GTX 1080 Ti'#GPU 1 : b'GeForce GTX 1080 Ti'#查看显存、温度、风扇、电源handle = nvmlDeviceGetHandleByIndex(0)info = nvmlDeviceGetMemoryInfo(handle)print("Memory Total: ",info.total)print("Memory Free: ",info.free)print("Memory Used: ",info.used)print("Temperature is %d C"%nvmlDeviceGetTemperature(handle,0))print("Fan speed is "nvmlDeviceGetFanSpeed(handle))print("Power ststus",nvmlDeviceGetPowerState(handle))#最后要关闭管理工具nvmlShutdown()#nvmlDeviceXXX有一系列函数可以调用,包括了NVML的大多数函数。#具体可以参考:https://docs.nvidia.com/deploy/nvml-api/group__nvmlDeviceQueries.html#group__nvmlDeviceQueries
nvml:
pypi: usage: nvidia_smi: py3status: unsplash: pexels:转载地址:http://owma.baihongyu.com/