Last active
February 7, 2016 09:40
-
-
Save InnovArul/68a720d6843aab6e246a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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