在流行的 gpuErrchk 解决方案what-is-the-canonical-way-to-check-for-errors-using-the-cuda-runtime-api之后,我使用 cuBLAS 替代方案重载了该函数,以处理 cuBLAS 错误。然后,您可以像往常一样使用 gpuErrchk 轻松包装您的 cuBLAS 函数调用。
从其他人提到的 helper_cuda.h 文件来看,很容易继续为其他 cuda 库(cuFFT 等)添加重载函数。希望这可以帮助某人。请告诉我是否有更好的方法!
inline void error::gpuAssert(cudaError_t code, const char *file, int line, bool abort=true) const
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
inline void error::gpuAssert(cublasStatus_t code, const char *file, int line, bool abort=true) const
{
if (code != CUBLAS_STATUS_SUCCESS)
{
switch (code) {
case CUBLAS_STATUS_NOT_INITIALIZED:
fprintf(stderr, "cuBLAS Error: CUBLAS_STATUS_NOT_INITIALIZED file: %s line: %d ", file, line);
break;
case CUBLAS_STATUS_ALLOC_FAILED:
fprintf(stderr, "cuBLAS Error: CUBLAS_STATUS_ALLOC_FAILED file: %s line: %d ", file, line);
break;
case CUBLAS_STATUS_INVALID_VALUE:
fprintf(stderr, "cuBLAS Error: CUBLAS_STATUS_INVALID_VALUE file: %s line: %d ", file, line);
break;
case CUBLAS_STATUS_ARCH_MISMATCH:
fprintf(stderr, "cuBLAS Error: CUBLAS_STATUS_ARCH_MISMATCH file: %s line: %d ", file, line);
break;
case CUBLAS_STATUS_MAPPING_ERROR:
fprintf(stderr, "cuBLAS Error: CUBLAS_STATUS_MAPPING_ERROR file: %s line: %d ", file, line);
break;
case CUBLAS_STATUS_EXECUTION_FAILED:
fprintf(stderr, "cuBLAS Error: CUBLAS_STATUS_EXECUTION_FAILED file: %s line: %d ", file, line);
break;
case CUBLAS_STATUS_INTERNAL_ERROR:
fprintf(stderr, "cuBLAS Error: CUBLAS_STATUS_INTERNAL_ERROR file: %s line: %d ", file, line);
break;
case CUBLAS_STATUS_NOT_SUPPORTED:
fprintf(stderr, "cuBLAS Error: CUBLAS_STATUS_NOT_SUPPORTED file: %s line: %d ", file, line);
break;
case CUBLAS_STATUS_LICENSE_ERROR:
fprintf(stderr, "cuBLAS Error: CUBLAS_STATUS_LICENSE_ERROR file: %s line: %d ", file, line);
break;
}
if (abort) exit(code);
}
}
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
示例输出:
** On entry to SGEMM parameter number 13 had an illegal value
cuBLAS Error: CUBLAS_STATUS_INVALID_VALUE file: ../src/XX.cu line: 323 Segmentation fault (core dumped)