2 Replies Latest reply on Jun 9, 2017 8:04 PM by pangea

    clBuildProgram crashes

    pangea

      Hey,

       

      i was trying to get into OpenCl programming. However the call into clBuildProgram (line 36) fails with an invalid floating point operation and crashes the application. According to the debugger this happens somwhere in a submethod called clSetKernelExecInfo. However this part of the code seems to be part of the OpenCl libary. Is there something i am doing worng or is this an issue of the library? Is there a workaround for this problem?

       

      This is the code so far:

       

      program pOpenClTest;
      
      {$mode objfpc}{$H+}
      {$APPTYPE CONSOLE}
      uses {$IFDEF UNIX} {$IFDEF UseCThreads}
        cthreads, {$ENDIF} {$ENDIF}
        Classes,
        cl,
        ctypes;
      {$R *.res}
      
      var
        platform: cl_platform_id;
        device: cl_device_id;
        context: cl_context;
        prog: cl_program;
      
        num: cl_uint;
        err: cl_int;
      
        str: ansistring;
        ptr: PAnsiChar;
        len: csize_t;
      begin
        clGetPlatformIDs(1, @platform, @num);
        clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, @device, @num);
      
        context := clCreateContext(nil, 1, @device, nil, nil, err);
      
        str := '__kernel void test (__global float* x) { const int i = get_global_id (0); x[i] = i;} ' + #0;
        ptr := @str[1];
        len := Length(str);
      
        prog := clCreateProgramWithSource(context, 1, @ptr, @len, err);
      
        clBuildProgram(prog, 1, @device, nil, nil, nil);
      
        clReleaseProgram(prog);
        clReleaseContext(context);
      
        ReadLn;
      end. 
      

       

      (Im trying to get this running on a Radeon RX480 under Windows 10 64 bit.)

        • Re: clBuildProgram crashes
          dipak

          Hi,

          The above kernel code looks okay to me and I don't see any reason to get the error. I was successfully able to build the kernel using a simple C/C++ wrapper.

          Btw, clSetKernelExecInfo is an OpenCL 2.0 API and I don't see any use of it inside the above code. You may try CodeXL (in "Analyze" mode) to verify the kernel build.

           

          P.S. You've been whitelisted now.

           

          Regards,

            • Re: clBuildProgram crashes
              pangea

              Mmhh, this is interesting. I experimented a bit and observed the following:

               

              - When i run the application while debugging it with CodeXL the error does not appear at all. (Maybe it catches the exception?)

              - Rewriting the code in c++ and compiling it with the microsoft compiler there is also no crash.

               

              This may be a problem of the pascal compiler i used. Thank you!

               

              Edit: Seems like the problem is simply an unhandled floating point exception. Disabling all related exceptions removes the problem and the code works just fine. A little bit surprised that this is not handled somewhere inside the OpenCL implementation.

               

              Setting a exceptionmask via SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]);  avoids the problem.