0

我正在尝试用 pyopencl 对 2 个数组求和,但我在输出中得到奇怪的数字。

代码:

def sum_arrays_with_cl(array1, array2):
"""
    Sums 2 arrays with GPU. 
"""
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
a_array = numpy.array(array1)
b_array = numpy.array(array2)
a_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a_array)
b_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b_array)
dest_buf = cl.Buffer(ctx, mf.WRITE_ONLY, b_array.nbytes)
prg = cl.Program(ctx, """
__kernel void sum(__global const float *a,
__global const float *b, __global float *res_g)
{
  int gid = get_global_id(0);
  res_g[gid] = a[gid] + b[gid];
}
""").build()
prg.sum(queue, a_array.shape, None, a_buf, b_buf, dest_buf)
a_plus_b = numpy.empty_like(a_array)
cl.enqueue_copy(queue, a_plus_b, dest_buf).wait()
return list(a_plus_b)

a = [1 代表范围 (10) 中的虚拟对象] b = [i 代表范围 (10) 中的 i]

打印 sum_arrays_with_cl(a,b)

输出:

[0, 0, 0, 0, 0, 5, 6, 7, 8, 9]

我做错了什么?

4

1 回答 1

2

您需要明确阵列的类型,否则在主机上创建的阵列将与设备期望的不匹配。由于您的内核需要 32 位浮点数据,您可以像这样创建数组:

a_array = numpy.array(array1).astype(numpy.float32)
b_array = numpy.array(array2).astype(numpy.float32)
于 2016-03-09T13:57:13.300 回答