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: GPL-3.0-or-later
4
11#ifndef EBGEOMETRY_MACROS_HPP
12#define EBGEOMETRY_MACROS_HPP
13
14#include <cstdio>
15#include <cstdlib>
16
17#include "EBGeometry_GPU.hpp"
18
19#if defined(EBGEOMETRY_HIP)
20// Declares the device printf used below. It must be visible before the first header that uses the
21// macros: in a template, a call that does not depend on a template parameter binds at the point of
22// definition, and would otherwise bind to the host printf.
23#include <hip/hip_runtime.h>
24#endif
25#if defined(EBGEOMETRY_DEVICE_COMPILE)
26// Device compilation pass (CUDA/HIP): std::fprintf/std::abort are host-only. assert() is not an
27// option either: NDEBUG, which CMake's Release configurations define, would silently remove every
28// device check while the host checks stayed. Print with the device printf and trap instead.
29#if defined(EBGEOMETRY_CUDA)
30#define EBGEOMETRY_DEVICE_TRAP() __trap()
31#else
32#define EBGEOMETRY_DEVICE_TRAP() __builtin_trap()
33#endif
34namespace EBGeometry {
35namespace MacrosDetail {
36// Out of line, like the backends' own assert: the device printf expands to a lot of code, and an
37// inline copy at every check site makes device compiles of large files take many minutes.
38__device__ __noinline__ inline void
39deviceAssertionFailed(const char* a_cond, const char* a_file, const int a_line)
40{
41 printf("EBGeometry device assertion failed: (%s)\n file: %s\n line: %d\n", a_cond, a_file, a_line);
42 EBGEOMETRY_DEVICE_TRAP();
43}
44} // namespace MacrosDetail
45} // namespace EBGeometry
46#endif
47
76#if defined(EBGEOMETRY_DEVICE_COMPILE)
77#define EBGEOMETRY_REQUIRE(cond, ...) \
78 do { \
79 if (!(cond)) { \
80 ::EBGeometry::MacrosDetail::deviceAssertionFailed(#cond, __FILE__, __LINE__); \
81 } \
82 } while (0)
83#else
84#define EBGEOMETRY_REQUIRE(cond, ...) \
85 do { \
86 if (!(cond)) { \
87 std::fprintf(stderr, "EBGeometry::" __VA_ARGS__); \
88 std::fprintf(stderr, "\n check: (%s)\n file: %s\n line: %d\n", #cond, __FILE__, __LINE__); \
89 std::abort(); \
90 } \
91 } while (0)
92#endif
93
129#if defined(EBGEOMETRY_ENABLE_ASSERTIONS)
130#if defined(EBGEOMETRY_DEVICE_COMPILE)
131#define EBGEOMETRY_EXPECT(cond) \
132 do { \
133 if (!(cond)) { \
134 ::EBGeometry::MacrosDetail::deviceAssertionFailed(#cond, __FILE__, __LINE__); \
135 } \
136 } while (0)
137#else
138#define EBGEOMETRY_EXPECT(cond) \
139 do { \
140 if (!(cond)) { \
141 std::fprintf(stderr, \
142 "EBGeometry assertion failed: (%s)\n file: %s\n line: %d\n function: %s\n", \
143 #cond, \
144 __FILE__, \
145 __LINE__, \
146 static_cast<const char*>(__func__)); \
147 std::abort(); \
148 } \
149 } while (0)
150#endif
151#else
152// Assertions off: expand to a no-op that does NOT evaluate cond -- an assertion costs exactly
153// nothing in release, including not running its predicate. A local computed solely to be asserted
154// therefore becomes unused; mark such a declaration [[maybe_unused]] at its site.
155#define EBGEOMETRY_EXPECT(cond) ((void)0)
156#endif
157
158#endif // EBGEOMETRY_MACROS_HPP
GPU portability layer: host/device decoration macros and backend detection.
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26