From 7fcd3c24a57518db740e5025260a2822ce87c9ba Mon Sep 17 00:00:00 2001 From: Chris Maffeo <cmaffeo2@illinois.edu> Date: Wed, 28 Aug 2024 15:47:37 -0500 Subject: [PATCH] Improve exception handling and logging on device code --- src/ARBDException.h | 33 ++++++++++++++++++++------------- src/GPUManager.h | 2 +- src/SignalManager.h | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/ARBDException.h b/src/ARBDException.h index d310a33..e47bfe2 100644 --- a/src/ARBDException.h +++ b/src/ARBDException.h @@ -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(...) \ diff --git a/src/GPUManager.h b/src/GPUManager.h index 1650ae3..24ffe5b 100644 --- a/src/GPUManager.h +++ b/src/GPUManager.h @@ -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 /* diff --git a/src/SignalManager.h b/src/SignalManager.h index 704f39b..5712583 100644 --- a/src/SignalManager.h +++ b/src/SignalManager.h @@ -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 //*/ /* -- GitLab