8 #include <cudf/utilities/export.hpp>
11 #include <cuda_runtime_api.h>
15 #include <type_traits>
17 namespace CUDF_EXPORT
cudf {
65 explicit cuda_error(std::string
const& message, cudaError_t
const& error)
66 : std::runtime_error(message), _cudaError(error)
75 [[nodiscard]] cudaError_t
error_code()
const {
return _cudaError; }
82 using cuda_error::cuda_error;
98 explicit data_type_error(
char const*
const message) : std::invalid_argument(message) {}
105 explicit data_type_error(std::string
const& message) : std::invalid_argument(message) {}
111 #define STRINGIFY_DETAIL(x) #x
112 #define CUDF_STRINGIFY(x) STRINGIFY_DETAIL(x)
145 #define CUDF_EXPECTS(...) \
146 GET_CUDF_EXPECTS_MACRO(__VA_ARGS__, CUDF_EXPECTS_3, CUDF_EXPECTS_2) \
151 #define GET_CUDF_EXPECTS_MACRO(_1, _2, _3, NAME, ...) NAME
153 #define CUDF_EXPECTS_3(_condition, _reason, _exception_type) \
155 static_assert(std::is_base_of_v<std::exception, _exception_type>); \
156 if (!(_condition)) { \
157 cudf::detail::cudf_fail<_exception_type>( \
158 [&]() -> std::string { return _reason; }, __LINE__, __FILE__); \
162 #define CUDF_EXPECTS_2(_condition, _reason) CUDF_EXPECTS_3(_condition, _reason, cudf::logic_error)
186 #define CUDF_FAIL(...) \
187 GET_CUDF_FAIL_MACRO(__VA_ARGS__, CUDF_FAIL_2, CUDF_FAIL_1) \
192 #define GET_CUDF_FAIL_MACRO(_1, _2, NAME, ...) NAME
194 #define CUDF_FAIL_2(_what, _exception_type) \
195 cudf::detail::cudf_fail<_exception_type>( \
196 [&]() -> std::string { return _what; }, __LINE__, __FILE__)
198 #define CUDF_FAIL_1(_what) CUDF_FAIL_2(_what, cudf::logic_error)
202 namespace CUDF_EXPORT
cudf {
205 inline void throw_cuda_error(cudaError_t error,
char const* file,
unsigned int line)
210 auto const last = cudaFree(
nullptr);
211 auto const msg = std::string{
"CUDA error encountered at: " + std::string{file} +
":" +
212 std::to_string(line) +
": " + std::to_string(error) +
" " +
213 cudaGetErrorName(error) +
" " + cudaGetErrorString(error)};
216 if (error == last && last == cudaDeviceSynchronize()) {
217 throw fatal_cuda_error{
"Fatal " + msg, error};
219 throw cuda_error{msg, error};
225 template <
typename Exception,
typename MsgFunc>
226 [[noreturn]]
void cudf_fail(MsgFunc&& msg_func,
int line_number,
char const* filename)
228 std::string
const msg = std::forward<MsgFunc>(msg_func)();
229 throw Exception{std::string{
"CUDF failure at: "} + filename +
":" + std::to_string(line_number) +
230 ": " + (msg.empty() ?
"(no message)" : msg)};
243 #define CUDF_CUDA_TRY(call) \
245 cudaError_t const status = (call); \
246 if (cudaSuccess != status) { cudf::detail::throw_cuda_error(status, __FILE__, __LINE__); } \
263 #define CUDF_CHECK_CUDA(stream) \
265 CUDF_CUDA_TRY(cudaStreamSynchronize(stream)); \
266 CUDF_CUDA_TRY(cudaPeekAtLastError()); \
269 #define CUDF_CHECK_CUDA(stream) CUDF_CUDA_TRY(cudaPeekAtLastError());
Exception thrown when a CUDA error is encountered.
cuda_error(std::string const &message, cudaError_t const &error)
Construct a new cuda error object with error message and code.
cudaError_t _cudaError
CUDA error code.
cudaError_t error_code() const
Returns the CUDA error code associated with the exception.
Exception thrown when an operation is attempted on an unsupported dtype.
data_type_error(std::string const &message)
Construct a new data_type_error object with error message.
data_type_error(char const *const message)
Constructs a data_type_error with the error message.
Exception thrown when logical precondition is violated.
logic_error(char const *const message)
Constructs a logic_error with the error message.
logic_error(std::string const &message)
Construct a new logic error object with error message.