Wierd kernel behavior

Hi,

 I have a kernel of the form
__kernel k()

{

    ...

    int loop_index=0;

while(1)

    {

        if(loop_index==1)

            return;

        ....                     

        ....

        loop_index++;                 //loop_index is not being used anywhere else

    }

}

It gives a CL_OUT_OF_RESOURCES_ERROR and crashes. But if the same kernel I rewrite as

__kernel k()

{

    ...

    int loop_index=0;

while(1)

    {

        ....                     

        ....

        loop_index++;                 //loop_index is not being used anywhere else

if(loop_index==1)

            return;

    }

}

It exits properly. I am trying to debug some unexpected conditions. But debugging variables are acting unexpectedly as well. I am not sure why this is happening? Any ideas? Thanks.

It’s tough to say, but the compiler could be optimizing out the if(loop_index == 1) in the first case…Typically when you get a CL_OUT_OF_RESOURCES error, the GPU has locked up (likely in a forever while loop). Why it would optimize it out? I’m not exactly sure, but you could try using the volatile keyword in front of the loop index declaration.