-3

I have a very strange error. I am writing a program in Cuda that emulates the Conway Game of Life. I transfered the 2D array to device and there is a if-case that check for the state's thread.

if(iam==-1)
{       //i am on
    iam=0;
}
else if(iam==1)
{       //i am dying
    iam=-1;
}
else    //i am off
{
        if(counter_alive==2)//two neighboors alive
        {
            iam=1;  //i will be on
        }
        //  iam = -999;
}

When the last line is in comment nothing works and the var "iam" has the first value. But if i drop the //, it will work. Of course, if the flow's code execute the else, the var "iam" will take the value -999. Any ideas? Have i missed something? Thanks in advance!

talonmies
  • 70,661
  • 34
  • 192
  • 269
fikos
  • 1
  • 1
  • @talonmies: How do you know the question has nothing to do with C or C++? As it stands, I'd rather suspect it does, but may not have any connection to CUDA at all. :) – tera Jan 09 '17 at 21:20
  • 1
    @tera: Because it is just tag spam, that is why. And I would prefer to just remove the tags quietly rather than have one of the usual C or C++ angry trolls come and remove it and start a flame war at the same time – talonmies Jan 09 '17 at 21:25
  • Ah ok. Note this was tong-in-cheek anyway. @fikos: You really need to provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – tera Jan 09 '17 at 21:34
  • Reading the comments makes me think you are debugging the CUDA code in some debugger, such as NVSight, right? Are you actually compiling it in debug mode and no optimization? That would be `-G`. Without it the debugger may be a bit lost and showing weird results. E.g. an `if` statement may be reduced to a predicated assignment. – CygnusX1 Jan 09 '17 at 22:15

1 Answers1

1

You do not show the initial value of the variable iam or counter_alive. Let us assume that the compiler has set it to 0. Of course even if the compiler just sets the space to a random value, this analysis would be the same.

if(iam==-1)
{       //i am on
    iam=0;
}

Since the initial value is 0, then this fails and drops through

else if(iam==1)
{       //i am dying
    iam=-1;
}

Again, since the initial value is 0, then it fails and drops through.

else    //i am off
{
    if(counter_alive==2)//two neighboors alive
        {
            iam=1;  //i will be on
        }
    //  iam = -999;
}

It enters here with a value of 0. However, since counter_alive has never been set, it is also 0 and the if fails.

Thus, the iam variable is never changed from 0. Note that since neither of the critical values changes, iam will never be reset from 0. If you uncomment the last line, it will always be explicitly set to -999 and will never change either. That is because you never test for 0 or 999. If you had it as -1 it would change to 0 and then never change unless you change counter_alive somewhere else to be 2.

Note that Explanation of CUDA C and C++ explains how the looping is handled as parallel processing. In that case, the reaction of the various items in the array may not be what you would expect in plain C (single stream) processing.

Community
  • 1
  • 1
sabbahillel
  • 4,357
  • 1
  • 19
  • 36
  • The var iam has 3 values. 1 -1 or 0. Lets forget about else statement. I checked with value -1 and last line (iam = -999) was comment. Never enters in first if statement. But if i will uncomment the iam = -999; it enters to the first if. I know that be sounded strange. I can post screenshots if you want. I don't know if it's something about cuda threads. – fikos Jan 09 '17 at 19:26
  • @fikos We would have to see the code around this. What is iam defined as? Is it an int, a short, an unsigned? How do you know that iam is set to -1 and not being picked up by the first if statement? How do you have counter_alive set? – sabbahillel Jan 09 '17 at 20:02