OpenCL NDRange用法 - 内核仅更改输出缓冲区的一部分

时间:2018-02-01 10:31:58

标签: c++ opencl

我有一个用三维NDRange调用的内核,但它只改变了部分输出缓冲区。

内核:

__kernel void Kernel2(
 __global const float* K2BufIn,
 __global float* K2BufOut)
{

    int id0 = get_global_id(0);
    int id1 = get_global_id(1);
    int id2 = get_global_id(2);
    int gs0=get_global_size(0);
    int gs1=get_global_size(1);
    int gs2=get_global_size(2);
    int globalThreadNum = id0 + id1 * gs0 + id2 * gs0 * gs1;

    K2BufOut[globalThreadNum] = K2BufIn[globalThreadNum] * K2BufIn[globalThreadNum];

}

这称为如下:

float pK2BufInData[24] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0};
float pK2BufOutData[24] = {};
int sizeK2BufIn = 24;
int sizeK2BufOut = 24;

K2BufIn = cl::Buffer(context_, CL_MEM_READ_WRITE, sizeK2BufIn);
K2BufOut = cl::Buffer(context_, CL_MEM_READ_WRITE, sizeK2BufOut);

commandQ_.enqueueWriteBuffer(K2BufIn, CL_TRUE, 0, sizeK2BufIn, pK2BufInData);

kernel_Kernel2_.setArg(0, K2BufIn);
kernel_Kernel2_.setArg(1, K2BufOut);


commandQ_.enqueueNDRangeKernel(kernel_Kernel2_, cl::NULLRange, cl::NDRange(4,3,2));

commandQ_.enqueueReadBuffer(K2BufOut, CL_TRUE, 0, sizeK2BufOut, pK2BufOutData);

我期望pK2BufOutData包含输入数字的平方。 然而,调试给了我:

(gdb) p *pK2BufOutData@24
$17 = {0 <repeats 16 times>, 289, 324, 361, 400, 441, 484, 529, 576}

这意味着该代码仅适用于最后8个条目。这有什么不对?

1 个答案:

答案 0 :(得分:1)

我在这个主题中找到了答案:Problem with get_global_id(1);

结果是

commandQ_.enqueueNDRangeKernel(kernel_Kernel2_, cl::NULLRange, cl::NDRange(4,3,2));

必须更改为

commandQ_.enqueueNDRangeKernel(kernel_Kernel2_, cl::NDRange(), cl::NDRange(4,3,2));