diff --git a/src/mesh/ItemValue.hpp b/src/mesh/ItemValue.hpp index 5534df4893230d4abfa9d0502a334a28b77eb71f..13ef536a962f80abf9aa812fe40ed155a73c479a 100644 --- a/src/mesh/ItemValue.hpp +++ b/src/mesh/ItemValue.hpp @@ -1,7 +1,8 @@ #ifndef ITEM_VALUE_HPP #define ITEM_VALUE_HPP -#include <Kokkos_Core.hpp> +//#include <Kokkos_Core.hpp> +#include <Array.hpp> #include <ItemType.hpp> #include <PastisAssert.hpp> @@ -19,7 +20,7 @@ class ItemValue private: bool m_is_built{false}; - Kokkos::View<DataType*> m_values; + Array<DataType> m_values; // Allows const version to access our data friend ItemValue<std::add_const_t<DataType>, @@ -45,7 +46,7 @@ class ItemValue size_t numberOfValues() const { Assert(m_is_built); - return m_values.extent(0); + return m_values.size(); } // Following Kokkos logic, these classes are view and const view does allow @@ -61,8 +62,7 @@ class ItemValue size_t numberOfItems() const { Assert(m_is_built); - Assert(m_values.extent(0) != 0); - return m_values.extent(0); + return m_values.size(); } template <typename DataType2> @@ -76,7 +76,7 @@ class ItemValue // ensures that const is not lost through copy static_assert(((std::is_const<DataType2>() and std::is_const<DataType>()) or not std::is_const<DataType2>()), - "Cannot assign const ItemValue to a non-const ItemValue"); + "Cannot assign ItemValue of const to ItemValue of non-const"); m_values = value_per_item.m_values; m_is_built = value_per_item.m_is_built; @@ -94,9 +94,10 @@ class ItemValue ItemValue() = default; ItemValue(const IConnectivity& connectivity) - : m_is_built{true} + : m_is_built{true}, + m_values(connectivity.numberOf<item_type>()) { - m_values = Kokkos::View<std::remove_const_t<DataType>*>("values", connectivity.numberOf<item_type>()); + ; } ~ItemValue() = default; diff --git a/src/utils/Array.hpp b/src/utils/Array.hpp new file mode 100644 index 0000000000000000000000000000000000000000..8cb91fcd5b70aa8384c40a75104a183fc2b154fd --- /dev/null +++ b/src/utils/Array.hpp @@ -0,0 +1,85 @@ +#ifndef ARRAY_HPP +#define ARRAY_HPP + +#include <Kokkos_Core.hpp> + +template <typename DataType> +class Array +{ + private: + Kokkos::View<DataType*> m_values; + + // Allows const version to access our data + friend Array<std::add_const_t<DataType>>; + + public: + KOKKOS_INLINE_FUNCTION + size_t size() const + { + return m_values.extent(0); + } + + KOKKOS_INLINE_FUNCTION + DataType& operator[](const size_t& i) + { + return m_values[i]; + } + + KOKKOS_INLINE_FUNCTION + DataType& operator[](const size_t& i) const + { + return m_values[i]; + } + + KOKKOS_INLINE_FUNCTION + DataType& operator()(const size_t& i) + { + return m_values(i); + } + + KOKKOS_INLINE_FUNCTION + DataType& operator()(const size_t& i) const + { + return m_values(i); + } + + KOKKOS_INLINE_FUNCTION + Array(const size_t& size) + : m_values("anonymous", size) + { + static_assert(not std::is_const<DataType>(), + "Cannot build Array of const data of given size"); + } + + template <typename DataType2> + KOKKOS_INLINE_FUNCTION + Array& operator=(const Array<DataType2>& array) + { + // ensures that DataType is the same as source DataType2 + static_assert(std::is_same<std::remove_const_t<DataType>, std::remove_const_t<DataType2>>(), + "Cannot assign Array of different type"); + // ensures that const is not lost through copy + static_assert(((std::is_const<DataType2>() and std::is_const<DataType>()) + or not std::is_const<DataType2>()), + "Cannot assign Array of const to Array of non-const"); + m_values = array.m_values; + return *this; + } + + KOKKOS_INLINE_FUNCTION + Array& operator=(const Array&) = default; + + KOKKOS_INLINE_FUNCTION + Array& operator=(Array&&) = default; + + KOKKOS_INLINE_FUNCTION + Array() = default; + + KOKKOS_INLINE_FUNCTION + Array(const Array&) = default; + + KOKKOS_INLINE_FUNCTION + ~Array() = default; +}; + +#endif // ARRAY_HPP