Morpheus 1.0.0
Dynamic matrix type and algorithms for sparse matrices
Loading...
Searching...
No Matches
Morpheus_Utils.hpp
1
24#ifndef MORPHEUS_UTILS_HPP
25#define MORPHEUS_UTILS_HPP
26
27#include <Morpheus_Macros.hpp>
28
29#include <iostream>
30
31namespace Morpheus {
32namespace Impl {
33
34template <typename Printable, typename Stream>
35void print_matrix_header(const Printable& p, Stream& s) {
36 s << "<" << p.nrows() << ", " << p.ncols() << "> with " << p.nnnz()
37 << " entries\n";
38}
39
40template <typename Size1, typename Size2>
41MORPHEUS_INLINE_FUNCTION Size1 DIVIDE_INTO(Size1 N, Size2 granularity) {
42 return (N + (granularity - 1)) / granularity;
43}
44
45template <typename T>
46MORPHEUS_INLINE_FUNCTION T ceil_div(T x, T y) {
47 return (x + y - 1) / y;
48}
49
50template <typename T>
51MORPHEUS_INLINE_FUNCTION T min(T x, T y) {
52 return x < y ? x : y;
53}
54
55template <typename T>
56MORPHEUS_INLINE_FUNCTION T max(T x, T y) {
57 return x > y ? y : x;
58}
59
60template <typename IndexType>
61MORPHEUS_INLINE_FUNCTION bool isPow2(
62 IndexType x,
63 typename std::enable_if<std::is_integral<IndexType>::value>::type* =
64 nullptr) {
65 return ((x & (x - 1)) == 0);
66}
67
68template <typename IndexType>
69MORPHEUS_INLINE_FUNCTION IndexType
70nextPow2(IndexType x,
71 typename std::enable_if<std::is_integral<IndexType>::value>::type* =
72 nullptr) {
73 --x;
74 x |= x >> 1;
75 x |= x >> 2;
76 x |= x >> 4;
77 x |= x >> 8;
78 x |= x >> 16;
79 return ++x;
80}
81
82#ifndef NDEBUG
83#define MORPHEUS_ASSERT(condition, message) \
84 do { \
85 if (!(condition)) { \
86 std::cerr << "Assertion `" #condition "` failed in " << __FILE__ \
87 << " line " << __LINE__ << ": " << message << std::endl; \
88 std::terminate(); \
89 } \
90 } while (false)
91#else
92#define MORPHEUS_ASSERT(condition, message) \
93 do { \
94 } while (false)
95#endif
96
97} // namespace Impl
98} // namespace Morpheus
99
100#endif // MORPHEUS_UTILS_HPP
Generic Morpheus interfaces.
Definition: dummy.cpp:24