Skip to content

Commit

Permalink
Enable MDSPAN support by default (kokkos#7069)
Browse files Browse the repository at this point in the history
Also updates mdspan to 6f20467 and fix a namespace issue thingy.
  • Loading branch information
crtrott authored Jun 17, 2024
1 parent 892e13c commit 0d5cc92
Show file tree
Hide file tree
Showing 18 changed files with 998 additions and 311 deletions.
1 change: 0 additions & 1 deletion .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ jobs:
-DKokkos_ENABLE_DEPRECATED_CODE_4=ON \
-DKokkos_ENABLE_DEPRECATION_WARNINGS=OFF \
-DKokkos_ENABLE_COMPILER_WARNINGS=ON \
-DKokkos_ENABLE_IMPL_MDSPAN=ON \
-DCMAKE_CXX_FLAGS="-Werror ${{ matrix.cxx_extra_flags }}" \
-DCMAKE_EXE_LINKER_FLAGS="${{ matrix.extra_linker_flags }}" \
-DCMAKE_CXX_COMPILER=${{ matrix.cxx }} \
Expand Down
5 changes: 2 additions & 3 deletions .jenkins
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ pipeline {
-DKokkos_ENABLE_CUDA=ON \
-DKokkos_ENABLE_CUDA_LAMBDA=ON \
-DKokkos_ENABLE_OPENMP=ON \
-DKokkos_ENABLE_IMPL_MDSPAN=ON \
-DKokkos_ENABLE_IMPL_NVHPC_AS_DEVICE_COMPILER=ON \
.. && \
make -j8 && ctest --verbose'''
Expand Down Expand Up @@ -166,6 +165,7 @@ pipeline {
-DKokkos_ENABLE_BENCHMARKS=ON \
-DKokkos_ENABLE_HIP=ON \
-DKokkos_ENABLE_OPENMP=ON \
-DKokkos_ENABLE_IMPL_MDSPAN=OFF \
-DKokkos_ENABLE_HIP_MULTIPLE_KERNEL_INSTANTIATIONS=ON \
.. && \
make -j8 && ctest --verbose'''
Expand Down Expand Up @@ -396,7 +396,6 @@ pipeline {
-DKokkos_ENABLE_CUDA_RELOCATABLE_DEVICE_CODE=ON \
-DKokkos_ENABLE_DEPRECATED_CODE_4=ON \
-DKokkos_ENABLE_DEPRECATION_WARNINGS=OFF \
-DKokkos_ENABLE_IMPL_MDSPAN=ON \
-DCMAKE_INSTALL_PREFIX=${PWD}/../install \
.. && \
make -j8 install && \
Expand Down Expand Up @@ -460,7 +459,7 @@ pipeline {
-DKokkos_ENABLE_CUDA=ON \
-DKokkos_ENABLE_CUDA_LAMBDA=ON \
-DKokkos_ENABLE_LIBDL=OFF \
-DKokkos_ENABLE_IMPL_MDSPAN=ON \
-DKokkos_ENABLE_IMPL_MDSPAN=OFF \
-DKokkos_ENABLE_IMPL_CUDA_MALLOC_ASYNC=OFF \
.. && \
make -j8 && ctest --verbose && \
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ build_script:
- cmd: >-
mkdir build &&
cd build &&
cmake c:\projects\source -DKokkos_ENABLE_TESTS=ON -DCMAKE_CXX_FLAGS="/W0 /EHsc" -DKokkos_ENABLE_DEPRECATED_CODE_4=ON -DKokkos_ENABLE_DEPRECATION_WARNINGS=OFF &&
cmake c:\projects\source -DKokkos_ENABLE_IMPL_MDSPAN=OFF -DKokkos_ENABLE_TESTS=ON -DCMAKE_CXX_FLAGS="/W0 /EHsc" -DKokkos_ENABLE_DEPRECATED_CODE_4=ON -DKokkos_ENABLE_DEPRECATION_WARNINGS=OFF &&
cmake --build . --target install &&
ctest -C Debug --output-on-failure
2 changes: 1 addition & 1 deletion cmake/kokkos_enable_options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ KOKKOS_ENABLE_OPTION(IMPL_HIP_UNIFIED_MEMORY OFF "Whether to leverage unified me
KOKKOS_ENABLE_OPTION(DESUL_ATOMICS_EXTERNAL OFF "Whether to use an external desul installation")
KOKKOS_ENABLE_OPTION(ATOMICS_BYPASS OFF "**NOT RECOMMENDED** Whether to make atomics non-atomic for non-threaded MPI-only use cases")

KOKKOS_ENABLE_OPTION(IMPL_MDSPAN OFF "Whether to enable experimental mdspan support")
KOKKOS_ENABLE_OPTION(IMPL_MDSPAN ON "Whether to enable experimental mdspan support")
KOKKOS_ENABLE_OPTION(MDSPAN_EXTERNAL OFF BOOL "Whether to use an external version of mdspan")
KOKKOS_ENABLE_OPTION(IMPL_SKIP_COMPILER_MDSPAN ON BOOL "Whether to use an internal version of mdspan even if the compiler supports mdspan")
mark_as_advanced(Kokkos_ENABLE_IMPL_MDSPAN)
Expand Down
26 changes: 14 additions & 12 deletions core/src/View/MDSpan/Kokkos_MDSpan_Layout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ struct LayoutFromArrayLayout;

template <>
struct LayoutFromArrayLayout<Kokkos::LayoutLeft> {
using type = Experimental::layout_left_padded<dynamic_extent>;
using type = Kokkos::Experimental::layout_left_padded<dynamic_extent>;
};

template <>
struct LayoutFromArrayLayout<Kokkos::LayoutRight> {
using type = Experimental::layout_right_padded<dynamic_extent>;
using type = Kokkos::Experimental::layout_right_padded<dynamic_extent>;
};

template <>
Expand Down Expand Up @@ -81,20 +81,22 @@ KOKKOS_INLINE_FUNCTION auto array_layout_from_mapping(
// We could conceivably fix this by adding an extra ViewCtorProp for
// an abritrary padding. For now we will check for this.
if constexpr (rank > 1 &&
(std::is_same_v<
typename mapping_type::layout_type,
Experimental::layout_left_padded<dynamic_extent>> ||
std::is_same_v<
typename mapping_type::layout_type,
Experimental::layout_right_padded<dynamic_extent>>)) {
(std::is_same_v<typename mapping_type::layout_type,
Kokkos::Experimental::layout_left_padded<
dynamic_extent>> ||
std::is_same_v<typename mapping_type::layout_type,
Kokkos::Experimental::layout_right_padded<
dynamic_extent>>)) {
[[maybe_unused]] constexpr size_t strided_index =
std::is_same_v<typename mapping_type::layout_type,
Experimental::layout_left_padded<dynamic_extent>>
std::is_same_v<
typename mapping_type::layout_type,
Kokkos::Experimental::layout_left_padded<dynamic_extent>>
? 1
: rank - 2;
[[maybe_unused]] constexpr size_t extent_index =
std::is_same_v<typename mapping_type::layout_type,
Experimental::layout_left_padded<dynamic_extent>>
std::is_same_v<
typename mapping_type::layout_type,
Kokkos::Experimental::layout_left_padded<dynamic_extent>>
? 0
: rank - 1;
KOKKOS_ASSERT(mapping.stride(strided_index) == ext.extent(extent_index));
Expand Down
2 changes: 1 addition & 1 deletion tpls/mdspan/include/experimental/__p0009_bits/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static_assert(_MDSPAN_CPLUSPLUS >= MDSPAN_CXX_STD_14, "mdspan requires C++14 or
#endif

#ifndef _MDSPAN_USE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION
# if (!defined(__NVCC__) || (__CUDACC_VER_MAJOR__ >= 11 && __CUDACC_VER_MINOR__ >= 7)) && \
# if (!defined(__NVCC__) || (__CUDACC_VER_MAJOR__ * 100 + __CUDACC_VER_MINOR__ * 10 >= 1170)) && \
((defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201703) || \
(!defined(__cpp_deduction_guides) && MDSPAN_HAS_CXX_17))
# define _MDSPAN_USE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION 1
Expand Down
95 changes: 85 additions & 10 deletions tpls/mdspan/include/experimental/__p0009_bits/extents.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

#pragma once
#include "dynamic_extent.hpp"
#include "utility.hpp"

#ifdef __cpp_lib_span
#include <span>
#endif
#include <array>
#include <type_traits>

#include <cassert>
#include <cinttypes>

namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
Expand All @@ -30,6 +33,7 @@ namespace detail {
// Function used to check compatibility of extents in converting constructor
// can't be a private member function for some reason.
template <size_t... Extents, size_t... OtherExtents>
MDSPAN_INLINE_FUNCTION
static constexpr std::integral_constant<bool, false> __check_compatible_extents(
std::integral_constant<bool, false>,
std::integer_sequence<size_t, Extents...>,
Expand All @@ -46,6 +50,7 @@ struct __compare_extent_compatible : std::integral_constant<bool,
{};

template <size_t... Extents, size_t... OtherExtents>
MDSPAN_INLINE_FUNCTION
static constexpr std::integral_constant<
bool, _MDSPAN_FOLD_AND(__compare_extent_compatible<Extents, OtherExtents>::value)>
__check_compatible_extents(
Expand All @@ -59,8 +64,8 @@ template<class IndexType, class ... Arguments>
MDSPAN_INLINE_FUNCTION
static constexpr bool are_valid_indices() {
return
(std::is_convertible<Arguments, IndexType>::value && ... && true) &&
(std::is_nothrow_constructible<IndexType, Arguments>::value && ... && true);
_MDSPAN_FOLD_AND(std::is_convertible<Arguments, IndexType>::value) &&
_MDSPAN_FOLD_AND(std::is_nothrow_constructible<IndexType, Arguments>::value);
}

// ------------------------------------------------------------------
Expand Down Expand Up @@ -538,14 +543,9 @@ template <class IndexType, size_t... Extents> class extents {
MDSPAN_INLINE_FUNCTION friend constexpr bool
operator==(const extents &lhs,
const extents<OtherIndexType, OtherExtents...> &rhs) noexcept {
if constexpr (rank() != extents<OtherIndexType, OtherExtents...>::rank()) {
return false;
} else {
using common_t = std::common_type_t<index_type, OtherIndexType>;
for (size_type r = 0; r < m_rank; r++)
if(static_cast<common_t>(rhs.extent(r)) != static_cast<common_t>(lhs.extent(r))) return false;
}
return true;
return
rank() == extents<OtherIndexType, OtherExtents...>::rank() &&
detail::rankwise_equal(detail::with_rank<rank()>{}, rhs, lhs, detail::extent);
}

#if !(MDSPAN_HAS_CXX_20)
Expand Down Expand Up @@ -614,5 +614,80 @@ static
#endif
constexpr bool __is_extents_v = __is_extents<T>::value;

template<class InputIndexType, class ExtentsIndexType>
MDSPAN_INLINE_FUNCTION
constexpr void
check_lower_bound(InputIndexType user_index,
ExtentsIndexType /* current_extent */,
std::true_type /* is_signed */)
{
(void) user_index; // prevent unused variable warning
#ifdef _MDSPAN_DEBUG
assert(static_cast<ExtentsIndexType>(user_index) >= 0);
#endif
}

template<class InputIndexType, class ExtentsIndexType>
MDSPAN_INLINE_FUNCTION
constexpr void
check_lower_bound(InputIndexType /* user_index */,
ExtentsIndexType /* current_extent */,
std::false_type /* is_signed */)
{}

template<class InputIndexType, class ExtentsIndexType>
MDSPAN_INLINE_FUNCTION
constexpr void
check_upper_bound(InputIndexType user_index,
ExtentsIndexType current_extent)
{
(void) user_index; // prevent unused variable warnings
(void) current_extent;
#ifdef _MDSPAN_DEBUG
assert(static_cast<ExtentsIndexType>(user_index) < current_extent);
#endif
}

// Returning true to use AND fold instead of comma
// CPP14 mode doesn't like the use of void expressions
// with the way the _MDSPAN_FOLD_AND is set up
template<class InputIndex, class ExtentsIndexType>
MDSPAN_INLINE_FUNCTION
constexpr bool
check_one_index(InputIndex user_index,
ExtentsIndexType current_extent)
{
check_lower_bound(user_index, current_extent,
std::integral_constant<bool, std::is_signed<ExtentsIndexType>::value>{});
check_upper_bound(user_index, current_extent);
return true;
}

template<size_t ... RankIndices,
class ExtentsIndexType, size_t ... Exts,
class ... Indices>
MDSPAN_INLINE_FUNCTION
constexpr void
check_all_indices_helper(std::index_sequence<RankIndices...>,
const extents<ExtentsIndexType, Exts...>& exts,
Indices... indices)
{
// Suppress warning about statement has no effect
(void) _MDSPAN_FOLD_AND(
(check_one_index(indices, exts.extent(RankIndices)))
);
}

template<class ExtentsIndexType, size_t ... Exts,
class ... Indices>
MDSPAN_INLINE_FUNCTION
constexpr void
check_all_indices(const extents<ExtentsIndexType, Exts...>& exts,
Indices... indices)
{
check_all_indices_helper(std::make_index_sequence<sizeof...(Indices)>(),
exts, indices...);
}

} // namespace detail
} // namespace MDSPAN_IMPL_STANDARD_NAMESPACE
26 changes: 11 additions & 15 deletions tpls/mdspan/include/experimental/__p0009_bits/layout_left.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
#include "macros.hpp"
#include "trait_backports.hpp"
#include "extents.hpp"
#include "layout_stride.hpp"
#include "utility.hpp"
#if MDSPAN_HAS_CXX_17
#include "../__p2642_bits/layout_padded_fwd.hpp"
#include <cassert>
#endif
#include <type_traits>

namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
Expand Down Expand Up @@ -133,11 +136,11 @@ class layout_left::mapping {
: __extents(__other.extents())
{
MDSPAN_IMPL_PROPOSED_NAMESPACE::detail::
check_padded_layout_converting_constructor_mandates<extents_type,
_Mapping>();
check_padded_layout_converting_constructor_mandates<
extents_type, _Mapping>(detail::with_rank<extents_type::rank()>{});
MDSPAN_IMPL_PROPOSED_NAMESPACE::detail::
check_padded_layout_converting_constructor_preconditions<
extents_type>(__other);
extents_type>(detail::with_rank<extents_type::rank()>{}, __other);
}
#endif

Expand All @@ -156,17 +159,7 @@ class layout_left::mapping {
* TODO: check precondition
* other.required_span_size() is a representable value of type index_type
*/
#if !defined(_MDSPAN_HAS_CUDA) && !defined(_MDSPAN_HAS_HIP) && !defined(NDEBUG)
if constexpr (extents_type::rank() > 0) {
index_type stride = 1;
using common_t = std::common_type_t<index_type, typename OtherExtents::index_type>;
for(rank_type r=0; r<__extents.rank(); r++) {
if(static_cast<common_t>(stride) != static_cast<common_t>(other.stride(r)))
std::abort(); // ("Assigning layout_stride to layout_left with invalid strides.");
stride *= __extents.extent(r);
}
}
#endif
detail::validate_strides(detail::with_rank<extents_type::rank()>{}, layout_left{}, __extents, other);
}

MDSPAN_INLINE_FUNCTION_DEFAULTED _MDSPAN_CONSTEXPR_14_DEFAULTED mapping& operator=(mapping const&) noexcept = default;
Expand Down Expand Up @@ -194,6 +187,9 @@ class layout_left::mapping {
)
_MDSPAN_HOST_DEVICE
constexpr index_type operator()(Indices... idxs) const noexcept {
#if ! defined(NDEBUG)
detail::check_all_indices(this->extents(), idxs...);
#endif // ! NDEBUG
return __compute_offset(__rank_count<0, extents_type::rank()>(), static_cast<index_type>(idxs)...);
}

Expand Down
25 changes: 10 additions & 15 deletions tpls/mdspan/include/experimental/__p0009_bits/layout_right.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
#include "macros.hpp"
#include "trait_backports.hpp"
#include "extents.hpp"
#include <stdexcept>
#include "layout_stride.hpp"
#include "utility.hpp"
#if MDSPAN_HAS_CXX_17
#include "../__p2642_bits/layout_padded_fwd.hpp"
#endif

namespace MDSPAN_IMPL_STANDARD_NAMESPACE {

Expand Down Expand Up @@ -134,11 +136,11 @@ class layout_right::mapping {
: __extents(__other.extents())
{
MDSPAN_IMPL_PROPOSED_NAMESPACE::detail::
check_padded_layout_converting_constructor_mandates<extents_type,
_Mapping>();
check_padded_layout_converting_constructor_mandates<
extents_type, _Mapping>(detail::with_rank<extents_type::rank()>{});
MDSPAN_IMPL_PROPOSED_NAMESPACE::detail::
check_padded_layout_converting_constructor_preconditions<
extents_type>(__other);
extents_type>(detail::with_rank<extents_type::rank()>{}, __other);
}
#endif

Expand All @@ -157,17 +159,7 @@ class layout_right::mapping {
* TODO: check precondition
* other.required_span_size() is a representable value of type index_type
*/
#if !defined(_MDSPAN_HAS_CUDA) && !defined(_MDSPAN_HAS_HIP) && !defined(NDEBUG)
if constexpr (extents_type::rank() > 0) {
index_type stride = 1;
using common_t = std::common_type_t<index_type, typename OtherExtents::index_type>;
for(rank_type r=__extents.rank(); r>0; r--) {
if(static_cast<common_t>(stride) != static_cast<common_t>(other.stride(r-1)))
std::abort(); // ("Assigning layout_stride to layout_right with invalid strides.");
stride *= __extents.extent(r-1);
}
}
#endif
detail::validate_strides(detail::with_rank<extents_type::rank()>{}, layout_right{}, __extents, other);
}

MDSPAN_INLINE_FUNCTION_DEFAULTED _MDSPAN_CONSTEXPR_14_DEFAULTED mapping& operator=(mapping const&) noexcept = default;
Expand Down Expand Up @@ -195,6 +187,9 @@ class layout_right::mapping {
)
_MDSPAN_HOST_DEVICE
constexpr index_type operator()(Indices... idxs) const noexcept {
#if ! defined(NDEBUG)
detail::check_all_indices(this->extents(), idxs...);
#endif // ! NDEBUG
return __compute_offset(__rank_count<0, extents_type::rank()>(), static_cast<index_type>(idxs)...);
}

Expand Down
Loading

0 comments on commit 0d5cc92

Please sign in to comment.