Skip to content

Commit

Permalink
restrict field types to trivially con-/destructible types
Browse files Browse the repository at this point in the history
Addresses alpaka-group#236.
  • Loading branch information
bernhardmgruber committed Jun 3, 2021
1 parent 91e6011 commit 234e2ba
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 81 deletions.
7 changes: 7 additions & 0 deletions include/llama/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ namespace llama
{
};

/// @brief Tells whether the given type is allowed as a field type in LLAMA. Such types need to be trivially
/// constructible and trivially destructible.
template <typename T>
inline constexpr bool isAllowedFieldType
= std::is_trivially_constructible_v<T>&& std::is_trivially_destructible_v<T>;

/// Record dimension tree node which may either be a leaf or refer to a child tree presented as another \ref
/// Record.
/// \tparam Tag Name of the node. May be any type (struct, class).
Expand All @@ -62,6 +68,7 @@ namespace llama
template <typename Tag, typename Type>
struct Field
{
static_assert(isAllowedFieldType<Type>, "This field's type is not allowed");
};

struct NrAndOffset
Expand Down
162 changes: 81 additions & 81 deletions tests/recorddimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ TEST_CASE("recorddim.record_with_int[3]")
int& e2 = view(0u)(Tag{})(2_RC);
}

TEST_CASE("recorddim.record_with_std::complex<float>")
{
using RecordDim = llama::Record<llama::Field<Tag, std::complex<float>>>;
auto view = allocView(llama::mapping::AoS{llama::ArrayDims{1}, RecordDim{}});

std::complex<float>& e = view(0u)(Tag{});
e = {2, 3};
}
// TEST_CASE("recorddim.record_with_std::complex<float>")
//{
// using RecordDim = llama::Record<llama::Field<Tag, std::complex<float>>>;
// auto view = allocView(llama::mapping::AoS{llama::ArrayDims{1}, RecordDim{}});
//
// std::complex<float>& e = view(0u)(Tag{});
// e = {2, 3};
//}

TEST_CASE("recorddim.record_with_std::array<float, 4>")
{
Expand All @@ -53,23 +53,23 @@ TEST_CASE("recorddim.record_with_std::array<float, 4>")
e = {2, 3, 4, 5};
}

TEST_CASE("recorddim.record_with_std::vector<float>")
{
using RecordDim = llama::Record<llama::Field<Tag, std::vector<float>>>;
auto view = allocView(llama::mapping::AoS{llama::ArrayDims{1}, RecordDim{}});

std::vector<float>& e = view(0u)(Tag{});
// e = {2, 3, 4, 5}; // FIXME: LLAMA memory is uninitialized
}

TEST_CASE("recorddim.record_with_std::atomic<int>")
{
using RecordDim = llama::Record<llama::Field<Tag, std::atomic<int>>>;
auto view = allocView(llama::mapping::AoS{llama::ArrayDims{1}, RecordDim{}});

std::atomic<int>& e = view(0u)(Tag{});
// e++; // FIXME: LLAMA memory is uninitialized
}
// TEST_CASE("recorddim.record_with_std::vector<float>")
//{
// using RecordDim = llama::Record<llama::Field<Tag, std::vector<float>>>;
// auto view = allocView(llama::mapping::AoS{llama::ArrayDims{1}, RecordDim{}});
//
// std::vector<float>& e = view(0u)(Tag{});
// // e = {2, 3, 4, 5}; // FIXME: LLAMA memory is uninitialized
//}

// TEST_CASE("recorddim.record_with_std::atomic<int>")
//{
// using RecordDim = llama::Record<llama::Field<Tag, std::atomic<int>>>;
// auto view = allocView(llama::mapping::AoS{llama::ArrayDims{1}, RecordDim{}});
//
// std::atomic<int>& e = view(0u)(Tag{});
// // e++; // FIXME: LLAMA memory is uninitialized
//}

TEST_CASE("recorddim.record_with_noncopyable")
{
Expand Down Expand Up @@ -113,62 +113,62 @@ TEST_CASE("recorddim.record_with_nonmoveable")
e.value = 0;
}

TEST_CASE("recorddim.record_with_nondefaultconstructible")
{
struct Element
{
Element() = delete;
int value;
};

using RecordDim = llama::Record<llama::Field<Tag, Element>>;
auto view = allocView(llama::mapping::AoS{llama::ArrayDims{1}, RecordDim{}});

Element& e = view(0u)(Tag{});
e.value = 0;
}

TEST_CASE("recorddim.record_with_nontrivial_ctor")
{
struct Element
{
int value = 42;
};

using RecordDim = llama::Record<llama::Field<Tag, Element>>;
auto view = allocView(llama::mapping::AoS{llama::ArrayDims{1}, RecordDim{}});

Element& e = view(0u)(Tag{});
// CHECK(e.value == 42); // FIXME: LLAMA memory is uninitialized
}

namespace
{
struct UniqueInt
{
int value = counter++;

explicit operator int() const
{
return value;
}

private:
inline static int counter = 0;
};
} // namespace

TEST_CASE("recorddim.record_with_nontrivial_ctor2")
{
using RecordDim = llama::Record<llama::Field<Tag, UniqueInt>>;
auto view = allocView(llama::mapping::AoS{llama::ArrayDims{1}, RecordDim{}});

// FIXME: LLAMA memory is uninitialized
// CHECK(view(ArrayDims{0})(Tag{}) == 0);
// CHECK(view(ArrayDims{1})(Tag{}) == 1);
// CHECK(view(ArrayDims{2})(Tag{}) == 2);
// CHECK(view(ArrayDims{15})(Tag{}) == 15);
}
// TEST_CASE("recorddim.record_with_nondefaultconstructible")
//{
// struct Element
// {
// Element() = delete;
// int value;
// };
//
// using RecordDim = llama::Record<llama::Field<Tag, Element>>;
// auto view = allocView(llama::mapping::AoS{llama::ArrayDims{1}, RecordDim{}});
//
// Element& e = view(0u)(Tag{});
// e.value = 0;
//}

// TEST_CASE("recorddim.record_with_nontrivial_ctor")
//{
// struct Element
// {
// int value = 42;
// };
//
// using RecordDim = llama::Record<llama::Field<Tag, Element>>;
// auto view = allocView(llama::mapping::AoS{llama::ArrayDims{1}, RecordDim{}});
//
// Element& e = view(0u)(Tag{});
// // CHECK(e.value == 42); // FIXME: LLAMA memory is uninitialized
//}

// namespace
//{
// struct UniqueInt
// {
// int value = counter++;
//
// explicit operator int() const
// {
// return value;
// }
//
// private:
// inline static int counter = 0;
// };
//} // namespace
//
// TEST_CASE("recorddim.record_with_nontrivial_ctor2")
//{
// using RecordDim = llama::Record<llama::Field<Tag, UniqueInt>>;
// auto view = allocView(llama::mapping::AoS{llama::ArrayDims{1}, RecordDim{}});
//
// // FIXME: LLAMA memory is uninitialized
// // CHECK(view(ArrayDims{0})(Tag{}) == 0);
// // CHECK(view(ArrayDims{1})(Tag{}) == 1);
// // CHECK(view(ArrayDims{2})(Tag{}) == 2);
// // CHECK(view(ArrayDims{15})(Tag{}) == 15);
//}

TEST_CASE("recorddim.int")
{
Expand Down

0 comments on commit 234e2ba

Please sign in to comment.