Debian Codegraph @

packages/kokkos-kernels/batched/dense/impl/KokkosBatched_ApplyQ_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_APPLY_Q_SERIAL_INTERNAL_HPP
17 #define KOKKOSBATCHED_APPLY_Q_SERIAL_INTERNAL_HPP
18
19 /// \author Kyungjoo Kim ([email protected])
20
21 #include "KokkosBatched_Util.hpp"
22 #include "KokkosBatched_ApplyHouseholder_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
33 struct SerialApplyQ_LeftForwardInternal {
34 template <typename ValueType>
35 KOKKOS_INLINE_FUNCTION static int invoke(const int m, const int n, const int k,
36 /* */ ValueType *A, const int as0, const int as1,
37 /* */ ValueType *t, const int ts,
38 /* */ ValueType *B, const int bs0, const int bs1,
39 /* */ ValueType *w) {
40 typedef ValueType value_type;
41
42 /// Given a matrix A that includes a series of householder vectors,
43 /// it applies a unitary matrix Q to B from left without transpose
44 /// B = Q B = (H0 H1 H2 H3 ... H(k-1)) B
45 /// where
46 /// A is m x k (holding H0, H1 ... H(k-1)
47 /// t is k x 1
48 /// B is m x n
49
50 // partitions used for loop iteration
51 Partition2x2<value_type> A_part2x2(as0, as1);
52 Partition3x3<value_type> A_part3x3(as0, as1);
53
54 Partition2x1<value_type> t_part2x1(ts);
55 Partition3x1<value_type> t_part3x1(ts);
56
57 Partition2x1<value_type> B_part2x1(bs0);
58 Partition3x1<value_type> B_part3x1(bs0);
59
60 // initial partition of A where ATL has a zero dimension
61 A_part2x2.partWithABR(A, m, k, m - k, 0);
62 t_part2x1.partWithAB(t, k, 0);
63 B_part2x1.partWithAB(B, m, m - k);
64
65 for (int m_A0 = (k - 1); m_A0 >= 0; --m_A0) {
66 // part 2x2 into 3x3
67 A_part3x3.partWithATL(A_part2x2, 1, 1);
68 t_part3x1.partWithAT(t_part2x1, 1);
69 value_type *tau = t_part3x1.A1;
70
71 B_part3x1.partWithAT(B_part2x1, 1);
72 const int m_A2 = m - m_A0 - 1;
73 /// -----------------------------------------------------
74 // left apply householder to partitioned B1 and B2
75 SerialApplyLeftHouseholderInternal::invoke(m_A2, n, tau, A_part3x3.A21, as0, B_part3x1.A1, bs1, B_part3x1.A2, bs0,
76 bs1, w);
77
78 /// -----------------------------------------------------
79 A_part2x2.mergeToABR(A_part3x3);
80 t_part2x1.mergeToAB(t_part3x1);
81 B_part2x1.mergeToAB(B_part3x1);
82 }
83 return 0;
84 }
85 };
86
87 struct SerialApplyQ_LeftBackwardInternal {
88 template <typename ValueType>
89 KOKKOS_INLINE_FUNCTION static int invoke(const int m, const int n, const int k,
90 /* */ ValueType *A, const int as0, const int as1,
91 /* */ ValueType *t, const int ts,
92 /* */ ValueType *B, const int bs0, const int bs1,
93 /* */ ValueType *w) {
94 typedef ValueType value_type;
95
96 /// Given a matrix A that includes a series of householder vectors,
97 /// it applies a unitary matrix Q to B from left without transpose
98 /// B = Q^H B = (H(k-1) H(k-2) ... H0) B
99 /// where
100 /// A is m x k (holding H0, H1 ... H(k-1)
101 /// t is k x 1
102 /// B is m x n
103
104 // partitions used for loop iteration
105 Partition2x2<value_type> A_part2x2(as0, as1);
106 Partition3x3<value_type> A_part3x3(as0, as1);
107
108 Partition2x1<value_type> t_part2x1(ts);
109 Partition3x1<value_type> t_part3x1(ts);
110
111 Partition2x1<value_type> B_part2x1(bs0);
112 Partition3x1<value_type> B_part3x1(bs0);
113
114 // initial partition of A where ATL has a zero dimension
115 A_part2x2.partWithATL(A, m, k, 0, 0);
116 t_part2x1.partWithAT(t, k, 0);
117 B_part2x1.partWithAT(B, m, 0);
118
119 for (int m_A0 = 0; m_A0 < k; ++m_A0) {
120 // part 2x2 into 3x3
121 A_part3x3.partWithABR(A_part2x2, 1, 1);
122 t_part3x1.partWithAB(t_part2x1, 1);
123 value_type *tau = t_part3x1.A1;
124
125 B_part3x1.partWithAB(B_part2x1, 1);
126 const int m_A2 = m - m_A0 - 1;
127 /// -----------------------------------------------------
128 // left apply householder to partitioned B1 and B2
129 SerialApplyLeftHouseholderInternal::invoke(m_A2, n, tau, A_part3x3.A21, as0, B_part3x1.A1, bs1, B_part3x1.A2, bs0,
130 bs1, w);
131
132 /// -----------------------------------------------------
133 A_part2x2.mergeToATL(A_part3x3);
134 t_part2x1.mergeToAT(t_part3x1);
135 B_part2x1.mergeToAT(B_part3x1);
136 }
137 return 0;
138 }
139 };
140
141 struct SerialApplyQ_RightForwardInternal {
142 template <typename ValueType>
143 KOKKOS_INLINE_FUNCTION static int invoke(const int m, const int n, const int k,
144 /* */ ValueType *A, const int as0, const int as1,
145 /* */ ValueType *t, const int ts,
146 /* */ ValueType *B, const int bs0, const int bs1,
147 /* */ ValueType *w) {
148 typedef ValueType value_type;
149
150 /// Given a matrix A that includes a series of householder vectors,
151 /// it applies a unitary matrix Q to B from left without transpose
152 /// B = B Q = B (H0 H1 H2 H3 ... H(k-1))
153 /// where
154 /// A is n x k (holding H0, H1 ... H(k-1)
155 /// t is k x 1
156 /// B is m x n
157
158 // partitions used for loop iteration
159 Partition2x2<value_type> A_part2x2(as0, as1);
160 Partition3x3<value_type> A_part3x3(as0, as1);
161
162 Partition2x1<value_type> t_part2x1(ts);
163 Partition3x1<value_type> t_part3x1(ts);
164
165 Partition1x2<value_type> B_part1x2(bs1);
166 Partition1x3<value_type> B_part1x3(bs1);
167
168 // initial partition of A where ATL has a zero dimension
169 A_part2x2.partWithATL(A, n, k, 0, 0);
170 t_part2x1.partWithAT(t, k, 0);
171 B_part1x2.partWithAL(B, n, 0);
172
173 for (int n_A0 = 0; n_A0 < k; ++n_A0) {
174 // part 2x2 into 3x3
175 A_part3x3.partWithABR(A_part2x2, 1, 1);
176 t_part3x1.partWithAB(t_part2x1, 1);
177 value_type *tau = t_part3x1.A1;
178
179 B_part1x3.partWithAR(B_part1x2, 1);
180 const int n_B2 = n - n_A0 - 1;
181 /// -----------------------------------------------------
182 // right apply householder to partitioned B1 and B2
183 SerialApplyRightHouseholderInternal::invoke(m, n_B2, tau, A_part3x3.A21, as0, B_part1x3.A1, bs0, B_part1x3.A2,
184 bs0, bs1, w);
185 /// -----------------------------------------------------
186 A_part2x2.mergeToATL(A_part3x3);
187 t_part2x1.mergeToAT(t_part3x1);
188 B_part1x2.mergeToAL(B_part1x3);
189 }
190 return 0;
191 }
192 };
193
194 } // end namespace KokkosBatched
195
196 #endif