使用GPUtil模块统计GPU的使用
pip install GPUtil
首先安装GPUtil模块
然后载入模块
import GPUtil as GPU
gpus = GPU.getGPUs()
for gpu in gpus:
print('GPU利用率',gpu.memoryUtil)
下面写个小例子,配合visdom一起使用,运行前要先运行
python -m visdom.server
import GPUtil as GPU
import time
import numpy as np
from visdom import Visdom
class GPU_stati():
def __init__(self):
pass
def compute_util(self):
gpu0_Util = 0
gpu1_Util = 0
i_th =0
time_gap =0.99
time_sample =60
while(1):
gpu0 , gpu1 = GPU.getGPUs()
gpu0_Util += gpu0.load#
gpu1_Util += gpu1.load#GPU计算利用率
time.sleep(time_gap)
i_th +=1
if i_th >=time_sample:
break
gpu0_Util = gpu0_Util/time_sample
gpu1_Util = gpu1_Util/time_sample
return gpu0_Util,gpu1_Util
#main
gpus = GPU_stati()
minute = 0
#print(GPU.getGPUs()[1].id)
viz = Visdom(env='gpu_utili')
x0_name = GPU.getGPUs()[0].name+'id_'+str(GPU.getGPUs()[0].id)
x1_name = GPU.getGPUs()[1].name+'id_'+str (GPU.getGPUs()[1].id)
viz.line([[0.,0.]],[0.],win='gpu_utilization',opts=dict(title='gpu_utilization',legend=[x0_name,x1_name]))
while(minute<1440):
x0,x1=gpus.compute_util()
viz.line([[x0,x1]],[minute],win='gpu_utilization',update='append')
minute +=1
下边的代码是GPUtil的作者写的
class GPU:
def __init__(self, ID, uuid, load, memoryTotal, memoryUsed, memoryFree, driver, gpu_name, serial, display_mode, display_active, temp_gpu):
self.id = ID
self.uuid = uuid
self.load = load #GPU计算利用率
self.memoryUtil = float(memoryUsed)/float(memoryTotal)
self.memoryTotal = memoryTotal
self.memoryUsed = memoryUsed
self.memoryFree = memoryFree
self.driver = driver
self.name = gpu_name
self.serial = serial
self.display_mode = display_mode
self.display_active = display_active
self.temperature = temp_gpu #温度