both of the implementations of printsomestuff
will only write up to res[7] (i.e.: WORLD!\0
or !ELLOWO\0
)
main.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';
}
sizeof(A)
when A
is a char*
is the size of the pointer, not the length of the array. So, if you pass your char A[16]
to this function:
kernel void printsomestuff(__global char *A, __global char *B,
__global char *res) {
sizeof(A)
will be the size of a pointer which is usually 4 or 8 char
s. It has no knowledge of the length.
You need to provide the size as a separate parameter or use a type created for carrying data of this type, like a std::string
.
If you only have fixed sized char
arrays, you could make some adjustment to your functions to make them preserve the array extent, like this:
template<std::size_t Asize, std::size_t Bsize, std::size_t Ressize>
kernel void printsomestuff(__global char (&A)[Asize], __global char (&B)[Bsize],
__global char (&res)[Ressize]) {
(I don't really know what __global
does but I'll leave it in)
You now have all sizes you need to use in the function.
Demo 2