Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_SerialPbtrf.hpp

raw
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_Pbtrf.hpp"
23 #include "Test_Batched_DenseUtils.hpp"
24
25 using namespace KokkosBatched;
26
27 namespace Test {
28 namespace Pbtrf {
29
30 template <typename U>
31 struct ParamTag {
32 using uplo = U;
33 };
34
35 template <typename DeviceType, typename ABViewType, typename ParamTagType, typename AlgoTagType>
36 struct Functor_BatchedSerialPbtrf {
37 using execution_space = typename DeviceType::execution_space;
38 ABViewType _ab;
39
40 KOKKOS_INLINE_FUNCTION
41 Functor_BatchedSerialPbtrf(const ABViewType &ab) : _ab(ab) {}
42
43 KOKKOS_INLINE_FUNCTION
44 void operator()(const ParamTagType &, const int k, int &info) const {
45 auto sub_ab = Kokkos::subview(_ab, k, Kokkos::ALL(), Kokkos::ALL());
46
47 info += KokkosBatched::SerialPbtrf<typename ParamTagType::uplo, AlgoTagType>::invoke(sub_ab);
48 }
49
50 inline int run() {
51 using value_type = typename ABViewType::non_const_value_type;
52 std::string name_region("KokkosBatched::Test::SerialPbtrf");
53 const std::string name_value_type = Test::value_type_name<value_type>();
54 std::string name = name_region + name_value_type;
55 int info_sum = 0;
56 Kokkos::Profiling::pushRegion(name.c_str());
57 Kokkos::RangePolicy<execution_space, ParamTagType> policy(0, _ab.extent(0));
58 Kokkos::parallel_reduce(name.c_str(), policy, *this, info_sum);
59 Kokkos::Profiling::popRegion();
60 return info_sum;
61 }
62 };
63
64 template <typename DeviceType, typename ScalarType, typename AViewType, typename BViewType, typename CViewType,
65 typename ArgTransA, typename ArgTransB>
66 struct Functor_BatchedSerialGemm {
67 using execution_space = typename DeviceType::execution_space;
68 AViewType _a;
69 BViewType _b;
70 CViewType _c;
71 ScalarType _alpha, _beta;
72
73 KOKKOS_INLINE_FUNCTION
74 Functor_BatchedSerialGemm(const ScalarType alpha, const AViewType &a, const BViewType &b, const ScalarType beta,
75 const CViewType &c)
76 : _a(a), _b(b), _c(c), _alpha(alpha), _beta(beta) {}
77
78 KOKKOS_INLINE_FUNCTION
79 void operator()(const int k) const {
80 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
81 auto bb = Kokkos::subview(_b, k, Kokkos::ALL(), Kokkos::ALL());
82 auto cc = Kokkos::subview(_c, k, Kokkos::ALL(), Kokkos::ALL());
83
84 KokkosBatched::SerialGemm<ArgTransA, ArgTransB, Algo::Gemm::Unblocked>::invoke(_alpha, aa, bb, _beta, cc);
85 }
86
87 inline void run() {
88 using value_type = typename AViewType::non_const_value_type;
89 std::string name_region("KokkosBatched::Test::SerialPbtrf");
90 const std::string name_value_type = Test::value_type_name<value_type>();
91 std::string name = name_region + name_value_type;
92 Kokkos::RangePolicy<execution_space> policy(0, _a.extent(0));
93 Kokkos::parallel_for(name.c_str(), policy, *this);
94 }
95 };
96
97 template <typename DeviceType, typename ScalarType, typename LayoutType, typename ParamTagType, typename AlgoTagType>
98 /// \brief Implementation details of batched pbtrf test
99 /// Confirm A = U**H * U or L * L**H, where
100 /// For full storage,
101 /// A: [[4, 1],
102 /// [1, 4]]
103 /// L: [[sqrt(4), 0],
104 /// [1/sqrt(4), sqrt(4 - (1/sqrt(4))**2)]
105 /// U: [[sqrt(4), 1/sqrt(4)],
106 /// [0, sqrt(4 - (1/sqrt(4))**2)]
107 ///
108 /// For lower banded storage, Ab = Lb * Lb**H
109 /// Ab: [[4, 4],
110 /// [1, 0]]
111 /// Lb: [[sqrt(4), sqrt(4 - (1/sqrt(4))**2)],
112 /// [1/sqrt(4), 0]]
113 ///
114 /// For upper banded storage, Ab = Ub**H * Ub
115 /// Ab: [[0, 1],
116 /// [4, 4]]
117 /// Ub: [[0, 1/sqrt(4)],
118 /// [sqrt(4), sqrt(4 - (1/sqrt(4))**2)]]
119 /// \param N [in] Batch size of AB
120 void impl_test_batched_pbtrf_analytical(const int N) {
121 using ats = typename Kokkos::ArithTraits<ScalarType>;
122 using RealType = typename ats::mag_type;
123 using View3DType = Kokkos::View<ScalarType ***, LayoutType, DeviceType>;
124
125 constexpr int BlkSize = 2, k = 1;
126 View3DType A("A", N, BlkSize, BlkSize), Ab("Ab", N, k + 1, BlkSize),
127 Ab_ref("Ab_ref", N, k + 1, BlkSize); // Banded matrix
128
129 auto h_A = Kokkos::create_mirror_view(A);
130 for (int ib = 0; ib < N; ib++) {
131 for (int i = 0; i < BlkSize; i++) {
132 for (int j = 0; j < BlkSize; j++) {
133 h_A(ib, i, j) = i == j ? 4.0 : 1.0;
134 }
135 }
136 }
137
138 Kokkos::deep_copy(A, h_A);
139
140 // Create banded triangluar matrix in normal and banded storage
141 using ArgUplo = typename ParamTagType::uplo;
142 create_banded_triangular_matrix<View3DType, View3DType, ArgUplo>(A, Ab, k, true);
143
144 // Make a reference using the naive Cholesky decomposition
145 // Cholesky decomposition for full storage
146 // l_kk = np.sqrt( a_kk - sum_{i=1}^{k-1}( l_ik^2 ) )
147 // l_ik = 1/l_kk * ( a_ik - sum_{j=1}^{k-1}( l_ij * l_kj ) )
148 auto h_Ab_ref = Kokkos::create_mirror_view(Ab_ref);
149 auto h_Ab = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Ab);
150 if (std::is_same_v<typename ParamTagType::uplo, KokkosBatched::Uplo::Upper>) {
151 // A = U**H * U
152 for (int ib = 0; ib < N; ib++) {
153 h_Ab_ref(ib, 1, 0) = Kokkos::sqrt(h_Ab(ib, 1, 0));
154 h_Ab_ref(ib, 0, 1) = 1.0 / h_Ab_ref(ib, 1, 0);
155 h_Ab_ref(ib, 1, 1) = Kokkos::sqrt(h_Ab(ib, 1, 1) - h_Ab_ref(ib, 0, 1) * h_Ab_ref(ib, 0, 1));
156 }
157 } else {
158 // A = L * L**H
159 for (int ib = 0; ib < N; ib++) {
160 h_Ab_ref(ib, 0, 0) = Kokkos::sqrt(h_Ab(ib, 0, 0));
161 h_Ab_ref(ib, 1, 0) = 1.0 / h_Ab_ref(ib, 0, 0);
162 h_Ab_ref(ib, 0, 1) = Kokkos::sqrt(h_Ab(ib, 0, 1) - h_Ab_ref(ib, 1, 0) * h_Ab_ref(ib, 1, 0));
163 }
164 }
165
166 // Factorize with Pbtrf: A = U**H * U or A = L * L**H
167 auto info = Functor_BatchedSerialPbtrf<DeviceType, View3DType, ParamTagType, AlgoTagType>(Ab).run();
168 Kokkos::fence();
169 EXPECT_EQ(info, 0);
170
171 // this eps is about 10^-14
172 RealType eps = 1.0e3 * ats::epsilon();
173
174 // Check if Ab == Ub or Lb
175 Kokkos::deep_copy(h_Ab, Ab);
176 for (int ib = 0; ib < N; ib++) {
177 for (int i = 0; i < k + 1; i++) {
178 for (int j = 0; j < BlkSize; j++) {
179 EXPECT_NEAR_KK(h_Ab(ib, i, j), h_Ab_ref(ib, i, j), eps);
180 }
181 }
182 }
183 }
184
185 template <typename DeviceType, typename ScalarType, typename LayoutType, typename ParamTagType, typename AlgoTagType>
186 /// \brief Implementation details of batched pbtrs test
187 ///
188 /// \param N [in] Batch size of RHS (banded matrix can also be batched matrix)
189 /// \param k [in] Number of superdiagonals or subdiagonals of matrix A
190 /// \param BlkSize [in] Block size of matrix A
191 void impl_test_batched_pbtrf(const int N, const int k, const int BlkSize) {
192 using View3DType = Kokkos::View<ScalarType ***, LayoutType, DeviceType>;
193 View3DType A("A", N, BlkSize, BlkSize), A_reconst("A_reconst", N, BlkSize, BlkSize),
194 Ab("Ab", N, k + 1, BlkSize); // Banded matrix
195
196 using execution_space = typename DeviceType::execution_space;
197 Kokkos::Random_XorShift64_Pool<execution_space> rand_pool(13718);
198 ScalarType randStart, randEnd;
199
200 // Initialize A_reconst with random matrix
201 KokkosKernels::Impl::getRandomBounds(1.0, randStart, randEnd);
202 Kokkos::fill_random(A, rand_pool, randStart, randEnd);
203
204 // Make the matrix Positive Definite Symmetric and Diagonal dominant
205 random_to_pds(A, A_reconst);
206 Kokkos::deep_copy(A, 0.0);
207
208 // Create banded triangluar matrix in normal and banded storage
209 using ArgUplo = typename ParamTagType::uplo;
210 create_banded_pds_matrix<View3DType, View3DType, ArgUplo>(A_reconst, A, k, false);
211
212 create_banded_triangular_matrix<View3DType, View3DType, ArgUplo>(A_reconst, Ab, k, true);
213
214 // Clear matrix
215 Kokkos::deep_copy(A_reconst, 0.0);
216
217 // Factorize with Pbtrf: A = U**H * U or A = L * L**H
218 auto info = Functor_BatchedSerialPbtrf<DeviceType, View3DType, ParamTagType, AlgoTagType>(Ab).run();
219
220 Kokkos::fence();
221 EXPECT_EQ(info, 0);
222
223 if (std::is_same_v<typename ParamTagType::uplo, KokkosBatched::Uplo::Upper>) {
224 // A = U**H * U
225 View3DType U("U", N, BlkSize, BlkSize), Uc("Uc", N, BlkSize, BlkSize);
226 banded_to_full<View3DType, View3DType, ArgUplo>(Ab, U, k);
227
228 // Compute the complex conjugate of U
229 // U -> conj(U)
230 auto h_U = Kokkos::create_mirror_view(U);
231 auto h_Uc = Kokkos::create_mirror_view(Uc);
232 Kokkos::deep_copy(h_U, U);
233 Kokkos::deep_copy(h_Uc, Uc);
234 for (int ib = 0; ib < N; ib++) {
235 for (int i = 0; i < BlkSize; i++) {
236 for (int j = 0; j < BlkSize; j++) {
237 h_Uc(ib, i, j) = Kokkos::ArithTraits<ScalarType>::conj(h_U(ib, i, j));
238 }
239 }
240 }
241 Kokkos::deep_copy(Uc, h_Uc);
242
243 // Create conjugate of U
244 Functor_BatchedSerialGemm<DeviceType, ScalarType, View3DType, View3DType, View3DType, Trans::Transpose,
245 Trans::NoTranspose>(1.0, Uc, U, 0.0, A_reconst)
246 .run();
247 } else {
248 // A = L * L**H
249 View3DType L("L", N, BlkSize, BlkSize), Lc("Lc", N, BlkSize, BlkSize);
250 banded_to_full<View3DType, View3DType, ArgUplo>(Ab, L, k);
251
252 // Compute the complex conjugate of L
253 // L -> conj(L)
254 auto h_L = Kokkos::create_mirror_view(L);
255 auto h_Lc = Kokkos::create_mirror_view(Lc);
256 Kokkos::deep_copy(h_L, L);
257 Kokkos::deep_copy(h_Lc, Lc);
258 for (int ib = 0; ib < N; ib++) {
259 for (int i = 0; i < BlkSize; i++) {
260 for (int j = 0; j < BlkSize; j++) {
261 h_Lc(ib, i, j) = Kokkos::ArithTraits<ScalarType>::conj(h_L(ib, i, j));
262 }
263 }
264 }
265 Kokkos::deep_copy(Lc, h_Lc);
266
267 // Create conjugate of L
268 Functor_BatchedSerialGemm<DeviceType, ScalarType, View3DType, View3DType, View3DType, Trans::NoTranspose,
269 Trans::Transpose>(1.0, L, Lc, 0.0, A_reconst)
270 .run();
271 }
272
273 // this eps is about 10^-14
274 using ats = typename Kokkos::ArithTraits<ScalarType>;
275 using RealType = typename ats::mag_type;
276 RealType eps = 1.0e3 * ats::epsilon();
277
278 auto h_A = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), A);
279 auto h_A_reconst = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), A_reconst);
280
281 // Check if A = U**H * U or A = L * L**H
282 for (int ib = 0; ib < N; ib++) {
283 for (int i = 0; i < BlkSize; i++) {
284 for (int j = 0; j < BlkSize; j++) {
285 EXPECT_NEAR_KK(h_A_reconst(ib, i, j), h_A(ib, i, j), eps);
286 }
287 }
288 }
289 }
290
291 } // namespace Pbtrf
292 } // namespace Test
293
294 template <typename DeviceType, typename ScalarType, typename ParamTagType, typename AlgoTagType>
295 int test_batched_pbtrf() {
296 #if defined(KOKKOSKERNELS_INST_LAYOUTLEFT)
297 {
298 using LayoutType = Kokkos::LayoutLeft;
299 Test::Pbtrf::impl_test_batched_pbtrf_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1);
300 Test::Pbtrf::impl_test_batched_pbtrf_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(2);
301 for (int i = 0; i < 10; i++) {
302 int k = 1;
303 Test::Pbtrf::impl_test_batched_pbtrf<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1, k, i);
304 Test::Pbtrf::impl_test_batched_pbtrf<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(2, k, i);
305 }
306 }
307 #endif
308 #if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT)
309 {
310 using LayoutType = Kokkos::LayoutRight;
311 Test::Pbtrf::impl_test_batched_pbtrf_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1);
312 Test::Pbtrf::impl_test_batched_pbtrf_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(2);
313 for (int i = 0; i < 10; i++) {
314 int k = 1;
315 Test::Pbtrf::impl_test_batched_pbtrf<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1, k, i);
316 Test::Pbtrf::impl_test_batched_pbtrf<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(2, k, i);
317 }
318 }
319 #endif
320
321 return 0;
322 }