Debian Codegraph @

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