EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_Macros.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
11#ifndef EBGEOMETRY_MACROS_HPP
12#define EBGEOMETRY_MACROS_HPP
13
14#include <cstdarg>
15#include <cstdio>
16#include <cstdlib>
17#include <stdexcept>
18#include <string>
19
20#include "EBGeometry_GPU.hpp"
21
22#if defined(EBGEOMETRY_HIP)
23// Declares the device printf used below. It must be visible before the first header that uses the
24// macros: in a template, a call that does not depend on a template parameter binds at the point of
25// definition, and would otherwise bind to the host printf.
26#include <hip/hip_runtime.h>
27#endif
28#if defined(EBGEOMETRY_DEVICE_COMPILE)
29// Device compilation pass (CUDA/HIP): std::fprintf/std::abort are host-only. assert() is not an
30// option either: NDEBUG, which CMake's Release configurations define, would silently remove every
31// device check while the host checks stayed. Print with the device printf and trap instead.
32#if defined(EBGEOMETRY_CUDA)
33#define EBGEOMETRY_DEVICE_TRAP() __trap()
34#else
35#define EBGEOMETRY_DEVICE_TRAP() __builtin_trap()
36#endif
37namespace EBGeometry {
38namespace MacrosDetail {
39// Out of line, like the backends' own assert: the device printf expands to a lot of code, and an
40// inline copy at every check site makes device compiles of large files take many minutes.
41__device__ __noinline__ inline void
42deviceAssertionFailed(const char* a_cond, const char* a_file, const int a_line)
43{
44 printf("EBGeometry device assertion failed: (%s)\n file: %s\n line: %d\n", a_cond, a_file, a_line);
45 EBGEOMETRY_DEVICE_TRAP();
46}
47} // namespace MacrosDetail
48} // namespace EBGeometry
49#endif
50
51namespace EBGeometry {
52
57class RequireError : public std::runtime_error
58{
59public:
60 using std::runtime_error::runtime_error;
61};
62
63namespace MacrosDetail {
64
69inline int&
71{
72 static thread_local int scopes = 0;
73
74 return scopes;
75}
76
88[[noreturn]]
89#if defined(__GNUC__)
90__attribute__((format(printf, 4, 5)))
91#endif
92inline void
93requireFailed(const char* a_cond, const char* a_file, const int a_line, const char* a_format, ...)
94{
95 // The message whole: its length first, then the text.
96 va_list arguments;
97 va_list again;
98
99 va_start(arguments, a_format);
100 va_copy(again, arguments);
101
102 const int length = std::vsnprintf(nullptr, 0, a_format, arguments);
103
104 // A message that cannot be formatted is reported by its format, so that the failure still says where it is.
105 std::string message(length > 0 ? static_cast<size_t>(length) : 0U, '\0');
106
107 if (length > 0) {
108 std::vsnprintf(&message[0], message.size() + 1, a_format, again);
109 }
110 else if (length < 0) {
111 message = a_format;
112 }
113
114 va_end(again);
115 va_end(arguments);
116
117 if (throwingScopes() > 0) {
118 throw RequireError(message);
119 }
120
121 std::fprintf(stderr, "%s\n check: (%s)\n file: %s\n line: %d\n", message.c_str(), a_cond, a_file, a_line);
122 std::abort();
123}
124
125} // namespace MacrosDetail
126
138{
139public:
143 ThrowOnRequire() noexcept
144 {
145 ++MacrosDetail::throwingScopes();
146 }
147
152 {
153 --MacrosDetail::throwingScopes();
154 }
155
156 ThrowOnRequire(const ThrowOnRequire&) = delete;
158 operator=(const ThrowOnRequire&) = delete;
159};
160
161} // namespace EBGeometry
162
192#if defined(EBGEOMETRY_DEVICE_COMPILE)
193#define EBGEOMETRY_REQUIRE(cond, ...) \
194 do { \
195 if (!(cond)) { \
196 ::EBGeometry::MacrosDetail::deviceAssertionFailed(#cond, __FILE__, __LINE__); \
197 } \
198 } while (0)
199#else
200#define EBGEOMETRY_REQUIRE(cond, ...) \
201 do { \
202 if (!(cond)) { \
203 ::EBGeometry::MacrosDetail::requireFailed(#cond, __FILE__, __LINE__, "EBGeometry::" __VA_ARGS__); \
204 } \
205 } while (0)
206#endif
207
243#if defined(EBGEOMETRY_ENABLE_ASSERTIONS)
244#if defined(EBGEOMETRY_DEVICE_COMPILE)
245#define EBGEOMETRY_EXPECT(cond) \
246 do { \
247 if (!(cond)) { \
248 ::EBGeometry::MacrosDetail::deviceAssertionFailed(#cond, __FILE__, __LINE__); \
249 } \
250 } while (0)
251#else
252#define EBGEOMETRY_EXPECT(cond) \
253 do { \
254 if (!(cond)) { \
255 std::fprintf(stderr, \
256 "EBGeometry assertion failed: (%s)\n file: %s\n line: %d\n function: %s\n", \
257 #cond, \
258 __FILE__, \
259 __LINE__, \
260 static_cast<const char*>(__func__)); \
261 std::abort(); \
262 } \
263 } while (0)
264#endif
265#else
266// Assertions off: expand to a no-op that does NOT evaluate cond -- an assertion costs exactly
267// nothing in release, including not running its predicate. A local computed solely to be asserted
268// therefore becomes unused; mark such a declaration [[maybe_unused]] at its site.
269#define EBGEOMETRY_EXPECT(cond) ((void)0)
270#endif
271
272#endif // EBGEOMETRY_MACROS_HPP
GPU portability layer: host/device decoration macros and backend detection.
void requireFailed(const char *a_cond, const char *a_file, const int a_line, const char *a_format,...)
A failed EBGEOMETRY_REQUIRE on the host: throws RequireError inside a ThrowOnRequire scope,...
Definition EBGeometry_Macros.hpp:93
int & throwingScopes() noexcept
How many ThrowOnRequire scopes are open on this thread.
Definition EBGeometry_Macros.hpp:70
What a failed EBGEOMETRY_REQUIRE throws on the host inside a ThrowOnRequire scope: its message,...
Definition EBGeometry_Macros.hpp:58
While an object of this class lives, a failed EBGEOMETRY_REQUIRE on this thread throws RequireError i...
Definition EBGeometry_Macros.hpp:138
ThrowOnRequire() noexcept
Open the scope.
Definition EBGeometry_Macros.hpp:143
~ThrowOnRequire() noexcept
Close the scope.
Definition EBGeometry_Macros.hpp:151
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
T length(const Vec2T< T > &v) noexcept
Euclidean length of a 2D vector.