1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright Contributors to the Kokkos project
#include <gtest/gtest.h>
#include <Kokkos_Macros.hpp>
#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
import kokkos.core;
#else
#include <Kokkos_Core.hpp>
#endif
namespace Test {
template <class T>
struct Greater {
KOKKOS_FUNCTION constexpr bool operator()(T const& lhs, T const& rhs) {
return lhs > rhs;
}
};
struct PairIntCompareFirst {
int first;
int second;
private:
friend KOKKOS_FUNCTION constexpr bool operator<(
PairIntCompareFirst const& lhs, PairIntCompareFirst const& rhs) {
return lhs.first < rhs.first;
}
};
} // namespace Test
// ----------------------------------------------------------
// test max()
// ----------------------------------------------------------
TEST(TEST_CATEGORY, max) {
int a = 1;
int b = 2;
EXPECT_EQ(Kokkos::max(a, b), 2);
a = 3;
b = 1;
EXPECT_EQ(Kokkos::max(a, b), 3);
static_assert(Kokkos::max(1, 2) == 2);
static_assert(Kokkos::max(1, 2, ::Test::Greater<int>{}) == 1);
EXPECT_EQ(Kokkos::max({3.f, -1.f, 0.f}), 3.f);
static_assert(Kokkos::max({3, -1, 0}) == 3);
static_assert(Kokkos::max({3, -1, 0}, ::Test::Greater<int>{}) == -1);
static_assert(Kokkos::max({
::Test::PairIntCompareFirst{255, 0},
::Test::PairIntCompareFirst{255, 1},
::Test::PairIntCompareFirst{0, 2},
::Test::PairIntCompareFirst{0, 3},
::Test::PairIntCompareFirst{255, 4},
::Test::PairIntCompareFirst{0, 5},
})
.second == 0); // leftmost element
}
template <class ViewType>
struct StdAlgoMinMaxOpsTestMax {
ViewType m_view;
KOKKOS_INLINE_FUNCTION
void operator()(const int& ind) const {
auto v1 = 10.;
if (Kokkos::max(v1, m_view(ind)) == 10.) {
m_view(ind) = 6.;
}
}
KOKKOS_INLINE_FUNCTION
StdAlgoMinMaxOpsTestMax(ViewType aIn) : m_view(aIn) {}
};
TEST(TEST_CATEGORY, max_within_parfor) {
namespace KE = Kokkos::Experimental;
using view_t = Kokkos::View<double*>;
view_t a("a", 10);
StdAlgoMinMaxOpsTestMax<view_t> fnc(a);
Kokkos::parallel_for(a.extent(0), fnc);
auto a_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), a);
for (int i = 0; i < 10; ++i) {
EXPECT_DOUBLE_EQ(a_h(0), 6.);
}
}
// ----------------------------------------------------------
// test min()
// ----------------------------------------------------------
TEST(TEST_CATEGORY, min) {
int a = 1;
int b = 2;
EXPECT_EQ(Kokkos::min(a, b), 1);
a = 3;
b = 2;
EXPECT_EQ(Kokkos::min(a, b), 2);
static_assert(Kokkos::min(3.f, 2.f) == 2.f);
static_assert(Kokkos::min(3.f, 2.f, ::Test::Greater<int>{}) == 3.f);
EXPECT_EQ(Kokkos::min({3.f, -1.f, 0.f}), -1.f);
static_assert(Kokkos::min({3, -1, 0}) == -1);
static_assert(Kokkos::min({3, -1, 0}, ::Test::Greater<int>{}) == 3);
static_assert(Kokkos::min({
::Test::PairIntCompareFirst{255, 0},
::Test::PairIntCompareFirst{255, 1},
::Test::PairIntCompareFirst{0, 2},
::Test::PairIntCompareFirst{0, 3},
::Test::PairIntCompareFirst{255, 4},
::Test::PairIntCompareFirst{0, 5},
})
.second == 2); // leftmost element
}
template <class ViewType>
struct StdAlgoMinMaxOpsTestMin {
ViewType m_view;
KOKKOS_INLINE_FUNCTION
void operator()(const int& ind) const {
auto v1 = 10.;
if (Kokkos::min(v1, m_view(ind)) == 0.) {
m_view(ind) = 8.;
}
}
KOKKOS_INLINE_FUNCTION
StdAlgoMinMaxOpsTestMin(ViewType aIn) : m_view(aIn) {}
};
TEST(TEST_CATEGORY, min_within_parfor) {
namespace KE = Kokkos::Experimental;
using view_t = Kokkos::View<double*>;
view_t a("a", 10);
StdAlgoMinMaxOpsTestMin<view_t> fnc(a);
Kokkos::parallel_for(a.extent(0), fnc);
auto a_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), a);
for (int i = 0; i < 10; ++i) {
EXPECT_DOUBLE_EQ(a_h(0), 8.);
}
}
// ----------------------------------------------------------
// test minmax()
// ----------------------------------------------------------
TEST(TEST_CATEGORY, minmax) {
int a = 1;
int b = 2;
const auto& r = Kokkos::minmax(a, b);
EXPECT_EQ(r.first, 1);
EXPECT_EQ(r.second, 2);
a = 3;
b = 2;
const auto& r2 = Kokkos::minmax(a, b);
EXPECT_EQ(r2.first, 2);
EXPECT_EQ(r2.second, 3);
static_assert((Kokkos::pair<float, float>(Kokkos::minmax(3.f, 2.f)) ==
Kokkos::make_pair(2.f, 3.f)));
static_assert(
(Kokkos::pair<float, float>(Kokkos::minmax(
3.f, 2.f, ::Test::Greater<int>{})) == Kokkos::make_pair(3.f, 2.f)));
EXPECT_EQ(Kokkos::minmax({3.f, -1.f, 0.f}), Kokkos::make_pair(-1.f, 3.f));
static_assert(Kokkos::minmax({3, -1, 0}) == Kokkos::make_pair(-1, 3));
static_assert(Kokkos::minmax({3, -1, 0}, ::Test::Greater<int>{}) ==
Kokkos::make_pair(3, -1));
static_assert(Kokkos::minmax({
::Test::PairIntCompareFirst{255, 0},
::Test::PairIntCompareFirst{255, 1},
::Test::PairIntCompareFirst{0, 2},
::Test::PairIntCompareFirst{0, 3},
::Test::PairIntCompareFirst{255, 4},
::Test::PairIntCompareFirst{0, 5},
})
.first.second == 2); // leftmost
static_assert(Kokkos::minmax({
::Test::PairIntCompareFirst{255, 0},
::Test::PairIntCompareFirst{255, 1},
::Test::PairIntCompareFirst{0, 2},
::Test::PairIntCompareFirst{0, 3},
::Test::PairIntCompareFirst{255, 4},
::Test::PairIntCompareFirst{0, 5},
})
.second.second == 4); // rightmost
}
template <class ViewType>
struct StdAlgoMinMaxOpsTestMinMax {
ViewType m_view;
KOKKOS_INLINE_FUNCTION
void operator()(const int& ind) const {
auto v1 = 7.;
const auto& r = Kokkos::minmax(v1, m_view(ind));
m_view(ind) = (double)(r.first - r.second);
}
KOKKOS_INLINE_FUNCTION
StdAlgoMinMaxOpsTestMinMax(ViewType aIn) : m_view(aIn) {}
};
TEST(TEST_CATEGORY, minmax_within_parfor) {
using view_t = Kokkos::View<double*>;
view_t a("a", 10);
StdAlgoMinMaxOpsTestMinMax<view_t> fnc(a);
Kokkos::parallel_for(a.extent(0), fnc);
auto a_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), a);
for (int i = 0; i < 10; ++i) {
EXPECT_DOUBLE_EQ(a_h(0), -7.);
}
}
// ----------------------------------------------------------
// test clamp()
// ----------------------------------------------------------
TEST(TEST_CATEGORY, clamp) {
int a = 1;
int b = 2;
int c = 19;
const auto& r = Kokkos::clamp(a, b, c);
EXPECT_EQ(&r, &b);
EXPECT_EQ(r, b);
a = 5;
b = -2;
c = 3;
const auto& r2 = Kokkos::clamp(a, b, c);
EXPECT_EQ(&r2, &c);
EXPECT_EQ(r2, c);
a = 5;
b = -2;
c = 7;
const auto& r3 = Kokkos::clamp(a, b, c);
EXPECT_EQ(&r3, &a);
EXPECT_EQ(r3, a);
}
template <class ViewType>
struct StdAlgoMinMaxOpsTestClamp {
ViewType m_view;
KOKKOS_INLINE_FUNCTION
void operator()(const int& ind) const {
m_view(ind) = 10.;
const auto b = -2.;
const auto c = 3.;
const auto& r = Kokkos::clamp(m_view(ind), b, c);
m_view(ind) = (double)(r);
}
KOKKOS_INLINE_FUNCTION
StdAlgoMinMaxOpsTestClamp(ViewType aIn) : m_view(aIn) {}
};
TEST(TEST_CATEGORY, clamp_within_parfor) {
using view_t = Kokkos::View<double*>;
view_t a("a", 10);
StdAlgoMinMaxOpsTestClamp<view_t> 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), 3.);
}
}
|