代码之家  ›  专栏  ›  技术社区  ›  kokosing

在内核调用中使用断言

  •  5
  • kokosing  · 技术社区  · 15 年前

    在设备模式下使用内核调用中的断言是否有方便的方法?

    谢谢,提前。

    2 回复  |  直到 9 年前
        1
  •  2
  •   Mr Fooz    13 年前
    #define MYASSERT(condition) \
      if (!(condition)) { return; }
    
    MYASSERT(condition);
    

    如果你需要更高级的东西,你可以用 cuPrintf() 它可以从CUDA网站上为注册开发者提供。

        2
  •  13
  •   Kevin Holt    9 年前

    CUDA现在有一个本机断言函数。使用 assert(...) . 如果它的参数为零,它将停止内核执行并返回错误。(或者在CUDA调试中触发断点。)

    确保包含“assert.h”。另外,这需要2.x或更高的计算能力,MacOS不支持。有关更多详细信息,请参阅《CUDA C编程指南》第B.16节。

    编程指南还包括以下示例:

    #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;
    }