19 #include <rmm/error.hpp>
21 #include <cuda_runtime_api.h>
27 #include <type_traits>
29 #define STRINGIFY_DETAIL(x) #x
30 #define RMM_STRINGIFY(x) STRINGIFY_DETAIL(x)
56 #define RMM_EXPECTS(...) \
57 GET_RMM_EXPECTS_MACRO(__VA_ARGS__, RMM_EXPECTS_3, RMM_EXPECTS_2) \
59 #define GET_RMM_EXPECTS_MACRO(_1, _2, _3, NAME, ...) NAME
60 #define RMM_EXPECTS_3(_condition, _reason, _exception_type) \
62 static_assert(std::is_base_of_v<std::exception, _exception_type>); \
64 (!!(_condition)) ? static_cast<void>(0) \
65 : throw _exception_type{std::string{"RMM failure at: "} + __FILE__ + ":" + \
66 RMM_STRINGIFY(__LINE__) + ": " + _reason}; \
68 #define RMM_EXPECTS_2(_condition, _reason) RMM_EXPECTS_3(_condition, _reason, rmm::logic_error)
82 #define RMM_FAIL(...) \
83 GET_RMM_FAIL_MACRO(__VA_ARGS__, RMM_FAIL_2, RMM_FAIL_1) \
85 #define GET_RMM_FAIL_MACRO(_1, _2, NAME, ...) NAME
86 #define RMM_FAIL_2(_what, _exception_type) \
88 throw _exception_type \
90 std::string{"RMM failure at:"} + __FILE__ + ":" + RMM_STRINGIFY(__LINE__) + ": " + _what \
92 #define RMM_FAIL_1(_what) RMM_FAIL_2(_what, rmm::logic_error)
115 #define RMM_CUDA_TRY(...) \
116 GET_RMM_CUDA_TRY_MACRO(__VA_ARGS__, RMM_CUDA_TRY_2, RMM_CUDA_TRY_1) \
118 #define GET_RMM_CUDA_TRY_MACRO(_1, _2, NAME, ...) NAME
119 #define RMM_CUDA_TRY_2(_call, _exception_type) \
121 cudaError_t const error = (_call); \
122 if (cudaSuccess != error) { \
123 cudaGetLastError(); \
125 throw _exception_type{std::string{"CUDA error at: "} + __FILE__ + ":" + \
126 RMM_STRINGIFY(__LINE__) + ": " + cudaGetErrorName(error) + " " + \
127 cudaGetErrorString(error)}; \
130 #define RMM_CUDA_TRY_1(_call) RMM_CUDA_TRY_2(_call, rmm::cuda_error)
146 #define RMM_CUDA_TRY_ALLOC(...) \
147 GET_RMM_CUDA_TRY_ALLOC_MACRO(__VA_ARGS__, RMM_CUDA_TRY_ALLOC_2, RMM_CUDA_TRY_ALLOC_1) \
149 #define GET_RMM_CUDA_TRY_ALLOC_MACRO(_1, _2, NAME, ...) NAME
151 #define RMM_CUDA_TRY_ALLOC_2(_call, num_bytes) \
153 cudaError_t const error = (_call); \
154 if (cudaSuccess != error) { \
155 cudaGetLastError(); \
156 auto const msg = std::string{"CUDA error (failed to allocate "} + \
157 std::to_string(num_bytes) + " bytes) at: " + __FILE__ + ":" + \
158 RMM_STRINGIFY(__LINE__) + ": " + cudaGetErrorName(error) + " " + \
159 cudaGetErrorString(error); \
160 if (cudaErrorMemoryAllocation == error) { throw rmm::out_of_memory{msg}; } \
161 throw rmm::bad_alloc{msg}; \
165 #define RMM_CUDA_TRY_ALLOC_1(_call) \
167 cudaError_t const error = (_call); \
168 if (cudaSuccess != error) { \
169 cudaGetLastError(); \
170 auto const msg = std::string{"CUDA error at: "} + __FILE__ + ":" + RMM_STRINGIFY(__LINE__) + \
171 ": " + cudaGetErrorName(error) + " " + cudaGetErrorString(error); \
172 if (cudaErrorMemoryAllocation == error) { throw rmm::out_of_memory{msg}; } \
173 throw rmm::bad_alloc{msg}; \
203 #define RMM_ASSERT_CUDA_SUCCESS(_call) \
208 #define RMM_ASSERT_CUDA_SUCCESS(_call) \
210 cudaError_t const status__ = (_call); \
211 if (status__ != cudaSuccess) { \
212 std::cerr << "CUDA Error detected. " << cudaGetErrorName(status__) << " " \
213 << cudaGetErrorString(status__) << std::endl; \
216 assert(status__ == cudaSuccess); \