【问题标题】:Cupy slower than numpy when iterating through array遍历数组时,Cupy 比 numpy 慢
【发布时间】:2019-12-14 14:44:51
【问题描述】:

我有代码,我想与 cupy 并行化。我认为这很简单——只需写“import cupy as cp”,然后将我写 np. 的所有地方都替换为 cp.,这样就可以了。

而且,它确实有效,代码确实可以运行,但速度要慢得多。我认为在迭代更大的数组时,与 numpy 相比,它最终会更快,但似乎永远不会发生。

代码是:

q = np.zeros((5,5))
q[:,0] = 20

def foo(array):

    result = array
    shedding_row = array*0
    for i in range((array.shape[0])):
        for j in range((array.shape[1])-1):

            shedding_param = 2 * (result[i,j])**.5             
            shedding = (np.random.poisson( (shedding_param), 1))[0]

            if shedding >= result[i,j]:
                shedding = result[i,j] - 1

            result[i,j+1] = result[i,j] - shedding

            if result[i,j+1]<0:
                result[i,j+1] = 0

            shedding_row[i,j+1] = shedding  

    return(result,shedding_row)

x,y = foo(q)

cupy 应该会更快吗?是不是我用错了?

【问题讨论】:

    标签: cupy


    【解决方案1】:

    要获得numpycupy 的快速性能,您应该使用并行操作而不是使用for 循环。

    举个例子

    for i in range((array.shape[0])):
        for j in range((array.shape[1])-1):
    
            shedding_param = 2 * (result[i,j])**.5
    

    这可以计算为

    xp = numpy  # change to cupy for GPU
    shedding_param = 2 * xp.sqrt(result[:, :-1])
    

    【讨论】:

      猜你喜欢
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 2013-10-14
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多