19 #define ITYR_CONCAT_(x, y) x##y
20 #define ITYR_CONCAT(x, y) ITYR_CONCAT_(x, y)
22 #ifdef DOCTEST_LIBRARY_INCLUDED
24 #define ITYR_TEST_CASE(name) DOCTEST_TEST_CASE(name)
25 #define ITYR_SUBCASE(name) DOCTEST_SUBCASE(name)
26 #define ITYR_REQUIRE(cond) DOCTEST_REQUIRE(cond)
27 #define ITYR_REQUIRE_MESSAGE(cond, ...) DOCTEST_REQUIRE_MESSAGE(cond, __VA_ARGS__)
28 #define ITYR_CHECK(cond) DOCTEST_CHECK(cond)
29 #define ITYR_CHECK_MESSAGE(cond, ...) DOCTEST_CHECK_MESSAGE(cond, __VA_ARGS__)
30 #define ITYR_CHECK_THROWS_AS(exp, exception) DOCTEST_CHECK_THROWS_AS(exp, exception)
35 #define ITYR_ANON_NAME(x) ITYR_CONCAT(x, __COUNTER__)
37 #define ITYR_ANON_NAME(x) ITYR_CONCAT(x, __LINE__)
40 #define ITYR_TEST_CASE(name) [[maybe_unused]] static inline void ITYR_ANON_NAME(__ityr_test_anon_fn)()
41 #define ITYR_SUBCASE(name)
42 #define ITYR_REQUIRE(cond) if (!(cond)) { ityr::common::die("Assertion failed (%s:%d)", __FILE__, __LINE__); }
43 #define ITYR_REQUIRE_MESSAGE(cond, msg, ...) if (!(cond)) { ityr::common::die(msg " (%s:%d)", ##__VA_ARGS__, __FILE__, __LINE__); }
45 #define ITYR_CHECK(cond) do { (void)sizeof(cond); } while (0)
46 #define ITYR_CHECK_MESSAGE(cond, ...) do { (void)sizeof(cond); } while (0)
48 #define ITYR_CHECK(cond) ITYR_REQUIRE(cond)
49 #define ITYR_CHECK_MESSAGE(cond, ...) ITYR_REQUIRE_MESSAGE(cond, __VA_ARGS__)
51 #define ITYR_CHECK_THROWS_AS(exp, exception) exp
55 #define ITYR_ANON_VAR ITYR_CONCAT(anon_, __LINE__)
59 #ifdef __cpp_lib_hardware_interference_size
67 clock_gettime(CLOCK_MONOTONIC, &ts);
68 return (uint64_t)ts.tv_sec * 1000000000 + (uint64_t)ts.tv_nsec;
72 inline void die(
const char* fmt, ...) {
73 constexpr
int slen = 256;
74 static char msg[slen];
81 fprintf(stderr,
"\x1b[31m%s\x1b[39m\n", msg);
89 if (
const char* val_str = std::getenv(env_var)) {
91 std::stringstream ss(val_str);
94 die(
"Environment variable '%s' is invalid.\n", env_var);
113 ITYR_TEST_CASE(
"[ityr::common::util] next_pow2") {
124 template <
typename T>
126 return !(x & (x - 1));
129 template <
typename T>
132 return x & ~(alignment - 1);
135 template <
typename T>
138 return reinterpret_cast<T*
>(
reinterpret_cast<uintptr_t
>(x) & ~(alignment - 1));
141 template <
typename T>
144 return (x + alignment - 1) & ~(alignment - 1);
147 template <
typename T>
150 return reinterpret_cast<T*
>((
reinterpret_cast<uintptr_t
>(x) + alignment - 1) & ~(alignment - 1));
153 ITYR_TEST_CASE(
"[ityr::common::util] round up/down for integers") {
171 static std::size_t pagesize = sysconf(_SC_PAGE_SIZE);
175 template <
typename T>
182 return *get_optional();
186 return get_optional().has_value();
189 template <
typename... Args>
191 get_optional().emplace(std::forward<Args>(
args)...);
196 get_optional().reset();
200 static auto& get_optional() {
206 template <
typename Singleton>
209 template <
typename... Args>
211 if (!Singleton::initialized()) {
213 should_finalize_ =
true;
218 if (should_finalize_) {
232 bool should_finalize_ =
false;
241 long value()
const {
return value_; }
move_only_t & operator=(const move_only_t &)=delete
move_only_t(move_only_t &&mo)
Definition: util.hpp:246
move_only_t()
Definition: util.hpp:238
move_only_t & operator=(move_only_t &&mo)
Definition: util.hpp:249
move_only_t(const long v)
Definition: util.hpp:239
long value() const
Definition: util.hpp:241
move_only_t(const move_only_t &)=delete
singleton_initializer & operator=(singleton_initializer &&)=delete
bool should_finalize() const
Definition: util.hpp:229
singleton_initializer(const singleton_initializer &)=delete
singleton_initializer & operator=(const singleton_initializer &)=delete
singleton_initializer(singleton_initializer &&)=delete
~singleton_initializer()
Definition: util.hpp:217
singleton_initializer(Args &&... args)
Definition: util.hpp:210
T instance_type
Definition: util.hpp:178
static bool initialized()
Definition: util.hpp:185
static auto & get()
Definition: util.hpp:180
static void init(Args &&... args)
Definition: util.hpp:190
static void fini()
Definition: util.hpp:194
#define ITYR_CHECK(cond)
Definition: util.hpp:48
singleton< profiler< mode > > instance
Definition: profiler.hpp:259
Definition: allocator.hpp:16
T round_up_pow2(T x, T alignment)
Definition: util.hpp:142
va_list args
Definition: util.hpp:76
bool is_pow2(T x)
Definition: util.hpp:125
fprintf(stderr, "\x1b[31m%s\x1b[39m\n", msg)
vsnprintf(msg, slen, fmt, args)
T round_down_pow2(T x, T alignment)
Definition: util.hpp:130
__attribute__((noinline)) inline void die(const char *fmt
constexpr std::size_t hardware_destructive_interference_size
Definition: util.hpp:62
std::size_t get_page_size()
Definition: util.hpp:170
T getenv_with_default(const char *env_var, T default_val)
Definition: util.hpp:88
move_only_t operator+(const move_only_t &mo1, const move_only_t &mo2)
Definition: util.hpp:259
uint64_t clock_gettime_ns()
Definition: util.hpp:65
uint64_t next_pow2(uint64_t x)
Definition: util.hpp:102
void fini()
Definition: ito.hpp:45
void init(MPI_Comm comm=MPI_COMM_WORLD)
Definition: ito.hpp:41