Debian Codegraph @

packages/kokkos-kernels/batched/dense/impl/KokkosBatched_Schur2x2_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_SCHUR2X2_SERIAL_INTERNAL_HPP
17 #define KOKKOSBATCHED_SCHUR2X2_SERIAL_INTERNAL_HPP
18
19 /// \author Kyungjoo Kim ([email protected])
20
21 #include "KokkosBatched_Util.hpp"
22
23 namespace KokkosBatched {
24
25 ///
26 /// Serial Internal Impl
27 /// ====================
28 ///
29 /// this impl follows the flame interface of householder transformation
30 ///
31 struct SerialSchur2x2Internal {
32 template <typename RealType>
33 KOKKOS_INLINE_FUNCTION static int invoke(RealType* alpha00, RealType* alpha01, RealType* alpha10, RealType* alpha11,
34 Kokkos::pair<RealType, RealType>* G, Kokkos::complex<RealType>* lambda1,
35 Kokkos::complex<RealType>* lambda2, bool* is_complex) {
36 typedef RealType real_type;
37 typedef Kokkos::ArithTraits<real_type> ats;
38 const real_type zero(0), one(1), half(0.5), minus_one(-1);
39 /// compute G = [ gamma -sigma;
40 /// sigma gamma ];
41 /// G.first = gamma and G.second = sigma
42 /// this rotation satisfy the following
43 /// G' [alpha00 alpha01 G = [ beta00 beta01;
44 /// alpha10 alpha11 ] beta10 beta11 ];
45 /// where either
46 /// 1) beta00 = beta11 and beta01*beta10 < 0
47 /// 2) beta10 = 0
48 const real_type tol = ats::epsilon() * real_type(100);
49 if (ats::abs(*alpha10) < tol) {
50 /// no rotation
51 *G = Kokkos::pair<real_type, real_type>(one, zero);
52 /// two real eigen values
53 *lambda1 = Kokkos::complex<real_type>(*alpha00, zero);
54 *lambda2 = Kokkos::complex<real_type>(*alpha11, zero);
55 *is_complex = false;
56 } else if (ats::abs(*alpha01) < tol) {
57 /// 90 degree rotation (permutation)
58 *G = Kokkos::pair<real_type, real_type>(zero, one);
59 /// [ 0 1 ][alpha00 0 [ 0 -1 --> [ alpha11 -alpha10
60 /// -1 0 ] alpha10 alpha11] 1 0] 0 alpha00]
61 const real_type tmp = *alpha00;
62 *alpha00 = *alpha11;
63 *alpha11 = tmp;
64 *alpha01 = -(*alpha10);
65 *alpha10 = zero;
66 /// two real eigen values
67 *lambda1 = Kokkos::complex<real_type>(*alpha00, zero);
68 *lambda2 = Kokkos::complex<real_type>(*alpha11, zero);
69 *is_complex = false;
70 } else if (ats::abs(*alpha00 - *alpha11) < tol && (*alpha01) * (*alpha10) > zero) {
71 // no rotation (already the standard schur form)
72 *G = Kokkos::pair<real_type, real_type>(one, zero);
73 /// two real eigen values
74 *lambda1 = Kokkos::complex<real_type>(*alpha00, zero);
75 *lambda2 = Kokkos::complex<real_type>(*alpha11, zero);
76 *is_complex = false;
77 } else {
78 /// rotation to equalize diagonals
79 const real_type a = (*alpha00) - (*alpha11);
80 const real_type b = (*alpha01) + (*alpha10);
81 const real_type l = ats::sqrt(a * a + b * b);
82 const real_type c = ats::sqrt(half * (one + ats::abs(b) / l));
83 const real_type s = -((half * a) / (l * c)) * (b > zero ? one : minus_one);
84 *G = Kokkos::pair<real_type, real_type>(c, s);
85 /// [ gamma sigma ][ alpha00 alpha01 [ gamma -sigma --> [ alpha11
86 /// -alpha10
87 /// -sigma gamma ] alpha10 alpha11 ] sigma gamma ] 0 alpha00]
88 const real_type a00 = *alpha00, a01 = *alpha01;
89 const real_type a10 = *alpha10, a11 = *alpha11;
90 const real_type cc = c * c, cs = c * s, ss = s * s;
91 *alpha00 = cc * a00 + cs * a01 + cs * a10 + ss * a11;
92 *alpha01 = -cs * a00 + cc * a01 - ss * a10 + cs * a11;
93 *alpha10 = -cs * a00 - ss * a01 + cc * a10 + cs * a11;
94 *alpha11 = ss * a00 - cs * a01 - cs * a10 + cc * a11;
95
96 const real_type tmp = (*alpha00 + *alpha11) * half;
97 *alpha00 = tmp;
98 *alpha11 = tmp;
99
100 const real_type mult_alpha_offdiags = (*alpha10) * (*alpha01);
101 if (mult_alpha_offdiags > zero) {
102 /// transforms the matrix into a upper triangular
103 const real_type sqrt_mult_alpha_offdiags = ats::sqrt(mult_alpha_offdiags);
104
105 /// redefine the rotation matrix
106 // const real_type sqrt_abs_alpha01 = ats::sqrt(ats::abs(*alpha01));
107 // const real_type sqrt_abs_alpha10 = ats::sqrt(ats::abs(*alpha10));
108 const real_type abs_sum_offidags = ats::abs((*alpha01) + (*alpha10));
109 const real_type c1 = ats::sqrt(ats::abs(*alpha01) / abs_sum_offidags);
110 const real_type s1 = ats::sqrt(ats::abs(*alpha10) / abs_sum_offidags);
111 const real_type sign_alpha10 = *alpha10 > zero ? one : minus_one;
112
113 *G = Kokkos::pair<real_type, real_type>(c * c1 - s * s1, c * s1 + s * c1);
114
115 /// apply rotation to 2x2 matrix so that alpha10 becomes zero
116 *alpha00 = tmp + sign_alpha10 * sqrt_mult_alpha_offdiags;
117 *alpha11 = tmp - sign_alpha10 * sqrt_mult_alpha_offdiags;
118 *alpha01 = (*alpha01) - (*alpha10);
119 *alpha10 = zero;
120
121 // two real eigen values
122 *lambda1 = Kokkos::complex<real_type>(*alpha00);
123 *lambda2 = Kokkos::complex<real_type>(*alpha11);
124 *is_complex = false;
125 } else {
126 /// two complex eigen values
127 const real_type sqrt_mult_alpha_offdiags = ats::sqrt(-mult_alpha_offdiags);
128 *lambda1 = Kokkos::complex<real_type>(tmp, sqrt_mult_alpha_offdiags);
129 *lambda2 = Kokkos::complex<real_type>(lambda1->real(), -lambda1->imag());
130 *is_complex = true;
131 }
132 }
133 return 0;
134 }
135 };
136
137 } // end namespace KokkosBatched
138
139 #endif