install and use nvidia management library (nvml)


Guide

nvidia-smi

> nvidia-smi
Thu Mar 21 09:41:18 2019       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 396.54                 Driver Version: 396.54                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 1060    Off  | 00000000:01:00.0 Off |                  N/A |
| N/A   65C    P0    30W /  N/A |    538MiB /  6078MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

nvidia-ml-py

This is a wrapper around the NVML library.

Python methods wrap NVML functions, implemented in a C shared library.

Each function’s use is the same with the following exceptions: Instead of returning error codes, failing error codes are raised as Python exceptions.

pip install nvidia-ml-py2 --user
pip install nvidia-ml-py3 --user

demo

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pip install nvidia-ml-py3 --user

import pynvml

try:
    pynvml.nvmlInit()
except pynvml.NVMLError as error:
    print(error) 
    # Driver Not Loaded      驱动加载失败(没装驱动或者驱动有问题)
    # Insufficent Permission 没有以管理员权限运行  pynvml.NVMLError_DriverNotLoaded: Driver Not Loaded
    exit()

try:
    print(pynvml.nvmlDeviceGetCount())
except pynvml.NVMLError as error:
    print(error)

print(pynvml.nvmlDeviceGetCount())# total gpu count = 1
print(pynvml.nvmlSystemGetDriverVersion()) # 396.54

GPU_ID = 0
handle = pynvml.nvmlDeviceGetHandleByIndex(GPU_ID)
print(pynvml.nvmlDeviceGetName(handle)) # GeForce GTX 1060

meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
MB_SIZE = 1024*1024
print(meminfo.total/MB_SIZE) # 6078 MB
print(meminfo.used/MB_SIZE)  # 531 MB
print(meminfo.free/MB_SIZE)  # 5546 MB

pynvml.nvmlShutdown()

Reference

History

  • 20190321: created.

Author: kezunlin
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source kezunlin !
评论
  TOC