5
$\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$

1 Answer 1

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$
5
  • 1
    $\begingroup$ @nikitablack depends on the hardware and is kinda impossible to extract naively from any marketing information. $\endgroup$ May 26, 2016 at 12:13
  • $\begingroup$ And also a question. Lets's say one thread in a group reads from some buffer in a branch based on that thread's id. Lets's say the buffer size is 1, so only the first thread can read, all other threads will cause out of bounds read. Is it safe? And one more thing - the read from the system memory is very slow, but all threads will read from it, even if they don't need, right? $\endgroup$ May 26, 2016 at 12:16
  • 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$ May 26, 2016 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$ May 26, 2016 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, 2016 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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