0
if(tid < size){
    function1();
    __syncthreads(); //In this synchronization, all threads will waiting here or only threads that enter in statement if?
}
__syncthreads();

I know that __syncthreads() will waiting all threads of blocks but what happens if the synchronization is only visible for part of threads?

talonmies
  • 70,661
  • 34
  • 192
  • 269
realbas
  • 3
  • 6

1 Answers1

1

what happens if the synchronization is only visible for part of threads?

In general, this is an illegal use-case for __syncthreads() and this is covered in the documentation:

__syncthreads() is allowed in conditional code but only if the conditional evaluates identically across the entire thread block, otherwise the code execution is likely to hang or produce unintended side effects.

So the result of your proposed usage inside the if statement would be undefined (assuming some threads have a tid that is greater than or equal to size).

There is additional discussion here and your question is arguably a duplicate of that one. I don't wish to quibble about whether or not questions are actually duplicates (dropped threads vs. excluded threads), so I've provided this CW answer linked to that one.

If someone wants to mark this is a duplicate I will delete this answer.

Community
  • 1
  • 1
Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
  • Well if is undefined then some threads will be waiting and depending on what is in the _function1()_ may cause a deadlock (possibly an infinite loop)? – realbas Feb 04 '16 at 19:45
  • 2
    Undefined means you can't predict the behavior and neither can I. I'm not able to answer questions about what to expect when the behavior is undefined. My response is not predicated on any characteristics of `function1()`. The documentation explicitly states that a **hang** (perhaps from a deadlock) is a possible outcome. – Robert Crovella Feb 04 '16 at 19:49