4
$\begingroup$

I'm working with DirectCompute, but this question can be applied to general gpu programming I suppose.

As far as I know the threads in a group works in a lockstep. It means that every instruction for every thread executes at the same time, right? But what if one thread out of 1024 entered if/else condition? All other 1023 will just wait or lockstep condition will be violated?

$\endgroup$
5
$\begingroup$

Not all threads will execute in lockstep but they are split into groups whose threads are locked to each other.

This means that if only 1 thread out of all threads enters a branch then only 1 group will need to enter that branch while all the others will skip it.

In that group that has to execute both branches it will actually execute both branches but threads will throw away the result of the branch that it didn't need to go into.

For more information see this question.

$\endgroup$
  • $\begingroup$ Thanks. And how big are these groups? $\endgroup$ – nikitablack May 26 '16 at 12:10
  • 1
    $\begingroup$ @nikitablack depends on the hardware and is kinda impossible to extract naively from any marketing information. $\endgroup$ – ratchet freak May 26 '16 at 12:13
  • 1
    $\begingroup$ @nikitablack that's going to depend on a few things; 1) if the driver is smart enough to forward the constant in the test into the then clause, 2) if the hardware can do a "masked read", 3) what exactly happens on out of bounds reads $\endgroup$ – ratchet freak May 26 '16 at 13:12
  • 2
    $\begingroup$ @nikitablack The lockstep group size is 32 for NVIDIA and 64 for AMD (they call them "warps" and "wavefronts" respectively). For Intel the size is variable from 8 to 32, determined by the shader compiler. Also, out-of-bounds reads are defined to just return zero in DirectX, and I'm pretty sure it's the same for GL and the other APIs. $\endgroup$ – Nathan Reed May 26 '16 at 17:13
  • 1
    $\begingroup$ For the reason Nathan just described, you should always make your work group size a multiple of 64 to avoid wasting shader invocations in the last warp / wavefront of each group. If you don't need to use shared variables, exactly 64 is usually a good size. $\endgroup$ – russ May 27 '16 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.