我试图从一般的pyOpenCL和GPGPU开始。pyOpenCL获得与numpy相比的不同结果
对于下面的dot产品代码,我得到了GPU和CPU版本之间相当不同的结果。我究竟做错了什么?
对于浮点错误来说,约0.5%的差异似乎很大,以解决差异。数组大小(〜9e-8相对于数组大小10000)的差异似乎增加了。也许这是跨块合并结果的问题......?无论哪种方式,让我感到不安。
我不知道它的问题:我在MacBook Air上,英特尔(R)酷睿(TM)i7-4650U CPU @ 1.70GHz运行此,与英特尔高清显卡5000
谢谢提前。
import pyopencl as cl
import numpy
from pyopencl.reduction import ReductionKernel
import pyopencl.clrandom as cl_rand
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
dot = ReductionKernel(ctx, \
dtype_out = numpy.float32, \
neutral = "0", \
map_expr = "x[i]*y[i]", \
reduce_expr = "a+b", \
arguments = "__global const float *x, __global const float *y"
)
x = cl_rand.rand(queue, 100000000, dtype = numpy.float32)
y = cl_rand.rand(queue, 100000000, dtype = numpy.float32)
x_dot_y = dot(x,y).get() # GPU: array(25001304.0, dtype=float32)
x_dot_y_cpu = numpy.dot(x.get(), y.get()) # CPU: 24875690.0
print abs(x_dot_y_cpu - x_dot_y)/x_dot_y # 0.0050496689740063489
谢谢@jprice!我接受这个答案。看起来像使用pyOpenCL/pyCuda为繁重的另一个原因! – rairan 2014-10-01 18:43:48
仅供参考 - 我在MATLAB中运行了一个类似的测试,除了无法使用GPU实现,因为AFAIK是基于Cuda的,我的GPU不支持Cuda。这里是结果: – rairan 2014-10-01 19:15:15
NAIVE:\t 1.4725e + 04 MATLAB:\t 1.4725e + 04(3.8913e-14 diff) – rairan 2014-10-01 19:15:55