Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_DenseUtils.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 TEST_BATCHED_DENSE_HELPER_HPP
17 #define TEST_BATCHED_DENSE_HELPER_HPP
18
19 #include "KokkosBatched_Util.hpp"
20
21 namespace KokkosBatched {
22 template <typename MatrixViewType, typename VectorViewType>
23 void create_tridiagonal_batched_matrices(const MatrixViewType& A, const VectorViewType& B) {
24 Kokkos::Random_XorShift64_Pool<typename VectorViewType::device_type::execution_space> random(13718);
25 Kokkos::fill_random(B, random, Kokkos::reduction_identity<typename VectorViewType::value_type>::prod());
26
27 auto A_host = Kokkos::create_mirror_view(A);
28
29 const int N = A.extent(0);
30 const int BlkSize = A.extent(1);
31
32 for (int l = 0; l < N; ++l) {
33 for (int i = 0; i < BlkSize; ++i) {
34 for (int j = i; j < BlkSize; ++j) {
35 if (i == j)
36 A_host(l, i, j) = typename VectorViewType::value_type(2.0);
37 else if (i == j - 1) {
38 A_host(l, i, j) = typename VectorViewType::value_type(-1.0);
39 A_host(l, j, i) = typename VectorViewType::value_type(-1.0);
40 } else {
41 A_host(l, i, j) = typename VectorViewType::value_type(0.0);
42 A_host(l, j, i) = typename VectorViewType::value_type(0.0);
43 }
44 }
45 }
46 }
47
48 Kokkos::fence();
49
50 Kokkos::deep_copy(A, A_host);
51
52 Kokkos::fence();
53 }
54
55 template <typename InViewType, typename OutViewType, typename UploType>
56 void create_banded_triangular_matrix(InViewType& in, OutViewType& out, int k = 1, bool band_storage = true) {
57 auto h_in = Kokkos::create_mirror_view(in);
58 auto h_out = Kokkos::create_mirror_view(out);
59 const int N = in.extent(0), BlkSize = in.extent(1);
60
61 Kokkos::deep_copy(h_in, in);
62 if (band_storage) {
63 assert(out.extent(0) == in.extent(0));
64 assert(out.extent(1) == static_cast<std::size_t>(k + 1));
65 assert(out.extent(2) == in.extent(2));
66 if constexpr (std::is_same_v<UploType, KokkosBatched::Uplo::Upper>) {
67 for (int i0 = 0; i0 < N; i0++) {
68 for (int i1 = 0; i1 < k + 1; i1++) {
69 for (int i2 = i1; i2 < BlkSize; i2++) {
70 h_out(i0, k - i1, i2) = h_in(i0, i2 - i1, i2);
71 }
72 }
73 }
74 } else {
75 for (int i0 = 0; i0 < N; i0++) {
76 for (int i1 = 0; i1 < k + 1; i1++) {
77 for (int i2 = 0; i2 < BlkSize - i1; i2++) {
78 h_out(i0, i1, i2) = h_in(i0, i2 + i1, i2);
79 }
80 }
81 }
82 }
83 } else {
84 for (std::size_t i = 0; i < InViewType::rank(); i++) {
85 assert(out.extent(i) == in.extent(i));
86 }
87
88 if constexpr (std::is_same_v<UploType, KokkosBatched::Uplo::Upper>) {
89 for (int i0 = 0; i0 < N; i0++) {
90 for (int i1 = 0; i1 < BlkSize; i1++) {
91 for (int i2 = i1; i2 < Kokkos::min(i1 + k + 1, BlkSize); i2++) {
92 h_out(i0, i1, i2) = h_in(i0, i1, i2);
93 }
94 }
95 }
96 } else {
97 for (int i0 = 0; i0 < N; i0++) {
98 for (int i1 = 0; i1 < BlkSize; i1++) {
99 for (int i2 = Kokkos::max(0, i1 - k); i2 <= i1; i2++) {
100 h_out(i0, i1, i2) = h_in(i0, i1, i2);
101 }
102 }
103 }
104 }
105 }
106 Kokkos::deep_copy(out, h_out);
107 }
108
109 /// \brief Create a diagonal matrix from an input vector:
110 /// Copies the input vector into the diagonal of the output matrix specified
111 /// by the parameter k. k > 0 means that the matrix is upper-diagonal and
112 /// k < 0 means the lower-diagonal. k = 0 means the diagonal.
113 ///
114 /// \tparam InViewType: Input type for the vector, needs to be a 2D view
115 /// \tparam OutViewType: Output type for the matrix, needs to be a 3D view
116 ///
117 /// \param in [in]: Input batched vector, a rank 2 view
118 /// \param out [out]: Output batched matrix, where the diagonal compnent
119 /// specified by k is filled with the input vector, a rank 3 view
120 /// \param k [in]: The diagonal offset to be filled (default is 0).
121 ///
122 template <typename InViewType, typename OutViewType>
123 void create_diagonal_matrix(InViewType& in, OutViewType& out, int k = 0) {
124 auto h_in = Kokkos::create_mirror_view(in);
125 auto h_out = Kokkos::create_mirror_view(out);
126 const int N = in.extent(0), BlkSize = in.extent(1);
127
128 assert(out.extent(0) == in.extent(0));
129 assert(out.extent(1) == in.extent(1) + abs(k));
130
131 int i1_start = k >= 0 ? 0 : -k;
132 int i2_start = k >= 0 ? k : 0;
133
134 // Zero clear the output matrix
135 using ScalarType = typename OutViewType::non_const_value_type;
136 Kokkos::deep_copy(h_out, ScalarType(0.0));
137
138 Kokkos::deep_copy(h_in, in);
139 for (int i0 = 0; i0 < N; i0++) {
140 for (int i1 = 0; i1 < BlkSize; i1++) {
141 h_out(i0, i1 + i1_start, i1 + i2_start) = h_in(i0, i1);
142 }
143 }
144
145 Kokkos::deep_copy(out, h_out);
146 }
147
148 /// \brief Creates a positive definite symmetric (PDS) matrix.
149 /// Takes a full random matrix and converts it to a full pds matrix.
150 ///
151 /// \tparam InViewType: Input type for the matrix, needs to be a 3D view
152 /// \tparam OutViewType: Output type for the matrix, needs to be a 3D view
153 ///
154 /// \param in [in]: Input batched banded matrix, a rank 3 view
155 /// \param out [out]: Output batched full matrix, a rank 3 view
156 ///
157 template <typename InViewType, typename OutViewType>
158 void random_to_pds(InViewType& in, OutViewType& out) {
159 auto h_in = Kokkos::create_mirror_view(in);
160 auto h_out = Kokkos::create_mirror_view(out);
161 const int N = in.extent(0), BlkSize = in.extent(1);
162 using value_type = typename InViewType::non_const_value_type;
163
164 for (std::size_t i = 0; i < InViewType::rank(); i++) {
165 assert(out.extent(i) == in.extent(i));
166 }
167
168 Kokkos::deep_copy(h_in, in);
169 Kokkos::deep_copy(h_out, 0.0);
170
171 for (int i0 = 0; i0 < N; i0++) {
172 // Make a hermitian matrix
173 for (int i1 = 0; i1 < BlkSize; i1++) {
174 for (int i2 = i1; i2 < BlkSize; i2++) {
175 if (i1 == i2) {
176 // Diagonal elements must be real
177 h_out(i0, i1, i2) = Kokkos::ArithTraits<value_type>::real(h_in(i0, i1, i2));
178 } else {
179 // Off-diagonal elements are complex and Hermitian
180 h_out(i0, i1, i2) = h_in(i0, i1, i2);
181 h_out(i0, i2, i1) = Kokkos::ArithTraits<value_type>::conj(h_in(i0, i1, i2));
182 }
183 }
184 }
185
186 // Make matrix diagonal dominant
187 for (int i1 = 0; i1 < BlkSize; i1++) {
188 value_type sum = 0;
189 for (int i2 = 0; i2 < BlkSize; i2++) {
190 if (i1 != i2) {
191 sum += Kokkos::abs(h_out(i0, i1, i2));
192 }
193 }
194 h_out(i0, i1, i1) = sum + 1.0;
195 }
196 }
197 Kokkos::deep_copy(out, h_out);
198 }
199
200 /// \brief Creates a banded positive definite symmetric (PDS) matrix.
201 /// Takes a full diagonal dominant matrix and converts it to a banded pds matrix either
202 /// in banded or full storage.
203 ///
204 /// \tparam InViewType: Input type for the matrix, needs to be a 3D view
205 /// \tparam OutViewType: Output type for the matrix, needs to be a 3D view
206 /// \tparam UploType: Type indicating whether the matrix is upper or lower triangular
207 ///
208 /// \param in [in]: Input batched banded matrix, a rank 3 view
209 /// \param out [out]: Output batched full matrix, a rank 3 view
210 /// \param k [in]: Number of sub/super-diagonals for lower/upper triangular (default is 1)
211 /// \param band_storage [in]: Boolean flag indicating whether the output should be in banded storage format (default is
212 /// true)
213 template <typename InViewType, typename OutViewType, typename UploType>
214 void create_banded_pds_matrix(InViewType& in, OutViewType& out, int k = 1, bool band_storage = true) {
215 auto h_in = Kokkos::create_mirror_view(in);
216 auto h_out = Kokkos::create_mirror_view(out);
217 using value_type = typename InViewType::non_const_value_type;
218 const int N = in.extent(0), BlkSize = in.extent(1);
219
220 Kokkos::deep_copy(h_in, in);
221
222 if (band_storage) {
223 assert(out.extent(0) == in.extent(0));
224 assert(out.extent(1) == static_cast<std::size_t>(k + 1));
225 assert(out.extent(2) == in.extent(2));
226 if constexpr (std::is_same_v<UploType, KokkosBatched::Uplo::Upper>) {
227 for (int i0 = 0; i0 < N; i0++) {
228 for (int i1 = 0; i1 < k + 1; i1++) {
229 for (int i2 = i1; i2 < BlkSize; i2++) {
230 h_out(i0, k - i1, i2) = h_in(i0, i2 - i1, i2);
231 }
232 }
233 }
234 } else {
235 for (int i0 = 0; i0 < N; i0++) {
236 for (int i1 = 0; i1 < k + 1; i1++) {
237 for (int i2 = 0; i2 < BlkSize - i1; i2++) {
238 h_out(i0, i1, i2) = h_in(i0, i2 + i1, i2);
239 }
240 }
241 }
242 }
243 } else {
244 for (std::size_t i = 0; i < InViewType::rank(); i++) {
245 assert(out.extent(i) == in.extent(i));
246 }
247
248 if constexpr (std::is_same_v<UploType, KokkosBatched::Uplo::Upper>) {
249 for (int i0 = 0; i0 < N; i0++) {
250 for (int i1 = 0; i1 < BlkSize; i1++) {
251 for (int i2 = i1; i2 < Kokkos::min(i1 + k + 1, BlkSize); i2++) {
252 h_out(i0, i1, i2) = h_in(i0, i1, i2);
253 h_out(i0, i2, i1) = Kokkos::ArithTraits<value_type>::conj(h_in(i0, i1, i2));
254 }
255 }
256 }
257 } else {
258 for (int i0 = 0; i0 < N; i0++) {
259 for (int i1 = 0; i1 < BlkSize; i1++) {
260 for (int i2 = Kokkos::max(0, i1 - k); i2 <= i1; i2++) {
261 h_out(i0, i1, i2) = h_in(i0, i1, i2);
262 h_out(i0, i2, i1) = Kokkos::ArithTraits<value_type>::conj(h_in(i0, i1, i2));
263 }
264 }
265 }
266 }
267 }
268 Kokkos::deep_copy(out, h_out);
269 }
270
271 /// \brief Converts a banded matrix to a full matrix.
272 /// Takes a banded matrix in banded storage and converts it to a full matrix.
273 ///
274 /// \tparam InViewType: Input type for the matrix, needs to be a 3D view
275 /// \tparam OutViewType: Output type for the matrix, needs to be a 3D view
276 /// \tparam UploType: Type indicating whether the matrix is upper or lower triangular
277 ///
278 /// \param in [in]: Input batched banded matrix, a rank 3 view
279 /// \param out [out]: Output batched full matrix, a rank 3 view
280 /// \param k [in]: Number of sub/super-diagonals for lower/upper triangular (default is 1)
281 ///
282 template <typename InViewType, typename OutViewType, typename UploType>
283 void banded_to_full(InViewType& in, OutViewType& out, int k = 1) {
284 auto h_in = Kokkos::create_mirror_view(in);
285 auto h_out = Kokkos::create_mirror_view(out);
286 const int N = in.extent(0), BlkSize = in.extent(2);
287
288 Kokkos::deep_copy(h_in, in);
289 assert(in.extent(0) == out.extent(0));
290 assert(in.extent(1) == static_cast<std::size_t>(k + 1));
291 assert(in.extent(2) == out.extent(2));
292 if constexpr (std::is_same_v<UploType, KokkosBatched::Uplo::Upper>) {
293 for (int i0 = 0; i0 < N; i0++) {
294 for (int i1 = 0; i1 < k + 1; i1++) {
295 for (int i2 = i1; i2 < BlkSize; i2++) {
296 h_out(i0, i2 - i1, i2) = h_in(i0, k - i1, i2);
297 }
298 }
299 }
300 } else {
301 for (int i0 = 0; i0 < N; i0++) {
302 for (int i1 = 0; i1 < k + 1; i1++) {
303 for (int i2 = 0; i2 < BlkSize - i1; i2++) {
304 h_out(i0, i2 + i1, i2) = h_in(i0, i1, i2);
305 }
306 }
307 }
308 }
309 Kokkos::deep_copy(out, h_out);
310 }
311
312 } // namespace KokkosBatched
313
314 #endif // TEST_BATCHED_DENSE_HELPER_HPP