views:

151

answers:

1

Is there convenient way for using asserts within the kernels invocation on device mode?

Thanks, in advance.

+1  A: 
#define MYASSERT(condition) \
  if (!condition) return;

MYASSERT(condition);

if you need something fancier you can use cuPrintf() which is available from the CUDA site for registered developers.

shoosh
There shouldn't be a semi-colon at the end of the macro definition - generally the use of the macro will have that semi-colon. Also, consider implementing it like the following to avoid it greedily attaching to any `else` keyword it might happen to immediately precede: `if (condition) /* do nothing */; else return`
Michael Burr
If you have a __syncthreads() at any point after this, you should ensure that all threads reach the same decision otherwise you may have deadlock. In addition, you could set a boolean flag (e.g. `bool success` initialised to true by the host) in global memory to indicate the event. It doesn't matter that multiple threads will write `false` to the flag since they are always writing the same value and hence the race is irrelevant.
Tom