Skip to content

Commit

Permalink
mdarray: demo that initializer_list ctor doesn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoemmen committed May 14, 2024
1 parent f648150 commit e4ea3c0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
26 changes: 26 additions & 0 deletions include/experimental/__p1684_bits/mdarray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,14 @@ class mdarray {
static_assert( std::is_constructible<extents_type, OtherExtents>::value, "");
}

#if 0
// Corresponds to deduction guide from std::initializer_list<value_type>
MDSPAN_INLINE_FUNCTION
constexpr mdarray(std::initializer_list<value_type> values)
: map_(extents_type{}), ctr_{values}
{}
#endif // 0

// Corresponds to deduction guide from C array
MDSPAN_TEMPLATE_REQUIRES(
class CArray,
Expand Down Expand Up @@ -600,5 +608,23 @@ mdarray(CArray& values) -> mdarray<
decltype(impl::carray_to_array(values))
>;

#if 0
// Adding a deduction guide from initializer_list<T> breaks the above
// C array deduction guide, because construction from
// initializer_list<T> catches every possible creation of an mdarray
// from curly braces.
//
// We may not know values.size() at compile time,
// so we have to use a dynamically allocated container.
template<class T>
mdarray(std::initializer_list<T> values) ->
mdarray<
std::remove_cvref_t<T>,
dextents<std::size_t, 1>,
layout_right,
std::vector<std::remove_cvref_t<T>>
>;
#endif // 0

} // end namespace MDSPAN_IMPL_PROPOSED_NAMESPACE
} // end namespace MDSPAN_IMPL_STANDARD_NAMESPACE
36 changes: 36 additions & 0 deletions tests/test_mdarray_carray_ctad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,39 @@ TEST(TestMdarrayCtorDataCArray, test_mdarray_carray_ctad) {
__MDSPAN_TESTS_RUN_TEST((test_mdarray_ctad_carray_rank2<float, 3, 4>()))
__MDSPAN_TESTS_RUN_TEST((test_mdarray_ctad_carray_rank3<float, 3, 4, 5>()))
}

#if 0
TEST(TestMdarrayCtorInitializerList, Rank1) {
using MDSPAN_IMPL_STANDARD_NAMESPACE::dextents;
using MDSPAN_IMPL_STANDARD_NAMESPACE::extents;
using MDSPAN_IMPL_STANDARD_NAMESPACE::layout_right;

auto error_buffer = allocate_error_buffer();
{
std::size_t* errors = error_buffer.get();
errors[0] = 0;
dispatch([errors] _MDSPAN_HOST_DEVICE () {
KokkosEx::mdarray m{{1.0f, 2.0f, 3.0f}};
static_assert(std::is_same_v<typename decltype(m)::extents_type,
dextents<std::size_t, 1>>);
static_assert(std::is_same_v<typename decltype(m)::layout_type,
layout_right>);
static_assert(std::is_same_v<typename decltype(m)::container_type,
std::vector<float>>);

__MDSPAN_DEVICE_ASSERT_EQ(m.rank(), 1);
__MDSPAN_DEVICE_ASSERT_EQ(m.rank_dynamic(), 1);
__MDSPAN_DEVICE_ASSERT_EQ(m.extent(0), 3);

for (std::size_t k = 0; k < 3; ++k) {
#if defined(MDSPAN_USE_BRACKET_OPERATOR) && (MDSPAN_USE_BRACKET_OPERATOR != 0)
__MDSPAN_DEVICE_ASSERT_EQ(m[k], (static_cast<float>(k) + 1.0f));
#else
__MDSPAN_DEVICE_ASSERT_EQ(m(k), (static_cast<float>(k) + 1.0f));
#endif
}
});
ASSERT_EQ(errors[0], 0);
}
}
#endif // 0

0 comments on commit e4ea3c0

Please sign in to comment.