CUDA:如何在内核代码中断言? [英] CUDA: How to assert in kernel code?

查看:49
本文介绍了CUDA:如何在内核代码中断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CUDA 内核代码中断言的等效技术是什么?

What is the equivalent technique of an assertion in CUDA kernel code?

似乎没有针对 CUDA 内核代码的断言.我想要一种在内核代码中轻松捕获程序员错误的方法.一种机制,我可以设置需要为真的条件,并且内核应该在条件为假时退出并显示错误消息.

There does not seem to be an assert for CUDA kernel code. I want a way to catch programmer mistakes easily in kernel code. A mechanism where I can set conditions that need to be true and the kernel should bail out when the condition is false with an error message.

推荐答案

对于 cc 2.x 或更高版本的设备,assertionvoid assert(int expression),可以在内核中使用,使得具有 expression = 的线程= 0 一旦主机同步函数被调用,就向 stderr 发送一条消息.

For devices of cc 2.x or above, assertion , void assert(int expression), could be used within a kernel such that threads with expression == 0 send a message to stderr once a host synchronization function is called.

对于其他情况或无法使用断言时(例如在 MacOS 上),您将无法从内核向主机返回错误消息或错误代码.

For other cases or when assertion cannot be used (e.g. on MacOS), you won't be able to return an error message or error code to the host from a kernel.

相反,我会设置错误状态并从主机进行检查.使用设备全局内存或(更好的)映射主机内存来存储错误状态,作为参数传递给每个内核调用.在内核中使用if语句,如果语句失败,设置错误码并返回.您将能够在内核调用后从主机检查错误代码,但请记住,在检查错误代码之前,您将在内核启动后同步主机和设备.我想这对开发来说会很好,但对生产来说不太好.

Instead, I would set a error state and check it from the host. Use device global memory or (better) mapped host memory for storing an error state, passed as a parameter to each kernel call. Use if statements in the kernel, and of if the statements fail, set the error code and return. You will be able to check the error code from the host after the kernel call, but keep in mind that you will have synchronize the host and device after the kernel launch before checking the error code. I guess this will work fine for development but not so much for production.

关于直接从设备打印错误信息

As to printing an error message straight from the device

  • 在 1.x、2.x 和 3.0 卡中,您可以使用仿真模式打印错误消息.
  • 在 3.1 forward (on fermi) 中,显然您可以在内核中使用 printf 来打印错误消息.似乎它并不总是立即起作用,例如http://forums.nvidia.com/index.php?showtopic=182448

这篇关于CUDA:如何在内核代码中断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