2424#include <hip/hip_runtime.h>
2525#endif
2626
27+ #define WHIP_STRINGIFY(x) WHIP_STRINGIFY_IMPL(x)
28+ #define WHIP_STRINGIFY_IMPL(x) #x
29+
2730#include <cstddef>
2831#include <stdexcept>
32+ #include <string>
2933
3034namespace whip {
3135inline constexpr std::size_t version_major = @PROJECT_VERSION_MAJOR@;
@@ -47,10 +51,32 @@ inline const char* get_error_string(error_t error) {
4751#endif
4852}
4953
54+ inline const char *get_error_name(error_t error) {
55+ #if defined(WHIP_CUDA)
56+ return cudaGetErrorName(error);
57+ #elif defined(WHIP_HIP)
58+ return hipGetErrorName(error);
59+ #endif
60+ }
61+
62+ namespace impl {
63+ inline std::string make_error_string(error_t error, char const *function) {
64+ return std::string("[whip] ") + function + " returned " + get_error_name(error) + " (" + get_error_string(error) +
65+ ")";
66+ }
67+
68+ inline std::string make_error_string(error_t error) {
69+ return std::string("[whip] ") + WHIP_STRINGIFY(WHIP_BACKEND) " function call returned " + get_error_name(error) + " (" +
70+ get_error_string(error) + ")";
71+ }
72+ } // namespace impl
73+
5074// Custom exception which wraps a CUDA/HIP error
5175class exception final : public std::runtime_error {
5276public:
53- explicit exception(error_t error) : std::runtime_error(get_error_string(error)), error(error) {}
77+ explicit exception(error_t error) : std::runtime_error(impl::make_error_string(error)), error(error) {}
78+ explicit exception(error_t error, char const *function)
79+ : std::runtime_error(impl::make_error_string(error, function)), error(error) {}
5480 error_t get_error() const noexcept { return error; }
5581
5682private:
@@ -65,16 +91,22 @@ inline void check_error(error_t e) {
6591}
6692
6793namespace impl {
94+ inline void check_error(error_t e, char const *function) {
95+ if (e != success) {
96+ throw exception(e, function);
97+ }
98+ }
99+
68100// Check an error and throw an exception on failure, except error_not_ready.
69101// This is useful for query functions.
70- inline bool check_error_query(error_t e) {
102+ inline bool check_error_query(error_t e, char const *function ) {
71103 switch (e) {
72- case success:
73- return true;
74- case error_not_ready:
75- return false;
76- default:
77- throw exception(e);
104+ case success:
105+ return true;
106+ case error_not_ready:
107+ return false;
108+ default:
109+ throw exception(e, function );
78110 }
79111}
80112} // namespace impl
0 commit comments