how to exit a kernel? return?

I use “return” in the kernel, as following:

__global__ void mykernel()


	int i=threadIdx.x;

	if(i>100) return;



When I run the kernel, i gol a lot of error.

Is it because I use “return” in the kernel?

kernel is always void type, So cant return anything

kernel is always void type, So cant use return statement

Why not? Indeed, you can’t

return value;



is always legal.



Yea, Navier is right. I should have say " Cant Return value"

I used to play with returns a lot with kernels, for some reason it is not mentioned at all in guides. My observations are:

  • if whole warp return, everything is OK
  • __syncthreads() work (do not hang) if some threads return early
  • if some threads from a warp return, but not all of them, things may become crazy!

It is perfectly OK to have some individual threads return early, despite what others in this thread have said. You just have to be careful with __synthreads() so you don’t cause deadlocks.

The return statement, however, does just that. Only the current thread returns. There is no way to stop an entire kernel from running with a decision made in a single thread.