| 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_Tbsv.hpp" |
| 23 |
#include "Test_Batched_DenseUtils.hpp" |
| 24 |
|
| 25 |
using namespace KokkosBatched; |
| 26 |
|
| 27 |
namespace Test { |
| 28 |
namespace Tbsv { |
| 29 |
|
| 30 |
template <typename U, typename T, typename D> |
| 31 |
struct ParamTag { |
| 32 |
using uplo = U; |
| 33 |
using trans = T; |
| 34 |
using diag = D; |
| 35 |
}; |
| 36 |
|
| 37 |
template <typename DeviceType, typename AViewType, typename BViewType, typename ScalarType, typename ParamTagType, |
| 38 |
typename AlgoTagType> |
| 39 |
struct Functor_BatchedSerialTrsv { |
| 40 |
using execution_space = typename DeviceType::execution_space; |
| 41 |
AViewType _a; |
| 42 |
BViewType _b; |
| 43 |
|
| 44 |
ScalarType _alpha; |
| 45 |
|
| 46 |
KOKKOS_INLINE_FUNCTION |
| 47 |
Functor_BatchedSerialTrsv(const ScalarType alpha, const AViewType &a, const BViewType &b) |
| 48 |
: _a(a), _b(b), _alpha(alpha) {} |
| 49 |
|
| 50 |
KOKKOS_INLINE_FUNCTION |
| 51 |
void operator()(const ParamTagType &, const int k) const { |
| 52 |
auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL()); |
| 53 |
auto bb = Kokkos::subview(_b, k, Kokkos::ALL()); |
| 54 |
|
| 55 |
KokkosBatched::SerialTrsv<typename ParamTagType::uplo, typename ParamTagType::trans, typename ParamTagType::diag, |
| 56 |
AlgoTagType>::invoke(_alpha, aa, bb); |
| 57 |
} |
| 58 |
|
| 59 |
inline void run() { |
| 60 |
using value_type = typename AViewType::non_const_value_type; |
| 61 |
std::string name_region("KokkosBatched::Test::SerialTbsv"); |
| 62 |
const std::string name_value_type = Test::value_type_name<value_type>(); |
| 63 |
std::string name = name_region + name_value_type; |
| 64 |
Kokkos::RangePolicy<execution_space, ParamTagType> policy(0, _b.extent(0)); |
| 65 |
Kokkos::parallel_for(name.c_str(), policy, *this); |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
template <typename DeviceType, typename AViewType, typename BViewType, typename ParamTagType, typename AlgoTagType> |
| 70 |
struct Functor_BatchedSerialTbsv { |
| 71 |
using execution_space = typename DeviceType::execution_space; |
| 72 |
AViewType _a; |
| 73 |
BViewType _b; |
| 74 |
int _k; |
| 75 |
|
| 76 |
KOKKOS_INLINE_FUNCTION |
| 77 |
Functor_BatchedSerialTbsv(const AViewType &a, const BViewType &b, const int k) : _a(a), _b(b), _k(k) {} |
| 78 |
|
| 79 |
KOKKOS_INLINE_FUNCTION |
| 80 |
void operator()(const ParamTagType &, const int k) const { |
| 81 |
auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL()); |
| 82 |
auto bb = Kokkos::subview(_b, k, Kokkos::ALL()); |
| 83 |
|
| 84 |
KokkosBatched::SerialTbsv<typename ParamTagType::uplo, typename ParamTagType::trans, typename ParamTagType::diag, |
| 85 |
AlgoTagType>::invoke(aa, bb, _k); |
| 86 |
} |
| 87 |
|
| 88 |
inline void run() { |
| 89 |
using value_type = typename AViewType::non_const_value_type; |
| 90 |
std::string name_region("KokkosBatched::Test::SerialTbsv"); |
| 91 |
const std::string name_value_type = Test::value_type_name<value_type>(); |
| 92 |
std::string name = name_region + name_value_type; |
| 93 |
Kokkos::Profiling::pushRegion(name.c_str()); |
| 94 |
Kokkos::RangePolicy<execution_space, ParamTagType> policy(0, _b.extent(0)); |
| 95 |
Kokkos::parallel_for(name.c_str(), policy, *this); |
| 96 |
Kokkos::Profiling::popRegion(); |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
template <typename DeviceType, typename ScalarType, typename LayoutType, typename ParamTagType, typename AlgoTagType> |
| 101 |
/// \brief Implementation details of batched tbsv test |
| 102 |
/// |
| 103 |
/// \param N [in] Batch size of RHS (banded matrix can also be batched matrix) |
| 104 |
/// \param k [in] Number of superdiagonals or subdiagonals of matrix A |
| 105 |
/// \param BlkSize [in] Block size of matrix A |
| 106 |
void impl_test_batched_tbsv(const int N, const int k, const int BlkSize) { |
| 107 |
using execution_space = typename DeviceType::execution_space; |
| 108 |
using View2DType = Kokkos::View<ScalarType **, LayoutType, execution_space>; |
| 109 |
using View3DType = Kokkos::View<ScalarType ***, LayoutType, execution_space>; |
| 110 |
|
| 111 |
// Reference is created by trsv from triangular matrix |
| 112 |
View3DType A("A", N, BlkSize, BlkSize), Ref("Ref", N, BlkSize, BlkSize); |
| 113 |
View3DType Ab("Ab", N, k + 1, BlkSize); // Banded matrix |
| 114 |
View2DType x0("x0", N, BlkSize), x1("x1", N, BlkSize); // Solutions |
| 115 |
|
| 116 |
Kokkos::Random_XorShift64_Pool<execution_space> rand_pool(13718); |
| 117 |
ScalarType randStart, randEnd; |
| 118 |
Test::getRandomBounds(1.0, randStart, randEnd); |
| 119 |
Kokkos::fill_random(Ref, rand_pool, randStart, randEnd); |
| 120 |
Kokkos::fill_random(x0, rand_pool, randStart, randEnd); |
| 121 |
|
| 122 |
Kokkos::deep_copy(x1, x0); |
| 123 |
|
| 124 |
// Create triangluar or banded matrix |
| 125 |
create_banded_triangular_matrix<View3DType, View3DType, typename ParamTagType::uplo>(Ref, A, k, false); |
| 126 |
create_banded_triangular_matrix<View3DType, View3DType, typename ParamTagType::uplo>(Ref, Ab, k, true); |
| 127 |
|
| 128 |
// Reference trsv |
| 129 |
Functor_BatchedSerialTrsv<DeviceType, View3DType, View2DType, ScalarType, ParamTagType, Algo::Trsv::Unblocked>(1.0, A, |
| 130 |
x0) |
| 131 |
.run(); |
| 132 |
|
| 133 |
// tbsv |
| 134 |
Functor_BatchedSerialTbsv<DeviceType, View3DType, View2DType, ParamTagType, AlgoTagType>(Ab, x1, k).run(); |
| 135 |
|
| 136 |
Kokkos::fence(); |
| 137 |
|
| 138 |
// this eps is about 10^-14 |
| 139 |
using ats = typename Kokkos::ArithTraits<ScalarType>; |
| 140 |
using mag_type = typename ats::mag_type; |
| 141 |
mag_type eps = 1.0e3 * ats::epsilon(); |
| 142 |
|
| 143 |
// Check x0 = x1 |
| 144 |
auto h_x0 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), x0); |
| 145 |
auto h_x1 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), x1); |
| 146 |
for (int i = 0; i < N; i++) { |
| 147 |
for (int j = 0; j < BlkSize; j++) { |
| 148 |
EXPECT_NEAR_KK(h_x0(i, j), h_x1(i, j), eps); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
template <typename DeviceType, typename ScalarType, typename LayoutType, typename ParamTagType, typename AlgoTagType> |
| 154 |
/// \brief Implementation details of batched tbsv test |
| 155 |
/// |
| 156 |
/// \param N [in] Batch size of RHS (banded matrix can also be batched matrix) |
| 157 |
void impl_test_batched_tbsv_analytical(const std::size_t N) { |
| 158 |
using execution_space = typename DeviceType::execution_space; |
| 159 |
using View2DType = Kokkos::View<ScalarType **, LayoutType, execution_space>; |
| 160 |
using StridedView2DType = Kokkos::View<ScalarType **, Kokkos::LayoutStride, execution_space>; |
| 161 |
using View3DType = Kokkos::View<ScalarType ***, LayoutType, execution_space>; |
| 162 |
|
| 163 |
// Reference is created by trsv from triangular matrix |
| 164 |
constexpr std::size_t BlkSize = 3, k = 2, incx = 2; |
| 165 |
|
| 166 |
View3DType A("A", N, BlkSize, BlkSize), ref("Ref", N, BlkSize, BlkSize); |
| 167 |
View3DType Ab("Ab", N, k + 1, BlkSize); // Banded matrix |
| 168 |
View2DType x0("x0", N, BlkSize), x_ref("x_ref", N, BlkSize); // Solutions |
| 169 |
|
| 170 |
// Testing incx argument with strided Views |
| 171 |
Kokkos::LayoutStride layout{N, incx, BlkSize, N * incx}; |
| 172 |
StridedView2DType x1("x1", layout); // Solutions |
| 173 |
|
| 174 |
Kokkos::RangePolicy<execution_space> policy(0, N); |
| 175 |
Kokkos::parallel_for( |
| 176 |
"KokkosBatched::Test::SerialTbsv::Initialize", policy, KOKKOS_LAMBDA(const std::size_t ib) { |
| 177 |
for (std::size_t i = 0; i < BlkSize; i++) { |
| 178 |
for (std::size_t j = 0; j < BlkSize; j++) { |
| 179 |
ref(ib, i, j) = i + 1; |
| 180 |
} |
| 181 |
} |
| 182 |
for (std::size_t j = 0; j < BlkSize; j++) { |
| 183 |
x0(ib, j) = 1; |
| 184 |
x1(ib, j) = 1; |
| 185 |
} |
| 186 |
|
| 187 |
if (std::is_same_v<typename ParamTagType::uplo, KokkosBatched::Uplo::Upper>) { |
| 188 |
if (std::is_same_v<typename ParamTagType::trans, Trans::NoTranspose>) { |
| 189 |
if (std::is_same_v<typename ParamTagType::diag, Diag::NonUnit>) { |
| 190 |
x_ref(ib, 0) = 1.0 / 2.0; |
| 191 |
x_ref(ib, 1) = 1.0 / 6.0; |
| 192 |
x_ref(ib, 2) = 1.0 / 3.0; |
| 193 |
} else { |
| 194 |
x_ref(ib, 0) = 1.0; |
| 195 |
x_ref(ib, 1) = -1.0; |
| 196 |
x_ref(ib, 2) = 1.0; |
| 197 |
} |
| 198 |
} else { |
| 199 |
if (std::is_same_v<typename ParamTagType::diag, Diag::NonUnit>) { |
| 200 |
x_ref(ib, 0) = 1.0; |
| 201 |
x_ref(ib, 1) = 0.0; |
| 202 |
x_ref(ib, 2) = 0.0; |
| 203 |
} else { |
| 204 |
x_ref(ib, 0) = 1.0; |
| 205 |
x_ref(ib, 1) = 0.0; |
| 206 |
x_ref(ib, 2) = 0.0; |
| 207 |
} |
| 208 |
} |
| 209 |
} else { |
| 210 |
if (std::is_same_v<typename ParamTagType::trans, Trans::NoTranspose>) { |
| 211 |
if (std::is_same_v<typename ParamTagType::diag, Diag::NonUnit>) { |
| 212 |
x_ref(ib, 0) = 1.0; |
| 213 |
x_ref(ib, 1) = -1.0 / 2.0; |
| 214 |
x_ref(ib, 2) = -1.0 / 6.0; |
| 215 |
} else { |
| 216 |
x_ref(ib, 0) = 1.0; |
| 217 |
x_ref(ib, 1) = -1.0; |
| 218 |
x_ref(ib, 2) = 1.0; |
| 219 |
} |
| 220 |
} else { |
| 221 |
if (std::is_same_v<typename ParamTagType::diag, Diag::NonUnit>) { |
| 222 |
x_ref(ib, 0) = 0.0; |
| 223 |
x_ref(ib, 1) = 0.0; |
| 224 |
x_ref(ib, 2) = 1.0 / 3.0; |
| 225 |
} else { |
| 226 |
x_ref(ib, 0) = 2.0; |
| 227 |
x_ref(ib, 1) = -2.0; |
| 228 |
x_ref(ib, 2) = 1.0; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
}); |
| 233 |
|
| 234 |
Kokkos::fence(); |
| 235 |
|
| 236 |
// Create triangluar or banded matrix |
| 237 |
create_banded_triangular_matrix<View3DType, View3DType, typename ParamTagType::uplo>(ref, A, k, false); |
| 238 |
create_banded_triangular_matrix<View3DType, View3DType, typename ParamTagType::uplo>(ref, Ab, k, true); |
| 239 |
|
| 240 |
// tbsv |
| 241 |
Functor_BatchedSerialTbsv<DeviceType, View3DType, View2DType, ParamTagType, AlgoTagType>(Ab, x0, k).run(); |
| 242 |
|
| 243 |
// tbsv with incx == 2 |
| 244 |
Functor_BatchedSerialTbsv<DeviceType, View3DType, StridedView2DType, ParamTagType, AlgoTagType>(Ab, x1, k).run(); |
| 245 |
|
| 246 |
Kokkos::fence(); |
| 247 |
|
| 248 |
// Check x0 = x_ref and x1 = x_ref |
| 249 |
// Firstly, prepare contiguous views on host |
| 250 |
auto h_x0 = Kokkos::create_mirror_view(x0); |
| 251 |
auto h_x1 = Kokkos::create_mirror_view(x0); |
| 252 |
|
| 253 |
Kokkos::deep_copy(h_x0, x0); |
| 254 |
|
| 255 |
// Pack x1 into x0 for contiguous storage |
| 256 |
Kokkos::parallel_for( |
| 257 |
"KokkosBatched::Test::SerialTbsv::Copy", policy, KOKKOS_LAMBDA(const std::size_t ib) { |
| 258 |
for (std::size_t j = 0; j < BlkSize; j++) { |
| 259 |
x0(ib, j) = x1(ib, j); |
| 260 |
} |
| 261 |
}); |
| 262 |
|
| 263 |
Kokkos::fence(); |
| 264 |
Kokkos::deep_copy(h_x1, x0); |
| 265 |
|
| 266 |
// this eps is about 10^-14 |
| 267 |
using ats = typename Kokkos::ArithTraits<ScalarType>; |
| 268 |
using mag_type = typename ats::mag_type; |
| 269 |
mag_type eps = 1.0e3 * ats::epsilon(); |
| 270 |
|
| 271 |
auto h_x_ref = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), x_ref); |
| 272 |
for (std::size_t ib = 0; ib < N; ib++) { |
| 273 |
for (std::size_t j = 0; j < BlkSize; j++) { |
| 274 |
// Check x0 = x_ref |
| 275 |
EXPECT_NEAR_KK(h_x0(ib, j), h_x_ref(ib, j), eps); |
| 276 |
|
| 277 |
// Check x1 = x_ref |
| 278 |
EXPECT_NEAR_KK(h_x1(ib, j), h_x_ref(ib, j), eps); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
} // namespace Tbsv |
| 284 |
} // namespace Test |
| 285 |
|
| 286 |
template <typename DeviceType, typename ScalarType, typename ParamTagType, typename AlgoTagType> |
| 287 |
int test_batched_tbsv() { |
| 288 |
#if defined(KOKKOSKERNELS_INST_LAYOUTLEFT) |
| 289 |
{ |
| 290 |
using LayoutType = Kokkos::LayoutLeft; |
| 291 |
Test::Tbsv::impl_test_batched_tbsv_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(0); |
| 292 |
Test::Tbsv::impl_test_batched_tbsv_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1); |
| 293 |
Test::Tbsv::impl_test_batched_tbsv<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(0, 1, 10); |
| 294 |
for (int i = 0; i < 10; i++) { |
| 295 |
Test::Tbsv::impl_test_batched_tbsv<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1, 1, i); |
| 296 |
} |
| 297 |
} |
| 298 |
#endif |
| 299 |
#if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT) |
| 300 |
{ |
| 301 |
using LayoutType = Kokkos::LayoutRight; |
| 302 |
Test::Tbsv::impl_test_batched_tbsv_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(0); |
| 303 |
Test::Tbsv::impl_test_batched_tbsv_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1); |
| 304 |
Test::Tbsv::impl_test_batched_tbsv<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(0, 1, 10); |
| 305 |
for (int i = 0; i < 10; i++) { |
| 306 |
Test::Tbsv::impl_test_batched_tbsv<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1, 1, i); |
| 307 |
} |
| 308 |
} |
| 309 |
#endif |
| 310 |
|
| 311 |
return 0; |
| 312 |
} |