如何在Windows中使用python测量每个进程的GPU使用率?

时间:2018-08-06 07:13:31

标签: windows-10 python-3.6 gpu-programming nvml

我想像Windows taskmgr.exe一样测量每个进程的GPU使用率,但是在尝试使用pyNVML库时遇到了几个问题。结果,我有几个问题。

首先,当前是否可以使用Python在Windows中测量每个进程的确切GPU使用率?我已经尝试过from skimage.exposure import cumulative_distribution import matplotlib.pylab as plt import numpy as np def cdf(im): ''' computes the CDF of an image im as 2D numpy ndarray ''' c, b = cumulative_distribution(im) # pad the beginning and ending pixels and their CDF values c = np.insert(c, 0, [0]*b[0]) c = np.append(c, [1]*(255-b[-1])) return c def hist_matching(c, c_t, im): ''' c: CDF of input image computed with the function cdf() c_t: CDF of template image computed with the function cdf() im: input image as 2D numpy ndarray returns the modified pixel values ''' pixels = np.arange(256) # find closest pixel-matches corresponding to the CDF of the input image, given the value of the CDF H of # the template image at the corresponding pixels, s.t. c_t = H(pixels) <=> pixels = H-1(c_t) new_pixels = np.interp(c, c_t, pixels) im = (np.reshape(new_pixels[im.ravel()], im.shape)).astype(np.uint8) return im 查询,但这似乎没有显示每个进程的已用内存和利用率百分比

第二,如果可以使用Python以这种方式测量GPU的使用情况,那么我想以与Windows 10的Windows taskmgr.exe类似的方式进行测量和显示。

到目前为止,这是我的代码:

nvidia-smi

和我的结果:

nvmlInit()
deviceCount = nvmlDeviceGetCount()
#print(deviceCount)
for device_id in range(deviceCount):
    hd = nvmlDeviceGetHandleByIndex(device_id)
    #print(handle)

cps = nvmlDeviceGetGraphicsRunningProcesses(hd)

for ps in cps :
    pp = ps.pid
    #print(pp)
    try :
        name = str(nvmlSystemGetProcessName(ps.pid))
        n = name.split("\\")
        #print(n[len(n)-1][:-1])
        process_name = n[len(n)-1][:-1]
        if process_name == 'chrome.exe':
            print(process_name, pp, ps.usedGpuMemory)
    except:
        pass

nvidia-smi

如您所见,这不会显示每个进程的GPU内存使用情况,但是我需要taskmgr的GPU部分中显示的信息。 (我不需要可视化。)

taskmgr

我的计算机规格是Windows 10专业版,GTX 950,i5-6600

如果目前在Python中这是不可能的,那么您还有其他建议自动收集每个进程的GPU使用情况吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

查看 Jonathan DEKHTIAR Here 的回答,解释了它不起作用的原因。 作为解决方法,您可以尝试从 powershell Get-Counter -Counter "\GPU Engine(*)\Utilization Percentage"

获取值