current position:Home>Python uses the pynvml tool to obtain the working status of GPU

Python uses the pynvml tool to obtain the working status of GPU

2022-01-31 04:50:39 Why

This is my participation 11 The fourth of the yuegengwen challenge 7 God , Check out the activity details :2021 One last more challenge

In the process of deep learning, we often need to communicate with GPU Dealing with , It needs to be determined before training GPU Working state . In this paper, python Medium pynvml How to use the package , Used to obtain the above information .

install

Directly through pip install :

pip install nvidia-ml-py
 Copy code 

Or according to the python Install the corresponding package :

pip install nvidia-ml-py2  # python2
pip install nvidia-ml-py3  # python3
 Copy code 

Usage method

#  Import package 
import pynvml 
#  Every time MB Number of bytes contained 
NUM_EXPAND = 1024 * 1024
#  initialization 
pynvml.nvmlInit()
 Copy code 
#GPU Driver information 
pynvml.nvmlSystemGetDriverVersion()
-> b'441.22'
 Copy code 
# obtain Nvidia GPU Number of blocks 
pynvml.nvmlDeviceGetCount()
-> 1
 Copy code 
# obtain GPU 0  Of handle
handle = pynvml.nvmlDeviceGetHandleByIndex(0)
 Copy code 
#  Equipment name 
pynvml.nvmlDeviceGetName(handle)
-> b'GeForce GTX 1660'
 Copy code 
# adopt handle obtain GPU 0  Information about 
info = pynvml.nvmlDeviceGetMemoryInfo(handle)
 Copy code 
#GPU 0  Total video memory 
info.total 
-> 6442450944
info.total / NUM_EXPAND
-> 6144.0
 Copy code 
#GPU 0  Video memory used 
info.used
-> 383299584
info.used / NUM_EXPAND
-> 365.54296875
 Copy code 
# close  pynvml
pynvml.nvmlShutdown() 
 Copy code 

Step on the pit

  • Report errors : pynvml.NVMLError_LibraryNotFound: NVML Shared Library Not Found

The pit has been filled : solve pynvml Report errors NVML Shared Library Not Found / WinError 126

Reference material

copyright notice
author[Why],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310450375824.html

Random recommended