Select Git revision
ValuePerItem.hpp
ValuePerItem.hpp 3.07 KiB
#ifndef VALUE_PER_ITEM_HPP
#define VALUE_PER_ITEM_HPP
#include <ItemType.hpp>
#include <PastisAssert.hpp>
#include <IConnectivity.hpp>
template <typename DataType,
ItemType item_type>
class ValuePerItem
{
public:
static const ItemType item_t{item_type};
private:
bool m_is_built{false};
Kokkos::View<DataType*> m_values;
// Allows const version to access our data
friend ValuePerItem<std::add_const_t<DataType>,
item_type>;
public:
KOKKOS_FORCEINLINE_FUNCTION
const bool& isBuilt() const
{
return m_is_built;
}
KOKKOS_FORCEINLINE_FUNCTION
DataType& operator()(const size_t& j)
{
Assert(m_is_built);
return m_values(j);
}
// Following Kokkos logic, these classes are view and const view does allow
// changes in data
KOKKOS_FORCEINLINE_FUNCTION
DataType& operator()(const size_t& j) const
{
Assert(m_is_built);
return m_values(j);
}
KOKKOS_INLINE_FUNCTION
size_t numberOfValues() const
{
Assert(m_is_built);
return m_values.extent(0);
}
KOKKOS_FORCEINLINE_FUNCTION
DataType& operator[](const size_t& i)
{
Assert(m_is_built);
return m_values[i];
}
// Following Kokkos logic, these classes are view and const view does allow
// changes in data
KOKKOS_FORCEINLINE_FUNCTION
DataType& operator[](const size_t & i) const
{
Assert(m_is_built);
return m_values[i];
}
KOKKOS_INLINE_FUNCTION
size_t numberOfItems() const
{
Assert(m_is_built);
Assert(m_values.extent(0) != 0);
return m_values.extent(0);
}
template <typename DataType2>
KOKKOS_INLINE_FUNCTION
ValuePerItem&
operator=(const ValuePerItem<DataType2, item_type>& value_per_item)
{
// 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 ValuePerItem 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 const ValuePerItem to a non const ValuePerItem");
m_values = value_per_item.m_values;
m_is_built = value_per_item.m_is_built;
return *this;
}
template <typename DataType2>
KOKKOS_INLINE_FUNCTION
ValuePerItem(const ValuePerItem<DataType2, item_type>& value_per_item)
{
this->operator=(value_per_item);
}
ValuePerItem() = default;
ValuePerItem(const IConnectivity& connectivity)
: m_is_built{true}
{
m_values = Kokkos::View<std::remove_const_t<DataType>*>("values", connectivity.numberOf<item_type>());
}
~ValuePerItem() = default;
};
template <typename DataType>
using ValuePerNode = ValuePerItem<DataType, ItemType::node>;
template <typename DataType>
using ValuePerEdge = ValuePerItem<DataType, ItemType::edge>;
template <typename DataType>
using ValuePerFace = ValuePerItem<DataType, ItemType::face>;
template <typename DataType>
using ValuePerCell = ValuePerItem<DataType, ItemType::cell>;
#endif // VALUE_PER_ITEM_HPP