Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is early exit of loops on GPU worth doing?

We've written GLSL shader code to do ray tracing visualisation using the GPU. It seems to be pretty standard to put an early exit break in the ray marching loop, so if the light is extinguished, the loop breaks.

But from what I know about GPU code, each render will take as long as the longest loop run. So my question is: is it worth the early exit?

e.g.

for(int i = 0; i < MAX_STEPS; i++){
        //Get the voxel intensity value from the 3D texture.    
        dataRGBA = getRGBAfromDataTex(dataTexture, currentPosition, dataShape, textureShape);

        // get contribution from the light
        lightRayPathRGBA = getPathRGBA(currentPosition, light.position, steps, tex); // this is the light absorbed so we need to take 1.0- to get the light transmitted
        lightRayRGBA = (vec4(1.0) - lightRayPathRGBA) * vec4(light.color, light.intensity);

        apparentRGB = (1.0 - accumulatedAlpha) * dataRGBA.rgb * lightRayRGBA.rgb * dataRGBA.a * lightRayRGBA.a;
        //apparentRGB = (1.0 - accumulatedAlpha) * dataRGBA.rgb * dataRGBA.a * lightRayRGBA.a;

        //Perform the composition.
        accumulatedColor += apparentRGB;
        //Store the alpha accumulated so far.
        accumulatedAlpha += dataRGBA.a;

        //Adva      nce the ray.
        currentPosition += deltaDirection;
        accumulatedLength += deltaDirectionLength;

        //If the length traversed is more than the ray length, or if the alpha accumulated reaches 1.0 then exit.
        if(accumulatedLength >= rayLength || accumulatedAlpha >= 1.0 ){
            break;
        }
    }
like image 604
nrob Avatar asked Dec 24 '22 19:12

nrob


1 Answers

The scheduling unit of GPUs is the warp / wavefront. Usually that's consecutive groups of 32 or 64 threads. The execution time of a warp is the maximum of the execution time of all threads within that warp.

So if your early exit can make a whole warp terminate sooner (for example, if threads 0 to 31 all take the early exit), then yes, it is worth it, because the hardware can schedule another warp to execute and that reduces the overall kernel run time. Otherwise, it probably isn't, because even if threads 1 to 31 take the early exit, the warp still occupies the hardware until thread 0 is done.

like image 113
user703016 Avatar answered Jan 03 '23 13:01

user703016