Skip to content

Commit

Permalink
Provide kokkos_swap as part of Core and deprecate `Experimental::sw…
Browse files Browse the repository at this point in the history
…ap` in Algorithms (#6697)

* Move Kokkos_Swap.hpp header to core

* Include only what we use (in <Kokkos_Swap.hpp>)

* Rename header guards in <Kokkos_Swap.hpp> (because I can)

* Drop useless comment

* Fix noexcept specification of Kokkos::swap

* Constexprify Kokkos::swap

* Fixup type requirements in Kokkos::swap

* Drop the inline (because I can)

* fixup! Move Kokkos_Swap.hpp header to core

* Promote swap to namespace Kokkos::

* Move and update Kokkos::swap unit test to core

* Add support for swapping arrays

* Fixup size_t

* Provide Kokkos::swap overload for a few classes

* Rename Kokkos::swap -> Kokkos::kokkos_swap

* For backward compatibility provide deprecated Experimental::swap
  • Loading branch information
dalg24 committed Jan 10, 2024
1 parent 96d530a commit 0ba8c40
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 95 deletions.
1 change: 0 additions & 1 deletion algorithms/src/Kokkos_StdAlgorithms.hpp
Expand Up @@ -35,7 +35,6 @@
// following the std classification.

// modifying ops
#include "std_algorithms/Kokkos_Swap.hpp"
#include "std_algorithms/Kokkos_IterSwap.hpp"

// non-modifying sequence
Expand Down
3 changes: 1 addition & 2 deletions algorithms/src/sorting/impl/Kokkos_NestedSortImpl.hpp
Expand Up @@ -18,7 +18,6 @@
#define KOKKOS_NESTED_SORT_IMPL_HPP_

#include <Kokkos_Core.hpp>
#include <std_algorithms/Kokkos_Swap.hpp>

namespace Kokkos {
namespace Experimental {
Expand Down Expand Up @@ -99,7 +98,7 @@ KOKKOS_INLINE_FUNCTION void sort_nested_impl(
keyView(elem1) = key2;
keyView(elem2) = key1;
if constexpr (!std::is_same_v<ValueViewType, std::nullptr_t>) {
Kokkos::Experimental::swap(valueView(elem1), valueView(elem2));
Kokkos::kokkos_swap(valueView(elem1), valueView(elem2));
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions algorithms/src/std_algorithms/Kokkos_IterSwap.hpp
Expand Up @@ -19,7 +19,6 @@

#include <Kokkos_Core.hpp>
#include "impl/Kokkos_Constraints.hpp"
#include "Kokkos_Swap.hpp"

namespace Kokkos {
namespace Experimental {
Expand All @@ -33,7 +32,7 @@ struct StdIterSwapFunctor {
KOKKOS_FUNCTION
void operator()(int i) const {
(void)i;
::Kokkos::Experimental::swap(*m_a, *m_b);
::Kokkos::kokkos_swap(*m_a, *m_b);
}

KOKKOS_FUNCTION
Expand All @@ -58,6 +57,16 @@ void iter_swap(IteratorType1 a, IteratorType2 b) {
Impl::iter_swap_impl(a, b);
}

#ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
template <class T>
KOKKOS_DEPRECATED_WITH_COMMENT("Use Kokkos::kokkos_swap instead!")
KOKKOS_FUNCTION
void swap(T& a, T& b) noexcept(::Kokkos::kokkos_swap(std::declval<T&>(),
std::declval<T&>())) {
::Kokkos::kokkos_swap(a, b);
}
#endif

} // namespace Experimental
} // namespace Kokkos

Expand Down
41 changes: 0 additions & 41 deletions algorithms/src/std_algorithms/Kokkos_Swap.hpp

This file was deleted.

3 changes: 1 addition & 2 deletions algorithms/src/std_algorithms/impl/Kokkos_Reverse.hpp
Expand Up @@ -21,7 +21,6 @@
#include "Kokkos_Constraints.hpp"
#include "Kokkos_HelperPredicates.hpp"
#include <std_algorithms/Kokkos_Distance.hpp>
#include <std_algorithms/Kokkos_Swap.hpp>
#include <string>

namespace Kokkos {
Expand All @@ -39,7 +38,7 @@ struct StdReverseFunctor {

KOKKOS_FUNCTION
void operator()(index_type i) const {
::Kokkos::Experimental::swap(m_first[i], m_last[-i - 1]);
::Kokkos::kokkos_swap(m_first[i], m_last[-i - 1]);
}

KOKKOS_FUNCTION
Expand Down
3 changes: 1 addition & 2 deletions algorithms/src/std_algorithms/impl/Kokkos_SwapRanges.hpp
Expand Up @@ -21,7 +21,6 @@
#include "Kokkos_Constraints.hpp"
#include "Kokkos_HelperPredicates.hpp"
#include <std_algorithms/Kokkos_Distance.hpp>
#include <std_algorithms/Kokkos_Swap.hpp>
#include <string>

namespace Kokkos {
Expand All @@ -36,7 +35,7 @@ struct StdSwapRangesFunctor {

KOKKOS_FUNCTION
void operator()(index_type i) const {
::Kokkos::Experimental::swap(m_first1[i], m_first2[i]);
::Kokkos::kokkos_swap(m_first1[i], m_first2[i]);
}

KOKKOS_FUNCTION
Expand Down
44 changes: 0 additions & 44 deletions algorithms/unit_tests/TestStdAlgorithmsModOps.cpp
Expand Up @@ -89,50 +89,6 @@ TEST(std_algorithms_mod_ops_test, move_within_parfor) {
}
}

// ------------
// swap
// ------------
TEST(std_algorithms_mod_ops_test, swap) {
{
int a = 1;
int b = 2;
KE::swap(a, b);
ASSERT_EQ(a, 2);
ASSERT_EQ(b, 1);
}

{
double a = 3.;
double b = 1.;
KE::swap(a, b);
EXPECT_DOUBLE_EQ(a, 1.);
EXPECT_DOUBLE_EQ(b, 3.);
}
}

template <class ViewType>
struct StdAlgoModSeqOpsTestSwap {
ViewType m_view;

KOKKOS_INLINE_FUNCTION
void operator()(const int index) const {
typename ViewType::value_type newval{11};
KE::swap(m_view(index), newval);
}

StdAlgoModSeqOpsTestSwap(ViewType aIn) : m_view(aIn) {}
};

TEST(std_algorithms_mod_ops_test, swap_within_parfor) {
auto a = create_view<double>(stdalgos::DynamicTag{}, 10, "a");
StdAlgoModSeqOpsTestSwap<decltype(a)> fnc(a);
Kokkos::parallel_for(a.extent(0), fnc);
auto a_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), a);
for (std::size_t i = 0; i < a.extent(0); ++i) {
EXPECT_DOUBLE_EQ(a_h(0), 11.);
}
}

// ------------
// iter_swap
// ------------
Expand Down
1 change: 1 addition & 0 deletions core/src/Kokkos_Array.hpp
Expand Up @@ -22,6 +22,7 @@
#endif

#include <Kokkos_Macros.hpp>
#include <Kokkos_Swap.hpp>
#include <impl/Kokkos_Error.hpp>
#include <impl/Kokkos_StringManipulation.hpp>

Expand Down
1 change: 1 addition & 0 deletions core/src/Kokkos_Core.hpp
Expand Up @@ -53,6 +53,7 @@
#include <Kokkos_MathematicalSpecialFunctions.hpp>
#include <Kokkos_NumericTraits.hpp>
#include <Kokkos_BitManipulation.hpp>
#include <Kokkos_Swap.hpp>
#include <Kokkos_MemoryPool.hpp>
#include <Kokkos_Array.hpp>
#include <Kokkos_View.hpp>
Expand Down
2 changes: 1 addition & 1 deletion core/src/Kokkos_Pair.hpp
Expand Up @@ -28,6 +28,7 @@
#endif

#include <Kokkos_Macros.hpp>
#include <Kokkos_Swap.hpp>
#include <utility>

namespace Kokkos {
Expand Down Expand Up @@ -484,7 +485,6 @@ KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(
}

namespace Impl {

template <class T>
struct is_pair_like : std::false_type {};
template <class T, class U>
Expand Down
68 changes: 68 additions & 0 deletions core/src/Kokkos_Swap.hpp
@@ -0,0 +1,68 @@
//@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

#ifndef KOKKOS_SWAP_HPP
#define KOKKOS_SWAP_HPP

#include <Kokkos_Macros.hpp>

#include <cstddef>
#include <type_traits>
#include <utility>

namespace Kokkos {

template <class T>
KOKKOS_FUNCTION constexpr std::enable_if_t<std::is_move_constructible_v<T> &&
std::is_move_assignable_v<T>>
kokkos_swap(T& a, T& b) noexcept(std::is_nothrow_move_constructible_v<T>&&
std::is_nothrow_move_assignable_v<T>) {
T t(std::move(a));
a = std::move(b);
b = std::move(t);
}

namespace Impl {

template <class T>
struct is_swappable {
template <class U>
static decltype(kokkos_swap(std::declval<T&>(), std::declval<T&>()))
test_swap(int);
struct Nope;
template <class U>
static Nope test_swap(long);
static constexpr bool value =
!std::is_same_v<decltype(test_swap<T>(0)), Nope>;
};

template <class T>
inline constexpr bool is_nothrow_swappable_v =
noexcept(kokkos_swap(std::declval<T&>(), std::declval<T&>()));

} // namespace Impl

template <class T, std::size_t N>
KOKKOS_FUNCTION constexpr std::enable_if_t<Impl::is_swappable<T>::value>
kokkos_swap(T (&a)[N], T (&b)[N]) noexcept(Impl::is_nothrow_swappable_v<T>) {
for (std::size_t i = 0; i < N; ++i) {
kokkos_swap(a[i], b[i]);
}
}

} // namespace Kokkos

#endif
1 change: 1 addition & 0 deletions core/unit_test/CMakeLists.txt
Expand Up @@ -200,6 +200,7 @@ foreach(Tag Threads;Serial;OpenMP;Cuda;HPX;OpenMPTarget;OpenACC;HIP;SYCL)
Reductions
Reductions_DeviceView
SharedAlloc
Swap
)
set(file ${dir}/Test${Tag}_${Name}.cpp)
# Write to a temporary intermediate file and call configure_file to avoid
Expand Down
68 changes: 68 additions & 0 deletions core/unit_test/TestSwap.hpp
@@ -0,0 +1,68 @@
//@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 <gtest/gtest.h>

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

namespace {

template <class ExecutionSpace>
struct TestSwap {
KOKKOS_FUNCTION void operator()(int, int& err) const {
{
int a = 1;
int b = 2;
Kokkos::kokkos_swap(a, b);
if (!(a == 2 && b == 1)) {
Kokkos::printf("Failed Kokkos::kokkos_swap(int, int)\n");
++err;
}
}
{
float a = 1;
float b = 2;
Kokkos::kokkos_swap(a, b);
if (!(a == 2 && b == 1)) {
Kokkos::printf("Failed Kokkos::kokkos_swap(float, float)\n");
++err;
}
}
{
int a[3] = {1, 2, 3};
int b[3] = {4, 5, 6};
Kokkos::kokkos_swap(a, b);
if (!(a[0] == 4 && a[1] == 5 && a[2] == 6 && b[0] == 1 && b[1] == 2 &&
b[2] == 3)) {
Kokkos::printf("Failed Kokkos::kokkos_swap(int[3], int[3])\n");
++err;
}
}
}

TestSwap() {
int errors;
Kokkos::parallel_reduce(
"TestSwap", Kokkos::RangePolicy<ExecutionSpace>(0, 1), *this, errors);
EXPECT_EQ(errors, 0);
}
};

TEST(TEST_CATEGORY, kokkos_swap) { TestSwap<TEST_EXECSPACE>(); }

} // namespace

0 comments on commit 0ba8c40

Please sign in to comment.