diff --git a/src/algebra/TinyMatrix.hpp b/src/algebra/TinyMatrix.hpp index 6c23b068bf1a8c8744e24ffbf60ec88b42b1317b..6bf810cde020681da88afedb6a815e54a563504d 100644 --- a/src/algebra/TinyMatrix.hpp +++ b/src/algebra/TinyMatrix.hpp @@ -1,13 +1,12 @@ #ifndef TINY_MATRIX_HPP #define TINY_MATRIX_HPP +#include <algebra/TinyVector.hpp> +#include <utils/InvalidData.hpp> #include <utils/PugsAssert.hpp> #include <utils/PugsMacros.hpp> - #include <utils/Types.hpp> -#include <algebra/TinyVector.hpp> - #include <iostream> template <size_t N, typename T = double> @@ -278,7 +277,14 @@ class [[nodiscard]] TinyMatrix // One does not use the '=default' constructor to avoid (unexpected) // performances issues PUGS_INLINE - constexpr TinyMatrix() noexcept {} + constexpr TinyMatrix() noexcept + { +#ifndef NDEBUG + for (size_t i = 0; i < N * N; ++i) { + m_values[i] = invalid_data_v<T>; + } +#endif // NDEBUG + } PUGS_INLINE constexpr TinyMatrix(ZeroType) noexcept diff --git a/src/algebra/TinyVector.hpp b/src/algebra/TinyVector.hpp index f24503aade42275e8b96f6958535fc8439da356c..69ecb14e39d626b1ed0e2709b6fd018af92714da 100644 --- a/src/algebra/TinyVector.hpp +++ b/src/algebra/TinyVector.hpp @@ -1,14 +1,13 @@ #ifndef TINY_VECTOR_HPP #define TINY_VECTOR_HPP -#include <iostream> - +#include <utils/InvalidData.hpp> #include <utils/PugsAssert.hpp> #include <utils/PugsMacros.hpp> - #include <utils/Types.hpp> #include <cmath> +#include <iostream> template <size_t N, typename T = double> class [[nodiscard]] TinyVector @@ -211,7 +210,14 @@ class [[nodiscard]] TinyVector // One does not use the '=default' constructor to avoid // (zero-initialization) performances issues PUGS_INLINE - constexpr TinyVector() noexcept {} + constexpr TinyVector() noexcept + { +#ifndef NDEBUG + for (size_t i = 0; i < N; ++i) { + m_values[i] = invalid_data_v<T>; + } +#endif // NDEBUG + } PUGS_INLINE constexpr TinyVector(ZeroType) noexcept diff --git a/src/utils/Array.hpp b/src/utils/Array.hpp index 9fa02e95a41a5200260fab84cda211412c4747e2..e47e56575fe725f67f6c6815c34c42eac4ce8d44 100644 --- a/src/utils/Array.hpp +++ b/src/utils/Array.hpp @@ -1,11 +1,11 @@ #ifndef ARRAY_HPP #define ARRAY_HPP +#include <utils/InvalidData.hpp> +#include <utils/PugsAssert.hpp> #include <utils/PugsMacros.hpp> #include <utils/PugsUtils.hpp> -#include <utils/PugsAssert.hpp> - #include <Kokkos_CopyViews.hpp> #include <algorithm> @@ -94,6 +94,19 @@ class [[nodiscard]] Array } else { m_values = Kokkos::View<DataType*>{"anonymous", size}; } + +#ifndef NDEBUG + if constexpr (not std::is_const_v<DataType>) { + using T = std::decay_t<DataType>; + if constexpr (std::is_arithmetic_v<T>) { + this->fill(invalid_data_v<T>); + } else if constexpr (is_tiny_vector_v<T>) { + this->fill(T{}); + } else if constexpr (is_tiny_matrix_v<T>) { + this->fill(T{}); + } + } +#endif // NDEBUG } PUGS_INLINE diff --git a/src/utils/InvalidData.hpp b/src/utils/InvalidData.hpp new file mode 100644 index 0000000000000000000000000000000000000000..78e354c25a9efcd46a93d4bc98259cc0ece82bde --- /dev/null +++ b/src/utils/InvalidData.hpp @@ -0,0 +1,30 @@ +#ifndef NDEBUG + +#ifndef INVALID_DATA_HPP +#define INVALID_DATA_HPP + +#include <limits> +#include <type_traits> + +template <typename DataType> +struct invalid_data +{ + static constexpr DataType value = [] { + using T = std::decay_t<DataType>; + static_assert(std::is_arithmetic_v<T>); + if constexpr (std::is_floating_point_v<T>) { + return std::numeric_limits<T>::signaling_NaN(); + } else if constexpr (std::is_integral_v<T>) { + return std::numeric_limits<T>::max() / 2; + } else { + return T{}; + } + }(); +}; + +template <typename DataType> +inline constexpr DataType invalid_data_v = invalid_data<DataType>::value; + +#endif // INVALID_DATA_HPP + +#endif // NDEBUG diff --git a/src/utils/Table.hpp b/src/utils/Table.hpp index 8dae32b97b0d15a99256fd38c4f4f7c5ecf29588..5f621ab2719e93ea63d3be610818aab1cb164665 100644 --- a/src/utils/Table.hpp +++ b/src/utils/Table.hpp @@ -1,12 +1,14 @@ #ifndef TABLE_HPP #define TABLE_HPP -#include <Kokkos_CopyViews.hpp> #include <utils/Array.hpp> +#include <utils/InvalidData.hpp> #include <utils/PugsAssert.hpp> #include <utils/PugsMacros.hpp> #include <utils/PugsUtils.hpp> +#include <Kokkos_CopyViews.hpp> + template <typename DataType> class [[nodiscard]] Table { @@ -109,6 +111,19 @@ class [[nodiscard]] Table } else { m_values = Kokkos::View<DataType**>{"anonymous", nb_lines, nb_columns}; } + +#ifndef NDEBUG + if constexpr (not std::is_const_v<DataType>) { + using T = std::decay_t<DataType>; + if constexpr (std::is_arithmetic_v<T>) { + this->fill(invalid_data_v<T>); + } else if constexpr (is_tiny_vector_v<T>) { + this->fill(T{}); + } else if constexpr (is_tiny_matrix_v<T>) { + this->fill(T{}); + } + } +#endif // NDEBUG } PUGS_INLINE