Skip to content
Snippets Groups Projects
Commit 7fcd3c24 authored by cmaffeo2's avatar cmaffeo2
Browse files

Improve exception handling and logging on device code

parent 5217f4a4
No related branches found
No related tags found
No related merge requests found
......@@ -11,15 +11,21 @@
#include <exception>
#include "SignalManager.h"
enum ExceptionType {
UnspeficiedError,
NotImplementedError,
ValueError,
DivideByZeroError,
CUDARuntimeError,
FileIoError,
FileOpenError
struct ExceptionType {
public:
explicit constexpr ExceptionType(int value) noexcept : value{value} {};
constexpr operator int() const { return value; };
private:
const int value;
};
// inline constexpr ExceptionType const UnspecifiedError{0};
constexpr ExceptionType const UnspecifiedError{0};
constexpr ExceptionType const NotImplementedError{1};
constexpr ExceptionType const ValueError{2};
constexpr ExceptionType const DivideByZeroError{3};
constexpr ExceptionType const CUDARuntimeError{4};
constexpr ExceptionType const FileIoError{5};
constexpr ExceptionType const FileOpenError{6};
class _ARBDException : public std::exception
{
......@@ -29,7 +35,7 @@ class _ARBDException : public std::exception
std::string sformat(const std::string &fmt, va_list &ap);
static std::string type_to_str(ExceptionType type) {
switch (type) {
case UnspeficiedError:
case UnspecifiedError:
return "Error";
case NotImplementedError:
return "NotImplementedError";
......@@ -44,11 +50,12 @@ class _ARBDException : public std::exception
};
// #include "common_macros.h"
#define S1(x) #x
#define S2(x) S1(x)
#define LOCATION __FILE__ "(" S2(__LINE__)")"
#ifdef CUDACC
#define Exception(...) throw _ARBDException(LOCATION, __VA_ARGS__)
#else
#define Exception(EXCPT,...) printf("Runtime CUDA exception at %s: ", LOCATION); \
printf("%s %s\n", #EXCPT, __VA_ARGS__);
#endif
//use illegal instruction to abort; used in functions defined both in __host__ and __device__
#if 0
#define CudaException(...) \
......
......@@ -81,7 +81,7 @@ struct has_copy_to_cuda<T, decltype(std::declval<T>().copy_to_cuda(), void())> :
#ifndef gpuErrchk
#define delgpuErrchk
#define gpuErrchk(code) { if ((code) != cudaSuccess) { \
Exception(CUDARuntimeError, " ", cudaGetErrorString(code)); \
Exception(CUDARuntimeError, cudaGetErrorString(code)); \
}}
#endif
/*
......
......@@ -11,7 +11,22 @@
/* #define ARBD_LOG_ACTIVE_LEVEL 0 */
/* #include "logger.h" */
#define S1(x) #x
#define S2(x) S1(x)
#define LOCATION __FILE__ "(" S2(__LINE__)")" /* mainly used by ARBDException */
//*
#ifdef __CUDA_ARCH__
// #define LOGHELPER(TYPE, FMT,...) printf("[%d,%d] [%s] [%s]: %s", blockIdx.x, threadIdx.x, TYPE, LOCATION, FMT);
#define LOGHELPER(TYPE, FMT,...) printf("[%s] [%s]: %s\n", TYPE, LOCATION, FMT);
#define LOGTRACE(...) LOGHELPER("trace", __VA_ARGS__)
#define LOGDEBUG(...) LOGHELPER("debug",__VA_ARGS__)
// #define DEBUG(...) spdlog::debug(__VA_ARGS__)
#define LOGINFO(...) LOGHELPER("info",__VA_ARGS__)
#define LOGWARN(...) LOGHELPER("warn",__VA_ARGS__)
#define LOGERROR(...) LOGHELPER("error",__VA_ARGS__)
#define LOGCRITICAL(...) LOGHELPER("critical",__VA_ARGS__)
#else
#define FMT_HEADER_ONLY
#include <spdlog/fmt/bundled/core.h>
#include <spdlog/fmt/bundled/format.h>
......@@ -29,6 +44,7 @@
#define LOGERROR(...) SPDLOG_ERROR(__VA_ARGS__)
#define LOGCRITICAL(...) SPDLOG_CRITICAL(__VA_ARGS__)
// spdlog::set_level(spdlog::level::trace);
#endif
//*/
/*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment