Skip to content
Snippets Groups Projects
Commit 129c04ee authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Initialize arrays types with invalid data (non Release mode)

This concerns Array, Table, TinyVector and TinyMatrix by now.
parent 77ff62b3
No related branches found
No related tags found
1 merge request!101Initialize arrays types with invalid data (non Release mode)
This commit is part of merge request !101. Comments created here will be created in the context of that merge request.
#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
......
#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
......
#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
......
#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
#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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment