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里导出的

How to do error checking in CUDA(如何在CUDA里做错误检查)的更多相关文章

  1. ECC(Error Checking and Correction)校验和纠错

    ECC的全称是 Error Checking and Correction or Error correction Coding,是一种用于差错检测和修正的算法.上一节的BBM中我们提到过,NAND闪 ...

  2. docker build提示error checking context:can't stat xxx

    现象描述 使用docker build一个镜像的时候,提示下面的错误: ➜ docker build -t image_name -f xxx.dockerfile . error checking ...

  3. HTTP 错误 500.21 - Internal Server Error 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

    HTTP 错误 500.21 - Internal Server Error 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipe ...

  4. MySQL ERROR 1005: Can't create table (errno: 150)的错误解决办法

    在mysql 中建立引用约束的时候会出现MySQL ERROR 1005: Can't create table (errno: 150)的错误信息结果是不能建立 引用约束. 出现问题的大致情况 1. ...

  5. HTTP 错误 500.21 - Internal Server Error处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

    HTTP 错误 500.21 - Internal Server Error处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipel ...

  6. 解决:安装SQL Server 2008 Native Client遇到错误(在Navicat premium新建sqlserver连接时 需要):An error occurred during ...HRESULT: 0x80070422(注意尾部的错误号)

    解决:安装SQL Server 2008 Native Client遇到错误(在Navicat premium新建sqlserver连接时 需要):An error occurred during . ...

  7. HTTP 错误 500.21 - Internal Server Error 处理程序“BlockViewHandler”在其模块列表中有一个错误模块“ManagedPipelineHandler

    HTTP 错误 500.21 - Internal Server Error  处理程序“BlockViewHandler”在其模块列表中有一个错误模块“ManagedPipelineHandler ...

  8. ubuntu14.04 安装 CUDA 7.5 / CUDA 8.0

    原文转自:http://blog.csdn.net/masa_fish/article/details/51882183 CUDA7.5和CUDA8.0的安装过程是一毛一样的.所以如果安装CUDA8. ...

  9. 【MySQL】ERROR 1005: Can't create table (errno: 150)的错误解决办法

    在mysql 中建立引用约束的时候会出现MySQL ERROR 1005: Can't create table (errno: 150)的错误信息结果是不能建立 引用约束. 出现问题的大致情况 1. ...

随机推荐

  1. 吴裕雄--天生自然python学习笔记:Python3 输入和输出

    输出格式美化 Python两种输出值的方式: 表达式语句和 print() 函数. 第三种方式是使用文件对象的 write() 方法,标准输出文件可以用 sys.stdout 引用. 如果你希望输出的 ...

  2. 几个简单又实用的PHP函数

    简单方便使用: /** * 将多维数组转为一维数组 * @param array $arr * @return array */ function ArrMd2Ud($arr) { #将数值第一元素作 ...

  3. 使用Commons Logging

    Commons Logging 和Java标准库提供的日志不同,Commons Logging是一个第三方日志库,它是由Apache创建的日志模块,需要导入commons-logging-1.2.ja ...

  4. 吴裕雄--天生自然 HADOOP大数据分布式处理:修改CenterOS 7系统时间为北京时间

  5. file / from install of XXX conflicts with file from package filesystem-XXX

    在centos上安装一个偏门软件时出现如下问题: rpm -ivh mNetAssist-0.1.1-2.x86_64.rpm #执行命令 file / from install of XXX con ...

  6. 【WPF学习】第五十三章 动画类型回顾

    创建动画面临的第一个挑战是为动画选择正确的属性.期望的结果(例如,在窗口中移动元素)与需要使用的属性(在这种情况下是Canvas.Left和Canvas.Top属性)之间的关系并不总是很直观.下面是一 ...

  7. 嗨! Apriori算法

    Association Rule 一:项集和规则 1.1 认识名词: Association Rule : 关联规则 Frequent Itemsets : 频繁项集 Sequential Patte ...

  8. spring jpa ManyToMany 理解和使用

    1.java和jpa 中所有的关系都是单向的.这个关系数据库不同,关系数据库,通过外键定义并查询,使得反向查询总是存在的. 2.JPA还定义了一个OneToMany关系,它与ManyToMany关系类 ...

  9. MySQL root密码重置问题

    1:进入cmd,停止mysql服务:Net stop mysql (进入服务---->MySql----->停止) 到mysql的安装路径启动mysql,在bin目录下使用mysqld-n ...

  10. 解决ionic2/ionic3轮播图切换页面或者点击过后不自动轮图

    我们在ionic2/ionic3开发的过程中会出现切换页面或者滑动切换轮播图出现轮播图不再轮播的情况,这其实需要一些配置. 首先在运用到轮播图的component中引入 import {ViewChi ...