Skip to content

Instantly share code, notes, and snippets.

@onionmk2
Last active December 20, 2017 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onionmk2/ac708ec10586e5dde972e07c45cde6d0 to your computer and use it in GitHub Desktop.
Save onionmk2/ac708ec10586e5dde972e07c45cde6d0 to your computer and use it in GitHub Desktop.
// Define this to turn on error checking
// original https://codeyarns.com/2011/03/02/how-to-do-error-checking-in-cuda/
#ifndef CUDA_ERROR_DETECTOR_H
#define CUDA_ERROR_DETECTOR_H
#define CUDA_ERROR_CHECK
#define CudaSafeCall(err) CUDA_ERROR_DETECTOR::__cudaSafeCall(__FUNCTION__, err, __FILE__, __LINE__ ) // __FUNCTION__ https://stackoverflow.com/questions/679021/how-to-find-the-name-of-the-current-function-at-runtime
#define CudaCheckError() CUDA_ERROR_DETECTOR::__cudaCheckError(__FUNCTION__, __FILE__, __LINE__ )
#include <cuda_runtime_api.h>
#include <iostream>
#include <string> // https://stackoverflow.com/questions/6320995/why-i-cannot-cout-a-string
class CUDA_ERROR_DETECTOR
{
public:
static inline void exit_on_error(const std::string func_name, const cudaError err, const char* file, const int line)
{
if (cudaSuccess != err)
{
std::cout << func_name << " failed at " << file << " : " << line << " : " << cudaGetErrorString(err) << "\n";
exit(-1);
}
}
static inline void __cudaSafeCall(const std::string func_name, const cudaError err, const char* file, const int line)
{
#ifdef CUDA_ERROR_CHECK
exit_on_error(func_name, err, file, line);
#endif
}
static inline void __cudaCheckError(const std::string func_name, const char* file, const int line)
{
#ifdef CUDA_ERROR_CHECK
cudaError err = cudaGetLastError();
exit_on_error(func_name, err, file, line);
// More careful checking. However, this will affect performance.
// Comment away if needed.
cudaError sync_error = cudaDeviceSynchronize();
exit_on_error(func_name, sync_error, file, line);
#endif
}
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment