写法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__)