error_cuda_msg.cu 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#ifndef PADDLE_FLUID_PLATFORM_ERR_MSG_CU
#define PADDLE_FLUID_PLATFORM_ERR_MSG_CU

#include"paddle/fluid/platform/enforce.h"
#define __LEN_ERROR_MSG 100

typedef  struct _CudaKerenlErrorPro{
    char _CudaErrorMsg[__LEN_ERROR_MSG];
    int _line;
    char _file[__LEN_ERROR_MSG];
    }CudaKerenlErrorPro;

    #define PADDLE_ENFORCE_CHECK_CUDA_KERNEL()                    \
    do {                                                          \
        char msg[__LEN_ERROR_MSG]={0};                            \
        int line=0;                                               \
        char occur;                                               \
        char file[__LEN_ERROR_MSG]={0};                           \
        get_msg_from_cuda(&occur,msg,file,&line);                 \
        if(occur) {                                               \
        throw ::paddle::platform::EnforceNotMet(msg,file,line);   \
      }                                                           \
    } while (0)

    #define PADDLE_ENFORCE_CUDA_KERNEL(CON, MSG)             \
    do {                                                     \
      if (!(CON)) {                                          \
    send_error_msg(MSG,__FILE__,__LINE__);                   \
                         asm("exit;");                       \
      }                                                      \
    } while (0)

  __device__ static char _CudaKernelErrorOccurred[1]={0};
  __device__ static CudaKerenlErrorPro _CudaKernelErrorMsg;
  
  __device__  __host__ inline void _strcpy(char* src,char* target){
      int i=0;
      while(0!=src[i] && i<__LEN_ERROR_MSG-1){
          target[i]=src[i];
          i++;
      }
      target[i]=0;
  }
  
   __device__ __host__ inline void send_error_msg(char*msg,char* file,int line){
      if(!_CudaKernelErrorOccurred[0]){
          _strcpy(msg,_CudaKernelErrorMsg._CudaErrorMsg);
          _strcpy(file,_CudaKernelErrorMsg._file);
          _CudaKernelErrorMsg._line=line;
          _CudaKernelErrorOccurred[0]=1;
      }
  }

extern "C"{
 void  inline get_msg_from_cuda(char* con,char* msg,char* file,int*line){
        CudaKerenlErrorPro temp;
        char occur[1]={0};
        cudaError_t err=cudaMemcpyFromSymbol(&temp,(_CudaKernelErrorMsg), sizeof(temp));
        cudaMemcpyFromSymbol(occur,_CudaKernelErrorOccurred,sizeof(char));
        strcpy(msg,temp._CudaErrorMsg);
        strcpy(file,temp._file);
        *line=temp._line;
        *con=occur[0];
        PADDLE_ENFORCE_CUDA_SUCCESS(cudaGetLastError());
    }
}

#endif