OpenMPCD
OPENMPCD_DEBUG_ASSERT.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Defines the `OPENMPCD_DEBUG_ASSERT` macro.
4  */
5 
6 #ifndef OPENMPCD_DEBUG_ASSERT_HPP
7 #define OPENMPCD_DEBUG_ASSERT_HPP
8 
9 /**
10  * @def OPENMPCD_DEBUG_ASSERT_HOST_EXCEPTIONTYPE
11  * If `OPENMPCD_DEBUG` is defined, this macro checks that the given condition
12  * evaluates to `true`, and throws an instance of `ExceptionType`
13  * otherwise.
14  *
15  * If `OPENMPCD_DEBUG` is not defined, this macro does nothing.
16  */
17 
18 #ifdef OPENMPCD_DEBUG
19  #include <OpenMPCD/Exceptions.hpp>
20  #define OPENMPCD_DEBUG_ASSERT_HOST_EXCEPTIONTYPE(assertion, ExceptionType) \
21  do { \
22  if(!(assertion)) \
23  OPENMPCD_THROW(ExceptionType, #assertion); \
24  } while(false)
25 #else
26  #define OPENMPCD_DEBUG_ASSERT_HOST_EXCEPTIONTYPE(assertion, ExceptionType) \
27  do{} while(false)
28 #endif
29 
30 /**
31  * If `OPENMPCD_DEBUG` is defined, this macro checks that the given condition
32  * evaluates to `true`, and throws an instance of `OpenMPCD::AssertionException`
33  * otherwise.
34  *
35  * If `OPENMPCD_DEBUG` is not defined, this macro does nothing.
36  */
37 #define OPENMPCD_DEBUG_ASSERT_HOST(assertion) \
38  OPENMPCD_DEBUG_ASSERT_HOST_EXCEPTIONTYPE( \
39  assertion, OpenMPCD::AssertionException)
40 
41 
42 /**
43  * @def OPENMPCD_DEBUG_ASSERT_DEVICE
44  * If `OPENMPCD_DEBUG` is defined, this macro checks that the given condition
45  * evaluates to `true`, and calls `assert` otherwise.
46  *
47  * If `OPENMPCD_DEBUG` is not defined, this macro does nothing.
48  */
49 
50 #ifdef OPENMPCD_DEBUG
51  #include <assert.h>
52  #define OPENMPCD_DEBUG_ASSERT_DEVICE(assertion) \
53  do { \
54  assert(assertion); \
55  } while(false)
56 #else
57  #define OPENMPCD_DEBUG_ASSERT_DEVICE(assertion) do{} while(false)
58 #endif
59 
60 
61 /**
62  * @def OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE
63  *
64  * Asserts that the given expression evaluates to `true`, but only if
65  * `OPENMPCD_DEBUG` is defined.
66  *
67  * If CUDA `__device__` or `__global__` functions are being compiled, this macro
68  * expands to `OPENMPCD_DEBUG_ASSERT_DEVICE`, and otherwise expands to
69  * `OPENMPCD_DEBUG_ASSERT_HOST_EXCEPTIONTYPE`, with the supplied `ExceptionType`
70  * being passed to that macro.
71  */
72 #ifdef __CUDA_ARCH__
73  #define OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE(assertion, ExceptionType) \
74  OPENMPCD_DEBUG_ASSERT_DEVICE(assertion)
75 #else
76  #define OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE(assertion, ExceptionType) \
77  OPENMPCD_DEBUG_ASSERT_HOST_EXCEPTIONTYPE(assertion, ExceptionType)
78 #endif
79 
80 /**
81  * Asserts that the given expression evaluates to `true`, but only if
82  * `OPENMPCD_DEBUG` is defined.
83  *
84  * If CUDA `__device__` or `__global__` functions are being compiled, this macro
85  * expands to `OPENMPCD_DEBUG_ASSERT_DEVICE`, and otherwise expands to
86  * `OPENMPCD_DEBUG_ASSERT_HOST`.
87  */
88 #define OPENMPCD_DEBUG_ASSERT(assertion) \
89  OPENMPCD_DEBUG_ASSERT_EXCEPTIONTYPE(assertion, OpenMPCD::AssertionException)
90 
91 #endif //OPENMPCD_DEBUG_ASSERT_HPP
Exceptions.hpp