# CUDA program issue, for loop

**URL:** <https://forums.developer.nvidia.com/t/cuda-program-issue-for-loop/305652>\
**Category:** CUDA Programming and Performance\
**Tags:** cuda\
**Created:** [September 4, 2024, 11:36am UTC](https://forums.developer.nvidia.com/t/cuda-program-issue-for-loop/305652 "2024-09-04T11:36:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![baishufan926](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@baishufan926](https://forums.developer.nvidia.com/u/baishufan926)\
**Post date:** [September 4, 2024, 11:36am UTC](https://forums.developer.nvidia.com/t/cuda-program-issue-for-loop/305652/1 "2024-09-04T11:36:36Z")

</div>

I met a trouble while using for loop in cuda kernel programming.  
I have already use 3-dim index parallel the problem, and still need some for loop within each thread.  
And the for loop counter went nuts driven me also.  
Here is a simpler kernel, which does not run as i wish, hope you can teach me some fundamental i missed!

**global** void update\_theta\_kernel(volatile float \*Z,volatile float \*beta,volatile float \*theta,  
int SampleSize  
){

```
    float priv_theta_sth = 0.0f;

    __syncthreads();
    for (int i = 0; i < 3;i+=1){

        printf("priv_theta = %d in loop i = %u\n",priv_theta_sth,i);
        printf("loop i = %u\n",i);

} 
__syncthreads();

```

}

the first “printf” do not update the counter, while the second does, I dont know why

---

<div class="post-metadata">

**Author:** ![Curefab](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Curefab](https://forums.developer.nvidia.com/u/Curefab)\
**Post date:** [September 4, 2024, 11:41am UTC](https://forums.developer.nvidia.com/t/cuda-program-issue-for-loop/305652/2 "2024-09-04T11:41:14Z")

</div>

What do you mean, it does not update it? The update of the counter 0-\>1 happens after the first iteration and after all following iterations. In the first iteration the counter is 0. That is how for loops work, nothing Cuda specific about it.

---

<div class="post-metadata">

**Author:** ![baishufan926](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@baishufan926](https://forums.developer.nvidia.com/u/baishufan926)\
**Post date:** [September 4, 2024, 11:44am UTC](https://forums.developer.nvidia.com/t/cuda-program-issue-for-loop/305652/3 "2024-09-04T11:44:35Z")

</div>

priv\_theta = 0 in loop i = 0  
priv\_theta = 0 in loop i = 0  
priv\_theta = 0 in loop i = 0  
priv\_theta = 0 in loop i = 0  
priv\_theta = 0 in loop i = 0  
priv\_theta = 0 in loop i = 0  
priv\_theta = 0 in loop i = 0  
priv\_theta = 0 in loop i = 0  
loop i = 1  
loop i = 1  
loop i = 1  
loop i = 1  
loop i = 1  
loop i = 1  
loop i = 1  
loop i = 1  
priv\_theta = 0 in loop i = 0  
priv\_theta = 0 in loop i = 0  
priv\_theta = 0 in loop i = 0  
priv\_theta = 0 in loop i = 0  
priv\_theta = 0 in loop i = 0  
priv\_theta = 0 in loop i = 0  
priv\_theta = 0 in loop i = 0  
priv\_theta = 0 in loop i = 0  
loop i = 2  
loop i = 2  
loop i = 2  
loop i = 2  
loop i = 2  
loop i = 2  
loop i = 2  
loop i = 2  
the outcome were like this

---

<div class="post-metadata">

**Author:** ![Curefab](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Curefab](https://forums.developer.nvidia.com/u/Curefab)\
**Post date:** [September 4, 2024, 11:47am UTC](https://forums.developer.nvidia.com/t/cuda-program-issue-for-loop/305652/4 "2024-09-04T11:47:38Z")

</div>

Perhaps also output threadIdx and blockIdx with each line to know, from which thread they are coming from. Each thread has its own counter.

I understand your issue is with `loop i = 0`.

Could you run just one block and one thread? Could you use the correct %d instead of %u?

---

<div class="post-metadata">

**Author:** ![baishufan926](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@baishufan926](https://forums.developer.nvidia.com/u/baishufan926)\
**Post date:** [September 4, 2024, 11:50am UTC](https://forums.developer.nvidia.com/t/cuda-program-issue-for-loop/305652/5 "2024-09-04T11:50:48Z")

</div>

yeah, i tired exactly what you said before. But in this case, apparently, none of the threads come up with the right answer

---

<div class="post-metadata">

**Author:** ![baishufan926](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@baishufan926](https://forums.developer.nvidia.com/u/baishufan926)\
**Post date:** [September 4, 2024, 11:56am UTC](https://forums.developer.nvidia.com/t/cuda-program-issue-for-loop/305652/6 "2024-09-04T11:56:35Z")

</div>

%d gives the same anser, one block one thread may not help with acceleration

---

<div class="post-metadata">

**Author:** ![Curefab](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Curefab](https://forums.developer.nvidia.com/u/Curefab)\
**Post date:** [September 4, 2024, 12:03pm UTC](https://forums.developer.nvidia.com/t/cuda-program-issue-for-loop/305652/7 "2024-09-04T12:03:35Z")

</div>

> [@baishufan926](#):
>
> one block one thread may not help with acceleration

Yes, but the first question is, what is going wrong. If there is a bug.

For the float value you need %f. That could damage the remaining line of printf.

---

<div class="post-metadata">

**Author:** ![baishufan926](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@baishufan926](https://forums.developer.nvidia.com/u/baishufan926)\
**Post date:** [September 4, 2024, 12:08pm UTC](https://forums.developer.nvidia.com/t/cuda-program-issue-for-loop/305652/8 "2024-09-04T12:08:32Z")

</div>

Wow, %f solves this issue!

---

<div class="post-metadata">

**Author:** ![Curefab](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Curefab](https://forums.developer.nvidia.com/u/Curefab)\
**Post date:** [September 4, 2024, 12:10pm UTC](https://forums.developer.nvidia.com/t/cuda-program-issue-for-loop/305652/9 "2024-09-04T12:10:00Z")

</div>

Sadly, there is no compile error.  
The parameters are passed and read differently, depending on type.

---

<div class="post-metadata">

**Author:** ![baishufan926](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@baishufan926](https://forums.developer.nvidia.com/u/baishufan926)\
**Post date:** [September 4, 2024, 12:15pm UTC](https://forums.developer.nvidia.com/t/cuda-program-issue-for-loop/305652/10 "2024-09-04T12:15:43Z")

</div>

thanks, that’s really inspiring. The big issue i met occures when i use the counter to index some long matrix, and the counter ovorflow, the ptr fly away. Do you have any ideas on that? Or maybe i would start a new issue later.

---

<div class="post-metadata">

**Author:** ![Curefab](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Curefab](https://forums.developer.nvidia.com/u/Curefab)\
**Post date:** [September 4, 2024, 12:56pm UTC](https://forums.developer.nvidia.com/t/cuda-program-issue-for-loop/305652/11 "2024-09-04T12:56:33Z")

</div>

I would start a new issue. Perhaps with the fixed printf you already find out, what went wrong.
