Skip to content

Commit

Permalink
implement conversion operators to const for ReferenceCountedDataHandl…
Browse files Browse the repository at this point in the history
…e and ReferenceCountedAccessor
  • Loading branch information
nmm0 committed Jul 10, 2024
1 parent 64c352c commit afadc55
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
26 changes: 24 additions & 2 deletions core/src/View/MDSpan/Kokkos_MDSpan_Accessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,12 @@ class ReferenceCountedDataHandle {
m_tracker.assign_allocated_record_to_uninitialized(rec);
m_handle = static_cast<pointer>(get_record()->data());
}
ReferenceCountedDataHandle(pointer ptr): m_tracker(), m_handle(ptr) {}

template <class OtherElementType,
class = std::enable_if_t<std::is_convertible_v<
OtherElementType (*)[], value_type (*)[]>>>
ReferenceCountedDataHandle(OtherElementType* ptr)
: m_tracker(), m_handle(ptr) {}

ReferenceCountedDataHandle(const ReferenceCountedDataHandle&) = default;
ReferenceCountedDataHandle(ReferenceCountedDataHandle&&) noexcept = default;
Expand Down Expand Up @@ -270,7 +275,12 @@ class ReferenceCountedDataHandle<ElementType, AnonymousSpace> {
m_tracker.assign_allocated_record_to_uninitialized(rec);
m_handle = static_cast<pointer>(get_record()->data());
}
ReferenceCountedDataHandle(pointer ptr) : m_tracker(), m_handle(ptr) {}

template <class OtherElementType,
class = std::enable_if_t<std::is_convertible_v<
OtherElementType (*)[], value_type (*)[]>>>
ReferenceCountedDataHandle(OtherElementType* ptr)
: m_tracker(), m_handle(ptr) {}

ReferenceCountedDataHandle(const ReferenceCountedDataHandle&) = default;
ReferenceCountedDataHandle(ReferenceCountedDataHandle&&) noexcept = default;
Expand Down Expand Up @@ -313,6 +323,12 @@ class ReferenceCountedAccessor {

constexpr ReferenceCountedAccessor() noexcept = default;

template <class OtherElementType,
class = std::enable_if_t<std::is_convertible_v<
OtherElementType (*)[], element_type (*)[]>>>
constexpr ReferenceCountedAccessor(
const ReferenceCountedAccessor<OtherElementType, MemorySpace>&) {}

constexpr reference access(data_handle_type p, size_t i) const {
return p.get()[i];
}
Expand All @@ -337,6 +353,12 @@ class ReferenceCountedAccessor<ElementType, AnonymousSpace> {
constexpr ReferenceCountedAccessor(
const ReferenceCountedAccessor<ElementType, OtherSpace>&) {}

template <class OtherElementType, class OtherSpace,
class = std::enable_if_t<std::is_convertible_v<
OtherElementType (*)[], element_type (*)[]>>>
constexpr ReferenceCountedAccessor(
const ReferenceCountedAccessor<OtherElementType, OtherSpace>&) {}

constexpr reference access(data_handle_type p, size_t i) const {
return p.get()[i];
}
Expand Down
1 change: 1 addition & 0 deletions core/unit_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ SET(COMPILE_ONLY_SOURCES
TestTeamMDRangePolicyCTAD.cpp
TestNestedReducerCTAD.cpp
view/TestExtentsDatatypeConversion.cpp
view/TestBasicViewMDSpanConversion.cpp
)

#testing if windows.h and Kokkos_Core.hpp can be included
Expand Down
45 changes: 45 additions & 0 deletions core/unit_test/view/TestBasicViewMDSpanConversion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER

#include <Kokkos_Core.hpp>
#include <type_traits>

#ifdef KOKKOS_ENABLE_IMPL_MDSPAN
static_assert(
std::is_convertible_v<
Kokkos::View<long long ****, Kokkos::LayoutRight, Kokkos::Serial>,
Kokkos::BasicView<long long, Kokkos::dextents<size_t, 4>,
Kokkos::Experimental::layout_right_padded<>,
Kokkos::Impl::checked_reference_counted_accessor<
long long, Kokkos::HostSpace>>>);
static_assert(
std::is_convertible_v<
Kokkos::BasicView<long long, Kokkos::dextents<size_t, 4>,
Kokkos::Experimental::layout_right_padded<>,
Kokkos::Impl::checked_reference_counted_accessor<
long long, Kokkos::HostSpace>>,
Kokkos::BasicView<const long long, Kokkos::dextents<size_t, 4>,
Kokkos::Experimental::layout_right_padded<>,
Kokkos::Impl::checked_reference_counted_accessor<
const long long, Kokkos::HostSpace>>>);
static_assert(
std::is_convertible_v<
Kokkos::View<long long ****, Kokkos::LayoutRight, Kokkos::Serial>,
Kokkos::BasicView<const long long, Kokkos::dextents<size_t, 4>,
Kokkos::Experimental::layout_right_padded<>,
Kokkos::Impl::checked_reference_counted_accessor<
const long long, Kokkos::HostSpace>>>);
#endif

0 comments on commit afadc55

Please sign in to comment.