WordCount.java
char i[] = new char[]{'a','b','c','d','e'};
char o[] = new char[i.length];
Pointer input = Pointer.to(i);
Pointer output = Pointer.to(o);
cl_mem memObjects[] = new cl_mem[2];
memObjects[0] = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, Sizeof.cl_char * (i.length), input, null);
memObjects[1] = clCreateBuffer(context, CL_MEM_READ_WRITE, Sizeof.cl_char * o.length, output, null);
<GETTING ALL PLATFORM AND DEVICE INFO>
String pr = utils.readKernelFile("kernel.cl");
cl_program program = clCreateProgramWithSource(context, 1, new String[]{ pr }, null, null);
clBuildProgram(program, 0, null, null, null, null);
cl_kernel kernel = clCreateKernel(program, "wordCount", null);
clEnqueueWriteBuffer(commandQueue, memObjects[0], CL_TRUE, 0, i.length * Sizeof.cl_char, input, 0, null, null);
clEnqueueWriteBuffer(commandQueue, memObjects[1], CL_TRUE, 0, o.length * Sizeof.cl_char, output, 0, null, null);
clSetKernelArg(kernel, 0, Sizeof.cl_mem, Pointer.to(memObjects[0]));
clSetKernelArg(kernel, 1, Sizeof.cl_mem, Pointer.to(memObjects[1]));
long global_work_size[] = new long[]{5};
long local_work_size[] = new long[]{1};
clEnqueueNDRangeKernel(commandQueue, kernel, 1, null, global_work_size, local_work_size, 0, null, null);
clEnqueueReadBuffer(commandQueue, memObjects[1], CL_TRUE, 0, o.length * Sizeof.cl_char, output, 0, null, null);
<RELEASING RESOURCES>
System.out.println("
Output text " + Arrays.toString(o)); // It is different from input
kernel.cl file
__kernel void wordCount(__global const char *i,__global char *o)
{
const int index = get_global_id(0);
barrier(CLK_GLOBAL_MEM_FENCE);
o[index] = i[index];
}
This is all my code. I think the error is the difference of char’s length.