Skip to main content
  1. Posts/

cuda error checking 学习笔记

Note: This article is available in Chinese only. 本文暂无英文版本。 View original

由于发现cuda c++ 的 debug方式和c++ 差别很大,因此打算再开一篇,专门记录一些和error checking 以及debug有关的内容.

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.

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

1
2    
3    if ( cudaSuccess != cudaMalloc( &fooPtr, fooSize ) )
4        printf( "Error!\n" );
5    

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

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

以及下面是一个check error的宏…

 1#define CHECK_ERROR( err ) \
 2  if( err != cudaSuccess ) { \
 3    std::cerr << "ERROR: " << cudaGetErrorString( err ) << std::endl; \
 4    exit( -1 ); \
 5  }
 6
 7#define CHECK_LAST_ERROR \
 8  { cudaError_t err = cudaGetLastError(); \
 9    if( err != cudaSuccess ) { \
10      std::cerr << cudaGetErrorString( err ) << std::endl; \
11      exit( -1 ); \
12    } \
13  }

Related

qt 5.x 初探 (5) 

·1 min
qt_5.9_ui_doc 还是比直接写代码方便点。。。所以不妨学习一个! 以及。。。qt在2017年6月1号发布了5.9。。。所以之前是5.8。。。现在变成5.9了。。。

qt 5.x初探 (3)

·2 mins
update3: 终于知道了正确的学习姿势… 用百度把要用的东西大概描述出来,然后总能找到一个是你要的。。。