-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy patherror.hpp
268 lines (249 loc) · 10.8 KB
/
error.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <rmm/logger.hpp>
#include <cuda_runtime_api.h>
#include <cassert>
#include <iostream>
#include <stdexcept>
#include <string>
namespace rmm {
/**
* @brief Exception thrown when logical precondition is violated.
*
* This exception should not be thrown directly and is instead thrown by the
* RMM_EXPECTS macro.
*
*/
struct logic_error : public std::logic_error {
using std::logic_error::logic_error;
};
/**
* @brief Exception thrown when a CUDA error is encountered.
*
*/
struct cuda_error : public std::runtime_error {
using std::runtime_error::runtime_error;
};
/**
* @brief Exception thrown when an RMM allocation fails
*
*/
class bad_alloc : public std::bad_alloc {
public:
bad_alloc(const char* msg) : _what{std::string{std::bad_alloc::what()} + ": " + msg} {}
bad_alloc(std::string const& msg) : bad_alloc{msg.c_str()} {}
[[nodiscard]] const char* what() const noexcept override { return _what.c_str(); }
private:
std::string _what;
};
/**
* @brief Exception thrown when RMM runs out of memory
*
* This error should only be thrown when we know for sure a resource is out of memory.
*/
class out_of_memory : public bad_alloc {
public:
out_of_memory(const char* msg) : bad_alloc{std::string{"out_of_memory: "} + msg} {}
out_of_memory(std::string const& msg) : out_of_memory{msg.c_str()} {}
};
/**
* @brief Exception thrown when attempting to access outside of a defined range
*
*/
class out_of_range : public std::out_of_range {
using std::out_of_range::out_of_range;
};
} // namespace rmm
#define STRINGIFY_DETAIL(x) #x
#define RMM_STRINGIFY(x) STRINGIFY_DETAIL(x)
/**
* @brief Macro for checking (pre-)conditions that throws an exception when
* a condition is violated.
*
* Defaults to throwing `rmm::logic_error`, but a custom exception may also be
* specified.
*
* Example usage:
* ```
* // throws rmm::logic_error
* RMM_EXPECTS(p != nullptr, "Unexpected null pointer");
*
* // throws std::runtime_error
* RMM_EXPECTS(p != nullptr, std::runtime_error, "Unexpected nullptr");
* ```
* @param[in] _condition Expression that evaluates to true or false
* @param[in] _expection_type The exception type to throw; must inherit
* `std::exception`. If not specified (i.e. if only two macro
* arguments are provided), defaults to `cudf::logic_error`
* @param[in] _what String literal description of why the exception was
* thrown, i.e. why `_condition` was expected to be true.
* @throw `_exception_type` if the condition evaluates to 0 (false).
*/
#define RMM_EXPECTS(...) \
GET_RMM_EXPECTS_MACRO(__VA_ARGS__, RMM_EXPECTS_3, RMM_EXPECTS_2) \
(__VA_ARGS__)
#define GET_RMM_EXPECTS_MACRO(_1, _2, _3, NAME, ...) NAME
#define RMM_EXPECTS_3(_condition, _exception_type, _reason) \
(!!(_condition)) ? static_cast<void>(0) \
: throw _exception_type /*NOLINT(bugprone-macro-parentheses)*/ \
{ \
"RMM failure at: " __FILE__ ":" RMM_STRINGIFY(__LINE__) ": " _reason \
}
#define RMM_EXPECTS_2(_condition, _reason) RMM_EXPECTS_3(_condition, rmm::logic_error, _reason)
/**
* @brief Indicates that an erroneous code path has been taken.
*
* Example usage:
* ```c++
* // Throws `rmm::logic_error`
* RMM_FAIL("Unsupported code path");
*
* // Throws `std::runtime_error`
* RMM_FAIL("Unsupported code path", std::runtime_error);
* ```
*/
#define RMM_FAIL(...) \
GET_RMM_FAIL_MACRO(__VA_ARGS__, RMM_FAIL_2, RMM_FAIL_1) \
(__VA_ARGS__)
#define GET_RMM_FAIL_MACRO(_1, _2, NAME, ...) NAME
#define RMM_FAIL_2(_what, _exception_type) \
/*NOLINTNEXTLINE(bugprone-macro-parentheses)*/ \
throw _exception_type{"RMM failure at:" __FILE__ ":" RMM_STRINGIFY(__LINE__) ": " _what};
#define RMM_FAIL_1(_what) RMM_FAIL_2(_what, rmm::logic_error)
/**
* @brief Error checking macro for CUDA runtime API functions.
*
* Invokes a CUDA runtime API function call. If the call does not return
* `cudaSuccess`, invokes cudaGetLastError() to clear the error and throws an
* exception detailing the CUDA error that occurred
*
* Defaults to throwing `rmm::cuda_error`, but a custom exception may also be
* specified.
*
* Example:
* ```c++
*
* // Throws `rmm::cuda_error` if `cudaMalloc` fails
* RMM_CUDA_TRY(cudaMalloc(&p, 100));
*
* // Throws `std::runtime_error` if `cudaMalloc` fails
* RMM_CUDA_TRY(cudaMalloc(&p, 100), std::runtime_error);
* ```
*
*/
#define RMM_CUDA_TRY(...) \
GET_RMM_CUDA_TRY_MACRO(__VA_ARGS__, RMM_CUDA_TRY_2, RMM_CUDA_TRY_1) \
(__VA_ARGS__)
#define GET_RMM_CUDA_TRY_MACRO(_1, _2, NAME, ...) NAME
#define RMM_CUDA_TRY_2(_call, _exception_type) \
do { \
cudaError_t const error = (_call); \
if (cudaSuccess != error) { \
cudaGetLastError(); \
/*NOLINTNEXTLINE(bugprone-macro-parentheses)*/ \
throw _exception_type{std::string{"CUDA error at: "} + __FILE__ + ":" + \
RMM_STRINGIFY(__LINE__) + ": " + cudaGetErrorName(error) + " " + \
cudaGetErrorString(error)}; \
} \
} while (0)
#define RMM_CUDA_TRY_1(_call) RMM_CUDA_TRY_2(_call, rmm::cuda_error)
/**
* @brief Error checking macro for CUDA memory allocation calls.
*
* Invokes a CUDA memory allocation function call. If the call does not return
* `cudaSuccess`, invokes cudaGetLastError() to clear the error and throws an
* exception detailing the CUDA error that occurred
*
* Defaults to throwing `rmm::bad_alloc`, but when `cudaErrorMemoryAllocation` is returned,
* `rmm::out_of_memory` is thrown instead.
*/
#define RMM_CUDA_TRY_ALLOC(_call) \
do { \
cudaError_t const error = (_call); \
if (cudaSuccess != error) { \
cudaGetLastError(); \
auto const msg = std::string{"CUDA error at: "} + __FILE__ + ":" + RMM_STRINGIFY(__LINE__) + \
": " + cudaGetErrorName(error) + " " + cudaGetErrorString(error); \
if (cudaErrorMemoryAllocation == error) { \
throw rmm::out_of_memory{msg}; \
} else { \
throw rmm::bad_alloc{msg}; \
} \
} \
} while (0)
/**
* @brief Error checking macro similar to `assert` for CUDA runtime API calls
*
* This utility should be used in situations where extra error checking is desired in "Debug"
* builds, or in situations where an error case cannot throw an exception (such as a class
* destructor).
*
* In "Release" builds, simply invokes the `_call`.
*
* In "Debug" builds, invokes `_call` and uses `assert` to verify the returned `cudaError_t` is
* equal to `cudaSuccess`.
*
*
* Replaces usecases such as:
* ```
* auto status = cudaRuntimeApi(...);
* assert(status == cudaSuccess);
* ```
*
* Example:
* ```
* RMM_ASSERT_CUDA_SUCCESS(cudaRuntimeApi(...));
* ```
*
*/
#ifdef NDEBUG
#define RMM_ASSERT_CUDA_SUCCESS(_call) \
do { \
(_call); \
} while (0);
#else
#define RMM_ASSERT_CUDA_SUCCESS(_call) \
do { \
cudaError_t const status__ = (_call); \
if (status__ != cudaSuccess) { \
std::cerr << "CUDA Error detected. " << cudaGetErrorName(status__) << " " \
<< cudaGetErrorString(status__) << std::endl; \
} \
/* NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay) */ \
assert(status__ == cudaSuccess); \
} while (0)
#endif
/**
* @brief Assertion that logs a CRITICAL log message on failure.
*/
#ifdef NDEBUG
#define RMM_LOGGING_ASSERT(_expr) (void)0
#elif SPDLOG_ACTIVE_LEVEL < SPDLOG_LEVEL_OFF
#define RMM_LOGGING_ASSERT(_expr) \
do { \
bool const success = (_expr); \
if (!success) { \
RMM_LOG_CRITICAL( \
"[" __FILE__ ":" RMM_STRINGIFY(__LINE__) "] Assertion " RMM_STRINGIFY(_expr) " failed."); \
rmm::logger().flush(); \
/* NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay) */ \
assert(success); \
} \
} while (0)
#else
#define RMM_LOGGING_ASSERT(_expr) assert((_expr));
#endif