# issue while using break statement in cuda kernel

**URL:** <https://forums.developer.nvidia.com/t/issue-while-using-break-statement-in-cuda-kernel/19929>\
**Category:** CUDA Programming and Performance\
**Created:** [November 24, 2010, 12:34pm UTC](https://forums.developer.nvidia.com/t/issue-while-using-break-statement-in-cuda-kernel/19929 "2010-11-24T12:34:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![anshu](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@anshu](https://forums.developer.nvidia.com/u/anshu)\
**Post date:** [November 24, 2010, 12:34pm UTC](https://forums.developer.nvidia.com/t/issue-while-using-break-statement-in-cuda-kernel/19929/1 "2010-11-24T12:34:06Z")

</div>

Hello,  
i am having a problem. My kernel has a loop which runs for say 20 times. But for most of the cases I need to break much before 20 times and hence dont need to execute other kernels. But when i use break statement all the values loaded in shared memory becomes wrong and hence i cannot use break statement.  
MY question is what will happen if there is a break statement in a loop in cuda kernel. I can understand that it will cause divergence but why should this affect the final output in terms of results.  
Please Help me explain this.

Regards,  
Mohit

---

<div class="post-metadata">

**Author:** ![anshu](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@anshu](https://forums.developer.nvidia.com/u/anshu)\
**Post date:** [November 24, 2010, 12:34pm UTC](https://forums.developer.nvidia.com/t/issue-while-using-break-statement-in-cuda-kernel/19929/2 "2010-11-24T12:34:06Z")

</div>

Hello,  
i am having a problem. My kernel has a loop which runs for say 20 times. But for most of the cases I need to break much before 20 times and hence dont need to execute other kernels. But when i use break statement all the values loaded in shared memory becomes wrong and hence i cannot use break statement.  
MY question is what will happen if there is a break statement in a loop in cuda kernel. I can understand that it will cause divergence but why should this affect the final output in terms of results.  
Please Help me explain this.

Regards,  
Mohit

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [November 24, 2010, 2:04pm UTC](https://forums.developer.nvidia.com/t/issue-while-using-break-statement-in-cuda-kernel/19929/3 "2010-11-24T14:04:57Z")

</div>

Do you use [font=“Courier New”]\_\_syncthreads()[/font] inside the loop?

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [November 24, 2010, 2:04pm UTC](https://forums.developer.nvidia.com/t/issue-while-using-break-statement-in-cuda-kernel/19929/4 "2010-11-24T14:04:57Z")

</div>

Do you use [font=“Courier New”]\_\_syncthreads()[/font] inside the loop?

---

<div class="post-metadata">

**Author:** ![anshu](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@anshu](https://forums.developer.nvidia.com/u/anshu)\
**Post date:** [November 25, 2010, 1:19am UTC](https://forums.developer.nvidia.com/t/issue-while-using-break-statement-in-cuda-kernel/19929/5 "2010-11-25T01:19:18Z")

</div>

Yes I did, just before the end of the loop.

> [@](#):
>
> Do you use [font=“Courier New”]\_\_syncthreads()[/font] inside the loop?

---

<div class="post-metadata">

**Author:** ![anshu](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@anshu](https://forums.developer.nvidia.com/u/anshu)\
**Post date:** [November 25, 2010, 1:19am UTC](https://forums.developer.nvidia.com/t/issue-while-using-break-statement-in-cuda-kernel/19929/6 "2010-11-25T01:19:18Z")

</div>

Yes I did, just before the end of the loop.

> [@](#):
>
> Do you use [font=“Courier New”]\_\_syncthreads()[/font] inside the loop?

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [November 25, 2010, 5:41am UTC](https://forums.developer.nvidia.com/t/issue-while-using-break-statement-in-cuda-kernel/19929/7 "2010-11-25T05:41:19Z")

</div>

This won’t work, since [font=“Courier New”]\_\_syncthreads()[/font] must be encountered by all threads in the same manner.[sup][1][/sup]  
Instead of using [font=“Courier New”]break[/font] inside the loop, you can set a flag variable and then make the whole loop body excluding the [font=“Courier New”]\_\_syncthreads()[/font] conditional depending of that variable.

[sup][1] It appears that on all current hardware actually only all warps need to encounter them, but lets keep things simple and safe for future hardware as well[/sup].

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [November 25, 2010, 5:41am UTC](https://forums.developer.nvidia.com/t/issue-while-using-break-statement-in-cuda-kernel/19929/8 "2010-11-25T05:41:19Z")

</div>

This won’t work, since [font=“Courier New”]\_\_syncthreads()[/font] must be encountered by all threads in the same manner.[sup][1][/sup]  
Instead of using [font=“Courier New”]break[/font] inside the loop, you can set a flag variable and then make the whole loop body excluding the [font=“Courier New”]\_\_syncthreads()[/font] conditional depending of that variable.

[sup][1] It appears that on all current hardware actually only all warps need to encounter them, but lets keep things simple and safe for future hardware as well[/sup].

---

<div class="post-metadata">

**Author:** ![anshu](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@anshu](https://forums.developer.nvidia.com/u/anshu)\
**Post date:** [November 26, 2010, 8:39am UTC](https://forums.developer.nvidia.com/t/issue-while-using-break-statement-in-cuda-kernel/19929/9 "2010-11-26T08:39:03Z")

</div>

Dear Tera,

Thanks a lot for pointing out this :). This is what was causing all problem.

> [@](#):
>
> This won’t work, since [font=“Courier New”]\_\_syncthreads()[/font] must be encountered by all threads in the same manner.[sup][1][/sup]
> 
> Instead of using [font=“Courier New”]break[/font] inside the loop, you can set a flag variable and then make the whole loop body excluding the [font=“Courier New”]\_\_syncthreads()[/font] conditional depending of that variable.
> 
> [sup][1] It appears that on all current hardware actually only all _warps_ need to encounter them, but lets keep things simple and safe for future hardware as well[/sup].

---

<div class="post-metadata">

**Author:** ![anshu](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@anshu](https://forums.developer.nvidia.com/u/anshu)\
**Post date:** [November 26, 2010, 8:39am UTC](https://forums.developer.nvidia.com/t/issue-while-using-break-statement-in-cuda-kernel/19929/10 "2010-11-26T08:39:03Z")

</div>

Dear Tera,

Thanks a lot for pointing out this :). This is what was causing all problem.

> [@](#):
>
> This won’t work, since [font=“Courier New”]\_\_syncthreads()[/font] must be encountered by all threads in the same manner.[sup][1][/sup]
> 
> Instead of using [font=“Courier New”]break[/font] inside the loop, you can set a flag variable and then make the whole loop body excluding the [font=“Courier New”]\_\_syncthreads()[/font] conditional depending of that variable.
> 
> [sup][1] It appears that on all current hardware actually only all _warps_ need to encounter them, but lets keep things simple and safe for future hardware as well[/sup].

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [November 26, 2010, 12:17pm UTC](https://forums.developer.nvidia.com/t/issue-while-using-break-statement-in-cuda-kernel/19929/11 "2010-11-26T12:17:55Z")

</div>

For threads that have finished their work, just execute the [font=“Courier New”]\_\_syncthreads()[/font] and any data transfers to/from shared memory that may be needed by other threads, but not the rest of the loop body:

```auto
int flag = 1;

    for (int i=0; i<20; i++) {

// load data...

__syncthreads();

        if (flag) {

// do work...

if (finished) {

                // this is where the break statement would have been

                flag = 0;

            }

        }

         __syncthreads();

   }

```

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [November 26, 2010, 12:17pm UTC](https://forums.developer.nvidia.com/t/issue-while-using-break-statement-in-cuda-kernel/19929/12 "2010-11-26T12:17:55Z")

</div>

For threads that have finished their work, just execute the [font=“Courier New”]\_\_syncthreads()[/font] and any data transfers to/from shared memory that may be needed by other threads, but not the rest of the loop body:

```auto
int flag = 1;

    for (int i=0; i<20; i++) {

// load data...

__syncthreads();

        if (flag) {

// do work...

if (finished) {

                // this is where the break statement would have been

                flag = 0;

            }

        }

         __syncthreads();

   }

```
