Debian Codegraph @

packages/kokkos-kernels/batched/dense/impl/KokkosBatched_LeftEigenvectorFromSchur_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_LEFT_EIGENVECTOR_FROM_SCHUR_SERIAL_INTERNAL_HPP
17 #define KOKKOSBATCHED_LEFT_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 SerialLeftEigenvectorFromSchurInternal {
33 /// Given a quasi upper triangular matrix S (m x m), this computes all left
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 left 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 /// SerialSetInternal::invoke(m, m, zero, V, vs0, vs1);
58
59 value_type *b = w; // consider complex case
60
61 /// partitions used for loop iteration
62 Partition2x2<value_type> S_part2x2(ss0, ss1);
63 Partition3x3<value_type> S_part3x3(ss0, ss1);
64
65 Partition2x1<value_type> V_part2x1(vs0);
66 Partition3x1<value_type> V_part3x1(vs0);
67
68 /// initial partition of S where ATL has a zero dimension
69 S_part2x2.partWithATL(S, m, m, 0, 0);
70 V_part2x1.partWithAT(V, m, 0);
71
72 // const mag_type tol = ats::epsilon();
73 int m_stl = 0;
74 for (; m_stl < (m - 1);) {
75 /// part 2x2 into 3x3
76 const int mA11 = blks[m_stl];
77 assert(((mA11 == 1) || (mA11 == 2)) && "LeftEigenvectorFromSchur: blk is not 1x1 nor 2x2");
78
79 S_part3x3.partWithABR(S_part2x2, mA11, mA11);
80 V_part3x1.partWithAB(V_part2x1, mA11);
81
82 const int m_stl_plus_mA11 = m_stl + mA11;
83 if (mA11 == 1) {
84 /// real eigenvalue
85 const value_type lambda = *S_part3x3.A11;
86
87 /// initialize a left hand side
88 b[m_stl] = one;
89 for (int j = 0; j < (m - m_stl_plus_mA11); ++j) b[j + m_stl_plus_mA11] = -S_part3x3.A12[j * ss1];
90
91 /// perform shifted trsv (transposed)
92 SerialShiftedTrsvInternalLower::invoke(m - m_stl_plus_mA11, lambda, S_part3x3.A22, ss1, ss0,
93 b + m_stl_plus_mA11, 1, blks + m_stl_plus_mA11);
94
95 /// copy back to V (row wise copy)
96 for (int j = 0; j < m_stl; ++j) V_part3x1.A1[j * vs1] = zero;
97 for (int j = m_stl; j < m; ++j) V_part3x1.A1[j * vs1] = b[j];
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 left hand side
107 bc[m_stl] = complex_type(beta, zero);
108 bc[m_stl + 1] = complex_type(zero, -alpha12);
109
110 const value_type *S_A12_a = S_part3x3.A12;
111 const value_type *S_A12_b = S_part3x3.A12 + ss0;
112 for (int j = 0; j < (m - m_stl_plus_mA11); ++j)
113 bc[j + m_stl_plus_mA11] = complex_type(-S_A12_a[j * ss1] * beta, S_A12_b[j * ss1] * alpha12);
114
115 /// perform shifted trsv
116 SerialShiftedTrsvInternalLower::invoke(m - m_stl_plus_mA11, lambda, S_part3x3.A22, ss1, ss0,
117 bc + m_stl_plus_mA11, 1, blks + m_stl_plus_mA11);
118
119 /// copy back to V
120 value_type *V_A1_r = V_part3x1.A1;
121 value_type *V_A1_i = V_part3x1.A1 + vs0;
122 for (int j = 0; j < m_stl; ++j) {
123 V_A1_r[j * vs1] = zero;
124 V_A1_i[j * vs1] = zero;
125 }
126 for (int j = m_stl; j < m; ++j) {
127 V_A1_r[j * vs1] = bc[j].real();
128 V_A1_i[j * vs1] = bc[j].imag();
129 }
130 /// ---------------------------------------------------
131 }
132 S_part2x2.mergeToATL(S_part3x3);
133 V_part2x1.mergeToAT(V_part3x1);
134 m_stl += mA11;
135 }
136
137 /// case: m_stl = m-1
138 if (m_stl < m) {
139 value_type *VV = V + m_stl * vs0;
140 for (int j = 0; j < m_stl; ++j) VV[j * vs1] = zero;
141 VV[m_stl * vs1] = one;
142 }
143
144 return 0;
145 }
146 };
147
148 } // namespace KokkosBatched
149
150 #endif