# Question about divergence and loops

**URL:** <https://forums.developer.nvidia.com/t/question-about-divergence-and-loops/19874>\
**Category:** CUDA Programming and Performance\
**Created:** [November 20, 2010, 5:11pm UTC](https://forums.developer.nvidia.com/t/question-about-divergence-and-loops/19874 "2010-11-20T17:11:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shinwano](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@shinwano](https://forums.developer.nvidia.com/u/shinwano)\
**Post date:** [November 20, 2010, 5:11pm UTC](https://forums.developer.nvidia.com/t/question-about-divergence-and-loops/19874/1 "2010-11-20T17:11:51Z")

</div>

Hi everyone.  
I Have just started learning about CUDA. I know that a warp (32 threads) is divergent if the control flow is different for any one of those threads. for example, an “if statement” that executes different code depending on the thread index.

My question is:  
suppose i have a “for” loop in my kernel that is dependent on the input data for it’s number of iterations. (i.e. the number of iterations per thread is not the same for all threads in the warp). Would this be considered divergent??

would each thread have to wait, or would they run in parallel?

Thank you

( i am sorry if this question was already posted, i couldn’t find it)

---

<div class="post-metadata">

**Author:** ![shinwano](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@shinwano](https://forums.developer.nvidia.com/u/shinwano)\
**Post date:** [November 20, 2010, 5:11pm UTC](https://forums.developer.nvidia.com/t/question-about-divergence-and-loops/19874/2 "2010-11-20T17:11:51Z")

</div>

Hi everyone.  
I Have just started learning about CUDA. I know that a warp (32 threads) is divergent if the control flow is different for any one of those threads. for example, an “if statement” that executes different code depending on the thread index.

My question is:  
suppose i have a “for” loop in my kernel that is dependent on the input data for it’s number of iterations. (i.e. the number of iterations per thread is not the same for all threads in the warp). Would this be considered divergent??

would each thread have to wait, or would they run in parallel?

Thank you

( i am sorry if this question was already posted, i couldn’t find it)

---

<div class="post-metadata">

**Author:** ![shinwano](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@shinwano](https://forums.developer.nvidia.com/u/shinwano)\
**Post date:** [November 21, 2010, 12:48pm UTC](https://forums.developer.nvidia.com/t/question-about-divergence-and-loops/19874/3 "2010-11-21T12:48:51Z")

</div>

did i just ask a question so stupid that noone would even try to answer it?

---

<div class="post-metadata">

**Author:** ![shinwano](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@shinwano](https://forums.developer.nvidia.com/u/shinwano)\
**Post date:** [November 21, 2010, 12:48pm UTC](https://forums.developer.nvidia.com/t/question-about-divergence-and-loops/19874/4 "2010-11-21T12:48:51Z")

</div>

did i just ask a question so stupid that noone would even try to answer it?

---

<div class="post-metadata">

**Author:** ![seibert](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/seibert/32/520688_2.png) [@seibert](https://forums.developer.nvidia.com/u/seibert)\
**Post date:** [November 21, 2010, 2:21pm UTC](https://forums.developer.nvidia.com/t/question-about-divergence-and-loops/19874/5 "2010-11-21T14:21:29Z")

</div>

Yes, a loop that terminates at different times for threads in the same warp creates divergence. I’m not sure whether the threads that finish the loop first have to wait at the end of the loop for the rest of the threads in their warp. Different warps can get out of sync with no speed penalty.

---

<div class="post-metadata">

**Author:** ![seibert](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/seibert/32/520688_2.png) [@seibert](https://forums.developer.nvidia.com/u/seibert)\
**Post date:** [November 21, 2010, 2:21pm UTC](https://forums.developer.nvidia.com/t/question-about-divergence-and-loops/19874/6 "2010-11-21T14:21:29Z")

</div>

Yes, a loop that terminates at different times for threads in the same warp creates divergence. I’m not sure whether the threads that finish the loop first have to wait at the end of the loop for the rest of the threads in their warp. Different warps can get out of sync with no speed penalty.

---

<div class="post-metadata">

**Author:** ![shinwano](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@shinwano](https://forums.developer.nvidia.com/u/shinwano)\
**Post date:** [November 21, 2010, 2:32pm UTC](https://forums.developer.nvidia.com/t/question-about-divergence-and-loops/19874/7 "2010-11-21T14:32:10Z")

</div>

> [@](#):
>
> Yes, a loop that terminates at different times for threads in the same warp creates divergence. I’m not sure whether the threads that finish the loop first have to wait at the end of the loop for the rest of the threads in their warp. Different warps can get out of sync with no speed penalty.

so the loops will be serialized? or is it just a matter of some loops finishing before others?

if it’s the latter, then there’s no problem, is there?

I could just place a barrier and wait, right?

Thank you for your answer.

---

<div class="post-metadata">

**Author:** ![shinwano](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@shinwano](https://forums.developer.nvidia.com/u/shinwano)\
**Post date:** [November 21, 2010, 2:32pm UTC](https://forums.developer.nvidia.com/t/question-about-divergence-and-loops/19874/8 "2010-11-21T14:32:10Z")

</div>

> [@](#):
>
> Yes, a loop that terminates at different times for threads in the same warp creates divergence. I’m not sure whether the threads that finish the loop first have to wait at the end of the loop for the rest of the threads in their warp. Different warps can get out of sync with no speed penalty.

so the loops will be serialized? or is it just a matter of some loops finishing before others?

if it’s the latter, then there’s no problem, is there?

I could just place a barrier and wait, right?

Thank you for your answer.
