| 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 |
#include <KokkosBlas2_gemv.hpp> |
| 21 |
#include "KokkosBatched_Util.hpp" |
| 22 |
#include "KokkosBatched_Pbtrf.hpp" |
| 23 |
#include "KokkosBatched_Pbtrs.hpp" |
| 24 |
#include "Test_Batched_DenseUtils.hpp" |
| 25 |
|
| 26 |
using namespace KokkosBatched; |
| 27 |
|
| 28 |
namespace Test { |
| 29 |
namespace Pbtrs { |
| 30 |
|
| 31 |
template <typename U> |
| 32 |
struct ParamTag { |
| 33 |
using uplo = U; |
| 34 |
}; |
| 35 |
|
| 36 |
template <typename DeviceType, typename ABViewType, typename ParamTagType, typename AlgoTagType> |
| 37 |
struct Functor_BatchedSerialPbtrf { |
| 38 |
using execution_space = typename DeviceType::execution_space; |
| 39 |
ABViewType _ab; |
| 40 |
|
| 41 |
KOKKOS_INLINE_FUNCTION |
| 42 |
Functor_BatchedSerialPbtrf(const ABViewType &ab) : _ab(ab) {} |
| 43 |
|
| 44 |
KOKKOS_INLINE_FUNCTION |
| 45 |
void operator()(const ParamTagType &, const int k) const { |
| 46 |
auto sub_ab = Kokkos::subview(_ab, k, Kokkos::ALL(), Kokkos::ALL()); |
| 47 |
|
| 48 |
KokkosBatched::SerialPbtrf<typename ParamTagType::uplo, AlgoTagType>::invoke(sub_ab); |
| 49 |
} |
| 50 |
|
| 51 |
inline void run() { |
| 52 |
using value_type = typename ABViewType::non_const_value_type; |
| 53 |
std::string name_region("KokkosBatched::Test::SerialPbtrs"); |
| 54 |
const std::string name_value_type = Test::value_type_name<value_type>(); |
| 55 |
std::string name = name_region + name_value_type; |
| 56 |
Kokkos::RangePolicy<execution_space, ParamTagType> policy(0, _ab.extent(0)); |
| 57 |
Kokkos::parallel_for(name.c_str(), policy, *this); |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
template <typename DeviceType, typename ABViewType, typename BViewType, typename ParamTagType, typename AlgoTagType> |
| 62 |
struct Functor_BatchedSerialPbtrs { |
| 63 |
using execution_space = typename DeviceType::execution_space; |
| 64 |
ABViewType _ab; |
| 65 |
BViewType _b; |
| 66 |
|
| 67 |
KOKKOS_INLINE_FUNCTION |
| 68 |
Functor_BatchedSerialPbtrs(const ABViewType &ab, const BViewType &b) : _ab(ab), _b(b) {} |
| 69 |
|
| 70 |
KOKKOS_INLINE_FUNCTION |
| 71 |
void operator()(const ParamTagType &, const int k, int &info) const { |
| 72 |
auto sub_ab = Kokkos::subview(_ab, k, Kokkos::ALL(), Kokkos::ALL()); |
| 73 |
auto bb = Kokkos::subview(_b, k, Kokkos::ALL()); |
| 74 |
|
| 75 |
info += KokkosBatched::SerialPbtrs<typename ParamTagType::uplo, AlgoTagType>::invoke(sub_ab, bb); |
| 76 |
} |
| 77 |
|
| 78 |
inline int run() { |
| 79 |
using value_type = typename ABViewType::non_const_value_type; |
| 80 |
std::string name_region("KokkosBatched::Test::SerialPbtrs"); |
| 81 |
const std::string name_value_type = Test::value_type_name<value_type>(); |
| 82 |
std::string name = name_region + name_value_type; |
| 83 |
int info_sum = 0; |
| 84 |
Kokkos::Profiling::pushRegion(name.c_str()); |
| 85 |
Kokkos::RangePolicy<execution_space, ParamTagType> policy(0, _b.extent(0)); |
| 86 |
Kokkos::parallel_reduce(name.c_str(), policy, *this, info_sum); |
| 87 |
Kokkos::Profiling::popRegion(); |
| 88 |
return info_sum; |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
template <typename DeviceType, typename ScalarType, typename AViewType, typename xViewType, typename yViewType> |
| 93 |
struct Functor_BatchedSerialGemv { |
| 94 |
using execution_space = typename DeviceType::execution_space; |
| 95 |
AViewType _a; |
| 96 |
xViewType _x; |
| 97 |
yViewType _y; |
| 98 |
ScalarType _alpha, _beta; |
| 99 |
|
| 100 |
KOKKOS_INLINE_FUNCTION |
| 101 |
Functor_BatchedSerialGemv(const ScalarType alpha, const AViewType &a, const xViewType &x, const ScalarType beta, |
| 102 |
const yViewType &y) |
| 103 |
: _a(a), _x(x), _y(y), _alpha(alpha), _beta(beta) {} |
| 104 |
|
| 105 |
KOKKOS_INLINE_FUNCTION |
| 106 |
void operator()(const int k) const { |
| 107 |
auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL()); |
| 108 |
auto xx = Kokkos::subview(_x, k, Kokkos::ALL()); |
| 109 |
auto yy = Kokkos::subview(_y, k, Kokkos::ALL()); |
| 110 |
|
| 111 |
KokkosBlas::SerialGemv<Trans::NoTranspose, Algo::Gemv::Unblocked>::invoke(_alpha, aa, xx, _beta, yy); |
| 112 |
} |
| 113 |
|
| 114 |
inline void run() { |
| 115 |
using value_type = typename AViewType::non_const_value_type; |
| 116 |
std::string name_region("KokkosBatched::Test::SerialPbtrs"); |
| 117 |
const std::string name_value_type = Test::value_type_name<value_type>(); |
| 118 |
std::string name = name_region + name_value_type; |
| 119 |
Kokkos::RangePolicy<execution_space> policy(0, _x.extent(0)); |
| 120 |
Kokkos::parallel_for(name.c_str(), policy, *this); |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
template <typename DeviceType, typename ScalarType, typename LayoutType, typename ParamTagType, typename AlgoTagType> |
| 125 |
/// \brief Implementation details of batched pbtrs test |
| 126 |
/// Confirm A * x = b, where |
| 127 |
/// A: [[4, 1, 0], |
| 128 |
/// [1, 4, 1], |
| 129 |
/// [0, 1, 4]] |
| 130 |
/// b: [1, 1, 1] |
| 131 |
/// x: [3/14, 1/7, 3/14] |
| 132 |
/// |
| 133 |
/// This corresponds to the following system of equations: |
| 134 |
/// 4 x0 + x1 = 1 |
| 135 |
/// x0 + 4 x1 + x2 = 1 |
| 136 |
/// x1 + 4 x2 = 1 |
| 137 |
/// \param N [in] Batch size of RHS (banded matrix can also be batched matrix) |
| 138 |
/// \param k [in] Number of superdiagonals or subdiagonals of matrix A |
| 139 |
/// \param BlkSize [in] Block size of matrix A |
| 140 |
void impl_test_batched_pbtrs_analytical(const int N) { |
| 141 |
using ats = typename Kokkos::ArithTraits<ScalarType>; |
| 142 |
using RealType = typename ats::mag_type; |
| 143 |
using View2DType = Kokkos::View<ScalarType **, LayoutType, DeviceType>; |
| 144 |
using View3DType = Kokkos::View<ScalarType ***, LayoutType, DeviceType>; |
| 145 |
|
| 146 |
constexpr int BlkSize = 3, k = 1; |
| 147 |
View3DType A("A", N, BlkSize, BlkSize), A_reconst("A_reconst", N, BlkSize, BlkSize); |
| 148 |
View3DType Ab("Ab", N, k + 1, BlkSize); // Banded matrix |
| 149 |
View2DType x0("x0", N, BlkSize), x_ref("x_ref", N, BlkSize), y0("y0", N, BlkSize); // Solutions |
| 150 |
|
| 151 |
auto h_A_reconst = Kokkos::create_mirror_view(A_reconst); |
| 152 |
auto h_x_ref = Kokkos::create_mirror_view(x_ref); |
| 153 |
|
| 154 |
for (int ib = 0; ib < N; ib++) { |
| 155 |
for (int i = 0; i < BlkSize; i++) { |
| 156 |
for (int j = 0; j < BlkSize; j++) { |
| 157 |
h_A_reconst(ib, i, j) = i == j ? 4.0 : 1.0; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
h_x_ref(ib, 0) = 3.0 / 14.0; |
| 162 |
h_x_ref(ib, 1) = 1.0 / 7.0; |
| 163 |
h_x_ref(ib, 2) = 3.0 / 14.0; |
| 164 |
} |
| 165 |
|
| 166 |
Kokkos::fence(); |
| 167 |
|
| 168 |
Kokkos::deep_copy(x0, ScalarType(1.0)); |
| 169 |
Kokkos::deep_copy(A_reconst, h_A_reconst); |
| 170 |
|
| 171 |
// Create banded triangluar matrix in normal and banded storage |
| 172 |
using ArgUplo = typename ParamTagType::uplo; |
| 173 |
create_banded_pds_matrix<View3DType, View3DType, ArgUplo>(A_reconst, A, k, false); |
| 174 |
create_banded_triangular_matrix<View3DType, View3DType, ArgUplo>(A_reconst, Ab, k, true); |
| 175 |
|
| 176 |
// Factorize with Pbtrf: A = U**H * U or A = L * L**H |
| 177 |
Functor_BatchedSerialPbtrf<DeviceType, View3DType, ParamTagType, AlgoTagType>(Ab).run(); |
| 178 |
|
| 179 |
// pbtrs (Note, Ab is a factorized matrix of A) |
| 180 |
auto info = Functor_BatchedSerialPbtrs<DeviceType, View3DType, View2DType, ParamTagType, AlgoTagType>(Ab, x0).run(); |
| 181 |
|
| 182 |
Kokkos::fence(); |
| 183 |
EXPECT_EQ(info, 0); |
| 184 |
|
| 185 |
// this eps is about 10^-14 |
| 186 |
RealType eps = 1.0e3 * ats::epsilon(); |
| 187 |
auto h_x0 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), x0); |
| 188 |
|
| 189 |
// Check x0 = x1 |
| 190 |
for (int ib = 0; ib < N; ib++) { |
| 191 |
for (int i = 0; i < BlkSize; i++) { |
| 192 |
Test::EXPECT_NEAR_KK_REL(h_x0(ib, i), h_x_ref(ib, i), eps); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
template <typename DeviceType, typename ScalarType, typename LayoutType, typename ParamTagType, typename AlgoTagType> |
| 198 |
/// \brief Implementation details of batched pbtrs test |
| 199 |
/// Confirm A * x = b, where |
| 200 |
/// |
| 201 |
/// \param N [in] Batch size of RHS (banded matrix can also be batched matrix) |
| 202 |
/// \param k [in] Number of superdiagonals or subdiagonals of matrix A |
| 203 |
/// \param BlkSize [in] Block size of matrix A |
| 204 |
void impl_test_batched_pbtrs(const int N, const int k, const int BlkSize) { |
| 205 |
using ats = typename Kokkos::ArithTraits<ScalarType>; |
| 206 |
using RealType = typename ats::mag_type; |
| 207 |
using View2DType = Kokkos::View<ScalarType **, LayoutType, DeviceType>; |
| 208 |
using View3DType = Kokkos::View<ScalarType ***, LayoutType, DeviceType>; |
| 209 |
|
| 210 |
View3DType A("A", N, BlkSize, BlkSize), A_reconst("A_reconst", N, BlkSize, BlkSize); |
| 211 |
View3DType Ab("Ab", N, k + 1, BlkSize); // Banded matrix |
| 212 |
View2DType x0("x0", N, BlkSize), x_ref("x_ref", N, BlkSize), y0("y0", N, BlkSize); // Solutions |
| 213 |
|
| 214 |
using execution_space = typename DeviceType::execution_space; |
| 215 |
Kokkos::Random_XorShift64_Pool<execution_space> rand_pool(13718); |
| 216 |
ScalarType randStart, randEnd; |
| 217 |
|
| 218 |
// Initialize A_reconst with random matrix |
| 219 |
KokkosKernels::Impl::getRandomBounds(1.0, randStart, randEnd); |
| 220 |
Kokkos::fill_random(A, rand_pool, randStart, randEnd); |
| 221 |
Kokkos::fill_random(x0, rand_pool, randStart, randEnd); |
| 222 |
Kokkos::deep_copy(x_ref, x0); |
| 223 |
|
| 224 |
// Make the matrix Positive Definite Symmetric and Diagonal dominant |
| 225 |
random_to_pds(A, A_reconst); |
| 226 |
Kokkos::deep_copy(A, ScalarType(0.0)); |
| 227 |
|
| 228 |
// Create banded triangluar matrix in normal and banded storage |
| 229 |
using ArgUplo = typename ParamTagType::uplo; |
| 230 |
create_banded_pds_matrix<View3DType, View3DType, ArgUplo>(A_reconst, A, k, false); |
| 231 |
|
| 232 |
create_banded_triangular_matrix<View3DType, View3DType, ArgUplo>(A_reconst, Ab, k, true); |
| 233 |
|
| 234 |
Kokkos::fence(); |
| 235 |
|
| 236 |
// Factorize with Pbtrf: A = U**H * U or A = L * L**H |
| 237 |
Functor_BatchedSerialPbtrf<DeviceType, View3DType, ParamTagType, AlgoTagType>(Ab).run(); |
| 238 |
|
| 239 |
// pbtrs (Note, Ab is a factorized matrix of A) |
| 240 |
auto info = Functor_BatchedSerialPbtrs<DeviceType, View3DType, View2DType, ParamTagType, AlgoTagType>(Ab, x0).run(); |
| 241 |
|
| 242 |
Kokkos::fence(); |
| 243 |
EXPECT_EQ(info, 0); |
| 244 |
|
| 245 |
// Gemv to compute A*x0, this should be identical to x_ref |
| 246 |
Functor_BatchedSerialGemv<DeviceType, ScalarType, View3DType, View2DType, View2DType>(1.0, A, x0, 0.0, y0).run(); |
| 247 |
|
| 248 |
Kokkos::fence(); |
| 249 |
|
| 250 |
// this eps is about 10^-14 |
| 251 |
RealType eps = 1.0e3 * ats::epsilon(); |
| 252 |
|
| 253 |
auto h_y0 = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), y0); |
| 254 |
auto h_x_ref = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), x_ref); |
| 255 |
|
| 256 |
// Check A * x0 = x_ref |
| 257 |
for (int ib = 0; ib < N; ib++) { |
| 258 |
for (int i = 0; i < BlkSize; i++) { |
| 259 |
Test::EXPECT_NEAR_KK_REL(h_y0(ib, i), h_x_ref(ib, i), eps); |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
} // namespace Pbtrs |
| 265 |
} // namespace Test |
| 266 |
|
| 267 |
template <typename DeviceType, typename ScalarType, typename ParamTagType, typename AlgoTagType> |
| 268 |
int test_batched_pbtrs() { |
| 269 |
#if defined(KOKKOSKERNELS_INST_LAYOUTLEFT) |
| 270 |
{ |
| 271 |
using LayoutType = Kokkos::LayoutLeft; |
| 272 |
Test::Pbtrs::impl_test_batched_pbtrs_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1); |
| 273 |
Test::Pbtrs::impl_test_batched_pbtrs_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(2); |
| 274 |
for (int i = 0; i < 10; i++) { |
| 275 |
int k = 1; |
| 276 |
Test::Pbtrs::impl_test_batched_pbtrs<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1, k, i); |
| 277 |
Test::Pbtrs::impl_test_batched_pbtrs<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(2, k, i); |
| 278 |
} |
| 279 |
} |
| 280 |
#endif |
| 281 |
#if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT) |
| 282 |
{ |
| 283 |
using LayoutType = Kokkos::LayoutRight; |
| 284 |
Test::Pbtrs::impl_test_batched_pbtrs_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1); |
| 285 |
Test::Pbtrs::impl_test_batched_pbtrs_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(2); |
| 286 |
for (int i = 0; i < 10; i++) { |
| 287 |
int k = 1; |
| 288 |
Test::Pbtrs::impl_test_batched_pbtrs<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1, k, i); |
| 289 |
Test::Pbtrs::impl_test_batched_pbtrs<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(2, k, i); |
| 290 |
} |
| 291 |
} |
| 292 |
#endif |
| 293 |
|
| 294 |
return 0; |
| 295 |
} |