views:

187

answers:

2

I was stepping through some C/CUDA code in the debugger, something like:

for(uint i = threadIdx.x; i < 8379; i+=256) 
    sum += d_PartialHistograms[blockIdx.x + i * HISTOGRAM64_BIN_COUNT];

And I was utterly confused because the debugger was passing by it in one step, although the output was correct. I realised that when I put curly brackets around my loop as in the following snippet, it behaved in the debugger as expected.

for(uint i = threadIdx.x; i < 8379; i+=256) {
    sum += d_PartialHistograms[blockIdx.x + i * HISTOGRAM64_BIN_COUNT];
}

So is are parenthesis-free for loops treated differently in C or in the debugger, or perhaps it is particular to CUDA.

Thanks

+9  A: 

The debugger executes one statement at a time. Check this out:

int sum = 0;                            /* one assignment statement */
for (int k = 0; k < 10; k++) sum += k;  /* one for statement */

and compare with this

int sum = 0;                            /* one assignment statement */
for (int k = 0; k < 10; k++)
{                                       /* for statement with the body
                                           in a block of statements */
    sum += k;                           /* assignment statement */
}

In the first example above, the sum += k is an integral part of the for statement; in the 2nd example, it is a full statement on its own.

pmg
+4  A: 

There isn't any execution difference between a single statement following the "for" or a block with one statement in it. Looking at your code though, do you realise that i isn't actually incremented? Perhaps you meant to put i+=256.

As far as the debugger is concerned the brackets constitute something else to "move into" whereas the single line is just that, a single line (just like an if statement with no block).

roguenut
Sorry it was i+=256 , I just made a mistake copying the code here
zenna