Skip to content

Commit 2e9226b

Browse files
authored
Refactor MATX_CUDA_CHECK to prevent multiple evaluation (#957)
1 parent 12adf9b commit 2e9226b

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

include/matx/core/error.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ namespace matx
153153

154154
#define MATX_EXIT_HANDLER() \
155155
} \
156-
catch (matx::detail::matxException & e) \
156+
catch (matx::detail::matxException & e) \
157157
{ \
158158
fprintf(stderr, "%s\n", e.what()); \
159159
fprintf(stderr, "Stack Trace:\n%s", e.stack.str().c_str()); \
@@ -185,10 +185,10 @@ namespace matx
185185
#define MATX_ASSERT_STR_EXP(a, expected, error, str) \
186186
{ \
187187
auto tmp = a; \
188-
if ((tmp != expected)) \
188+
if ((tmp != expected)) \
189189
{ \
190190
std::cout << #a ": " << str << "(" << tmp << " != " << expected << ")\n";\
191-
MATX_THROW(error, ""); \
191+
MATX_THROW(error, ""); \
192192
} \
193193
}
194194

@@ -205,30 +205,33 @@ namespace matx
205205

206206
#define MATX_STATIC_ASSERT_STR(a, error, str) \
207207
{ \
208-
static_assert((a), #error ": " #str); \
208+
static_assert((a), #error ": " #str); \
209209
}
210210

211211
#define MATX_CUDA_CHECK(e) \
212-
if (e != cudaSuccess) \
213-
{ \
214-
fprintf(stderr, "%s:%d CUDA Error: %s\n", __FILE__,__LINE__, cudaGetErrorString(e)); \
215-
MATX_THROW(matx::matxCudaError, cudaGetErrorString(e)); \
216-
}
212+
do { \
213+
const auto e_ = (e); \
214+
if (e_ != cudaSuccess) \
215+
{ \
216+
fprintf(stderr, "%s:%d CUDA Error: %s (%d)\n", __FILE__,__LINE__, cudaGetErrorString(e_), e_); \
217+
MATX_THROW(matx::matxCudaError, cudaGetErrorString(e_)); \
218+
} \
219+
} while (0)
217220

218221
// Macro for checking cuda errors following a cuda launch or api call
219-
#define MATX_CUDA_CHECK_LAST_ERROR() \
222+
#define MATX_CUDA_CHECK_LAST_ERROR() \
220223
{ \
221224
const auto e = cudaGetLastError(); \
222225
MATX_CUDA_CHECK(e); \
223226
}
224227

225228
// This macro asserts compatible dimensions of current class to an operator.
226-
#define MATX_ASSERT_COMPATIBLE_OP_SIZES(op) \
229+
#define MATX_ASSERT_COMPATIBLE_OP_SIZES(op) \
227230
if constexpr (Rank() > 0) { \
228231
bool compatible = true; \
229232
_Pragma("unroll") \
230233
for (int32_t i = 0; i < Rank(); i++) { \
231-
[[maybe_unused]] index_t size = matx::detail::get_expanded_size<Rank()>(op, i); \
234+
[[maybe_unused]] index_t size = matx::detail::get_expanded_size<Rank()>(op, i); \
232235
compatible = (size == 0 || size == Size(i)); \
233236
} \
234237
if (!compatible) { \
@@ -248,7 +251,7 @@ namespace matx
248251
} \
249252
std::cerr << ")" << std::endl; \
250253
MATX_THROW(matxInvalidSize, "Incompatible operator sizes"); \
251-
} \
254+
} \
252255
}
253256

254257
} // end namespace matx

0 commit comments

Comments
 (0)