Debian Codegraph @

packages/kokkos-kernels/batched/dense/impl/KokkosBatched_Householder_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_HOUSEHOLDER_SERIAL_INTERNAL_HPP
17 #define KOKKOSBATCHED_HOUSEHOLDER_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 SerialLeftHouseholderInternal {
32 template <typename ValueType>
33 KOKKOS_INLINE_FUNCTION static int invoke(const int m_x2,
34 /* */ ValueType* chi1,
35 /* */ ValueType* x2, const int x2s,
36 /* */ ValueType* tau) {
37 typedef ValueType value_type;
38 typedef typename Kokkos::ArithTraits<ValueType>::mag_type mag_type;
39
40 const mag_type zero(0);
41 const mag_type half(0.5);
42 const mag_type one(1);
43 const mag_type minus_one(-1);
44
45 /// compute the 2norm of x2
46 mag_type norm_x2_square(0);
47 for (int i = 0; i < m_x2; ++i) {
48 const auto x2_at_i = x2[i * x2s];
49 norm_x2_square += x2_at_i * x2_at_i;
50 }
51
52 /// if norm_x2 is zero, return with trivial values
53 if (norm_x2_square == zero) {
54 *chi1 = -(*chi1);
55 *tau = half;
56
57 return 0;
58 }
59
60 /// compute magnitude of chi1, equal to norm2 of chi1
61 const mag_type norm_chi1 = Kokkos::ArithTraits<value_type>::abs(*chi1);
62
63 /// compute 2 norm of x using norm_chi1 and norm_x2
64 const mag_type norm_x = Kokkos::ArithTraits<mag_type>::sqrt(norm_x2_square + norm_chi1 * norm_chi1);
65
66 /// compute alpha
67 const mag_type alpha = (*chi1 < 0 ? one : minus_one) * norm_x;
68
69 /// overwrite x2 with u2
70 const value_type chi1_minus_alpha = *chi1 - alpha;
71 const value_type inv_chi1_minus_alpha = one / chi1_minus_alpha;
72 for (int i = 0; i < m_x2; ++i) x2[i * x2s] *= inv_chi1_minus_alpha;
73
74 // later consider to use the following
75 // SerialScaleInternal::invoke(m_x2, inv_chi1_minus_alpha, x2, x2s);
76
77 /// compute tau
78 const mag_type chi1_minus_alpha_square = chi1_minus_alpha * chi1_minus_alpha;
79 *tau = half + half * (norm_x2_square / chi1_minus_alpha_square);
80
81 /// overwrite chi1 with alpha
82 *chi1 = alpha;
83
84 return 0;
85 }
86 };
87
88 } // end namespace KokkosBatched
89
90 #endif