Skip to content

Commit

Permalink
Make tile data item optional and off by default (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipFackler committed Dec 13, 2021
1 parent e72e336 commit d71b3cf
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 35 deletions.
2 changes: 1 addition & 1 deletion include/plsm/Subpaving.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct SubpavingTester;
* @test benchmark_Subpaving.cpp
*/
template <typename TScalar, DimType Dim, typename TEnumIndex = void,
typename TItemData = IdType, typename TMemSpace = DefaultMemSpace>
typename TItemData = void, typename TMemSpace = DefaultMemSpace>
class Subpaving
{
template <typename>
Expand Down
124 changes: 91 additions & 33 deletions include/plsm/Tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,79 @@

namespace plsm
{
template <typename TRegion, typename TItem = void>
struct TileData
{
//! Alias for Region
using RegionType = TRegion;
//! User data type to be mapped from Tile
using ItemType = TItem;

KOKKOS_INLINE_FUNCTION
decltype(auto)
getItem() noexcept
{
return item;
}

KOKKOS_INLINE_FUNCTION
decltype(auto)
getItem() const noexcept
{
return item;
}

template <typename T>
KOKKOS_INLINE_FUNCTION
void
setItem(T&& v)
{
item = std::forward<T>(v);
}

//! Region mapped from by this Tile
RegionType region;
//! Index of owning Zone
IdType owningZoneId{invalid<IdType>};

static_assert(std::is_integral_v<ItemType>);
//! Mapped user data item
ItemType item{invalid<ItemType>};
};

template <typename TRegion>
struct TileData<TRegion, void>
{
//! Alias for Region
using RegionType = TRegion;

KOKKOS_INLINE_FUNCTION
decltype(auto)
getItem() noexcept
{
return invalid<IdType>;
}

KOKKOS_INLINE_FUNCTION
decltype(auto)
getItem() const noexcept
{
return invalid<IdType>;
}

template <typename T>
KOKKOS_INLINE_FUNCTION
void
setItem([[maybe_unused]] T&&)
{
}

//! Region mapped from by this Tile
RegionType region;
//! Index of owning Zone
IdType owningZoneId{invalid<IdType>};
};

/*!
* @brief Tile is used as a non-overlapped lattice region
*
Expand All @@ -19,7 +92,7 @@ namespace plsm
*
* @test test_Tile.cpp
*/
template <typename TRegion, typename TItemData = IdType>
template <typename TRegion, typename TItemData = void>
class Tile
{
public:
Expand All @@ -38,28 +111,29 @@ class Tile
*/
KOKKOS_INLINE_FUNCTION
Tile(const RegionType& region, IdType owningZoneId) :
_region(region), _owningZoneId(owningZoneId)
_data{region, owningZoneId}
{
}

//!@{
/*!
* @brief Get/Set the Region mapped from by this Tile
*/
KOKKOS_INLINE_FUNCTION
const RegionType&
getRegion() const noexcept
{
return _region;
return _data.region;
}

/*!
* @brief Check if this tile has a valid owning zone id
*/
KOKKOS_INLINE_FUNCTION
bool
hasOwningZone() const noexcept
{
return _owningZoneId != invalid<IdType>;
return _data.owningZoneId != invalid<IdType>;
}
//!}

//!@{
/*!
Expand All @@ -69,14 +143,14 @@ class Tile
IdType
getOwningZoneIndex() const noexcept
{
return _owningZoneId;
return _data.owningZoneId;
}

KOKKOS_INLINE_FUNCTION
void
setOwningZoneIndex(IdType id) noexcept
{
_owningZoneId = id;
_data.owningZoneId = id;
}
//!@}

Expand All @@ -87,53 +161,37 @@ class Tile
bool
hasData() const noexcept
{
return _data != invalid<ItemDataType>;
return !isInvalid(_data.getItem());
}

//!@{
/*!
* @brief Get/Set mapped data item
*/
KOKKOS_INLINE_FUNCTION
ItemDataType&
decltype(auto)
getData() noexcept
{
return _data;
return _data.getItem();
}

KOKKOS_INLINE_FUNCTION
const ItemDataType&
decltype(auto)
getData() const noexcept
{
return _data;
}

KOKKOS_INLINE_FUNCTION
void
setData(const ItemDataType& data)
{
_data = data;
return _data.getItem();
}

template <typename T>
KOKKOS_INLINE_FUNCTION
void
setData(ItemDataType&& data)
setData(T&& data)
{
_data = std::move(data);
_data.setItem(std::forward<T>(data));
}
//!@}

private:
//! Region mapped from by this Tile
RegionType _region;
//! Index of owning Zone
IdType _owningZoneId{invalid<IdType>};

// FIXME: Idea would be to use optional<ItemDataType> to hold arbitrary
// data in a Tile. It needs to be initialized to an invalid state.
static_assert(std::is_same<ItemDataType, IdType>::value,
"Only IdType supported for now");
//! Mapped user data item
ItemDataType _data{invalid<ItemDataType>};
TileData<RegionType, ItemDataType> _data;
};
} // namespace plsm
8 changes: 8 additions & 0 deletions include/plsm/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ using DimType = std::size_t;
template <typename T>
inline constexpr T invalid = std::numeric_limits<T>::max() - static_cast<T>(1);

template <typename T>
KOKKOS_INLINE_FUNCTION
constexpr bool
isInvalid(const T& val) noexcept
{
return val == invalid<T>;
}

//! Convention chosen to represent a wildcard value for the given type
template <typename T>
inline constexpr T wildcard = std::numeric_limits<T>::max();
Expand Down
1 change: 1 addition & 0 deletions test/include/plsm/RenderSubpaving.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ inline void
renderSubpaving(Subpaving<TScalar, Dim, TEnum, TItemData, TMemSpace>& subpaving)
{
std::cout << "\nNumber of Tiles: " << subpaving.getNumberOfTiles()
<< "\nMemory Size: " << subpaving.getDeviceMemorySize()
<< std::endl;
}
} // namespace test
Expand Down
2 changes: 1 addition & 1 deletion test/unittest_Tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ TEMPLATE_LIST_TEST_CASE("Tile Basic", "[Tile][template]", test::IntTypes)

using Ival = typename RegionType::IntervalType;
RegionType r1{Ival{12}, Ival{8}};
TileType t1{r1, 0};
Tile<RegionType, TestType> t1{r1, 0};
REQUIRE(t1.hasOwningZone());
REQUIRE(t1.getOwningZoneIndex() == 0);
REQUIRE(!t1.getRegion().empty());
Expand Down

0 comments on commit d71b3cf

Please sign in to comment.