參考:
https://blog.csdn.net/TracelessLe/article/details/107405544
====================================================
發現了一個python版本的可以查看和管理NVIDIA顯卡的工具pynvml,安裝方法:
pip install nvidia-ml-py
在這個庫的基礎上自己寫了一個小程序,用來監控GPU的使用情況:
import psutil import pynvml #導包 UNIT = 1024 * 1024 pynvml.nvmlInit() #初始化 gpuDeriveInfo = pynvml.nvmlSystemGetDriverVersion() print("Drive版本: ", str(gpuDeriveInfo, encoding='utf-8')) #顯示驅動信息 gpuDeviceCount = pynvml.nvmlDeviceGetCount()#獲取Nvidia GPU塊數 print("GPU個數:", gpuDeviceCount ) for i in range(gpuDeviceCount): handle = pynvml.nvmlDeviceGetHandleByIndex(i)#獲取GPU i的handle,后續通過handle來處理 memoryInfo = pynvml.nvmlDeviceGetMemoryInfo(handle)#通過handle獲取GPU i的信息 gpuName = str(pynvml.nvmlDeviceGetName(handle), encoding='utf-8') gpuTemperature = pynvml.nvmlDeviceGetTemperature(handle, 0) gpuFanSpeed = pynvml.nvmlDeviceGetFanSpeed(handle) gpuPowerState = pynvml.nvmlDeviceGetPowerState(handle) gpuUtilRate = pynvml.nvmlDeviceGetUtilizationRates(handle).gpu gpuMemoryRate = pynvml.nvmlDeviceGetUtilizationRates(handle).memory print("第 %d 張卡:"%i, "-"*30) print("顯卡名:", gpuName) print("內存總容量:", memoryInfo.total/UNIT, "MB") print("使用容量:", memoryInfo.used/UNIT, "MB") print("剩余容量:", memoryInfo.free/UNIT, "MB") print("顯存空閑率:", memoryInfo.free/memoryInfo.total) print("溫度:", gpuTemperature, "攝氏度") print("風扇速率:", gpuFanSpeed) print("供電水平:", gpuPowerState) print("gpu計算核心滿速使用率:", gpuUtilRate) print("gpu內存讀寫滿速使用率:", gpuMemoryRate) print("內存占用率:", memoryInfo.used/memoryInfo.total) """ # 設置顯卡工作模式 # 設置完顯卡驅動模式后,需要重啟才能生效 # 0 為 WDDM模式,1為TCC 模式 gpuMode = 0 # WDDM gpuMode = 1 # TCC pynvml.nvmlDeviceSetDriverModel(handle, gpuMode) # 很多顯卡不支持設置模式,會報錯 # pynvml.nvml.NVMLError_NotSupported: Not Supported """ # 對pid的gpu消耗進行統計 pidAllInfo = pynvml.nvmlDeviceGetComputeRunningProcesses(handle)#獲取所有GPU上正在運行的進程信息 for pidInfo in pidAllInfo: pidUser = psutil.Process(pidInfo.pid).username() print("進程pid:", pidInfo.pid, "用戶名:", pidUser, "顯存占有:", pidInfo.usedGpuMemory/UNIT, "Mb") # 統計某pid使用的顯存 pynvml.nvmlShutdown() #最后關閉管理工具
輸出:
Drive版本: 470.63.01 GPU個數: 4 第 0 張卡: ------------------------------ 顯卡名: NVIDIA GeForce RTX 3090 內存總容量: 24268.3125 MB 使用容量: 14168.625 MB 剩余容量: 10099.6875 MB 顯存空閑率: 0.41616768780276747 溫度: 59 攝氏度 風扇速率: 51 供電水平: 2 gpu計算核心滿速使用率: 46 gpu內存讀寫滿速使用率: 46 內存占用率: 0.5838323121972325 進程pid: 68028 用戶名: xxxxxx 顯存占有: 14165.0 Mb 第 1 張卡: ------------------------------ 顯卡名: NVIDIA GeForce RTX 3090 內存總容量: 24268.3125 MB 使用容量: 3.625 MB 剩余容量: 24264.6875 MB 顯存空閑率: 0.9998506282626779 溫度: 32 攝氏度 風扇速率: 30 供電水平: 8 gpu計算核心滿速使用率: 0 gpu內存讀寫滿速使用率: 0 內存占用率: 0.0001493717373220738 第 2 張卡: ------------------------------ 顯卡名: NVIDIA GeForce RTX 3090 內存總容量: 24268.3125 MB 使用容量: 3.625 MB 剩余容量: 24264.6875 MB 顯存空閑率: 0.9998506282626779 溫度: 33 攝氏度 風扇速率: 30 供電水平: 8 gpu計算核心滿速使用率: 0 gpu內存讀寫滿速使用率: 0 內存占用率: 0.0001493717373220738 第 3 張卡: ------------------------------ 顯卡名: NVIDIA GeForce RTX 3090 內存總容量: 24268.3125 MB 使用容量: 3.625 MB 剩余容量: 24264.6875 MB 顯存空閑率: 0.9998506282626779 溫度: 33 攝氏度 風扇速率: 30 供電水平: 8 gpu計算核心滿速使用率: 0 gpu內存讀寫滿速使用率: 0 內存占用率: 0.0001493717373220738
注意的是上面代碼需要安裝第三方依賴庫:psutil
相關資料:
[1] NVIDIA官方GPU管理和部署文檔
[2] 【Python管理GPU】pynvml工具的安裝與使用
[3] pypi下的nvidia-ml-py包
https://pythonhosted.org/nvidia-ml-py/
https://github.com/gpuopenanalytics/pynvml
==================================================
