LWJGL Forum

Please login or register.

Login with username, password and session length

Author Topic: Java says "hello" does not equals "hello"  (Read 221 times)

Evan407

  • Newbie
  • *
  • Offline Offline
  • Posts: 42
Java says "hello" does not equals "hello"
« on: August 29, 2017, 09:45:08 »

Code: [Select]
package com.evanstools.opencl.demo;
import com.evanstools.opencl.*;
import java.nio.*;
import java.io.*;
import org.lwjgl.opencl.CLProgramCallback;
import static com.evanstools.opencl.Platform.*;
import static org.lwjgl.opencl.CL10.*;
import static org.lwjgl.opencl.CL11.*;
import static org.lwjgl.opencl.CL12.*;
import static org.lwjgl.BufferUtils.*;
import org.lwjgl.system.MemoryUtil;
import java.util.*;
import org.lwjgl.*;
class Demo{
  static LinkedList<Kernel> kernels;
  public static void main(String[] args){
    if(args.length == 0){System.out.printf("Demo [demo source]%n");return;}
    //import kernels
    LinkedList<String> KERNEL_SOURCE_LIST = new LinkedList<>();
    try{
      for(Scanner scanner = new Scanner(new File(args[0]));scanner.hasNextLine();KERNEL_SOURCE_LIST.add(scanner.nextLine())){}
    }catch(Exception e){e.printStackTrace();}
    final String[] KERNEL_SOURCE = new String[KERNEL_SOURCE_LIST.size()];
    for(int i = 0;i < KERNEL_SOURCE.length;i++)KERNEL_SOURCE[i] = KERNEL_SOURCE_LIST.get(i);
System.out.printf("Kernel Source Code:%n");
for(String s:KERNEL_SOURCE)System.out.printf("%s%n",s);
    IntBuffer errcode_ret = createIntBuffer(1);
    long program = clCreateProgramWithSource(platforms[0].context,KERNEL_SOURCE,errcode_ret);
    System.out.printf("clCreateProgramWithSource errcode_ret = %d %n",errcode_ret.get());
    errcode_ret.rewind();
    check(clBuildProgram(program,
                         platforms[0]._devices,
                         "",//options
                         null,//If pfn_notify is NULL, clBuildProgram does not return until the build has completed.
                         MemoryUtil.NULL),"build");

    check(clCompileProgram(program,
                           platforms[0]._devices,
                           "",//options
                           (PointerBuffer)null,//input header
                           null,//header include name
                           null,//If pfn_notify is NULL, clCompileProgram does not return until the compiler has completed.
                           MemoryUtil.NULL),"compile");

    long programObject = clLinkProgram(platforms[0].context,
                                       null,//platforms[0]._devices,//If device_list is a NULL value, the link is performed for all devices associated with context for which a compiled object is available.
                                       "",//options
                                       program,
                                       (org.lwjgl.opencl.CLProgramCallbackI)null,//If pfn_notify is NULL, clLinkProgram does not return until the linker has completed.
                                       MemoryUtil.NULL);
    //System.out.printf("linked, program object created.%n");
    //kernels
    int[] num_kernels_ret = new int[1];
    clCreateKernelsInProgram(programObject,null,num_kernels_ret);
    PointerBuffer kernelsPointerBuffer = createPointerBuffer(num_kernels_ret[0]);
    check(clCreateKernelsInProgram(programObject,kernelsPointerBuffer,num_kernels_ret),"clCreateKernelsInProgram");
    kernels = new LinkedList<>();
    System.out.printf("KERNELS: ");
    for(int i = 0;i < num_kernels_ret[0];i++){
      long kernel = kernelsPointerBuffer.get();
      PointerBuffer param_value_size_ret = createPointerBuffer(1);
      clGetKernelInfo(kernel,CL_KERNEL_FUNCTION_NAME,(ByteBuffer)null,param_value_size_ret);
      ByteBuffer param_value = createByteBuffer((int)param_value_size_ret.get());
      clGetKernelInfo(kernel,CL_KERNEL_FUNCTION_NAME,param_value,null);
      Kernel k = Kernel.createKernel(kernel, new String(array(param_value)));
      System.out.printf("%s, ",k.name);
      kernels.add(k);
    }
    System.out.printf("%n");
    kernelsPointerBuffer.rewind();
    //index space?
    //OpenCL_Buffer openCL_Buffer = buffer(platforms[0],platforms[0].commandQueues[0],1337 * 4);
    //ByteBuffer bb = openCL_Buffer.buffer;
    //System.out.printf("%s%n",bb.toString());
    //for(int i = 0;i < 1337;i++)bb.putInt(1337);
    //bb.flip();
    //System.out.printf("%d%n",bb.getInt());
    ByteBuffer arg = ByteBuffer.wrap(new String("hi").getBytes());
    long helloKernel = kernel("hello");
    if(helloKernel == -1)helloKernel = kernels.getFirst().kernel;//wtf   
    check(clSetKernelArg(helloKernel,0,arg),"clSetKernelArg");

    PointerBuffer global_work_size = createPointerBuffer(1);
    global_work_size.put(1).flip();
    PointerBuffer local_work_size = createPointerBuffer(1);
    local_work_size.put(1).flip();
    long eventLong = clCreateUserEvent(platforms[0].context,errcode_ret);
    System.out.printf("clCreateUserEvent errcode_ret = %d %n",errcode_ret.get());
    errcode_ret.rewind();
    PointerBuffer event = createPointerBuffer(1);
    event.put(eventLong).flip();
    check(clEnqueueNDRangeKernel( platforms[0].commandQueues[0],
                                  helloKernel,
                                  1,
                                  null,
                                  global_work_size,
                                  local_work_size,
                                  null,
                                  event),"clEnqueueNDRangeKernel");
  }
  static void check(int code, String insight){
    System.out.printf(insight+" ");
    switch(code){
case CL_SUCCESS:System.out.printf("CL_SUCCESS");break;case CL_DEVICE_NOT_FOUND:System.out.printf("CL_DEVICE_NOT_FOUND");break;case CL_DEVICE_NOT_AVAILABLE:System.out.printf("CL_DEVICE_NOT_AVAILABLE");break;case CL_COMPILER_NOT_AVAILABLE:System.out.printf("CL_COMPILER_NOT_AVAILABLE");break;case CL_MEM_OBJECT_ALLOCATION_FAILURE:System.out.printf("CL_MEM_OBJECT_ALLOCATION_FAILURE");break;case CL_OUT_OF_RESOURCES:System.out.printf("CL_OUT_OF_RESOURCES");break;case CL_OUT_OF_HOST_MEMORY:System.out.printf("CL_OUT_OF_HOST_MEMORY");break;case CL_PROFILING_INFO_NOT_AVAILABLE:System.out.printf("CL_PROFILING_INFO_NOT_AVAILABLE");break;case CL_MEM_COPY_OVERLAP:System.out.printf("CL_MEM_COPY_OVERLAP");break;case CL_IMAGE_FORMAT_MISMATCH:System.out.printf("CL_IMAGE_FORMAT_MISMATCH");break;case CL_IMAGE_FORMAT_NOT_SUPPORTED:System.out.printf("CL_IMAGE_FORMAT_NOT_SUPPORTED");break;case CL_BUILD_PROGRAM_FAILURE:System.out.printf("CL_BUILD_PROGRAM_FAILURE");break;case CL_MAP_FAILURE:System.out.printf("CL_MAP_FAILURE");break;case CL_INVALID_VALUE:System.out.printf("CL_INVALID_VALUE");break;case CL_INVALID_DEVICE_TYPE:System.out.printf("CL_INVALID_DEVICE_TYPE");break;case CL_INVALID_PLATFORM:System.out.printf("CL_INVALID_PLATFORM");break;case CL_INVALID_DEVICE:System.out.printf("CL_INVALID_DEVICE");break;case CL_INVALID_CONTEXT:System.out.printf("CL_INVALID_CONTEXT");break;case CL_INVALID_QUEUE_PROPERTIES:System.out.printf("CL_INVALID_QUEUE_PROPERTIES");break;case CL_INVALID_COMMAND_QUEUE:System.out.printf("CL_INVALID_COMMAND_QUEUE");break;case CL_INVALID_HOST_PTR:System.out.printf("CL_INVALID_HOST_PTR");break;case CL_INVALID_MEM_OBJECT:System.out.printf("CL_INVALID_MEM_OBJECT");break;case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR:System.out.printf("CL_INVALID_IMAGE_FORMAT_DESCRIPTOR");break;case CL_INVALID_IMAGE_SIZE:System.out.printf("CL_INVALID_IMAGE_SIZE");break;case CL_INVALID_SAMPLER:System.out.printf("CL_INVALID_SAMPLER");break;case CL_INVALID_BINARY:System.out.printf("CL_INVALID_BINARY");break;case CL_INVALID_BUILD_OPTIONS:System.out.printf("CL_INVALID_BUILD_OPTIONS");break;case CL_INVALID_PROGRAM:System.out.printf("CL_INVALID_PROGRAM");break;case CL_INVALID_PROGRAM_EXECUTABLE:System.out.printf("CL_INVALID_PROGRAM_EXECUTABLE");break;case CL_INVALID_KERNEL_NAME:System.out.printf("CL_INVALID_KERNEL_NAME");break;case CL_INVALID_KERNEL_DEFINITION:System.out.printf("CL_INVALID_KERNEL_DEFINITION");break;case CL_INVALID_KERNEL:System.out.printf("CL_INVALID_KERNEL");break;case CL_INVALID_ARG_INDEX:System.out.printf("CL_INVALID_ARG_INDEX");break;case CL_INVALID_ARG_VALUE:System.out.printf("CL_INVALID_ARG_VALUE");break;case CL_INVALID_ARG_SIZE:System.out.printf("CL_INVALID_ARG_SIZE");break;case CL_INVALID_KERNEL_ARGS:System.out.printf("CL_INVALID_KERNEL_ARGS");break;case CL_INVALID_WORK_DIMENSION:System.out.printf("CL_INVALID_WORK_DIMENSION");break;case CL_INVALID_WORK_GROUP_SIZE:System.out.printf("CL_INVALID_WORK_GROUP_SIZE");break;case CL_INVALID_WORK_ITEM_SIZE:System.out.printf("CL_INVALID_WORK_ITEM_SIZE");break;case CL_INVALID_GLOBAL_OFFSET:System.out.printf("CL_INVALID_GLOBAL_OFFSET");break;case CL_INVALID_EVENT_WAIT_LIST:System.out.printf("CL_INVALID_EVENT_WAIT_LIST");break;case CL_INVALID_EVENT:System.out.printf("CL_INVALID_EVENT");break;case CL_INVALID_OPERATION:System.out.printf("CL_INVALID_OPERATION");break;case CL_INVALID_BUFFER_SIZE:System.out.printf("CL_INVALID_BUFFER_SIZE");break;case CL_INVALID_GLOBAL_WORK_SIZE:System.out.printf("CL_INVALID_GLOBAL_WORK_SIZE");break;
    }
    System.out.printf("%n");
  }
  static byte[] array(ByteBuffer bb){
    byte[] results = new byte[bb.capacity()];
    for(int i = 0;i < results.length;i++)results[i] = bb.get();
    return results;
  }
  static long kernel(String s){
    for(Kernel k:kernels){
      if(k.name.equals(s))return k.kernel;
      else System.out.printf("Java says \"%s\" does not equals \"%s\"%n",k.name,s);
    }
    System.err.printf("Kernel %s not found.%n",s);
    return -1;
  }
}

