11 #include <source_location>
15 #include <string_view>
17 #include <rapidsmpf/utils/misc.hpp>
27 using std::runtime_error::runtime_error;
57 [[nodiscard]]
char const*
what() const noexcept
override {
80 :
bad_alloc{std::string{
"out_of_memory: "} + msg} {}
108 :
bad_alloc{std::string{
"reservation_error: "} + msg} {}
128 std::string_view reason,
129 std::source_location
const& loc = std::source_location::current()
131 std::ostringstream ss;
132 ss <<
"RapidsMPF fatal error at: " << loc.file_name() <<
":" << loc.line() <<
": "
145 template <
typename ThrowFn>
148 std::string_view reason,
150 std::source_location
const& loc = std::source_location::current()
165 cudaError_t error, std::source_location
const& loc = std::source_location::current()
167 std::ostringstream ss;
168 ss <<
"CUDA error at: " << loc.file_name() <<
":" << loc.line() <<
": "
169 << cudaGetErrorName(error) <<
" " << cudaGetErrorString(error);
183 std::size_t num_bytes,
184 std::source_location
const& loc = std::source_location::current()
186 std::ostringstream ss;
187 ss <<
"CUDA error (failed to allocate " << num_bytes
188 <<
" bytes) at: " << loc.file_name() <<
":" << loc.line() <<
": "
189 << cudaGetErrorName(error) <<
" " << cudaGetErrorString(error);
204 std::string_view reason,
205 std::source_location
const& loc = std::source_location::current()
207 std::cerr <<
"RapidsMPF fatal error at: " << loc.file_name() <<
":" << loc.line()
208 <<
": " << reason << std::endl;
221 std::string_view reason,
222 std::source_location
const& loc = std::source_location::current()
236 std::string_view reason,
237 std::source_location
const& loc = std::source_location::current()
268 #define RAPIDSMPF_EXPECTS(...) \
269 GET_RAPIDSMPF_EXPECTS_MACRO(__VA_ARGS__, RAPIDSMPF_EXPECTS_3, RAPIDSMPF_EXPECTS_2) \
272 #define GET_RAPIDSMPF_EXPECTS_MACRO(_1, _2, _3, NAME, ...) NAME
274 #define RAPIDSMPF_EXPECTS_3(_condition, _reason, _exception_type) \
276 static_assert(std::is_base_of_v<std::exception, _exception_type>); \
277 rapidsmpf::detail::expects_impl( \
278 static_cast<bool>(_condition), (_reason), [](auto&& msg) { \
279 throw _exception_type{msg}; \
284 #define RAPIDSMPF_EXPECTS_2(_condition, _reason) \
285 RAPIDSMPF_EXPECTS_3(_condition, _reason, std::logic_error)
306 #define RAPIDSMPF_FAIL(...) \
307 GET_RAPIDSMPF_FAIL_MACRO(__VA_ARGS__, RAPIDSMPF_FAIL_2, RAPIDSMPF_FAIL_1) \
310 #define GET_RAPIDSMPF_FAIL_MACRO(_1, _2, NAME, ...) NAME
312 #define RAPIDSMPF_FAIL_2(_what, _exception_type) \
313 throw _exception_type { \
314 rapidsmpf::detail::build_error_message((_what)) \
317 #define RAPIDSMPF_FAIL_1(_what) RAPIDSMPF_FAIL_2(_what, std::logic_error)
335 #define RAPIDSMPF_EXPECTS_FATAL(_condition, _reason) \
336 rapidsmpf::detail::expects_fatal_impl(static_cast<bool>(_condition), (_reason))
352 #define RAPIDSMPF_FATAL(_reason) rapidsmpf::detail::fatal_impl((_reason))
373 #define RAPIDSMPF_CUDA_TRY(...) \
374 GET_RAPIDSMPF_CUDA_TRY_MACRO( \
375 __VA_ARGS__, RAPIDSMPF_CUDA_TRY_2, RAPIDSMPF_CUDA_TRY_1 \
378 #define GET_RAPIDSMPF_CUDA_TRY_MACRO(_1, _2, NAME, ...) NAME
379 #define RAPIDSMPF_CUDA_TRY_2(_call, _exception_type) \
381 cudaError_t const error = (_call); \
382 if (cudaSuccess != error) { \
385 cudaGetLastError(); \
386 throw _exception_type{rapidsmpf::detail::build_cuda_error_message(error)}; \
389 #define RAPIDSMPF_CUDA_TRY_1(_call) RAPIDSMPF_CUDA_TRY_2(_call, rapidsmpf::cuda_error)
405 #define RAPIDSMPF_CUDA_TRY_FATAL(_call) \
407 cudaError_t const error = (_call); \
408 if (cudaSuccess != error) { \
409 std::cerr << rapidsmpf::detail::build_cuda_error_message(error) \
431 #define RAPIDSMPF_CUDA_TRY_ALLOC(...) \
432 GET_RAPIDSMPF_CUDA_TRY_ALLOC_MACRO( \
433 __VA_ARGS__, RAPIDSMPF_CUDA_TRY_ALLOC_2, RAPIDSMPF_CUDA_TRY_ALLOC_1 \
436 #define GET_RAPIDSMPF_CUDA_TRY_ALLOC_MACRO(_1, _2, NAME, ...) NAME
438 #define RAPIDSMPF_CUDA_TRY_ALLOC_2(_call, num_bytes) \
440 cudaError_t const error = (_call); \
441 if (cudaSuccess != error) { \
444 cudaGetLastError(); \
446 rapidsmpf::detail::build_cuda_alloc_error_message(error, (num_bytes)); \
447 if (cudaErrorMemoryAllocation == error) { \
448 throw rapidsmpf::out_of_memory{msg}; \
450 throw rapidsmpf::bad_alloc{msg}; \
454 #define RAPIDSMPF_CUDA_TRY_ALLOC_1(_call) \
456 cudaError_t const error = (_call); \
457 if (cudaSuccess != error) { \
460 cudaGetLastError(); \
461 auto const msg = rapidsmpf::detail::build_cuda_error_message(error); \
462 if (cudaErrorMemoryAllocation == error) { \
463 throw rapidsmpf::out_of_memory{msg}; \
465 throw rapidsmpf::bad_alloc{msg}; \
Exception thrown when a RapidsMPF allocation fails.
bad_alloc(char const *msg)
Construct a bad_alloc with the error message.
char const * what() const noexcept override
Return the explanatory string.
bad_alloc(std::string const &msg)
Construct a bad_alloc with the error message.
Exception thrown when RapidsMPF runs out of memory.
out_of_memory(std::string const &msg)
Construct an out_of_memory with the error message.
out_of_memory(char const *msg)
Construct an out_of_memory with the error message.
Exception thrown when a memory reservation fails in RapidsMPF.
reservation_error(char const *msg)
Construct a reservation_error with an error message.
reservation_error(std::string const &msg)
Construct a reservation_error with an error message.
std::string build_cuda_error_message(cudaError_t error, std::source_location const &loc=std::source_location::current())
Build a CUDA error message with source location information.
void fatal_impl(std::string_view reason, std::source_location const &loc=std::source_location::current()) noexcept
Implementation for RAPIDSMPF_FATAL.
constexpr void expects_fatal_impl(bool condition, std::string_view reason, std::source_location const &loc=std::source_location::current()) noexcept
Implementation for RAPIDSMPF_EXPECTS_FATAL.
constexpr void expects_impl(bool condition, std::string_view reason, ThrowFn &&throw_fn, std::source_location const &loc=std::source_location::current())
Core implementation for RAPIDSMPF_EXPECTS.
std::string build_error_message(std::string_view reason, std::source_location const &loc=std::source_location::current())
Build an error message with source location information.
std::string build_cuda_alloc_error_message(cudaError_t error, std::size_t num_bytes, std::source_location const &loc=std::source_location::current())
Build a CUDA allocation error message with source location information.
void fatal_error(std::string_view reason, std::source_location const &loc=std::source_location::current()) noexcept
Print a fatal error message and terminate.
RAPIDS Multi-Processor interfaces.
Exception thrown when a CUDA error is encountered.