| 1 |
//@HEADER |
| 2 |
// ************************************************************************ |
| 3 |
// |
| 4 |
// Kokkos v. 4.0 |
| 5 |
// Copyright (2022) National Technology & Engineering |
| 6 |
// Solutions of Sandia, LLC (NTESS). |
| 7 |
// |
| 8 |
// Under the terms of Contract DE-NA0003525 with NTESS, |
| 9 |
// the U.S. Government retains certain rights in this software. |
| 10 |
// |
| 11 |
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. |
| 12 |
// See https://kokkos.org/LICENSE for license information. |
| 13 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 14 |
// |
| 15 |
//@HEADER |
| 16 |
/// \author Yuuichi Asahi ([email protected]) |
| 17 |
#include <gtest/gtest.h> |
| 18 |
#include <Kokkos_Core.hpp> |
| 19 |
#include <Kokkos_Random.hpp> |
| 20 |
|
| 21 |
#include "KokkosBatched_Util.hpp" |
| 22 |
#include "KokkosBatched_Iamax.hpp" |
| 23 |
|
| 24 |
using namespace KokkosBatched; |
| 25 |
|
| 26 |
namespace Test { |
| 27 |
namespace Iamax { |
| 28 |
|
| 29 |
template <typename DeviceType, typename XViewType, typename RViewType> |
| 30 |
struct Functor_BatchedSerialIamax { |
| 31 |
using execution_space = typename DeviceType::execution_space; |
| 32 |
XViewType m_x; |
| 33 |
RViewType m_r; |
| 34 |
|
| 35 |
KOKKOS_INLINE_FUNCTION |
| 36 |
Functor_BatchedSerialIamax(const XViewType &x, const RViewType &r) : m_x(x), m_r(r) {} |
| 37 |
|
| 38 |
KOKKOS_INLINE_FUNCTION |
| 39 |
void operator()(const int k) const { |
| 40 |
auto sub_x = Kokkos::subview(m_x, k, Kokkos::ALL()); |
| 41 |
auto iamax = KokkosBatched::SerialIamax::invoke(sub_x); |
| 42 |
m_r(k) = static_cast<int>(iamax); |
| 43 |
} |
| 44 |
|
| 45 |
inline void run() { |
| 46 |
using value_type = typename XViewType::non_const_value_type; |
| 47 |
std::string name_region("KokkosBatched::Test::SerialIamax"); |
| 48 |
std::string name_value_type = Test::value_type_name<value_type>(); |
| 49 |
std::string name = name_region + name_value_type; |
| 50 |
Kokkos::Profiling::pushRegion(name.c_str()); |
| 51 |
Kokkos::RangePolicy<execution_space> policy(0, m_x.extent(0)); |
| 52 |
Kokkos::parallel_for(name.c_str(), policy, *this); |
| 53 |
Kokkos::Profiling::popRegion(); |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
/// \brief Implementation details of batched iamax analytical test |
| 58 |
/// A0: [1, 2, 0] -> 1 |
| 59 |
/// A1: [-5, 4, 3] -> 0 |
| 60 |
/// A2: [0, 0, 0] -> 0 |
| 61 |
/// A3: [0, -1, -1] -> 1 |
| 62 |
/// |
| 63 |
/// \param N [in] Batch size of A |
| 64 |
template <typename DeviceType, typename ScalarType, typename LayoutType> |
| 65 |
void impl_test_batched_iamax_analytical(const std::size_t N) { |
| 66 |
using View2DType = Kokkos::View<ScalarType **, LayoutType, DeviceType>; |
| 67 |
using StridedView2DType = Kokkos::View<ScalarType **, Kokkos::LayoutStride, DeviceType>; |
| 68 |
using MaxView1DType = Kokkos::View<int *, LayoutType, DeviceType>; |
| 69 |
|
| 70 |
View2DType A0("A0", N, 3), A1("A1", N, 3), A2("A2", N, 3), A3("A3", N, 3); |
| 71 |
MaxView1DType iamax0("iamax0", N), iamax_ref0("iamax_ref0", N), iamax1("iamax1", N), iamax_ref1("iamax_ref1", N), |
| 72 |
iamax2("iamax2", N), iamax_ref2("iamax_ref2", N), iamax3("iamax3", N), iamax_ref3("iamax_ref3", N); |
| 73 |
|
| 74 |
// Testing incx argument with strided views |
| 75 |
constexpr std::size_t incx = 2; |
| 76 |
Kokkos::LayoutStride layout{N, incx, 3, N * incx}; |
| 77 |
StridedView2DType A0_s("A0_s", layout), A1_s("A1_s", layout), A2_s("A2_s", layout), A3_s("A3_s", layout); |
| 78 |
MaxView1DType iamax_s0("iamax_s0", N), iamax_s1("iamax_s1", N), iamax_s2("iamax_s2", N), iamax_s3("iamax_s3", N); |
| 79 |
|
| 80 |
// Initialize A0, A1, A2, A3 |
| 81 |
auto h_A0 = Kokkos::create_mirror_view(A0); |
| 82 |
auto h_A1 = Kokkos::create_mirror_view(A1); |
| 83 |
auto h_A2 = Kokkos::create_mirror_view(A2); |
| 84 |
auto h_A3 = Kokkos::create_mirror_view(A3); |
| 85 |
|
| 86 |
auto h_iamax_ref0 = Kokkos::create_mirror_view(iamax_ref0); |
| 87 |
auto h_iamax_ref1 = Kokkos::create_mirror_view(iamax_ref1); |
| 88 |
auto h_iamax_ref2 = Kokkos::create_mirror_view(iamax_ref2); |
| 89 |
auto h_iamax_ref3 = Kokkos::create_mirror_view(iamax_ref3); |
| 90 |
for (std::size_t k = 0; k < N; k++) { |
| 91 |
h_A0(k, 0) = 1; |
| 92 |
h_A0(k, 1) = 2; |
| 93 |
h_A0(k, 2) = 0; |
| 94 |
|
| 95 |
h_A1(k, 0) = -5; |
| 96 |
h_A1(k, 1) = 4; |
| 97 |
h_A1(k, 2) = 3; |
| 98 |
|
| 99 |
h_A2(k, 0) = 0; |
| 100 |
h_A2(k, 1) = 0; |
| 101 |
h_A2(k, 2) = 0; |
| 102 |
|
| 103 |
h_A3(k, 0) = 0; |
| 104 |
h_A3(k, 1) = -1; |
| 105 |
h_A3(k, 2) = -1; |
| 106 |
|
| 107 |
h_iamax_ref0(k) = 1; |
| 108 |
h_iamax_ref1(k) = 0; |
| 109 |
h_iamax_ref2(k) = 0; |
| 110 |
h_iamax_ref3(k) = 1; |
| 111 |
} |
| 112 |
Kokkos::deep_copy(A0, h_A0); |
| 113 |
Kokkos::deep_copy(A1, h_A1); |
| 114 |
Kokkos::deep_copy(A2, h_A2); |
| 115 |
Kokkos::deep_copy(A3, h_A3); |
| 116 |
|
| 117 |
// Strided view can be copied only on the same device |
| 118 |
Kokkos::deep_copy(A0_s, A0); |
| 119 |
Kokkos::deep_copy(A1_s, A1); |
| 120 |
Kokkos::deep_copy(A2_s, A2); |
| 121 |
Kokkos::deep_copy(A3_s, A3); |
| 122 |
|
| 123 |
Functor_BatchedSerialIamax<DeviceType, View2DType, MaxView1DType>(A0, iamax0).run(); |
| 124 |
Functor_BatchedSerialIamax<DeviceType, View2DType, MaxView1DType>(A1, iamax1).run(); |
| 125 |
Functor_BatchedSerialIamax<DeviceType, View2DType, MaxView1DType>(A2, iamax2).run(); |
| 126 |
Functor_BatchedSerialIamax<DeviceType, View2DType, MaxView1DType>(A3, iamax3).run(); |
| 127 |
|
| 128 |
// For strided views |
| 129 |
Functor_BatchedSerialIamax<DeviceType, StridedView2DType, MaxView1DType>(A0_s, iamax_s0).run(); |
| 130 |
Functor_BatchedSerialIamax<DeviceType, StridedView2DType, MaxView1DType>(A1_s, iamax_s1).run(); |
| 131 |
Functor_BatchedSerialIamax<DeviceType, StridedView2DType, MaxView1DType>(A2_s, iamax_s2).run(); |
| 132 |
Functor_BatchedSerialIamax<DeviceType, StridedView2DType, MaxView1DType>(A3_s, iamax_s3).run(); |
| 133 |
|
| 134 |
Kokkos::fence(); |
| 135 |
|
| 136 |
// Copy to host for comparison |
| 137 |
auto h_iamax0 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), iamax0); |
| 138 |
auto h_iamax1 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), iamax1); |
| 139 |
auto h_iamax2 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), iamax2); |
| 140 |
auto h_iamax3 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), iamax3); |
| 141 |
auto h_iamax_s0 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), iamax_s0); |
| 142 |
auto h_iamax_s1 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), iamax_s1); |
| 143 |
auto h_iamax_s2 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), iamax_s2); |
| 144 |
auto h_iamax_s3 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), iamax_s3); |
| 145 |
|
| 146 |
// Check if max index is correct |
| 147 |
for (std::size_t k = 0; k < N; k++) { |
| 148 |
EXPECT_EQ(h_iamax0(k), h_iamax_ref0(k)); |
| 149 |
EXPECT_EQ(h_iamax1(k), h_iamax_ref1(k)); |
| 150 |
EXPECT_EQ(h_iamax2(k), h_iamax_ref2(k)); |
| 151 |
EXPECT_EQ(h_iamax3(k), h_iamax_ref3(k)); |
| 152 |
EXPECT_EQ(h_iamax_s0(k), h_iamax_ref0(k)); |
| 153 |
EXPECT_EQ(h_iamax_s1(k), h_iamax_ref1(k)); |
| 154 |
EXPECT_EQ(h_iamax_s2(k), h_iamax_ref2(k)); |
| 155 |
EXPECT_EQ(h_iamax_s3(k), h_iamax_ref3(k)); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/// \brief Implementation details of batched pbtrs test |
| 160 |
/// |
| 161 |
/// \param N [in] Batch size of RHS (banded matrix can also be batched matrix) |
| 162 |
/// \param BlkSize [in] Block size of matrix A |
| 163 |
template <typename DeviceType, typename ScalarType, typename LayoutType> |
| 164 |
void impl_test_batched_iamax(const std::size_t N, const std::size_t BlkSize) { |
| 165 |
using ats = typename Kokkos::ArithTraits<ScalarType>; |
| 166 |
using RealType = typename ats::mag_type; |
| 167 |
using View2DType = Kokkos::View<ScalarType **, LayoutType, DeviceType>; |
| 168 |
using StridedView2DType = Kokkos::View<ScalarType **, Kokkos::LayoutStride, DeviceType>; |
| 169 |
using MaxView1DType = Kokkos::View<int *, LayoutType, DeviceType>; |
| 170 |
|
| 171 |
View2DType A("A", N, BlkSize); |
| 172 |
MaxView1DType iamax("iamax", N), iamax_ref("iamax_ref", N); |
| 173 |
|
| 174 |
// Testing incx argument with strided views |
| 175 |
constexpr std::size_t incx = 2; |
| 176 |
Kokkos::LayoutStride layout{N, incx, BlkSize, N * incx}; |
| 177 |
StridedView2DType A_s("A_s", layout); |
| 178 |
MaxView1DType iamax_s("iamax_s", N); |
| 179 |
|
| 180 |
// Initialize A with random values |
| 181 |
using execution_space = typename DeviceType::execution_space; |
| 182 |
Kokkos::Random_XorShift64_Pool<execution_space> rand_pool(13718); |
| 183 |
ScalarType randStart, randEnd; |
| 184 |
|
| 185 |
KokkosKernels::Impl::getRandomBounds(1.0, randStart, randEnd); |
| 186 |
Kokkos::fill_random(A, rand_pool, randStart, randEnd); |
| 187 |
|
| 188 |
// Strided view can be copied only on the same device |
| 189 |
Kokkos::deep_copy(A_s, A); |
| 190 |
|
| 191 |
Functor_BatchedSerialIamax<DeviceType, View2DType, MaxView1DType>(A, iamax).run(); |
| 192 |
|
| 193 |
// For strided views |
| 194 |
Functor_BatchedSerialIamax<DeviceType, StridedView2DType, MaxView1DType>(A_s, iamax_s).run(); |
| 195 |
|
| 196 |
Kokkos::fence(); |
| 197 |
|
| 198 |
// Reference |
| 199 |
auto h_iamax_ref = Kokkos::create_mirror_view(iamax_ref); |
| 200 |
if (BlkSize == 0) { |
| 201 |
// As well as blas, we store 0 (0 in Fortran) for empty matrix |
| 202 |
for (std::size_t k = 0; k < N; k++) { |
| 203 |
h_iamax_ref(k) = 0; |
| 204 |
} |
| 205 |
} else { |
| 206 |
auto h_A = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), A); |
| 207 |
for (std::size_t k = 0; k < N; k++) { |
| 208 |
RealType amax = Kokkos::abs(h_A(k, 0)); |
| 209 |
int iamax_tmp = 0; |
| 210 |
for (std::size_t i = 1; i < BlkSize; i++) { |
| 211 |
const RealType abs_A_i = Kokkos::abs(h_A(k, i)); |
| 212 |
if (abs_A_i > amax) { |
| 213 |
amax = abs_A_i; |
| 214 |
iamax_tmp = static_cast<int>(i); |
| 215 |
} |
| 216 |
} |
| 217 |
h_iamax_ref(k) = iamax_tmp; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
// Copy to host for comparison |
| 222 |
auto h_iamax = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), iamax); |
| 223 |
auto h_iamax_s = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), iamax_s); |
| 224 |
|
| 225 |
// Check if max index is correct |
| 226 |
for (std::size_t k = 0; k < N; k++) { |
| 227 |
EXPECT_EQ(h_iamax(k), h_iamax_ref(k)); |
| 228 |
EXPECT_EQ(h_iamax_s(k), h_iamax_ref(k)); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
} // namespace Iamax |
| 233 |
} // namespace Test |
| 234 |
|
| 235 |
template <typename DeviceType, typename ScalarType> |
| 236 |
int test_batched_iamax() { |
| 237 |
#if defined(KOKKOSKERNELS_INST_LAYOUTLEFT) |
| 238 |
{ |
| 239 |
using LayoutType = Kokkos::LayoutLeft; |
| 240 |
Test::Iamax::impl_test_batched_iamax_analytical<DeviceType, ScalarType, LayoutType>(1); |
| 241 |
Test::Iamax::impl_test_batched_iamax_analytical<DeviceType, ScalarType, LayoutType>(2); |
| 242 |
for (std::size_t i = 0; i < 10; i++) { |
| 243 |
Test::Iamax::impl_test_batched_iamax<DeviceType, ScalarType, LayoutType>(1, i); |
| 244 |
Test::Iamax::impl_test_batched_iamax<DeviceType, ScalarType, LayoutType>(2, i); |
| 245 |
} |
| 246 |
} |
| 247 |
#endif |
| 248 |
#if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT) |
| 249 |
{ |
| 250 |
using LayoutType = Kokkos::LayoutRight; |
| 251 |
Test::Iamax::impl_test_batched_iamax_analytical<DeviceType, ScalarType, LayoutType>(1); |
| 252 |
Test::Iamax::impl_test_batched_iamax_analytical<DeviceType, ScalarType, LayoutType>(2); |
| 253 |
for (std::size_t i = 0; i < 10; i++) { |
| 254 |
Test::Iamax::impl_test_batched_iamax<DeviceType, ScalarType, LayoutType>(1, i); |
| 255 |
Test::Iamax::impl_test_batched_iamax<DeviceType, ScalarType, LayoutType>(2, i); |
| 256 |
} |
| 257 |
} |
| 258 |
#endif |
| 259 |
|
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
#if defined(KOKKOSKERNELS_INST_FLOAT) |
| 264 |
TEST_F(TestCategory, test_batched_iamax_float) { test_batched_iamax<TestDevice, float>(); } |
| 265 |
#endif |
| 266 |
|
| 267 |
#if defined(KOKKOSKERNELS_INST_DOUBLE) |
| 268 |
TEST_F(TestCategory, test_batched_iamax_double) { test_batched_iamax<TestDevice, double>(); } |
| 269 |
#endif |
| 270 |
|
| 271 |
#if defined(KOKKOSKERNELS_INST_COMPLEX_FLOAT) |
| 272 |
TEST_F(TestCategory, test_batched_iamax_fcomplex) { test_batched_iamax<TestDevice, Kokkos::complex<float>>(); } |
| 273 |
#endif |
| 274 |
|
| 275 |
#if defined(KOKKOSKERNELS_INST_COMPLEX_DOUBLE) |
| 276 |
TEST_F(TestCategory, test_batched_iamax_dcomplex) { test_batched_iamax<TestDevice, Kokkos::complex<double>>(); } |
| 277 |
#endif |