Debian Codegraph @

packages/kokkos-kernels/batched/dense/impl/KokkosBatched_Householder_TeamVector_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_TEAMVECTOR_INTERNAL_HPP
17 #define KOKKOSBATCHED_HOUSEHOLDER_TEAMVECTOR_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 TeamVectorLeftHouseholderInternal {
32 template <typename MemberType, typename ValueType>
33 KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, 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 Kokkos::parallel_reduce(
48 Kokkos::TeamVectorRange(member, m_x2),
49 [&](const int &i, mag_type &val) {
50 const auto x2_at_i = x2[i * x2s];
51 val += x2_at_i * x2_at_i;
52 },
53 norm_x2_square);
54
55 /// if norm_x2 is zero, return with trivial values
56 if (norm_x2_square == zero) {
57 Kokkos::single(Kokkos::PerTeam(member), [&]() {
58 *chi1 = -(*chi1);
59 *tau = half;
60 });
61 member.team_barrier();
62 return 0;
63 }
64
65 /// compute magnitude of chi1, equal to norm2 of chi1
66 const mag_type norm_chi1 = Kokkos::ArithTraits<value_type>::abs(*chi1);
67
68 /// compute 2 norm of x using norm_chi1 and norm_x2
69 const mag_type norm_x = Kokkos::ArithTraits<mag_type>::sqrt(norm_x2_square + norm_chi1 * norm_chi1);
70
71 /// compute alpha
72 const mag_type alpha = (*chi1 < 0 ? one : minus_one) * norm_x;
73
74 /// overwrite x2 with u2
75 const value_type chi1_minus_alpha = *chi1 - alpha;
76 const value_type inv_chi1_minus_alpha = one / chi1_minus_alpha;
77 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, m_x2),
78 [&](const int &i) { x2[i * x2s] *= inv_chi1_minus_alpha; });
79 member.team_barrier();
80
81 // later consider to use the following
82 // SerialScaleInternal::invoke(m_x2, inv_chi1_minus_alpha, x2, x2s);
83
84 /// compute tau
85 Kokkos::single(Kokkos::PerTeam(member), [&]() {
86 const mag_type chi1_minus_alpha_square = chi1_minus_alpha * chi1_minus_alpha;
87 *tau = half + half * (norm_x2_square / chi1_minus_alpha_square);
88
89 /// overwrite chi1 with alpha
90 *chi1 = alpha;
91 });
92
93 return 0;
94 }
95 };
96
97 } // end namespace KokkosBatched
98
99 #endif