的两个实现printsomestuff
最多只能写入 res[7] (即:WORLD!\0
或!ELLOWO\0
)
主.cpp:
...
char A[16] = "HELLO ";
cl::Buffer aBuf(context, CL_MEM_READ_WRITE, sizeof(A));
char B[16] = "WORLD!";
cl::Buffer bBuf(context, CL_MEM_READ_WRITE, sizeof(B));
queue.enqueueWriteBuffer(aBuf, CL_TRUE, 0, sizeof(A) * sizeof(char), A);
queue.enqueueWriteBuffer(bBuf, CL_TRUE, 0, sizeof(B) * sizeof(char), B);
char res[16];
cl::Buffer resBuf(context, CL_MEM_READ_WRITE, sizeof(res) * sizeof(char));
cl::Kernel kernel(program, "printsomestuff");
kernel.setArg(0, aBuf);
kernel.setArg(1, bBuf);
kernel.setArg(2, resBuf);
queue.enqueueTask(kernel);
//queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(32), cl::NullRange);
queue.enqueueReadBuffer(resBuf, CL_TRUE, 0, sizeof(resBuf), res);
std::cout << res;
...
my_kernels.cl
kernel void printsomestuff(__global char *A, __global char *B,
__global char *res) {
/* for (int i = 0; i < 8; i++) {
res[i] = B[i];
res[i + 8] = A[i];
}*/
res[0] = '!';
res[1] = 'E';
res[2] = 'L';
res[3] = 'L';
res[4] = 'O';
res[5] = ' ';
res[6] = 'W';
res[7] = 'O';
res[8] = 'R';
res[9] = 'L';
res[10] = 'D';
res[11] = '!';
res[12] = '\n';
}