我试图在迭代内部循环数组的同时提高给定距离函数 - c(x,y)的计算速度。我试图使用随机值的cupy和基准测试。到目前为止,我有以下代码:
import time
import contextlib
import cupy as cp
import numpy as np
squared_diff = cp.ElementwiseKernel(
'float64 x, float64 y',
'float64 z',
'z = (x - y) * (x - y)',
'squared_diff')
x, y = np.random.randn(1000), np.random.randn(1000)
x_gpu, y_gpu = cp.random.randn(1000), cp.random.randn(1000)
c = np.zeros((len(x), len(y)))
c_gpu = cp.zeros((len(x), len(y)))
@contextlib.contextmanager
def timer(message):
cp.cuda.Stream.null.synchronize()
start = time.time()
yield
cp.cuda.Stream.null.synchronize()
end = time.time()
print('%s: %f sec' % (message, end - start))
with timer(' CPU '):
for i in range(len(x)):
for j in range(len(y)):
c[i, j] = (x[i] - y[i]) ** 2
with timer(' GPU '):
for i in range(len(x)):
for j in range(len(y)):
c_gpu[i, j] = squared_diff(x_gpu[i], y_gpu[j])
但是,与CPU相比,GPU时间似乎要高得多。
CPU : 0.486763 sec
GPU : 26.627597 sec
在考虑使用CUDA提高计算速度背后的理论时,是否有任何重大提示或问题?
答案 0 :(得分:2)
您需要广播输入数组以使其按元素计算。
def bcast(x, y, xp):
return (xp.broadcast_to(x[:, None], (1000, 1000)),
xp.broadcast_to(y, (1000, 1000)))
x, y = bcast(x, y, np)
with timer(' CPU '):
c = (x - y) ** 2
x_gpu, y_gpu = bcast(x_gpu, y_gpu, cp)
with timer(' GPU '):
c_gpu2 = squared_diff(x_gpu, y_gpu)