cuda错误检查的几种常见写法

写法1

#pragma once
#include <cuda_runtime.h>
#define CUDA_CHECK(call)                                                   \
  {                                                                        \
    const cudaError_t error = call;                                        \
    if (error != cudaSuccess) {                                            \
      printf("Error: %s:%d, ", __FILE__, __LINE__);                        \
      printf("code: %d, reasone: %s\n", error, cudaGetErrorString(error)); \
      return false;                                                        \
    }                                                                      \
  }

写法2

#pragma once
#include <stdio.h>
#include <cuda_runtime_api.h>

#define DIVUP(m, n) ((m) / (n) + ((m) % (n) > 0))

#define GPU_CHECK(ans) \
  { GPUAssert((ans), __FILE__, __LINE__); }
inline void GPUAssert(cudaError_t code, const char* file, int line,
                      bool abort = true) {
  if (code != cudaSuccess) {
    fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file,
            line);
    if (abort) exit(code);
  }
}

写法3

#pragma once
#include <cuda_runtime_api.h>
#define BASE_GPU_CHECK(condition) \
  { GPUAssert((condition), __FILE__, __LINE__); }

inline void GPUAssert(cudaError_t code, const char *file, int line,
                      bool abort = true) {
  if (code != cudaSuccess) {
    fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file,
            line);
    if (abort) {
      exit(code);
    }
  }
}

写法4

#include <stdio.h>
#include <cuda_runtime_api.h>
#include <cuda.h>

#define CUDA_KERNEL_LOOP(i, n) for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < (n); i += blockDim.x * gridDim.x)

#define CUDACHECK(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);  

写法5

#include <iostream>
#include <cuda_runtime.h>

#define checkCudaErrors(op)                                                                  \
  {                                                                                          \
    auto status = ((op));                                                                    \
    if (status != 0) {                                                                       \
      std::cout << "Cuda failure: " << cudaGetErrorString(status) << " in file " << __FILE__ \
                << ":" << __LINE__ << " error status: " << status << std::endl;              \
      abort();                                                                               \
    }                                                                                        \
  }

写法6

#include <cuda_runtime.h>
#include <stdio.h>

static inline bool check_runtime(cudaError_t e, const char *call, int line, const char *file) {
    if (e != cudaSuccess) {
        fprintf(stderr,
                "CUDA Runtime error %s # %s, code = %s [ %d ] in file "
                "%s:%d\n",
                call, cudaGetErrorString(e), cudaGetErrorName(e), e, file, line);
        abort();
        return false;
    }
    return true;
}

#define checkRuntime(call) check_runtime(call, #call, __LINE__, __FILE__)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值