Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_SerialSVD.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 /// \author Brian Kelley ([email protected])
17
18 #include "KokkosBatched_SVD_Decl.hpp" //For testing overall kernel
19 #include "KokkosBatched_SVD_Serial_Internal.hpp" //For unit testing individual components
20 #include "KokkosBatched_SetIdentity_Decl.hpp"
21 #include "KokkosKernels_TestMatrixUtils.hpp"
22 #include "KokkosKernels_TestVanilla.hpp"
23
24 namespace Test {
25 template <typename Scalar>
26 Scalar svdEpsilon() {
27 throw std::runtime_error("Unsupported scalar type");
28 }
29
30 template <>
31 double svdEpsilon() {
32 return 1e-13;
33 }
34
35 template <>
36 float svdEpsilon() {
37 return 2e-6f;
38 }
39 } // namespace Test
40
41 // NOTE: simpleDot and simpleNorm2 currently support only real scalars (OK since
42 // SVD does as well)
43 template <typename V1, typename V2>
44 typename V1::non_const_value_type simpleDot(const V1& v1, const V2& v2) {
45 using Scalar = typename V1::non_const_value_type;
46 Scalar d;
47 Kokkos::parallel_reduce(
48 Kokkos::RangePolicy<typename V1::execution_space>(0, v1.extent(0)),
49 KOKKOS_LAMBDA(int i, Scalar& ld) { ld += v1(i) * v2(i); }, d);
50 return d;
51 }
52 template <typename V>
53 typename V::non_const_value_type simpleNorm2(const V& v) {
54 return Kokkos::sqrt(simpleDot(v, v));
55 }
56
57 // Check that all columns of X are unit length and pairwise orthogonal
58 template <typename Mat>
59 void verifyOrthogonal(const Mat& X) {
60 using Scalar = typename Mat::non_const_value_type;
61 int k = X.extent(1);
62 for (int i = 0; i < k; i++) {
63 auto col1 = Kokkos::subview(X, Kokkos::ALL(), i);
64 double len = simpleNorm2(col1);
65 Test::EXPECT_NEAR_KK(len, 1.0, Test::svdEpsilon<Scalar>());
66 for (int j = 0; j < i; j++) {
67 auto col2 = Kokkos::subview(X, Kokkos::ALL(), j);
68 double d = Kokkos::ArithTraits<Scalar>::abs(simpleDot(col1, col2));
69 Test::EXPECT_NEAR_KK(d, 0.0, Test::svdEpsilon<Scalar>());
70 }
71 }
72 }
73
74 template <typename AView, typename UView, typename VtView, typename SigmaView>
75 void verifySVD(const AView& A, const UView& U, const VtView& Vt, const SigmaView& sigma) {
76 using Scalar = typename AView::non_const_value_type;
77 using KAT = Kokkos::ArithTraits<Scalar>;
78 // Check that U/V columns are unit length and orthogonal, and that U *
79 // diag(sigma) * V^T == A
80 int m = A.extent(0);
81 int n = A.extent(1);
82 int maxrank = std::min(m, n);
83 verifyOrthogonal(U);
84 // NOTE: V^T being square and orthonormal implies that V is, so we don't have
85 // to transpose it here.
86 verifyOrthogonal(Vt);
87 Kokkos::View<Scalar**, typename AView::device_type> usvt("USV^T", m, n);
88 for (int i = 0; i < maxrank; i++) {
89 auto Ucol = Kokkos::subview(U, Kokkos::ALL(), Kokkos::make_pair<int>(i, i + 1));
90 auto Vtrow = Kokkos::subview(Vt, Kokkos::make_pair<int>(i, i + 1), Kokkos::ALL());
91 Test::vanillaGEMM(sigma(i), Ucol, Vtrow, 1.0, usvt);
92 }
93 for (int i = 0; i < m; i++) {
94 for (int j = 0; j < n; j++) {
95 Test::EXPECT_NEAR_KK(usvt(i, j), A(i, j), Test::svdEpsilon<Scalar>());
96 }
97 }
98 // Make sure all singular values are positive
99 for (int i = 0; i < maxrank; i++) {
100 EXPECT_GE(sigma(i), KAT::zero());
101 }
102 // Make sure singular values are in descending order
103 for (int i = 0; i < maxrank - 1; i++) {
104 EXPECT_GE(sigma(i), sigma(i + 1));
105 }
106 }
107
108 template <typename Matrix, typename Vector>
109 struct SerialSVDFunctor_Full {
110 SerialSVDFunctor_Full(const Matrix& A_, const Matrix& U_, const Matrix& Vt_, const Vector& sigma_,
111 const Vector& work_)
112 : A(A_), U(U_), Vt(Vt_), sigma(sigma_), work(work_) {}
113
114 // NOTE: this functor is only meant to be launched with a single element range
115 // policy
116 KOKKOS_INLINE_FUNCTION void operator()(int) const {
117 KokkosBatched::SerialSVD::invoke(KokkosBatched::SVD_USV_Tag(), A, U, sigma, Vt, work);
118 }
119
120 Matrix A;
121 Matrix U;
122 Matrix Vt;
123 Vector sigma;
124 Vector work;
125 };
126
127 template <typename Matrix, typename Vector>
128 struct SerialSVDFunctor_SingularValuesOnly {
129 SerialSVDFunctor_SingularValuesOnly(const Matrix& A_, const Vector& sigma_, const Vector& work_)
130 : A(A_), sigma(sigma_), work(work_) {}
131
132 // NOTE: this functor is only meant to be launched with a single element range
133 // policy
134 KOKKOS_INLINE_FUNCTION void operator()(int) const {
135 KokkosBatched::SerialSVD::invoke(KokkosBatched::SVD_S_Tag(), A, sigma, work);
136 }
137
138 Matrix A;
139 Vector sigma;
140 Vector work;
141 };
142
143 template <typename Matrix>
144 Matrix randomMatrixWithRank(int m, int n, int rank) {
145 Matrix A("A", m, n);
146 if (rank == Kokkos::min(m, n)) {
147 // A is full-rank so as a shortcut, fill it with random values directly.
148 Kokkos::Random_XorShift64_Pool<typename Matrix::device_type> rand_pool(13318);
149 Kokkos::fill_random(A, rand_pool, -1.0, 1.0);
150 } else {
151 // A is rank-deficient, so compute it as a product of two random matrices
152 using MatrixHost = typename Matrix::HostMirror;
153 auto Ahost = Kokkos::create_mirror_view(A);
154 Kokkos::Random_XorShift64_Pool<Kokkos::DefaultHostExecutionSpace> rand_pool(13318);
155 MatrixHost U("U", m, rank);
156 MatrixHost Vt("Vt", rank, n);
157 Kokkos::fill_random(U, rand_pool, -1.0, 1.0);
158 Kokkos::fill_random(Vt, rand_pool, -1.0, 1.0);
159 Test::vanillaGEMM(1.0, U, Vt, 0.0, Ahost);
160 Kokkos::deep_copy(A, Ahost);
161 }
162 return A;
163 }
164
165 template <typename Matrix>
166 Matrix randomMatrixWithRank(int m, int n) {
167 return randomMatrixWithRank<Matrix>(m, n, Kokkos::min(m, n));
168 }
169
170 template <typename Scalar, typename Layout, typename Device>
171 void testSerialSVD(int m, int n, int rank) {
172 using Matrix = Kokkos::View<Scalar**, Layout, Device>;
173 using Vector = Kokkos::View<Scalar*, Device>;
174 using ExecSpace = typename Device::execution_space;
175 Matrix A = randomMatrixWithRank<Matrix>(m, n, rank);
176 // Fill U, Vt, sigma with nonzeros as well to make sure they are properly
177 // overwritten
178 Matrix U("U", m, m);
179 Matrix Vt("Vt", n, n);
180 int maxrank = std::min(m, n);
181 Vector sigma("sigma", maxrank);
182 Vector work("work", std::max(m, n));
183 // Fill these views with an arbitrary value, to make sure SVD
184 // doesn't rely on them being zero initialized.
185 Kokkos::deep_copy(U, -5.0);
186 Kokkos::deep_copy(Vt, -5.0);
187 Kokkos::deep_copy(sigma, -5.0);
188 Kokkos::deep_copy(work, -5.0);
189 // Make a copy of A (before SVD) for verification, since the original will be
190 // overwritten
191 typename Matrix::HostMirror Acopy("Acopy", m, n);
192 Kokkos::deep_copy(Acopy, A);
193 // Run the SVD
194 Kokkos::parallel_for(Kokkos::RangePolicy<ExecSpace>(0, 1),
195 SerialSVDFunctor_Full<Matrix, Vector>(A, U, Vt, sigma, work));
196 // Get the results back
197 auto Uhost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), U);
198 auto Vthost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Vt);
199 auto sigmaHost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), sigma);
200 // Verify the SVD is correct
201 verifySVD(Acopy, Uhost, Vthost, sigmaHost);
202 }
203
204 template <typename Scalar, typename Layout, typename Device>
205 void testSerialSVD(int m, int n) {
206 testSerialSVD<Scalar, Layout, Device>(m, n, Kokkos::min(m, n));
207 }
208
209 template <typename Scalar, typename Layout, typename Device>
210 void testSerialSVDSingularValuesOnly(int m, int n) {
211 using Matrix = Kokkos::View<Scalar**, Layout, Device>;
212 using Vector = Kokkos::View<Scalar*, Device>;
213 using ExecSpace = typename Device::execution_space;
214 Matrix A = randomMatrixWithRank<Matrix>(m, n);
215 // Fill U, Vt, sigma with nonzeros as well to make sure they are properly
216 // overwritten
217 Matrix U("U", m, m);
218 Matrix Vt("Vt", n, n);
219 int maxrank = std::min(m, n);
220 Vector sigma1("sigma", maxrank);
221 Vector sigma2("sigma", maxrank);
222 Vector work("work", std::max(m, n));
223 Kokkos::deep_copy(U, -5.0);
224 Kokkos::deep_copy(Vt, -5.0);
225 Kokkos::deep_copy(sigma1, -5.0);
226 Kokkos::deep_copy(sigma2, -7.0);
227 Kokkos::deep_copy(work, -5.0);
228 // Make a copy of A (before SVD) for verification, since the original will be
229 // overwritten
230 typename Matrix::HostMirror Acopy("Acopy", m, n);
231 Kokkos::deep_copy(Acopy, A);
232 // Run the SVD (full mode)
233 Kokkos::parallel_for(Kokkos::RangePolicy<ExecSpace>(0, 1),
234 SerialSVDFunctor_Full<Matrix, Vector>(A, U, Vt, sigma1, work));
235 Kokkos::deep_copy(A, Acopy);
236 // Run the same SVD (singular values only mode)
237 Kokkos::parallel_for(Kokkos::RangePolicy<ExecSpace>(0, 1),
238 SerialSVDFunctor_SingularValuesOnly<Matrix, Vector>(A, sigma2, work));
239 auto sigma1Host = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), sigma1);
240 auto sigma2Host = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), sigma2);
241 // Make sure they match
242 for (int i = 0; i < maxrank; i++) {
243 Test::EXPECT_NEAR_KK(sigma1Host(i), sigma2Host(i), Test::svdEpsilon<Scalar>());
244 }
245 }
246
247 // Test the bidiagonal n*n SVD step where the last diagonal entry is 0
248 template <typename Scalar, typename Layout>
249 void testSerialSVDZeroLastRow(int n) {
250 // Generate a bidiagonal matrix
251 using Matrix = Kokkos::View<Scalar**, Layout, Kokkos::HostSpace>;
252 using KAT = Kokkos::ArithTraits<Scalar>;
253 Matrix B = randomMatrixWithRank<Matrix>(n, n);
254 // Zero out entries to make B bidiagonal
255 for (int i = 0; i < n; i++) {
256 for (int j = 0; j < n; j++) {
257 if (i != j && i + 1 != j) {
258 B(i, j) = KAT::zero();
259 }
260 }
261 }
262 // Also zero out the final diagonal to test this routine
263 B(n - 1, n - 1) = KAT::zero();
264 Matrix Vt("Vt", n, n);
265 KokkosBatched::SerialSetIdentity::invoke<Matrix>(Vt);
266 // Compute the initial product to make sure it's maintained by the routine
267 Matrix BVt("UBVt", n, n);
268 Test::vanillaGEMM(1.0, B, Vt, 0.0, BVt);
269 // Run the routine (just on host)
270 KokkosBatched::SerialSVDInternal::svdZeroLastColumn<Scalar>(B.data(), n, B.stride(0), B.stride(1), n, Vt.data(),
271 Vt.stride(0), Vt.stride(1));
272 // Check that B is still bidiagonal (to a tight tolerance, but not exactly
273 // zero)
274 for (int i = 0; i < n; i++) {
275 for (int j = 0; j < n; j++) {
276 if (i != j && i + 1 != j) {
277 Test::EXPECT_NEAR_KK(B(i, j), KAT::zero(), Test::svdEpsilon<Scalar>());
278 }
279 }
280 }
281 // Check that the last superdiagonal is now zero
282 Test::EXPECT_NEAR_KK(B(n - 2, n - 1), KAT::zero(), Test::svdEpsilon<Scalar>());
283 // Check that the product is still maintained
284 Matrix BVt2("UBVt", n, n);
285 Test::vanillaGEMM(1.0, B, Vt, 0.0, BVt2);
286 for (int i = 0; i < n; i++) {
287 for (int j = 0; j < n; j++) {
288 Test::EXPECT_NEAR_KK(BVt(i, j), BVt2(i, j), Test::svdEpsilon<Scalar>());
289 }
290 }
291 // Check that Vt is still orthogonal
292 verifyOrthogonal(Vt);
293 }
294
295 // Test bidiagonal n*n SVD step where some diagonal i (not the last) is 0.
296 template <typename Scalar, typename Layout>
297 void testSerialSVDZeroDiagonal(int n, int row) {
298 // Generate a bidiagonal matrix
299 using Matrix = Kokkos::View<Scalar**, Layout, Kokkos::HostSpace>;
300 using KAT = Kokkos::ArithTraits<Scalar>;
301 int m = n + 2; // Make U somewhat bigger to make sure the Givens transforms
302 // are applied correctly
303 Matrix B = randomMatrixWithRank<Matrix>(m, n);
304 // Zero out entries to make B bidiagonal
305 for (int i = 0; i < m; i++) {
306 for (int j = 0; j < n; j++) {
307 if (i != j && i + 1 != j) {
308 B(i, j) = KAT::zero();
309 }
310 }
311 }
312 // Also zero out a diagonal to test this routine
313 B(row, row) = KAT::zero();
314 Matrix U("U", m, m);
315 KokkosBatched::SerialSetIdentity::invoke<Matrix>(U);
316 // Compute the initial product to make sure it's maintained by the routine
317 Matrix UB("UB", m, n);
318 Test::vanillaGEMM(1.0, U, B, 0.0, UB);
319 // Run the routine (just on host)
320 KokkosBatched::SerialSVDInternal::svdZeroRow<Scalar>(row, B.data(), n, B.stride(0), B.stride(1), U.data(), m,
321 U.stride(0), U.stride(1));
322 // Check that B is still bidiagonal (to a tight tolerance, but not exactly
323 // zero)
324 for (int i = 0; i < m; i++) {
325 for (int j = 0; j < n; j++) {
326 if (i != j && i + 1 != j) {
327 Test::EXPECT_NEAR_KK(B(i, j), KAT::zero(), Test::svdEpsilon<Scalar>());
328 }
329 }
330 }
331 // Check that row's diagonal is now zero
332 Test::EXPECT_NEAR_KK(B(row, row), KAT::zero(), Test::svdEpsilon<Scalar>());
333 // Check that the product is still maintained
334 Matrix UB2("UB", m, n);
335 Test::vanillaGEMM(1.0, U, B, 0.0, UB2);
336 for (int i = 0; i < m; i++) {
337 for (int j = 0; j < n; j++) {
338 Test::EXPECT_NEAR_KK(UB(i, j), UB2(i, j), Test::svdEpsilon<Scalar>());
339 }
340 }
341 // Check that U is still orthogonal
342 verifyOrthogonal(U);
343 }
344
345 template <typename Scalar, typename Layout, typename Device>
346 void testSVD() {
347 testSerialSVD<Scalar, Layout, Device>(0, 0);
348 testSerialSVD<Scalar, Layout, Device>(1, 0);
349 testSerialSVD<Scalar, Layout, Device>(0, 1);
350 testSerialSVD<Scalar, Layout, Device>(2, 2);
351 testSerialSVD<Scalar, Layout, Device>(2, 2, 1);
352 testSerialSVD<Scalar, Layout, Device>(10, 8);
353 testSerialSVD<Scalar, Layout, Device>(8, 10);
354 testSerialSVD<Scalar, Layout, Device>(10, 1);
355 testSerialSVD<Scalar, Layout, Device>(1, 10, 0);
356 testSerialSVD<Scalar, Layout, Device>(10, 8, 3);
357 testSerialSVD<Scalar, Layout, Device>(8, 10, 4);
358 testSerialSVD<Scalar, Layout, Device>(8, 10, 7);
359 // Test some important internal routines which are not called often
360 testSerialSVDZeroLastRow<Scalar, Layout>(10);
361 testSerialSVDZeroDiagonal<Scalar, Layout>(10, 3);
362 // Test the mode that just computes singular values
363 testSerialSVDSingularValuesOnly<Scalar, Layout, Device>(10, 8);
364 }
365
366 template <typename ViewT>
367 KOKKOS_INLINE_FUNCTION constexpr auto Determinant(ViewT F)
368 -> std::enable_if_t<Kokkos::is_view<ViewT>::value && ViewT::rank == 2, double> {
369 return (F(0, 0) * F(1, 1) * F(2, 2) + F(0, 1) * F(1, 2) * F(2, 0) + F(0, 2) * F(1, 0) * F(2, 1) -
370 (F(0, 2) * F(1, 1) * F(2, 0) + F(0, 1) * F(1, 0) * F(2, 2) + F(0, 0) * F(1, 2) * F(2, 1)));
371 }
372
373 template <typename ExeSpace, typename ViewT>
374 void GenerateTestData(ViewT data) {
375 using memory_space = typename ExeSpace::memory_space;
376 // finite difference should return dPK2dU. So, we can analyze two cases.
377 Kokkos::Random_XorShift64_Pool<memory_space> random(13718);
378 Kokkos::fill_random(data, random, 1.0);
379 Kokkos::parallel_for(
380 Kokkos::RangePolicy<ExeSpace>(0, data.extent(0)), KOKKOS_LAMBDA(int i) {
381 auto data_i = Kokkos::subview(data, i, Kokkos::ALL(), Kokkos::ALL());
382 while (Determinant(data_i) < 0.5) {
383 data_i(0, 0) += 1.0;
384 data_i(1, 1) += 1.0;
385 data_i(2, 2) += 1.0;
386 }
387 });
388 }
389
390 template <typename Scalar, typename Layout, typename Device, int N = 3>
391 void testIssue1786() {
392 using execution_space = typename Device::execution_space;
393 using memory_space = typename Device::memory_space;
394 constexpr int num_tests = 4;
395 Kokkos::View<Scalar* [3][3], Layout, memory_space> matrices("data", num_tests);
396 GenerateTestData<execution_space>(matrices);
397 Kokkos::View<Scalar* [N][N], Layout, memory_space> Us("Us", matrices.extent(0));
398 Kokkos::View<Scalar* [N], Layout, memory_space> Ss("Ss", matrices.extent(0));
399 Kokkos::View<Scalar* [N][N], Layout, memory_space> Vts("Vts", matrices.extent(0));
400 // Make sure the 2nd dimension of works is contiguous
401 Kokkos::View<Scalar* [N], Kokkos::LayoutRight, memory_space> works("works", matrices.extent(0));
402 Kokkos::View<Scalar* [N][N], Layout, memory_space> matrices_copy("matrices_copy", matrices.extent(0));
403 // make a copy of the input data to avoid overwriting it
404 Kokkos::deep_copy(matrices_copy, matrices);
405 auto policy = Kokkos::RangePolicy<execution_space>(0, matrices.extent(0));
406 Kokkos::parallel_for(
407 "polar decomposition", policy, KOKKOS_LAMBDA(int i) {
408 auto matrix_copy = Kokkos::subview(matrices_copy, i, Kokkos::ALL(), Kokkos::ALL());
409 auto U = Kokkos::subview(Us, i, Kokkos::ALL(), Kokkos::ALL());
410 auto S = Kokkos::subview(Ss, i, Kokkos::ALL());
411 auto Vt = Kokkos::subview(Vts, i, Kokkos::ALL(), Kokkos::ALL());
412 auto work = Kokkos::subview(works, i, Kokkos::ALL());
413 KokkosBatched::SerialSVD::invoke(KokkosBatched::SVD_USV_Tag{}, matrix_copy, U, S, Vt, work);
414 });
415
416 auto Us_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, Us);
417 auto Ss_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, Ss);
418 auto Vts_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, Vts);
419 auto matrices_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, matrices);
420 for (int i = 0; i < num_tests; i++) {
421 auto A = Kokkos::subview(matrices_h, i, Kokkos::ALL(), Kokkos::ALL());
422 auto U = Kokkos::subview(Us_h, i, Kokkos::ALL(), Kokkos::ALL());
423 auto S = Kokkos::subview(Ss_h, i, Kokkos::ALL());
424 auto Vt = Kokkos::subview(Vts_h, i, Kokkos::ALL(), Kokkos::ALL());
425 verifySVD(A, U, Vt, S);
426 }
427 }
428
429 // Generate specific test cases
430 template <typename Scalar, typename Layout, typename Device>
431 Kokkos::View<Scalar**, Layout, Device> getTestCase(int testCase) {
432 using MatrixHost = Kokkos::View<Scalar**, Layout, Kokkos::HostSpace>;
433 MatrixHost Ahost;
434 int m, n;
435 switch (testCase) {
436 case 0:
437 // Issue #2344 case 1
438 m = 3;
439 n = 3;
440 Ahost = MatrixHost("A0", m, n);
441 Ahost(1, 0) = 3.58442287931538747e-02;
442 Ahost(1, 1) = 3.81743062695684907e-02;
443 Ahost(2, 2) = -5.55555555555555733e-02;
444 break;
445 case 1:
446 // Test a matrix that is strictly lower triangular (so the diagonal
447 // is zero)
448 m = 8;
449 n = 8;
450 Ahost = MatrixHost("A1", m, n);
451 for (int i = 0; i < m; i++) {
452 for (int j = 0; j < i; j++) {
453 Ahost(i, j) = 1;
454 }
455 }
456 break;
457 case 2:
458 // Test a matrix that's already diagonal, except for one superdiagonal in the middle
459 m = 10;
460 n = 5;
461 Ahost = MatrixHost("A2", m, n);
462 for (int i = 0; i < n; i++) Ahost(i, i) = 1.0;
463 Ahost(2, 3) = 2.2;
464 break;
465 case 3:
466 // Test a matrix that is already bidiagonal, and has a zero diagonal in the middle
467 m = 10;
468 n = 7;
469 Ahost = MatrixHost("A3", m, n);
470 for (int i = 0; i < n; i++) Ahost(i, i) = 1.0;
471 for (int i = 0; i < n - 1; i++) Ahost(i, i + 1) = 0.7;
472 Ahost(4, 4) = 0;
473 break;
474 case 4: {
475 // Issue #2344 case 2
476 m = 3;
477 n = 4;
478 Ahost = MatrixHost("A4", m, n);
479 Ahost(0, 0) = -2.0305040121856084e-02;
480 Ahost(1, 0) = 0.0000000000000000e+00;
481 Ahost(2, 0) = 0.0000000000000000e+00;
482 Ahost(0, 1) = -0.0000000000000000e+00;
483 Ahost(1, 1) = -0.0000000000000000e+00;
484 Ahost(2, 1) = 1.9506119814028472e-02;
485 Ahost(0, 2) = -2.0305040121856091e-02;
486 Ahost(1, 2) = 0.0000000000000000e+00;
487 Ahost(2, 2) = 0.0000000000000000e+00;
488 Ahost(0, 3) = -0.0000000000000000e+00;
489 Ahost(1, 3) = -0.0000000000000000e+00;
490 Ahost(2, 3) = 1.9506119814028472e-02;
491 break;
492 }
493 case 5: {
494 // Test with all-zero matrix
495 m = 17;
496 n = 19;
497 Ahost = MatrixHost("A5", m, n);
498 break;
499 }
500 default: throw std::runtime_error("Test case out of bounds.");
501 }
502 Kokkos::View<Scalar**, Layout, Device> A(Ahost.label(), m, n);
503 Kokkos::deep_copy(A, Ahost);
504 return A;
505 }
506
507 template <typename Scalar, typename Layout, typename Device>
508 void testSpecialCases() {
509 using Matrix = Kokkos::View<Scalar**, Layout, Device>;
510 using Vector = Kokkos::View<Scalar*, Device>;
511 using ExecSpace = typename Device::execution_space;
512 for (int i = 0; i < 6; i++) {
513 Matrix A = getTestCase<Scalar, Layout, Device>(i);
514 int m = A.extent(0);
515 int n = A.extent(1);
516 Matrix U("U", m, m);
517 Matrix Vt("Vt", n, n);
518 int maxrank = std::min(m, n);
519 Vector sigma("sigma", maxrank);
520 Vector work("work", std::max(m, n));
521 Kokkos::deep_copy(U, -5.0);
522 Kokkos::deep_copy(Vt, -5.0);
523 Kokkos::deep_copy(sigma, -5.0);
524 Kokkos::deep_copy(work, -5.0);
525 // Make a copy of A (before SVD) for verification, since the original will be
526 // overwritten
527 typename Matrix::HostMirror Acopy("Acopy", m, n);
528 Kokkos::deep_copy(Acopy, A);
529 // Run the SVD
530 Kokkos::parallel_for(Kokkos::RangePolicy<ExecSpace>(0, 1),
531 SerialSVDFunctor_Full<Matrix, Vector>(A, U, Vt, sigma, work));
532 // Get the results back
533 auto Uhost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), U);
534 auto Vthost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Vt);
535 auto sigmaHost = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), sigma);
536
537 // Verify the SVD is correct
538 verifySVD(Acopy, Uhost, Vthost, sigmaHost);
539 }
540 }
541
542 #if defined(KOKKOSKERNELS_INST_DOUBLE)
543 TEST_F(TestCategory, batched_scalar_serial_svd_double) {
544 // Test general SVD on a few different input sizes (full rank randomized)
545 testSVD<double, Kokkos::LayoutLeft, TestDevice>();
546 testSVD<double, Kokkos::LayoutRight, TestDevice>();
547 testIssue1786<double, Kokkos::LayoutLeft, TestDevice>();
548 testIssue1786<double, Kokkos::LayoutRight, TestDevice>();
549 testSpecialCases<double, Kokkos::LayoutLeft, TestDevice>();
550 testSpecialCases<double, Kokkos::LayoutRight, TestDevice>();
551 }
552 #endif
553
554 #if defined(KOKKOSKERNELS_INST_FLOAT)
555 TEST_F(TestCategory, batched_scalar_serial_svd_float) {
556 // Test general SVD on a few different input sizes (full rank randomized)
557 testSVD<float, Kokkos::LayoutLeft, TestDevice>();
558 testSVD<float, Kokkos::LayoutRight, TestDevice>();
559 testIssue1786<float, Kokkos::LayoutLeft, TestDevice>();
560 testIssue1786<float, Kokkos::LayoutRight, TestDevice>();
561 testSpecialCases<float, Kokkos::LayoutLeft, TestDevice>();
562 testSpecialCases<float, Kokkos::LayoutRight, TestDevice>();
563 }
564 #endif