Search code examples
cruntime-errorkernelopencl

OpenCL RT errors in kernel


How can i find out about the runtime error in kernel code. For example A[0] == 0:

__kernel void eval(__global float* A, __global float* C) {

    C[0] = 3/A[0];

}

After evaluating C[0] == 3

I tried -Werrer like parameter for clBuildProgram, but he finds error only at compilation stage(C[0] = 3/0 -> 1 error generated: division by zero is undefined)


Solution

  • Unfortunately there is no way to have OpenCL serve you runtime errors such as division by zero on a silver platter. A division like 3.0f/A[0] is a perfectly valid operation. It is up to the user to ensure, through algorithm design, that the input A[0] is never zero. For large kernels, this may not be trivial. And if this cannot be ensured, you have to check the results for Inf/NaN and manually throw the error. Alternatively, if A>=0 is distance and A!=0 cannot be ensured, a common trick is to add a tolerance value in the division: 3.0f/(A[0]+1E-9f)

    Same with array out of bounds exception: there is no runtime error, but in this case it's likely that the application will crash. You have to make sure by algorithm design that the array index is always valid.