Doubts in using printf in kernel code

Hi All,
I’m trying to debug a code and using printf in the kernel code for the same. In my kernel code I have a statement like this

printf(“Entering thread Id is %d\n”, threadIdx.x);

When I execute the kernel it doesn’t print the 0 thread id. Again when I change the printf to this statement

if (threadIdx.x == 0)
printf(“Entering thread Id is %d\n”, threadIdx.x);

now it prints that 0 thread is entering.

Does this mean printf doesn’t always print the statement whenever it was executed…? If yes then is there some way to force it to print…?

or

When I’m including the if statement my code execution flow is changing and it is printing…?

I doubt the latter case but are there chances of it…?

Regards,
M. Kiran Kumar.

printf really just prints to a buffer of memory which is then printed out by the host after the kernel terminates successfully. Thus if your kernel crashes or does not terminate you will not see it print out. Hopefully in the future we can change this so that it prints out immediately. If you are debugging have you looked into cuda-gdb or parallel insight?