本文介绍了如何获取应用程序结束时CUDA运行时错误的摘要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现为每个单独的代码段(cudaMalloc,cudaMemCpy,内核代码...)获取cuda状态报告是有用的,但是无聊。如果我不在乎错误是从哪里来的?

解决方案

您可以使用宏来检查错误,因此几乎没有额外的代码可以写入。我在下面包含了一些允许你编写CUDA调用的函数,例如:

  cudaCheck(cudaMalloc(& my_array ,10)); 

然后,调用内核后:

  cudaCheckLastError(MyKernel failed); 

这种方法的优点是可以获得有关失败的信息,尝试继续运行。我建议你使用这种方法,因为它将从长远来看为你节省大量的工作。



在更多的学术说明,你可以使用异步CUDA调用,然后在最后检查错误。例如:

  cudaMemcpyAsync(...);然后,您可以运行 cudaDeviceSynchronize();  c>在结束时,它将返回发生的最新错误(如果发生)。



这真的不是建议。有关详细信息,请参阅CUDA C编程指南4.2中的第3.2.8节错误检查。



用于检查错误的宏。如果出现错误,这些会打印错误并退出程序:



.h:

  void __cudaCheck(cudaError err,const char * file,const int line); 
#define cudaCheck(err)__cudaCheck(err,__FILE__,__LINE__)

void __cudaCheckLastError(const char * errorMessage,const char * file,const int line)
#define cudaCheckLastError(msg)__cudaCheckLastError(msg,__FILE__,__LINE__)

.cpp:

  void __cudaCheck(cudaError err,const char * file,const int line)
{
if cudaSuccess!= err){
fprintf(stderr,%s(%i):CUDA运行时API错误%d:%s.\\\

文件,行, cudaGetErrorString(err));
exit(-1);
}
}

void __cudaCheckLastError(const char * errorMessage,const char * file,const int line)
{
cudaError_t err = cudaGetLastError ;
if(cudaSuccess!= err){
fprintf(stderr,%s(%i):getLastCudaError()CUDA error:%s:(%d)%s.\\\

file,line,errorMessage,(int)err,cudaGetErrorString(err));
exit(-1);
}
}


I found that getting cuda status report for each individual piece of code (cudaMalloc, cudaMemCpy, kernel code...) is useful but boring. Is there anyway to do it a single time at the end of code (if I don't care where the error comes from)?

解决方案

You can use a macro to check for an error, so there's almost no extra code to write. I've included a couple of functions below that allow you to write CUDA calls such as:

cudaCheck(cudaMalloc(&my_array, 10));

And, after calling a kernel:

cudaCheckLastError("MyKernel failed");

The advantage with this approach is that you get information about exactly what failed, and your program doesn't try to keep running. I recommend that you use this method, as it will save you lots of work in the long run.

On more of an academic note, you could probably use asynchronous CUDA calls, and then check for an error at the very end. So, for instance:

cudaMemcpyAsync(...);

And then, you can run cudaDeviceSynchronize(); at the end, which will return the latest error that occured (if any occured).

That's really not recommended though. See chapter 3.2.8, Error Checking, in the CUDA C Programming Guide 4.2 for more information.

Macros for checking for errors. If an error has occured, these will print the error and exit the program:

.h:

void __cudaCheck(cudaError err, const char* file, const int line);
#define cudaCheck(err) __cudaCheck (err, __FILE__, __LINE__)

void __cudaCheckLastError(const char* errorMessage, const char* file, const int line);
#define cudaCheckLastError(msg) __cudaCheckLastError (msg, __FILE__, __LINE__)

.cpp:

void __cudaCheck(cudaError err, const char *file, const int line)
{
  if( cudaSuccess != err) {
    fprintf(stderr, "%s(%i) : CUDA Runtime API error %d: %s.\n",
      file, line, (int)err, cudaGetErrorString( err ) );
    exit(-1);
  }
}

void __cudaCheckLastError(const char *errorMessage, const char *file, const int line)
{
  cudaError_t err = cudaGetLastError();
  if( cudaSuccess != err) {
    fprintf(stderr, "%s(%i) : getLastCudaError() CUDA error : %s : (%d) %s.\n",
      file, line, errorMessage, (int)err, cudaGetErrorString( err ) );
    exit(-1);
  }
}

这篇关于如何获取应用程序结束时CUDA运行时错误的摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 10:48