为什么此Python脚本在CP​​U中比在GPU中运行更快?

时间:2018-09-09 23:15:47

标签: python parallel-processing gpu self-organizing-maps

我正在使用Python的库somoclu来训练使用Python的自组织地图。该库允许用户在CPU(Intel Core i7-8700)或GPU(GeForce GTX 1080 Ti)上进行培训。

我注意到CPU运行脚本的速度比GPU运行速度快,因此我进行了一次扫描,以改变数据点的数量和映射的大小,以查看GPU在某个时候是否优于CPU。这是脚本:

import numpy as np
import somoclu
import time

m = 3 # Number of dimensions
points = [5000, 30000, 80000, 150000, 300000] # Number of datapoints
iterMax = 200 # Max number of iterations
mapSize = [4, 32, 64, 128] # Dimensions of SOM
np.random.seed(0)
#%% SOM
for n in points:
    for size in mapSize:
        y = np.random.rand(n,m) # Input data
        # With CPU
        t = time.clock() # Start time
        som = somoclu.Somoclu(size,
                              size,
                              compactsupport = False,
                              kerneltype = 0)
        som.train(y.astype(np.float32), epochs = iterMax)
        elapsedTime = time.clock() - t
        # With GPU
        t = time.clock() # Start time
        som = somoclu.Somoclu(size,
                              size,
                              compactsupport = False,
                              kerneltype = 1)
        som.train(y.astype(np.float32), epochs = iterMax)
        elapsedTime = time.clock() - t

我将时间保存为CSV,这就是我得到的:

CPU                 GPU
2.7632589999999997  5.935387999999999
60.340638           82.796062
228.292085          305.75625900000006
861.3243            1141.331934
11.692982999999913  24.568256999999903
330.17140100000006  443.82112400000005
1354.677431         1749.3110039999992
5559.308704         6990.034151000002
29.3726179999976    47.36881999999969
913.3250950000001   1163.5942189999987
3703.653313999999   4615.292857
14868.418703000003  18635.051464000004
37.40133600000263   68.64375999999902
1699.020611         2141.047305
6925.692426000009   8645.564134
27887.844171999997  illegal memory access was encountered

如您所见,CPU在每种情况下都胜过GPU(最重要的是,运行具有150000个数据点和64x64映射的脚本时,GPU版本崩溃了)。这怎么可能?那么,使用GPU训练SOM有什么优势?

编辑:

我在R中尝试了相同的库,并且用这种语言,GPU的性能优于CPU。因此,显然这只是Python问题,但我不是编程专家来弄清楚发生了什么。我相信内核运行是相同的,因此只是接口发生了变化。让我们看看这是否可以帮助人们找到为什么在Python中CPU的运行速度要快于GPU。

1 个答案:

答案 0 :(得分:1)

根据this paper on somoclu中的图5,GPU速度更快。但是,本文没有显示广泛的基准测试。我只能建议对于您的机器,CPU功能更强大。但是您可以研究该论文以进行更相似的测试以进行比较。 enter image description here

  

为了确保结果的可复制性,我们公开进行了基准测试   Amazon Web Services提供的可用集群GPU实例。的   实例类型为cg1.4xlarge   (https://aws.amazon.com/ec2/instance-types/),配备了22 GiB的   内存,两个IntelXeon X5570四核CPU和两个NVIDIA Tesla M2050   运行Ubuntu 12.04的GPU。

     

(16)Somoclu:一种用于自组织地图的高效并行库,可从以下网站获得:   https://www.researchgate.net/publication/236635216_Somoclu_An_Efficient_Parallel_Library_for_Self-Organizing_Maps

您的CPU和GPU似乎都比AWS基准测试功能强大。