Debian Codegraph @

packages/kokkos-kernels/batched/dense/impl/KokkosBatched_ApplyHouseholder_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_APPLY_HOUSEHOLDER_TEAMVECTOR_INTERNAL_HPP
17 #define KOKKOSBATCHED_APPLY_HOUSEHOLDER_TEAMVECTOR_INTERNAL_HPP
18
19 /// \author Kyungjoo Kim ([email protected])
20
21 #include "KokkosBatched_Util.hpp"
22
23 namespace KokkosBatched {
24
25 ///
26 /// TeamVector Internal Impl
27 /// ========================
28 ///
29 /// this impl follows the flame interface of householder transformation
30 ///
31 struct TeamVectorApplyLeftHouseholderInternal {
32 template <typename MemberType, typename ValueType>
33 KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const int m, const int n, const ValueType *tau,
34 /* */ ValueType *u2, const int u2s,
35 /* */ ValueType *a1t, const int a1ts,
36 /* */ ValueType *A2, const int as0, const int as1,
37 /* */ ValueType *w1t) {
38 typedef ValueType value_type;
39
40 /// u2 m x 1
41 /// a1t 1 x n
42 /// A2 m x n
43
44 // apply a single householder transform H from the left to a row vector a1t
45 // and a matrix A2
46 const value_type inv_tau = value_type(1) / (*tau);
47
48 // compute the followings:
49 // a1t -= inv(tau)(a1t + u2'A2)
50 // A2 -= u2 inv(tau)(a1t + u2'A2)
51
52 // w1t = a1t + u2'A2 = A2^T conj(u2)
53 // w1t /= tau
54 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, n), [&](const int &j) {
55 value_type tmp(0);
56 Kokkos::parallel_reduce(
57 Kokkos::ThreadVectorRange(member, m),
58 [&](const int &i, value_type &val) {
59 val += Kokkos::ArithTraits<value_type>::conj(u2[i * u2s]) * A2[i * as0 + j * as1];
60 },
61 tmp);
62 Kokkos::single(Kokkos::PerThread(member), [&]() {
63 w1t[j] = (tmp + a1t[j * a1ts]) * inv_tau; // /= (*tau);
64 });
65 });
66 member.team_barrier();
67
68 // a1t -= w1t (axpy)
69 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, n), [&](const int &j) { a1t[j * a1ts] -= w1t[j]; });
70
71 // A2 -= u2 w1t (ger)
72 if (as0 <= as1) {
73 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, n), [&](const int &j) {
74 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, m),
75 [&](const int &i) { A2[i * as0 + j * as1] -= u2[i * u2s] * w1t[j]; });
76 });
77 } else {
78 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, n), [&](const int &j) {
79 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, m),
80 [&](const int &i) { A2[i * as0 + j * as1] -= u2[i * u2s] * w1t[j]; });
81 });
82 }
83
84 return 0;
85 }
86 };
87
88 struct TeamVectorApplyRightHouseholderInternal {
89 template <typename MemberType, typename ValueType>
90 KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const int m, const int n, const ValueType *tau,
91 /* */ ValueType *u2, const int u2s,
92 /* */ ValueType *a1, const int a1s,
93 /* */ ValueType *A2, const int as0, const int as1,
94 /* */ ValueType *w1) {
95 typedef ValueType value_type;
96 /// u2 n x 1
97 /// a1 m x 1
98 /// A2 m x n
99
100 // apply a single householder transform H from the left to a row vector a1t
101 // and a matrix A2
102 const value_type inv_tau = value_type(1) / (*tau);
103
104 // compute the followings:
105 // a1 -= inv(tau)(a1 + A2 u2)
106 // A2 -= inv(tau)(a1 + A2 u2) u2'
107
108 // w1 = a1 + A2 u2
109 // w1 /= tau
110 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, m), [&](const int &i) {
111 value_type tmp(0);
112 Kokkos::parallel_reduce(
113 Kokkos::ThreadVectorRange(member, n),
114 [&](const int &j, value_type &val) { val += A2[i * as0 + j * as1] * u2[j * u2s]; }, tmp);
115 Kokkos::single(Kokkos::PerThread(member), [&]() {
116 w1[i] = (tmp + a1[i * a1s]) * inv_tau; // \= (*tau);
117 });
118 });
119 member.team_barrier();
120
121 // a1 -= w1 (axpy)
122 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, m), [&](const int &i) { a1[i * a1s] -= w1[i]; });
123
124 // A2 -= w1 * u2' (ger with conjugate)
125 if (as0 <= as1) {
126 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, n), [&](const int &j) {
127 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, m), [&](const int &i) {
128 A2[i * as0 + j * as1] -= w1[i] * Kokkos::ArithTraits<ValueType>::conj(u2[j * u2s]);
129 });
130 });
131 } else {
132 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, n), [&](const int &j) {
133 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, m), [&](const int &i) {
134 A2[i * as0 + j * as1] -= w1[i] * Kokkos::ArithTraits<ValueType>::conj(u2[j * u2s]);
135 });
136 });
137 }
138
139 return 0;
140 }
141 };
142
143 } // end namespace KokkosBatched
144
145 #endif