From 129c04eebf2340cfa3bc2636cec0f121dc168ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Del=20Pino?= <stephane.delpino44@gmail.com> Date: Thu, 22 Jul 2021 19:55:42 +0200 Subject: [PATCH] Initialize arrays types with invalid data (non Release mode) This concerns Array, Table, TinyVector and TinyMatrix by now. --- src/algebra/TinyMatrix.hpp | 14 ++++++++++---- src/algebra/TinyVector.hpp | 14 ++++++++++---- src/utils/Array.hpp | 17 +++++++++++++++-- src/utils/InvalidData.hpp | 30 ++++++++++++++++++++++++++++++ src/utils/Table.hpp | 17 ++++++++++++++++- 5 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 src/utils/InvalidData.hpp diff --git a/src/algebra/TinyMatrix.hpp b/src/algebra/TinyMatrix.hpp index 6c23b068b..6bf810cde 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 f24503aad..69ecb14e3 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 9fa02e95a..e47e56575 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 000000000..78e354c25 --- /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 8dae32b97..5f621ab27 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 -- GitLab