Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_SerialPttrs.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_Pttrs.hpp"
23 #include "Test_Batched_DenseUtils.hpp"
24
25 using namespace KokkosBatched;
26
27 namespace Test {
28 namespace Pttrs {
29
30 template <typename U>
31 struct ParamTag {
32 using uplo = U;
33 };
34
35 template <typename DeviceType, typename DViewType, typename EViewType, typename AlgoTagType>
36 struct Functor_BatchedSerialPttrf {
37 using execution_space = typename DeviceType::execution_space;
38 DViewType _d;
39 EViewType _e;
40
41 KOKKOS_INLINE_FUNCTION
42 Functor_BatchedSerialPttrf(const DViewType &d, const EViewType &e) : _d(d), _e(e) {}
43
44 KOKKOS_INLINE_FUNCTION
45 void operator()(const int k) const {
46 auto dd = Kokkos::subview(_d, k, Kokkos::ALL());
47 auto ee = Kokkos::subview(_e, k, Kokkos::ALL());
48
49 KokkosBatched::SerialPttrf<AlgoTagType>::invoke(dd, ee);
50 }
51
52 inline void run() {
53 using value_type = typename EViewType::non_const_value_type;
54 std::string name_region("KokkosBatched::Test::SerialPttrs");
55 const std::string name_value_type = Test::value_type_name<value_type>();
56 std::string name = name_region + name_value_type;
57 Kokkos::RangePolicy<execution_space> policy(0, _d.extent(0));
58 Kokkos::parallel_for(name.c_str(), policy, *this);
59 }
60 };
61
62 template <typename DeviceType, typename DViewType, typename EViewType, typename BViewType, typename ParamTagType,
63 typename AlgoTagType>
64 struct Functor_BatchedSerialPttrs {
65 using execution_space = typename DeviceType::execution_space;
66 DViewType _d;
67 EViewType _e;
68 BViewType _b;
69
70 KOKKOS_INLINE_FUNCTION
71 Functor_BatchedSerialPttrs(const DViewType &d, const EViewType &e, const BViewType &b) : _d(d), _e(e), _b(b) {}
72
73 KOKKOS_INLINE_FUNCTION
74 void operator()(const ParamTagType &, const int k, int &info) const {
75 auto dd = Kokkos::subview(_d, k, Kokkos::ALL());
76 auto ee = Kokkos::subview(_e, k, Kokkos::ALL());
77 auto bb = Kokkos::subview(_b, k, Kokkos::ALL());
78
79 info += KokkosBatched::SerialPttrs<typename ParamTagType::uplo, AlgoTagType>::invoke(dd, ee, bb);
80 }
81
82 inline int run() {
83 using value_type = typename BViewType::non_const_value_type;
84 std::string name_region("KokkosBatched::Test::SerialPttrs");
85 const std::string name_value_type = Test::value_type_name<value_type>();
86 std::string name = name_region + name_value_type;
87 int info_sum = 0;
88 Kokkos::Profiling::pushRegion(name.c_str());
89 Kokkos::RangePolicy<execution_space, ParamTagType> policy(0, _d.extent(0));
90 Kokkos::parallel_reduce(name.c_str(), policy, *this, info_sum);
91 Kokkos::Profiling::popRegion();
92 return info_sum;
93 }
94 };
95
96 template <typename DeviceType, typename ScalarType, typename AViewType, typename xViewType, typename yViewType>
97 struct Functor_BatchedSerialGemv {
98 using execution_space = typename DeviceType::execution_space;
99 AViewType _a;
100 xViewType _x;
101 yViewType _y;
102 ScalarType _alpha, _beta;
103
104 KOKKOS_INLINE_FUNCTION
105 Functor_BatchedSerialGemv(const ScalarType alpha, const AViewType &a, const xViewType &x, const ScalarType beta,
106 const yViewType &y)
107 : _a(a), _x(x), _y(y), _alpha(alpha), _beta(beta) {}
108
109 KOKKOS_INLINE_FUNCTION
110 void operator()(const int k) const {
111 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
112 auto xx = Kokkos::subview(_x, k, Kokkos::ALL());
113 auto yy = Kokkos::subview(_y, k, Kokkos::ALL());
114
115 KokkosBlas::SerialGemv<Trans::NoTranspose, Algo::Gemv::Unblocked>::invoke(_alpha, aa, xx, _beta, yy);
116 }
117
118 inline void run() {
119 using value_type = typename AViewType::non_const_value_type;
120 std::string name_region("KokkosBatched::Test::SerialPttrs");
121 const std::string name_value_type = Test::value_type_name<value_type>();
122 std::string name = name_region + name_value_type;
123 Kokkos::RangePolicy<execution_space> policy(0, _a.extent(0));
124 Kokkos::parallel_for(name.c_str(), policy, *this);
125 }
126 };
127
128 template <typename DeviceType, typename ScalarType, typename AViewType, typename BViewType, typename CViewType,
129 typename ArgTransB>
130 struct Functor_BatchedSerialGemm {
131 using execution_space = typename DeviceType::execution_space;
132 AViewType _a;
133 BViewType _b;
134 CViewType _c;
135 ScalarType _alpha, _beta;
136
137 KOKKOS_INLINE_FUNCTION
138 Functor_BatchedSerialGemm(const ScalarType alpha, const AViewType &a, const BViewType &b, const ScalarType beta,
139 const CViewType &c)
140 : _a(a), _b(b), _c(c), _alpha(alpha), _beta(beta) {}
141
142 KOKKOS_INLINE_FUNCTION
143 void operator()(const int k) const {
144 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
145 auto bb = Kokkos::subview(_b, k, Kokkos::ALL(), Kokkos::ALL());
146 auto cc = Kokkos::subview(_c, k, Kokkos::ALL(), Kokkos::ALL());
147
148 KokkosBatched::SerialGemm<Trans::NoTranspose, ArgTransB, Algo::Gemm::Unblocked>::invoke(_alpha, aa, bb, _beta, cc);
149 }
150
151 inline void run() {
152 using value_type = typename AViewType::non_const_value_type;
153 std::string name_region("KokkosBatched::Test::SerialPttrs");
154 const std::string name_value_type = Test::value_type_name<value_type>();
155 std::string name = name_region + name_value_type;
156 Kokkos::RangePolicy<execution_space> policy(0, _a.extent(0));
157 Kokkos::parallel_for(name.c_str(), policy, *this);
158 }
159 };
160
161 template <typename DeviceType, typename ScalarType, typename LayoutType, typename ParamTagType, typename AlgoTagType>
162 /// \brief Implementation details of batched pttrs test
163 /// Confirm A * x = b, where
164 /// A: [[4, 1],
165 /// [1, 4]]
166 /// b: [1, 1]
167 /// x: [1/5, 1/5]
168 ///
169 /// This corresponds to the following system of equations:
170 /// 4 x0 + x1 = 1
171 /// x0 + 4 x1 = 1
172 ///
173 /// \param N [in] Batch size of RHS (banded matrix can also be batched matrix)
174 /// \param k [in] Number of superdiagonals or subdiagonals of matrix A
175 /// \param BlkSize [in] Block size of matrix A
176 void impl_test_batched_pttrs_analytical(const int N) {
177 using ats = typename Kokkos::ArithTraits<ScalarType>;
178 using RealType = typename ats::mag_type;
179 using RealView2DType = Kokkos::View<RealType **, LayoutType, DeviceType>;
180 using View2DType = Kokkos::View<ScalarType **, LayoutType, DeviceType>;
181
182 constexpr int BlkSize = 2;
183 RealView2DType d(Kokkos::view_alloc("d", Kokkos::WithoutInitializing), N,
184 BlkSize); // Diagonal components
185 View2DType e(Kokkos::view_alloc("e", Kokkos::WithoutInitializing), N,
186 BlkSize - 1); // Upper and lower diagonal components (identical)
187 View2DType x(Kokkos::view_alloc("x", Kokkos::WithoutInitializing), N,
188 BlkSize); // Solutions
189
190 Kokkos::deep_copy(d, RealType(4.0));
191 Kokkos::deep_copy(e, ScalarType(1.0));
192 Kokkos::deep_copy(x, ScalarType(1.0)); // This initialy stores b
193
194 // Factorize matrix A -> L * D * L**H
195 // d and e are updated by pttrf
196 Functor_BatchedSerialPttrf<DeviceType, RealView2DType, View2DType, AlgoTagType>(d, e).run();
197
198 // pttrs (Note, d and e must be factorized by pttrf)
199 auto info =
200 Functor_BatchedSerialPttrs<DeviceType, RealView2DType, View2DType, View2DType, ParamTagType, AlgoTagType>(d, e, x)
201 .run();
202
203 Kokkos::fence();
204
205 EXPECT_EQ(info, 0);
206
207 // this eps is about 10^-14
208 RealType eps = 1.0e3 * ats::epsilon();
209
210 auto h_x = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), x);
211
212 // Check x = [1/5, 1/5]
213 for (int ib = 0; ib < N; ib++) {
214 for (int i = 0; i < BlkSize; i++) {
215 EXPECT_NEAR_KK(h_x(ib, i), ScalarType(1.0 / 5.0), eps);
216 }
217 }
218 }
219
220 template <typename DeviceType, typename ScalarType, typename LayoutType, typename ParamTagType, typename AlgoTagType>
221 /// \brief Implementation details of batched pttrs test
222 ///
223 /// \param N [in] Batch size of RHS (banded matrix can also be batched matrix)
224 /// \param k [in] Number of superdiagonals or subdiagonals of matrix A
225 /// \param BlkSize [in] Block size of matrix A
226 void impl_test_batched_pttrs(const int N, const int BlkSize) {
227 using ats = typename Kokkos::ArithTraits<ScalarType>;
228 using RealType = typename ats::mag_type;
229 using RealView2DType = Kokkos::View<RealType **, LayoutType, DeviceType>;
230 using View2DType = Kokkos::View<ScalarType **, LayoutType, DeviceType>;
231 using View3DType = Kokkos::View<ScalarType ***, LayoutType, DeviceType>;
232
233 View3DType A("A", N, BlkSize, BlkSize), EL("EL", N, BlkSize, BlkSize), EU("EU", N, BlkSize, BlkSize),
234 D("D", N, BlkSize, BlkSize), I("I", N, BlkSize, BlkSize);
235 RealView2DType d(Kokkos::view_alloc("d", Kokkos::WithoutInitializing), N,
236 BlkSize), // Diagonal components
237 ones(Kokkos::view_alloc("ones", Kokkos::WithoutInitializing), N, BlkSize);
238 View2DType e_upper("e_upper", N, BlkSize - 1), e_lower("e_lower", N,
239 BlkSize - 1); // upper and lower diagonal components
240 View2DType x("x", N, BlkSize), b_ref("x_ref", N, BlkSize), b("b", N, BlkSize); // Solutions
241
242 using execution_space = typename DeviceType::execution_space;
243 Kokkos::Random_XorShift64_Pool<execution_space> rand_pool(13718);
244 RealType realRandStart, realRandEnd;
245 ScalarType randStart, randEnd;
246
247 KokkosKernels::Impl::getRandomBounds(1.0, realRandStart, realRandEnd);
248 KokkosKernels::Impl::getRandomBounds(1.0, randStart, randEnd);
249
250 // Add BlkSize to ensure positive definiteness
251 Kokkos::fill_random(d, rand_pool, realRandStart + BlkSize, realRandEnd + BlkSize);
252 Kokkos::fill_random(e_upper, rand_pool, randStart, randEnd);
253 Kokkos::fill_random(x, rand_pool, randStart, randEnd);
254
255 auto h_e_upper = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), e_upper);
256 auto h_e_lower = Kokkos::create_mirror_view(e_lower);
257
258 for (int ib = 0; ib < N; ib++) {
259 for (int i = 0; i < BlkSize - 1; i++) {
260 // Fill the lower diagonal with conjugate of the upper diagonal
261 h_e_lower(ib, i) = Kokkos::ArithTraits<ScalarType>::conj(h_e_upper(ib, i));
262 }
263 }
264
265 Kokkos::deep_copy(e_lower, h_e_lower);
266 Kokkos::deep_copy(b_ref, x);
267 Kokkos::deep_copy(ones, RealType(1.0));
268
269 // Reconstruct Tridiagonal matrix A
270 // A = D + EL + EU
271 create_diagonal_matrix(e_lower, EL, -1);
272 create_diagonal_matrix(e_upper, EU, 1);
273 create_diagonal_matrix(d, D);
274 create_diagonal_matrix(ones, I);
275
276 // Matrix matrix addition by Gemm
277 // D + EU by D * I + EU (result stored in EU)
278 Functor_BatchedSerialGemm<DeviceType, ScalarType, View3DType, View3DType, View3DType, Trans::NoTranspose>(1.0, D, I,
279 1.0, EU)
280 .run();
281
282 // Copy EL to A
283 Kokkos::deep_copy(A, EL);
284
285 // EU + EL by EU * I + A (result stored in A)
286 Functor_BatchedSerialGemm<DeviceType, ScalarType, View3DType, View3DType, View3DType, Trans::NoTranspose>(1.0, EU, I,
287 1.0, A)
288 .run();
289
290 Kokkos::fence();
291
292 int info = 0;
293 if (std::is_same_v<typename ParamTagType::uplo, KokkosBatched::Uplo::Upper>) {
294 // Factorize matrix A -> U**H * D * U
295 // d and e are updated by pttrf
296 Functor_BatchedSerialPttrf<DeviceType, RealView2DType, View2DType, AlgoTagType>(d, e_upper).run();
297
298 // pttrs (Note, d and e must be factorized by pttrf)
299 info = Functor_BatchedSerialPttrs<DeviceType, RealView2DType, View2DType, View2DType, ParamTagType, AlgoTagType>(
300 d, e_upper, x)
301 .run();
302 } else {
303 // Factorize matrix A -> L * D * L**H
304 // d and e are updated by pttrf
305 Functor_BatchedSerialPttrf<DeviceType, RealView2DType, View2DType, AlgoTagType>(d, e_lower).run();
306
307 // pttrs (Note, d and e must be factorized by pttrf)
308 info = Functor_BatchedSerialPttrs<DeviceType, RealView2DType, View2DType, View2DType, ParamTagType, AlgoTagType>(
309 d, e_lower, x)
310 .run();
311 }
312
313 Kokkos::fence();
314
315 EXPECT_EQ(info, 0);
316
317 // Gemv to compute b = A * x, this should be identical to b_ref
318 Functor_BatchedSerialGemv<DeviceType, ScalarType, View3DType, View2DType, View2DType>(1.0, A, x, 0.0, b).run();
319
320 // this eps is about 10^-14
321 RealType eps = 1.0e3 * ats::epsilon();
322
323 auto h_b = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), b);
324 auto h_b_ref = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), b_ref);
325
326 // Check A * x = b_ref
327 for (int ib = 0; ib < N; ib++) {
328 for (int i = 0; i < BlkSize; i++) {
329 EXPECT_NEAR_KK(h_b(ib, i), h_b_ref(ib, i), eps);
330 }
331 }
332 }
333
334 template <typename DeviceType, typename ScalarType, typename LayoutType, typename ParamTagType, typename AlgoTagType>
335 /// \brief Implementation details of batched pttrs test for early return
336 /// BlkSize must be 0 or 1
337 ///
338 /// \param N [in] Batch size of RHS (banded matrix can also be batched matrix)
339 /// \param k [in] Number of superdiagonals or subdiagonals of matrix A
340 /// \param BlkSize [in] Block size of matrix A
341 void impl_test_batched_pttrs_quick_return(const int N, const int BlkSize) {
342 using ats = typename Kokkos::ArithTraits<ScalarType>;
343 using RealType = typename ats::mag_type;
344 using RealView2DType = Kokkos::View<RealType **, LayoutType, DeviceType>;
345 using View2DType = Kokkos::View<ScalarType **, LayoutType, DeviceType>;
346
347 if (BlkSize > 1) return;
348
349 const int BlkSize_minus_1 = BlkSize > 0 ? BlkSize - 1 : 0;
350
351 RealView2DType d(Kokkos::view_alloc("d", Kokkos::WithoutInitializing), N,
352 BlkSize); // Diagonal components
353 View2DType e(Kokkos::view_alloc("e", Kokkos::WithoutInitializing), N,
354 BlkSize_minus_1); // lower diagonal components
355 View2DType x("x", N, BlkSize); // Solutions
356
357 const RealType reference_value = 4.0;
358
359 Kokkos::deep_copy(d, reference_value);
360 Kokkos::deep_copy(e, ScalarType(1.0));
361 Kokkos::deep_copy(x, ScalarType(1.0));
362
363 // Factorize matrix A -> U**H * D * U or L * D * L**H
364 // d and e are updated by pttrf
365 Functor_BatchedSerialPttrf<DeviceType, RealView2DType, View2DType, AlgoTagType>(d, e).run();
366
367 // pttrs (Note, d and e must be factorized by pttrf)
368 auto info =
369 Functor_BatchedSerialPttrs<DeviceType, RealView2DType, View2DType, View2DType, ParamTagType, AlgoTagType>(d, e, x)
370 .run();
371
372 Kokkos::fence();
373
374 EXPECT_EQ(info, 0);
375
376 // this eps is about 10^-14
377 RealType eps = 1.0e3 * ats::epsilon();
378
379 auto h_x = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), x);
380
381 // Check x = x_ref
382 for (int ib = 0; ib < N; ib++) {
383 for (int i = 0; i < BlkSize; i++) {
384 EXPECT_NEAR_KK(h_x(ib, i), ScalarType(1.0 / reference_value), eps);
385 }
386 }
387 }
388
389 } // namespace Pttrs
390 } // namespace Test
391
392 template <typename DeviceType, typename ScalarType, typename ParamTagType, typename AlgoTagType>
393 int test_batched_pttrs() {
394 #if defined(KOKKOSKERNELS_INST_LAYOUTLEFT)
395 {
396 using LayoutType = Kokkos::LayoutLeft;
397 Test::Pttrs::impl_test_batched_pttrs_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1);
398 Test::Pttrs::impl_test_batched_pttrs_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(2);
399 for (int i = 0; i < 2; i++) {
400 Test::Pttrs::impl_test_batched_pttrs_quick_return<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(
401 1, i);
402 Test::Pttrs::impl_test_batched_pttrs_quick_return<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(
403 2, i);
404 }
405 for (int i = 2; i < 10; i++) {
406 Test::Pttrs::impl_test_batched_pttrs<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1, i);
407 Test::Pttrs::impl_test_batched_pttrs<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(2, i);
408 }
409 }
410 #endif
411 #if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT)
412 {
413 using LayoutType = Kokkos::LayoutRight;
414 Test::Pttrs::impl_test_batched_pttrs_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1);
415 Test::Pttrs::impl_test_batched_pttrs_analytical<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(2);
416 for (int i = 0; i < 2; i++) {
417 Test::Pttrs::impl_test_batched_pttrs_quick_return<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(
418 1, i);
419 Test::Pttrs::impl_test_batched_pttrs_quick_return<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(
420 2, i);
421 }
422 for (int i = 2; i < 10; i++) {
423 Test::Pttrs::impl_test_batched_pttrs<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(1, i);
424 Test::Pttrs::impl_test_batched_pttrs<DeviceType, ScalarType, LayoutType, ParamTagType, AlgoTagType>(2, i);
425 }
426 }
427 #endif
428
429 return 0;
430 }