Debian Codegraph @

packages/kokkos-kernels/batched/dense/impl/KokkosBatched_RightEigenvectorFromSchur_Serial_Internal.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 #ifndef KOKKOSBATCHED_RIGHT_EIGENVECTOR_FROM_SCHUR_SERIAL_INTERNAL_HPP
17 #define KOKKOSBATCHED_RIGHT_EIGENVECTOR_FROM_SCHUR_SERIAL_INTERNAL_HPP
18
19 /// \author Kyungjoo Kim ([email protected])
20
21 #include "KokkosBatched_Util.hpp"
22 #include "KokkosBatched_ShiftedTrsv_Serial_Internal.hpp"
23
24 namespace KokkosBatched {
25
26 ///
27 /// Serial Internal Impl
28 /// ====================
29 ///
30 /// this impl follows the flame interface of householder transformation
31 ///
32 struct SerialRightEigenvectorFromSchurInternal {
33 /// Given a quasi upper triangular matrix S (m x m), this computes all right
34 /// eigenvectors.
35 ///
36 /// Parameters:
37 /// [in]m
38 /// A dimension of the square matrix S.
39 /// [in]S, [in]ss0, [in]ss1
40 /// A quasi upper triangular part of Schur decomposition which is computed
41 /// A = U^H S U
42 /// [out]V, [in]vs0, [out]vs1
43 /// A set of right eigen vectors.
44 /// [in]w
45 /// contiguous workspace that can hold complex array (m)
46 template <typename ValueType>
47 KOKKOS_INLINE_FUNCTION static int invoke(const int m,
48 /* */ ValueType *S, const int ss0, const int ss1,
49 /* */ ValueType *V, const int vs0, const int vs1,
50 /* */ ValueType *w, const int *blks) {
51 typedef ValueType value_type;
52 typedef Kokkos::ArithTraits<value_type> ats;
53 // typedef typename ats::mag_type mag_type;
54 typedef Kokkos::complex<value_type> complex_type;
55
56 const value_type zero(0), one(1);
57 // const int ss(ss0+ss1);
58 /// SerialSetInternal::invoke(m, m, zero, V, vs0, vs1);
59
60 value_type *b = w; // consider complex case
61
62 /// partitions used for loop iteration
63 Partition2x2<value_type> S_part2x2(ss0, ss1);
64 Partition3x3<value_type> S_part3x3(ss0, ss1);
65
66 Partition1x2<value_type> V_part1x2(vs1);
67 Partition1x3<value_type> V_part1x3(vs1);
68
69 /// initial partition of S where ABR has a zero dimension
70 S_part2x2.partWithABR(S, m, m, 0, 0);
71 V_part1x2.partWithAR(V, m, 0);
72
73 // const mag_type tol = ats::epsilon();
74 int m_stl = m;
75 for (; m_stl > 0;) {
76 /// part 2x2 into 3x3
77 const int mA11 = blks[m_stl - 1];
78 assert(((mA11 == 1) || (mA11 == 2)) && "RightEigenvectorFromSchur: blk is not 1x1 nor 2x2");
79
80 S_part3x3.partWithATL(S_part2x2, mA11, mA11);
81 V_part1x3.partWithAL(V_part1x2, mA11);
82
83 const int m_stl_minus_mA11 = m_stl - mA11;
84 if (mA11 == 1) {
85 /// real eigenvalue
86 const value_type lambda = *S_part3x3.A11;
87
88 /// initialize a right eigen vector
89 for (int i = 0; i < m_stl_minus_mA11; ++i) b[i] = -S_part3x3.A01[i * ss0];
90 b[m_stl - 1] = one;
91
92 /// perform shifted trsv
93 SerialShiftedTrsvInternalUpper::invoke(m_stl_minus_mA11, lambda, S_part3x3.A00, ss0, ss1, w, 1, blks);
94
95 /// copy back to V
96 for (int i = 0; i < m_stl; ++i) V_part1x3.A1[i * vs0] = w[i];
97 for (int i = m_stl; i < m; ++i) V_part1x3.A1[i * vs0] = zero;
98 } else {
99 /// complex eigen pair
100 const value_type alpha11 = S_part3x3.A11[0], alpha12 = S_part3x3.A11[ss1], alpha21 = S_part3x3.A11[ss0],
101 beta = ats::sqrt(-alpha12 * alpha21);
102
103 const complex_type lambda(alpha11, beta);
104 complex_type *bc = (complex_type *)(b);
105
106 /// initialize a right eigen vector
107 const value_type *S_A01_a = S_part3x3.A01;
108 const value_type *S_A01_b = S_part3x3.A01 + ss1;
109 for (int i = 0; i < m_stl_minus_mA11; ++i)
110 bc[i] = complex_type(-S_A01_a[i * ss0] * beta, S_A01_b[i * ss0] * alpha21);
111 bc[m_stl - 2] = complex_type(beta, zero);
112 bc[m_stl - 1] = complex_type(zero, -alpha21);
113
114 /// perform shifted trsv
115 SerialShiftedTrsvInternalUpper::invoke(m_stl_minus_mA11, lambda, S_part3x3.A00, ss0, ss1, bc, 1, blks);
116
117 /// copy back to V
118 value_type *V_A1_r = V_part1x3.A1;
119 value_type *V_A1_i = V_part1x3.A1 + vs1;
120 for (int i = 0; i < m_stl; ++i) {
121 V_A1_r[i * vs0] = bc[i].real();
122 V_A1_i[i * vs0] = bc[i].imag();
123 }
124 for (int i = m_stl; i < m; ++i) {
125 V_A1_r[i * vs0] = zero;
126 V_A1_i[i * vs0] = zero;
127 }
128 /// ---------------------------------------------------
129 }
130 S_part2x2.mergeToABR(S_part3x3);
131 V_part1x2.mergeToAR(V_part1x3);
132 m_stl -= mA11;
133 }
134 return 0;
135 }
136 };
137
138 } // namespace KokkosBatched
139
140 #endif