Skip to content

Commit 7e7476e

Browse files
authored
Update error messages to include function name, error name, and error description (#8)
* Add whip::get_error_name * Add error name and string to exception description * Add constructor to exception which accepts a function name to be printed alongside the error code and string * Add function name when checking errors on wrapped functions
1 parent acba8b3 commit 7e7476e

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ target_include_directories(
3939
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
4040
target_compile_features(whip INTERFACE cxx_std_17)
4141
target_compile_definitions(whip INTERFACE "WHIP_${WHIP_BACKEND}")
42+
target_compile_definitions(whip INTERFACE "WHIP_BACKEND=${WHIP_BACKEND}")
4243
target_compile_options(
4344
whip INTERFACE
4445
$<$<AND:$<COMPILE_LANGUAGE:CUDA>,$<CUDA_COMPILER_ID:NVIDIA>>:--extended-lambda --expt-relaxed-constexpr>)

cmake/whip.hpp.in

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
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

3034
namespace whip {
3135
inline 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
5175
class exception final : public std::runtime_error {
5276
public:
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

5682
private:
@@ -65,16 +91,22 @@ inline void check_error(error_t e) {
6591
}
6692

6793
namespace 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

cmake/whip_helpers.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ function(whip_add_function var wrapper params wrapped)
6969
string(REGEX REPLACE "^, " "" wrapped_call_args "${wrapped_call_args}")
7070
set(wrapped_call "${prefixed_wrapped}(${wrapped_call_args})")
7171

72-
set(checker "whip::check_error")
72+
set(checker "whip::impl::check_error")
7373
if(whip_add_function_QUERY)
7474
set(checker "whip::impl::check_error_query")
7575
endif()
76-
set(fun "inline constexpr auto ${wrapper} = [](${wrapper_definition_params}) { return ${checker}(${wrapped_call}); };")
76+
set(fun "inline constexpr auto ${wrapper} = [](${wrapper_definition_params}) { return ${checker}(${wrapped_call}, \"${prefixed_wrapped}\"); };")
7777
set(${var} "${${var}}\n${fun}" PARENT_SCOPE)
7878
endfunction()

0 commit comments

Comments
 (0)