Using assert within kernel invocation

六月ゝ 毕业季﹏ 提交于 2019-12-08 20:28:36

问题


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

Thanks, in advance.


回答1:


CUDA now has a native assert function. Use assert(...). If its argument is zero, it will stop kernel execution and return an error. (or trigger a breakpoint if in CUDA debugging.)

Make sure to include "assert.h". Also, this requires compute capability 2.x or higher, and is not supported on MacOS. For more details see CUDA C Programming Guide, Section B.16.

The programming guide also includes this example:

#include <assert.h>
__global__ void testAssert(void)
{
   int is_one = 1;
   int should_be_one = 0;
   // This will have no effect
   assert(is_one);
   // This will halt kernel execution
   assert(should_be_one);
}
int main(int argc, char* argv[])
{
   testAssert<<<1,1>>>();
   cudaDeviceSynchronize();
   return 0;
}



回答2:


#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.



来源:https://stackoverflow.com/questions/2080364/using-assert-within-kernel-invocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!