【发布时间】: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