Skip to content

Instantly share code, notes, and snippets.

@InnovArul
Last active February 7, 2016 09:40
Show Gist options
  • Save InnovArul/68a720d6843aab6e246a to your computer and use it in GitHub Desktop.
Save InnovArul/68a720d6843aab6e246a to your computer and use it in GitHub Desktop.
#include "stdio.h"
//enable error check
#define CUDA_ERROR_CHECK
//check the synchronous function call errorcode 'err' if it is a cudaSuccess
#define CudaSafeCall( err ) __cudaSafeCall( err, __FILE__, __LINE__ )
//check if any error happened during asynchronous execution of Cuda kernel __global__ function
#define CudaCheckError() __cudaCheckError( __FILE__, __LINE__ )
/**
* API to call Cuda APIs safely
* @param err - error code to be checked
* @param file - file name
* @param line - line number
*/
inline void __cudaSafeCall(cudaError err, const char *file, const int line) {
#ifdef CUDA_ERROR_CHECK
if (cudaSuccess != err) {
fprintf(stderr, "cudaSafeCall() failed at %s:%i : %s\n", file, line,
cudaGetErrorString(err));
exit(-1);
}
#endif
return;
}
/**
* API to check the last returned cuda error
* @param file - filename
* @param line - line number
*/
inline void __cudaCheckError(const char *file, const int line) {
#ifdef CUDA_ERROR_CHECK
cudaError err = cudaGetLastError();
if (cudaSuccess != err) {
fprintf(stderr, "cudaCheckError() failed at %s:%i : %s\n", file, line,
cudaGetErrorString(err));
exit(-1);
}
// More careful checking. However, this will affect performance.
// Comment away if needed.
err = cudaDeviceSynchronize();
if (cudaSuccess != err) {
fprintf(stderr, "cudaCheckError() with sync failed at %s:%i : %s\n",
file, line, cudaGetErrorString(err));
exit(-1);
}
#endif
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment