Skip to content

Commit

Permalink
New version of constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenarnst committed Sep 24, 2024
1 parent ab3abc6 commit d5c69e2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
28 changes: 17 additions & 11 deletions core/src/View/Kokkos_ViewCtor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ struct ViewCtorProp<void, CommonViewAllocProp<Specialize, T>> {
KOKKOS_FUNCTION
ViewCtorProp(const type &arg) : value(arg) {}
KOKKOS_FUNCTION
ViewCtorProp(type &&arg) : value(std::move(arg)) {}
ViewCtorProp(type &&arg) : value(arg) {}

type value;
};
Expand Down Expand Up @@ -212,12 +212,16 @@ struct ViewCtorProp : public ViewCtorProp<void, P>... {
using memory_space = typename var_memory_space::type;
using execution_space = typename var_execution_space::type;
using pointer_type = typename var_pointer::type;

//! Construct from a matching argument list.

// Construct from a matching argument list.
//
// Note that if P is empty, this constructor is the default constructor.
// On the other hand, if P is not empty, the constraint ensures that
// the default constructor cannot be called.
template <typename... Args,
typename = std::enable_if_t<std::conjunction_v<
std::is_same<P, typename ViewCtorProp<void, Kokkos::Impl::remove_cvref_t<Args>>::type>...>>>
explicit ViewCtorProp(Args&&...args)
std::is_constructible<ViewCtorProp<void, P>, Args &&>...>>>
ViewCtorProp(Args &&...args)
: ViewCtorProp<void, P>(std::forward<Args>(args))... {}

template <typename... Args>
Expand All @@ -237,9 +241,11 @@ struct ViewCtorProp : public ViewCtorProp<void, P>... {
template <typename... Args>
ViewCtorProp(view_ctor_prop_args<Args...> const &arg)
: view_ctor_prop_base<Args>(
static_cast<view_ctor_prop_base<Args> const &>(arg))... {}

//ViewCtorProp(view_ctor_prop_args<> const &) {}
static_cast<view_ctor_prop_base<Args> const &>(arg))... {
// Suppress an unused argument warning that (at least at one point) would
// show up if sizeof...(Args) == 0
(void)arg;
}
};

#if !defined(KOKKOS_COMPILER_MSVC) || !defined(KOKKOS_COMPILER_NVCC)
Expand Down Expand Up @@ -453,9 +459,9 @@ inline constexpr Kokkos::Impl::AllowPadding_t AllowPadding{};
* alignment
*/
template <class... Args>
auto view_alloc(Args&&...args) {
using return_type =
Impl::ViewCtorProp<typename Impl::ViewCtorProp<void, Kokkos::Impl::remove_cvref_t<Args>>::type...>;
auto view_alloc(Args &&...args) {
using return_type = Impl::ViewCtorProp<typename Impl::ViewCtorProp<
void, Kokkos::Impl::remove_cvref_t<Args>>::type...>;

static_assert(!return_type::has_pointer,
"Cannot give pointer-to-memory for view allocation");
Expand Down
54 changes: 45 additions & 9 deletions core/unit_test/TestViewCtorProp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <Kokkos_Core.hpp>

namespace Test {
namespace {

// Check Kokkos::Impl::is_view_label.
TEST(TEST_CATEGORY, is_view_label) {
Expand All @@ -28,17 +28,29 @@ TEST(TEST_CATEGORY, is_view_label) {
static_assert(Kokkos::Impl::is_view_label<const char[N]>::value);
static_assert(Kokkos::Impl::is_view_label<char[N]>::value);

// A char* is not a label. Thus, a label is distinguished from a pointer type.
static_assert(!Kokkos::Impl::is_view_label<char*>::value);
}

// Check traits of Kokkos::Impl::ViewCtorProp<void, std::string>.
// Check traits of Kokkos::Impl::ViewCtorProp<>.
TEST(TEST_CATEGORY, vcp_empty_traits) {
using vcp_empty_t = Kokkos::Impl::ViewCtorProp<>;

// Check that the empty view constructor properties class is default
// constructible. This is needed for calls of Kokkos::view_alloc().
static_assert(std::is_default_constructible_v<vcp_empty_t>);

static_assert(std::is_same_v<decltype(Kokkos::view_alloc()), vcp_empty_t>);
}

// Check traits of base class Kokkos::Impl::ViewCtorProp<void, std::string>.
TEST(TEST_CATEGORY, vcp_label_base_traits) {
using vcp_label_base_t = Kokkos::Impl::ViewCtorProp<void, std::string>;

static_assert(std::is_same_v<typename vcp_label_base_t::type, std::string>);

// Check that the base class is default constructible. The default constructor
// may be called by the copy constructor of the derived class, such as when
// may be called by the copy constructor of deriveded classes, such as when
// copy constructing a view constructor properties object from another view
// constructor properties object that holds fewer properties.
static_assert(std::is_default_constructible_v<vcp_label_base_t>);
Expand All @@ -54,7 +66,7 @@ TEST(TEST_CATEGORY, vcp_label_base_traits) {
static_assert(std::is_constructible_v<vcp_label_base_t, char*>);
}

// Check traits of Kokkos::Impl::ViewCtorProp<std::string>.
// Check traits of derived class Kokkos::Impl::ViewCtorProp<std::string>.
TEST(TEST_CATEGORY, vcp_label_traits) {
using vcp_label_base_t = Kokkos::Impl::ViewCtorProp<void, std::string>;
using vcp_label_t = Kokkos::Impl::ViewCtorProp<std::string>;
Expand All @@ -75,10 +87,7 @@ TEST(TEST_CATEGORY, vcp_label_traits) {
static_assert(std::is_constructible_v<vcp_label_t, const char[N]>);
static_assert(std::is_constructible_v<vcp_label_t, char[N]>);

// Kokkos::Impl::ViewCtorProp<std::string> cannot be constructed from
// a `char*` because `char*` does not satisfy `Kokkos::Impl::is_view_label`,
// hence the constructor cannot access the type alias `type`.
static_assert(!std::is_constructible_v<vcp_label_t, char*>);
static_assert(std::is_constructible_v<vcp_label_t, char*>);
}

// Check that the constructor of Kokkos::Impl::ViewCtorProp<std::string>
Expand Down Expand Up @@ -106,4 +115,31 @@ TEST(TEST_CATEGORY, vcp_label_view_alloc_can_move) {
"our label");
}

} // namespace Test
// Check the copy constructor of Kokkos::Impl::ViewCtorProp<std::string>.
TEST(TEST_CATEGORY, vcp_label_copy_constructor) {
using vcp_empty_t = Kokkos::Impl::ViewCtorProp<>;
using vcp_label_t = Kokkos::Impl::ViewCtorProp<std::string>;

// Copy construction from an empty view constructor properties object.
static_assert(std::is_constructible_v<vcp_label_t, const vcp_empty_t&>);

vcp_empty_t prop_empty;
vcp_label_t prop_empty_copy(prop_empty);

ASSERT_TRUE(
Kokkos::Impl::get_property<Kokkos::Impl::LabelTag>(prop_empty_copy)
.empty());

// Copy construction from a view constructor properties object with a label.
static_assert(std::is_copy_constructible_v<vcp_label_t>);

auto prop = Kokkos::view_alloc("our label");
vcp_label_t prop_copy(prop);

ASSERT_EQ(Kokkos::Impl::get_property<Kokkos::Impl::LabelTag>(prop),
"our label");
ASSERT_EQ(Kokkos::Impl::get_property<Kokkos::Impl::LabelTag>(prop_copy),
"our label");
}

} // namespace

0 comments on commit d5c69e2

Please sign in to comment.