# strange problem cuda

**URL:** <https://forums.developer.nvidia.com/t/strange-problem-cuda/12344>\
**Category:** CUDA Programming and Performance\
**Created:** [September 23, 2009, 12:02pm UTC](https://forums.developer.nvidia.com/t/strange-problem-cuda/12344 "2009-09-23T12:02:13Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cherishcc](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cherishcc](https://forums.developer.nvidia.com/u/cherishcc)\
**Post date:** [September 23, 2009, 12:02pm UTC](https://forums.developer.nvidia.com/t/strange-problem-cuda/12344/1 "2009-09-23T12:02:13Z")

</div>

Here is a part of the cuda code:

int i=blockIdx.x\*blockDim.x+threadIdx.x;  
int is=0;  
if(i\<n)  
{  
if(poids[i]!=0) is=is+1;  
}  
\*set\_is=is;

It is very strange that when I copy the value of the set\_is to the host, it always equals to 1;

---

<div class="post-metadata">

**Author:** ![Letharion](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Letharion](https://forums.developer.nvidia.com/u/Letharion)\
**Post date:** [September 23, 2009, 12:41pm UTC](https://forums.developer.nvidia.com/t/strange-problem-cuda/12344/2 "2009-09-23T12:41:52Z")

</div>

There is nothing strange with  
is = 0;  
is++;  
is == 1;

---

<div class="post-metadata">

**Author:** ![cherishcc](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cherishcc](https://forums.developer.nvidia.com/u/cherishcc)\
**Post date:** [September 23, 2009, 12:53pm UTC](https://forums.developer.nvidia.com/t/strange-problem-cuda/12344/3 "2009-09-23T12:53:38Z")

</div>

> [@](#):
>
> There is nothing strange with
> 
> is = 0;
> 
> is++;
> 
> is == 1;

but it is a loop. it has the sentence “if(i\<n)” and i is the index of the thread.

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [September 23, 2009, 12:56pm UTC](https://forums.developer.nvidia.com/t/strange-problem-cuda/12344/4 "2009-09-23T12:56:56Z")

</div>

> [@](#):
>
> but it is a loop. it has the sentence “if(i\<n)” and i is the index of the thread.

That isn’t a loop. If you think it is, you really ought to go away and learn a bit of C programming before trying to go much further with CUDA.

---

<div class="post-metadata">

**Author:** ![Letharion](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Letharion](https://forums.developer.nvidia.com/u/Letharion)\
**Post date:** [September 23, 2009, 1:05pm UTC](https://forums.developer.nvidia.com/t/strange-problem-cuda/12344/5 "2009-09-23T13:05:56Z")

</div>

> [@](#):
>
> but it is a loop. it has the sentence “if(i\<n)” and i is the index of the thread.

There is no loop.

if n is set to arbitrary large value, and poids is filled with 0s, then your result will always be 1.

Since you haven’t provided _any_ meaningful information about neither n nor poids, there is no reason to expect any different result.

---

<div class="post-metadata">

**Author:** ![cherishcc](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cherishcc](https://forums.developer.nvidia.com/u/cherishcc)\
**Post date:** [September 23, 2009, 1:21pm UTC](https://forums.developer.nvidia.com/t/strange-problem-cuda/12344/6 "2009-09-23T13:21:12Z")

</div>

> [@](#):
>
> That isn’t a loop. If you think it is, you really ought to go away and learn a bit of C programming before trying to go much further with CUDA.

I know that the expression “if” is not a loop in C.

but in code:

if(i\<n)

{

if(poids[i]!=0) ecart[i]=pixel[i]-3;

}

it works very well. each element of the array ecart equals to each element of pixel minus 3.

I am very new in CUDA, the thread index i does not increment automatically? I just wonder how threads function.

---

<div class="post-metadata">

**Author:** ![cherishcc](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cherishcc](https://forums.developer.nvidia.com/u/cherishcc)\
**Post date:** [September 23, 2009, 1:22pm UTC](https://forums.developer.nvidia.com/t/strange-problem-cuda/12344/7 "2009-09-23T13:22:50Z")

</div>

> [@](#):
>
> There is no loop.
> 
> if n is set to arbitrary large value, and poids is filled with 0s, then your result will always be 1.
> 
> Since you haven’t provided _any_ meaningful information about neither n nor poids, there is no reason to expect any different result.

n is the size of the array poids, and you can think poids is just the array of some numbers

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [September 23, 2009, 1:30pm UTC](https://forums.developer.nvidia.com/t/strange-problem-cuda/12344/8 "2009-09-23T13:30:56Z")

</div>

Where do you imagine the iteration could possibly come from in that code?

---

<div class="post-metadata">

**Author:** ![cherishcc](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cherishcc](https://forums.developer.nvidia.com/u/cherishcc)\
**Post date:** [September 23, 2009, 1:47pm UTC](https://forums.developer.nvidia.com/t/strange-problem-cuda/12344/9 "2009-09-23T13:47:48Z")

</div>

> [@](#):
>
> Where do you imagine the iteration could possibly come from in that code?

from the code:

int i=blockIdx.x\*blockDim.x+threadIdx.x;

if(i\<n)

{

if(poids[i]!=0) ecart[i]=pixel[i]-3;

}

if it is not the iteration, how can it work well? I know that my question is very stupid, but I really don’t know it. can you explain more specific?

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [September 23, 2009, 2:01pm UTC](https://forums.developer.nvidia.com/t/strange-problem-cuda/12344/10 "2009-09-23T14:01:35Z")

</div>

> [@](#):
>
> from the code:
> 
> int i=blockIdx.x\*blockDim.x+threadIdx.x;
> 
> if(i\<n)
> 
> {
> 
> if(poids[i]!=0) ecart[i]=pixel[i]-3;
> 
> }
> 
> if it is not the iteration, how can it work well? I know that my question is very stupid, but I really don’t know it. can you explain more specific?

There is no iteration in that code either. i gets a different value for each thread, but that isn’t iteration. In your original code you asked the question about, , you unconditionally set a local variable to 0 in every thread and then increment it exactly once in every thread, which means the variable will **always** take a value of 1. If any of this is a surprise you really ought to go and read some introductory material about the CUDA programming paradigm.

---

<div class="post-metadata">

**Author:** ![Letharion](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Letharion](https://forums.developer.nvidia.com/u/Letharion)\
**Post date:** [September 23, 2009, 2:20pm UTC](https://forums.developer.nvidia.com/t/strange-problem-cuda/12344/11 "2009-09-23T14:20:51Z")

</div>

I need to agree with avidday. Go back the documentation, and start over.  
Or show how you would do it with just c code.  
Your current code makes no sense, so you need to express yourself completely different.  
Even explaining what you want i pseudo-code could do it.
