strange problem cuda

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;

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.

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.

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.

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.

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

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?

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.

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.