CUDA异常捕获
技术背景
在CUDA编程中有可能会遇到一些相对比较隐蔽的报错,但是直接编译运行cu文件是不显现的。那么可以通过添加一个用于检查的宏,来监测CUDA程序运行过程中可能出现的报错。
error.cuh
我们在CUDA头文件中实现这个宏:
#pragma once
#include <stdio.h>
#define CHECK(call) do{const cudaError_t error_code = call; if (error_code != cudaSuccess){printf("CUDA Error:\n"); printf(" File: %s\n", __FILE__); printf(" Line: %d\n", __LINE__); printf(" Error code: %d\n", error_code); printf(" Error text: %s\n", cudaGetErrorString(error_code)); exit(1);}} while (0)
然后在调用CUDA相关函数或者核函数的时候,就可以使用CHECK操作来监测其中有无相关异常。
调用测试
先用一个简单的测试案例,就是显存分配的场景,如果是一个正常的显存分配:
// nvcc ./test_error.cu -Xcompiler -fPIC -o ./test_error && ./test_error
#include "error.cuh"
#include <stdio.h>
int main(void){
const int N = 100000000;
const int M = sizeof(double) * N;
double *d_x;
CHECK(cudaMalloc((void **)&d_x, M));
CHECK(cudaFree(d_x));
printf("Success!\n");
}
运行结果是没有报错的:
Success!
但是如果我们调大N的值,使其超出显存大小:
// nvcc ./test_error.cu -Xcompiler -fPIC -o ./test_error && ./test_error
#include "error.cuh"
#include <stdio.h>
int main(void){
const int N = 1000000000;
const int M = sizeof(double) * N;
double *d_x;
CHECK(cudaMalloc((void **)&d_x, M));
CHECK(cudaFree(d_x));
printf("Success!\n");
}
再次运行,就会报OOM错误:
./test_error.cu(7): warning #69-D: integer conversion resulted in truncation
const int M = sizeof(double) * N;
^
Remark: The warnings can be suppressed with "-diag-suppress <warning-number>"
./test_error.cu(9): warning #68-D: integer conversion resulted in a change of sign
do{const cudaError_t error_code = cudaMalloc((void **)&d_x, M); if (error_code != cudaSuccess){printf("CUDA Error:\n"); printf(" File: %s\n", "./test_error.cu"); printf(" Line: %d\n", 9); printf(" Error code: %d\n", error_code); printf(" Error text: %s\n", cudaGetErrorString(error_code)); exit(1);}} while (0);
^
./test_error.cu(7): warning #69-D: integer conversion resulted in truncation
const int M = sizeof(double) * N;
^
Remark: The warnings can be suppressed with "-diag-suppress <warning-number>"
./test_error.cu(9): warning #68-D: integer conversion resulted in a change of sign
do{const cudaError_t error_code = cudaMalloc((void **)&d_x, M); if (error_code != cudaSuccess){printf("CUDA Error:\n"); printf(" File: %s\n", "./test_error.cu"); printf(" Line: %d\n", 9); printf(" Error code: %d\n", error_code); printf(" Error text: %s\n", cudaGetErrorString(error_code)); exit(1);}} while (0);
^
./test_error.cu: In function 'int main()':
./test_error.cu:7:31: warning: overflow in conversion from 'long unsigned int' to 'int' changes value from '8000000000' to '-589934592' [-Woverflow]
7 | const int M = sizeof(double) * N;
| ~~~~~~~~~~~~~~~~^~~~
CUDA Error:
File: ./test_error.cu
Line: 9
Error code: 2
Error text: out of memory
当然,中间因为整形溢出,还有一些其他的warnning信息,但是这里主要要展现的是OOM报错问题。
核函数检测
上面的异常检测针对是cudaMalloc这个CUDA操作,其实对于核函数,也是一样可以检测出其异常。我们先演示一个正常的示例:
// nvcc ./test_error.cu -Xcompiler -fPIC -o ./test_error && chmod +x ./test_error && ./test_error
#include "error.cuh"
#include <math.h>
#include <stdio.h>
void __global__ add(const double *x, const double *y, double *z, const int N){
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < N){
z[idx] = x[idx] + y[idx];
}
}
int main(void){
const int N = 10;
const int M = sizeof(double) * N;
const double a = 1.23;
double *h_x = (double*) malloc(M);
for (int n = 0; n < N; ++n)
{
h_x[n] = a;
}
double *d_x, *d_z;
CHECK(cudaMalloc((void **)&d_x, M));
CHECK(cudaMalloc((void **)&d_z, M));
CHECK(cudaMemcpy(d_x, h_x, M, cudaMemcpyHostToDevice));
const int block_size = 1024;
const int grid_size = (N + block_size - 1) / block_size;
add<<<grid_size, block_size>>>(d_x, d_x, d_z, N);
CHECK(cudaGetLastError());
CHECK(cudaDeviceSynchronize());
CHECK(cudaFree(d_x));
CHECK(cudaFree(d_z));
free(h_x);
printf("Success!\n");
return 0;
}
这个CUDA程序运行的是一个数组加法。运行结果:
$ nvcc ./test_error.cu -Xcompiler -fPIC -o ./test_error && chmod +x ./test_error && ./test_error
Success!
调整一下block_size参数:
// nvcc ./test_error.cu -Xcompiler -fPIC -o ./test_error && chmod +x ./test_error && ./test_error
#include "error.cuh"
#include <math.h>
#include <stdio.h>
void __global__ add(const double *x, const double *y, double *z, const int N){
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < N){
z[idx] = x[idx] + y[idx];
}
}
int main(void){
const int N = 10;
const int M = sizeof(double) * N;
const double a = 1.23;
double *h_x = (double*) malloc(M);
for (int n = 0; n < N; ++n)
{
h_x[n] = a;
}
double *d_x, *d_z;
CHECK(cudaMalloc((void **)&d_x, M));
CHECK(cudaMalloc((void **)&d_z, M));
CHECK(cudaMemcpy(d_x, h_x, M, cudaMemcpyHostToDevice));
const int block_size = 1025;
const int grid_size = (N + block_size - 1) / block_size;
add<<<grid_size, block_size>>>(d_x, d_x, d_z, N);
CHECK(cudaGetLastError());
CHECK(cudaDeviceSynchronize());
CHECK(cudaFree(d_x));
CHECK(cudaFree(d_z));
free(h_x);
printf("Success!\n");
return 0;
}
由于Block大小在CUDA程序中最大只能是1024,因此如果超出这个数就会出现异常,但是如果没有异常检测函数的话,程序是能够正常执行下去的,这样这个异常就会一直保留在程序中。运行结果:
$ nvcc ./test_error.cu -Xcompiler -fPIC -o ./test_error && chmod +x ./test_error && ./test_error
CUDA Error:
File: ./test_error.cu
Line: 29
Error code: 9
Error text: invalid configuration argument
因为加上了cudaGetLastError()函数,并使用了异常捕获的宏,所以这里就会提示参数配置异常。
总结概要
本文主要介绍了在CUDA编程的实践中,增加一个异常捕获的宏模块,以保障CUDA项目结果的准确性。主要代码内容参考了樊哲勇所著的《CUDA编程基础与实践》,是一本很好的CUDA编程入门书籍。
版权声明
本文首发链接为:https://www.cnblogs.com/dechinphy/p/cuda_error.html
作者ID:DechinPhy
更多原著文章:https://www.cnblogs.com/dechinphy/
请博主喝咖啡:https://www.cnblogs.com/dechinphy/gallery/image/379634.html
参考内容
- 《CUDA编程基础与实践》——樊哲勇
CUDA异常捕获的更多相关文章
- .NET 基础 一步步 一幕幕[数组、集合、异常捕获]
数组.集合.异常捕获 数组: 一次性存储多个相同类型的变量. 一维数组: 语法: 数组类型[] 数组名=new 数组类型[数组长度]; 声明数组的语法: A.数据类型 [] 数组名称= new 数据类 ...
- MVC 好记星不如烂笔头之 ---> 全局异常捕获以及ACTION捕获
public class BaseController : Controller { /// <summary> /// Called after the action method is ...
- atitit.js浏览器环境下的全局异常捕获
atitit.js浏览器环境下的全局异常捕获 window.onerror = function(errorMessage, scriptURI, lineNumber) { var s= JSON. ...
- C#中的那些全局异常捕获
1.WPF全局捕获异常 public partial class App : Application { public App() { // 在异 ...
- Spring-MVC开发之全局异常捕获全面解读
异常,异常 我们一定要捕获一切该死的异常,宁可错杀一千也不能放过一个! 产品上线后的异常更要命,一定要屏蔽错误内容,以免暴露敏感信息! 在用Spring MVC开发WEB应用时捕获全局异常的方法基本有 ...
- JavaScript异常捕获
理论准备 ★ 异常捕获 △ 异常:当JavaScript引擎执行JavaScript代码时,发生了错误,导致程序停止运行: △ 异常抛出:当异常产生,并且这个异常生成一个错误信息: △ 异常捕获: ...
- SQLServer异常捕获
在SQLserver数据库中,如果有很多存储过程的时候,我们会使用动态SQL进行存储过程调用存储过程,这时候,很可能在某个环节就出错了,但是出错了我们很难去跟踪到出错的存储过程,此时我们就可以使用异常 ...
- Asp.Net MVC3(三)-MvcApp实现全局异常捕获
定义异常捕获类: [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMu ...
- iphone 异常捕获处理
iphone 异常捕获处理 1 void UncaughtExceptionHandler(NSException *exception) { 2 NSArray *arr = [exception ...
- iOS异常捕获
文章目录 一. 系统Crash 二. 处理signal 下面是一些信号说明 关键点注意 三. 实战 四. Crash Callstack分析 – 进⼀一步分析 五. demo地址 六. 参考文献 前言 ...
随机推荐
- jmeter使用jdbc连接SQL server,执行SQL报错处理
前置环境参数:jdk-8u391-windows-x64,驱动:sqljdbc4.jar 备注:这是解决后的截图,将就用 问题一:使用jmeter5.5,使用jdbc连接SQL server,执行SQ ...
- python多版本
1.分别下载并安装两个版本的python 2.去安装的文件夹中将python.exe 和pythonw.exe改名加上版本号 3.将python.exe文件目录和当前目录下的Scripts目录都加到用 ...
- DBeaver 不错大家都来用 DBeaver 吧
支持 windows linux 支持 pg 等 n 多数据库
- 在docker中使用主机串口通讯
在进行软件docker化的过程时,很大的一个阻碍就是软件与各种外围硬件设备的交互,网口通信的设备能够很容易地接入容器,但是串口设备则要复杂一些.本文讨论在windows和linux下docker容器使 ...
- https://gitlab.com/volian/nala/-/wikis/Installation
Installation Debian Testing/Sid Nala is officially in the testing and sid repos. sudo apt install ...
- HikariCP不断打印WARN日志Failed to validate connection com.mysql.jdbc.JDBC4Connection@xxxxx (...) Possibly consider using a shorter maxLifetime value.
最终解决方案(结论) maxLifeTime参数需要设置为小于min(数据库的wait_timeout,HA代理的超时时间,其他代理的超时时间);也就是说maxLifeTime不仅要像HikariCP ...
- Macos 安装md5sum、sha1sum、md5deep、sha1deep
一.安装md5sum和sha1sum 方法一:brew 安装 # brew install md5sha1sum 方法二:编译安装 源码下载地址:http://www.microbrew.org/to ...
- 9.24java wab实现创建新界面验证码
<!DOCTYPE html> <html> <head> <title>User Login</title> <style> ...
- yarn : 无法加载文件 C:\Users\duany\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本
在win10 下安装yarn后,在编辑器中检查yarn的安装的时候会出现: 解决方法 1:搜索powershell,以管理员方式运行powershell2:使用命令更改计算机的执行策略 执行: set ...
- 修改led-core.c 让led的delay_on和delay_off时间不会应为trigger配置改版而重置为1HZ
先列一下leds trigger的设置流程 echo none > trigger 的流程 led_trigger_set() | led_stop_software_blink() echo ...