https://codeyarns.com/2011/03/02/how-to-do-error-checking-in-cuda/

Error checks in CUDA code can help catch CUDA errors at their source. There are 2 sources of errors in CUDA source code:

  1. Errors from CUDA API calls. For example, a call to cudaMalloc() might fail.
  2. Errors from CUDA kernel calls. For example, there might be invalid memory access inside a kernel

在CUDA代码里,错误检查可以帮助找到CUDA代码里的错误,有两种从代码里产生的错误

  1. CUDA API调用错误。如,一个cudaMalloc()调用可能会失败。
  2. CUDA kernel调用错误。如,可能会在某个kernel的实现了访问了非法的内存。

All CUDA API calls return a cudaError value, so these calls are easy to check:

所有CUDA API调用都会返回一个cudaError值,所以这种调用非常容易检查。

if ( cudaSuccess != cudaMalloc( &fooPtr, fooSize ) )
printf( "Error!\n" );

CUDA kernel invocations do not return any value. Error from a CUDA kernel call can be checked after its execution by calling cudaGetLastError():

CUDA kernel不返回任何值。从CUDA kernel调用产生的错误可以在该调用完毕后,从cudaGetLastError()中检查到。

fooKernel<<< x, y >>>(); // Kernel call
if ( cudaSuccess != cudaGetLastError() )
printf( "Error!\n" );

These two types of checks can be elegantly wrapped up in two simple error-checking functions like this:

这两种检查可以非常优雅地封装在两个错误检查函数中,如下,

// Define this to turn on error checking
#define CUDA_ERROR_CHECK #define CudaSafeCall( err ) __cudaSafeCall( err, __FILE__, __LINE__ )
#define CudaCheckError() __cudaCheckError( __FILE__, __LINE__ ) inline void __cudaSafeCall( cudaError err, const char *file, const int line )
{
#ifdef CUDA_ERROR_CHECK
if ( cudaSuccess != err )
{
fprintf( stderr, "cudaSafeCall() failed at %s:%i : %s\n",
file, line, cudaGetErrorString( err ) );
exit( - );
}
#endif return;
} inline void __cudaCheckError( const char *file, const int line )
{
#ifdef CUDA_ERROR_CHECK
cudaError err = cudaGetLastError();
if ( cudaSuccess != err )
{
fprintf( stderr, "cudaCheckError() failed at %s:%i : %s\n",
file, line, cudaGetErrorString( err ) );
exit( - );
} // More careful checking. However, this will affect performance.
// Comment away if needed.
err = cudaDeviceSynchronize();
if( cudaSuccess != err )
{
fprintf( stderr, "cudaCheckError() with sync failed at %s:%i : %s\n",
file, line, cudaGetErrorString( err ) );
exit( - );
}
#endif return;
}

Using these error checking functions is easy:

使用这两个错误检查函数非常简单:

CudaSafeCall( cudaMalloc( &fooPtr, fooSize ) );

fooKernel<<< x, y >>>(); // Kernel call
CudaCheckError();

These functions are actually derived from similar functions which used to be available in the cutil.h in old CUDA SDKs.

这两个函数实际上也是从简单的旧CUDA SDK里导出的

最新文章

  1. Centos 下 mysql root 密码重置
  2. 前端组件化Polymer入门教程(8)——事件
  3. apk反编译、smali修改、回编译笔记
  4. mysql概要(二)类型
  5. Freezing Your Tuples Off 之 vacuum_freeze_min_age
  6. # 基于Gitolite搭建Git Server - 支持SSH&amp;HTTP
  7. ios专题 - CocoaPods - 安装
  8. ViewPager+Fragment的结合使用,实现QQ界面的理解
  9. HDU 4633 Who&#39;s Aunt Zhang (Polya定理+快速幂)
  10. JSP中include指令和include动作区别
  11. 团队作业4——第一次项目冲刺 SiStH DaY
  12. ReferenceError: Error #1065: 变量 dataGridArray 未定义
  13. [rrdtool]监控和自动画图,简单的监控.md
  14. 关于token和refresh token
  15. c++多继承多态
  16. MySQL之实现Oracle中的rank()函数的功能
  17. docker容器与宿主交互数据
  18. ubuntu环境下安装 eclipse
  19. java1.8新特性(四 创建 stream对象)
  20. mongodb对数据库的基本操作

热门文章

  1. 【一定要记得填坑】LG_3822_[NOI2017]整数
  2. 解析Maven的settings.xml文件
  3. 腾讯云服务器(centos7.2)上安装MySQL
  4. maven工程根项目运行ok但是子项目就报错的解决办法
  5. &lt;JZOJ5907&gt;轻功
  6. 【XP系统下载U盘装系统】用电脑店超级U盘装XP系统详细图文教程
  7. –IDEA+Maven+JavaWeb+tomcat项目搭建(图文并茂,详细)
  8. /lib64/libc.so.6: version `GLIBC_2.18&#39; not found报错解决
  9. Jekyll的_config文件配置报错
  10. iOS自建分发