Hello Guest

OpenCL - float buffer values not the same in OpenCL and Java

  • 2 Replies
  • 711 Views
I create two float buffers like so

Code: [Select]
      int size = 1;
      inputBuffer = BufferUtils.createFloatBuffer(size);
      while (inputBuffer.position() < inputBuffer.capacity()) {
        inputBuffer.put(-1.0f);
      }
      inputBuffer.rewind();
      inputMemory = CL10.clCreateBuffer(clContext, CL10.CL_MEM_READ_WRITE, inputBuffer, clErrorBuffer);

      outputBuffer = BufferUtils.createFloatBuffer(size);
      outputBuffer.rewind();
      outputMemory = CL10.clCreateBuffer(clContext, CL10.CL_MEM_READ_WRITE, outputBuffer, clErrorBuffer);

then in my kernel I have

Code: [Select]
kernel void myKernel(global const float* input, global float* output){
  if(input[0] < 0.0f){
    output[0] = 1;
  } else {
    output[0] = 2;
  }
}

When I retrieve output[0] in java later it is always 2. Any idea why this is? I explicitly set the input buffer to be -1 for all values, and then in the kernel if input[0] is less than 0, which it should be, set output[0] to 1.

*

Kai

Re: OpenCL - float buffer values not the same in OpenCL and Java
« Reply #1 on: June 13, 2017, 22:05:41 »
You are missing the CL_MEM_COPY_HOST_PTR flag for clCreateBuffer (or use clEnqueueWriteBuffer). See the documentation:
https://www.khronos.org/registry/OpenCL/sdk/1.0/docs/man/xhtml/clCreateBuffer.html
That should probably be it. However, it would still be interesting to see how you set the buffer argument to the kernel parameter.
« Last Edit: June 13, 2017, 22:08:54 by Kai »

Re: OpenCL - float buffer values not the same in OpenCL and Java
« Reply #2 on: June 14, 2017, 13:48:40 »
That did the trick! Thanks!

also

Code: [Select]
    clSetKernelArg1p(clKernel, 0, inputMemory);
    clSetKernelArg1p(clKernel, 1, outputMemory);