Quote
$ java -Dorg.lwjgl.util.Debug=true -Dorg.lwjgl.util.DebugLoader=true -cp Dependencies/lwjgl/*:. com.evanstools.opencl.demo.Demo 'com/evanstools/opencl/demo/hello.cl'
Kernel Source Code:
    __kernel void hello(__global char* string)
    {
    string[0] = 'H';
    string[1] = 'e';
    string[2] = 'l';
    string[3] = 'l';
    string[4] = 'o';
    string[5] = ',';
    string[6] = ' ';
    string[7] = 'W';
    string[8] = 'o';
    string[9] = 'r';
    string[10] = 'l';
    string[11] = 'd';
    string[12] = '!';
    string[13] = '\0';
    }
[LWJGL] Version: 3.1.1 build 16
[LWJGL]     OS: Linux v4.10.0-32-generic
[LWJGL]    JRE: 1.8.0_131 amd64
[LWJGL]    JVM: OpenJDK 64-Bit Server VM v25.131-b11 by Oracle Corporation
[LWJGL] Loading library (system): lwjgl
[LWJGL]    Using SharedLibraryLoader...
[LWJGL]    Found at: /tmp/lwjglevan/3.1.1-build-16/liblwjgl.so
[LWJGL]    Loaded from org.lwjgl.librarypath: /tmp/lwjglevan/3.1.1-build-16/liblwjgl.so
[LWJGL] MemoryUtil accessor: MemoryAccessorUnsafe
[LWJGL] Loading library: OpenCL
[LWJGL]    libOpenCL.so not found in org.lwjgl.librarypath=/tmp/lwjglevan/3.1.1-build-16
[LWJGL]    Loaded from java.library.path: /usr/lib/x86_64-linux-gnu/libOpenCL.so
Number of platforms: 1
Platform id: 140682396380944
Number devices: 1
Platform version: OpenCL 1.2 CUDA 8.0.0
[LWJGL] Loading library: jemalloc
[LWJGL]    Using SharedLibraryLoader...
[LWJGL]    Found at: /tmp/lwjglevan/3.1.1-build-16/libjemalloc.so
[LWJGL]    Loaded from org.lwjgl.librarypath: /tmp/lwjglevan/3.1.1-build-16/libjemalloc.so
[LWJGL] MemoryUtil allocator: JEmallocAllocator
clCreateProgramWithSource errcode_ret = 0
build CL_SUCCESS
compile CL_SUCCESS
clCreateKernelsInProgram CL_SUCCESS
KERNELS: hello,
Java says "hello" does not equals "hello"
Kernel hello not found.
clSetKernelArg CL_SUCCESS
clCreateUserEvent errcode_ret = 0
clEnqueueNDRangeKernel CL_SUCCESS

Why isn't my kernel method working? Why does it think it's not equal?
Logged

spasi

  • Administrator
  • Nerdus Imperius
  • *****
  • Offline Offline
  • Posts: 1856
Re: Java says "hello" does not equals "hello"
« Reply #1 on: August 29, 2017, 10:45:14 »

Sounds like you might have included a null-terminator in the String. Check their lengths, they're likely different.

Also, this line is wrong:

ByteBuffer arg = ByteBuffer.wrap(new String("hi").getBytes());

This will create a heap ByteBuffer. LWJGL supports direct ByteBuffers only. See the memASCII/memUTF8/memUTF16 methods in MemoryUtil for ways to serialize a String.

Logged

Evan407

  • Newbie
  • *
  • Offline Offline
  • Posts: 42
Re: Java says "hello" does not equals "hello"
« Reply #2 on: September 01, 2017, 09:11:03 »

Sounds like you might have included a null-terminator in the String. Check their lengths, they're likely different.

Also, this line is wrong:

ByteBuffer arg = ByteBuffer.wrap(new String("hi").getBytes());

This will create a heap ByteBuffer. LWJGL supports direct ByteBuffers only. See the memASCII/memUTF8/memUTF16 methods in MemoryUtil for ways to serialize a String.
You're right they are different length the string has a zero char at the end. The wtf is out of my code now   ;D
Logged