ITPub博客

首页 > Linux操作系统 > Linux操作系统 > CUDA程序优化心得之错误处理

CUDA程序优化心得之错误处理

原创 Linux操作系统 作者:yyfn风辰 时间:2010-05-14 14:10:32 0 删除 编辑
标准C提供了错误输出函数perror,其功能是将在调用其处最近之前出错的错误输出到的标准错误流,当然利用操作系统的功能,你也可以将它们重定向到文件。
由于CUDA函数设计为都返回错误码,而且CUDA本身也提供了两个函数cudaGetLastError和cudaGetErrorString,因此其处理可充分利用这两个函数,另外我们在写CUDA程序时,经常要验证结果和CPU上的结果是否一致,因此我也提供了这个功能。
程序代码如下:
class ErrorHander{//file:file name;line:lineNo
        const static float EPSFLOAT = 0.000001;
        const static double EPSDOUBLE = 0.0000000001;
        public :
#ifdef __CUDACC__
        //can't use it at asynchronize situation
                void static printError(cudaError_t cet){
                        if(cudaSuccess != cet){
                                printf("error:%s\n",cudaGetErrorString(cet));
                                fflush(stdout);
                                exit(1);
                        }
                }
        //can use it at anywhere
                void static printLastError(){
                        cudaThreadSynchronize();
                        cudaError_t cet = cudaGetLastError();
                        if(cudaSuccess != cet){
                                printf("error:%s!\n",cudaGetErrorString(cet));
                                fflush(stdout);
                                exit(1);
                        }
                }
                void static printError(int line,cudaError_t cet){
                        if(cudaSuccess !=cet){
                                printf("line:%d,error:%s\n",line,cudaGetErrorString(cet));
                                fflush(stdout);
                                exit(1);
                        }
                }
                void static printLastError(int line){
                        cudaThreadSynchronize();
                        cudaError_t cet = cudaGetLastError();
                        if(cudaSuccess != cet){
                                printf("line:%d,error:%s!\n",line,cudaGetErrorString(cet));
                                fflush(stdout);
                                exit(1);
                        }
                }
                void static printError(char *file,int line,cudaError_t cet){
                        if(cudaSuccess != cet){
                                printf("%s,line:%d,error:%s\n",file,line,cudaGetErrorString(cet));
                                fflush(stdout);
                                exit(1);
                        }
                }
                void static printLastError(char *file,int line){
                        cudaThreadSynchronize();
                        cudaError_t cet = cudaGetLastError();
                        if(cudaSuccess != cet){
                                printf("%s,line:%d,error:%s!\n",file,line,cudaGetErrorString(cet));
                                fflush(stdout);
                                exit(1);
                        }
                }
#endif
                void static valid(const int *first,const int *second, int num, int firstSpan = 1,
                                int secondSpan = 1, int firstOffset = 0, int secondOffset = 0){
                        for(int i=0;i                                 assert(first[firstOffset+i*firstSpan]==second[secondOffset+i*secondSpan]);
                        }
                }
                void static valid(const float *first, const float *second, int num, int firstSpan = 1,
                        int secondSpan = 1, int firstOffset = 0, int secondOffset = 0){
                        for(int i=0;i         assert(abs(1-(first[firstOffset + i*firstSpan]/second[secondOffset+i*secondSpan]))         }
                         }
                        void static valid(const double *first, const double *second, int num, int firstSpan = 1,
                             int secondSpan = 1, int firstOffset = 0, int secondOffset = 0){
                             for(int i=0;i         assert(abs(1-(first[firstOffset + i*firstSpan]/second[secondOffset+i*secondSpan]))                      }
                 }
};
printError函数打印传入的cuda函数返回的错误码所代表的错误,printLastError打印最后一次错误的错误字符串。file代表文件名,调用时可用__FILE__宏代替,line代表行数,调用时可传入__LINE__宏,当然由于设计上的缺陷,你也可以传入其它的信息。valid函数用于确定两个数组的数据是不是一样的,本来想使用模板,但是还是使用了重载。由于全部采用静态函数,所以我们没有必要建立对象,直接使用类名调用就行了,这样可以方便的使用条件编译来启用/关闭调试信息,这一点比cutil库的cudaSafeCall好用得多。另外这个类的功能也足够使用了。如果你想改变精度限的话,改变EPS变量的值就可以了。非常的方便。
        另,由于能力所限或者说设计缺陷,printError各函数都无法处理异步调用错误。另外这些工具函数只是拿来调试用的,真正的出错处理不能这样做。
        当然我个人的库包含的类远比这要多,比如还有文件输入输出,随机数初始化等。这些内部就不包括在本节了。
        整体而言,这两节都是一些准备工作。从下一节起才真正涉及到优化的内容。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23057064/viewspace-662699/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-12-21

  • 博文量
    28
  • 访问量
    128593