difference between if, and while in CUDA

in my kernel,

global kernel ( …, imax, jmax, kmax, …) {

tx = threadIdx.x; ty = threadIdx.y; tz = threadIdx.z;
dx = blockDim.x; dy = blockDim.y; dz = blockDim.z;
bx = blockIdx.x; by = blockIdx.y; bz = blockIdx.z;

i = dxbx+tx;
j = dy
by+ty;
k = tz;

if ((i<=imax) && (j<=jmax) && (k<=kmax)) {

/* i don't have any increment of i, j, k */

}
}

it is working fine but once i replace “if” to “while” like

while ((i<=imax) && (j<=jmax) && (k<=kmax)) {

/* i don’t have any increment of i, j, k */
}

my code quits right away…it seems the kernel’s never been launched at all.

i know in c/c++, all statements following “if” are executed exactly once if the if statement is true,

and all statements following “while” are executed over and over for as long as the while statement remains true.

and i also understand “while” loop may not even run at all.

in CUDA, i think its no different i guess.

then for my case why “if” statement is working but “while” statement is not?

without any increment of i j k, it must run at least once in my opinion.

any comments are welcome. and thanks in well advance.

in my kernel,

global kernel ( …, imax, jmax, kmax, …) {

tx = threadIdx.x; ty = threadIdx.y; tz = threadIdx.z;
dx = blockDim.x; dy = blockDim.y; dz = blockDim.z;
bx = blockIdx.x; by = blockIdx.y; bz = blockIdx.z;

i = dxbx+tx;
j = dy
by+ty;
k = tz;

if ((i<=imax) && (j<=jmax) && (k<=kmax)) {

/* i don't have any increment of i, j, k */

}
}

it is working fine but once i replace “if” to “while” like

while ((i<=imax) && (j<=jmax) && (k<=kmax)) {

/* i don’t have any increment of i, j, k */
}

my code quits right away…it seems the kernel’s never been launched at all.

i know in c/c++, all statements following “if” are executed exactly once if the if statement is true,

and all statements following “while” are executed over and over for as long as the while statement remains true.

and i also understand “while” loop may not even run at all.

in CUDA, i think its no different i guess.

then for my case why “if” statement is working but “while” statement is not?

without any increment of i j k, it must run at least once in my opinion.

any comments are welcome. and thanks in well advance.

You do realize that your while() statement puts a lot of threads in an infinite loop, right?

You do realize that your while() statement puts a lot of threads in an infinite loop, right?

Oh…

let me explain if i understand correctly.

the key point in my case is the fact that i don’t have any increment in the while {}

so if it satisfies the condition it will run forever…

if not, it does not even get to the loop…

did i understand correct? i think i got it. what was i thinking?

Thanks bunch!!!

I better be more careful or i think i need a day off i become a fool.

Oh…

let me explain if i understand correctly.

the key point in my case is the fact that i don’t have any increment in the while {}

so if it satisfies the condition it will run forever…

if not, it does not even get to the loop…

did i understand correct? i think i got it. what was i thinking?

Thanks bunch!!!

I better be more careful or i think i need a day off i become a fool.