Skip to content

Commit

Permalink
applying clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
lucbv committed Jan 31, 2024
1 parent 9b5cab9 commit b733595
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 43 deletions.
9 changes: 5 additions & 4 deletions lapack/src/KokkosLapack_svd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ void svd(const ExecutionSpace& space, const char jobu[], const char jobvt[],
}

#if defined(KOKKOSKERNELS_ENABLE_TPL_CUSOLVER)
if(std::is_same_v<ExecutionSpace, Kokkos::Cuda> && (A.extent(0) < A.extent(1))) {
if (std::is_same_v<ExecutionSpace, Kokkos::Cuda> &&
(A.extent(0) < A.extent(1))) {
throw std::runtime_error(
"CUSOLVER does not support SVD for matrices with more columns "
"than rows, you can transpose you matrix first then compute "
"SVD of that transpose: At=VSUt, and swap the output U and Vt"
" and transpose them to recover the desired SVD.");
"than rows, you can transpose you matrix first then compute "
"SVD of that transpose: At=VSUt, and swap the output U and Vt"
" and transpose them to recover the desired SVD.");
}
#endif

Expand Down
80 changes: 41 additions & 39 deletions lapack/unit_test/Test_Lapack_svd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ void check_triple_product(const AMatrix& A, const SVector& S, const UMatrix& U,
AMatrix M("U*S*V product", A.extent(0), A.extent(1));

// First compute the left side of the product: temp = U*S
Kokkos::parallel_for(Kokkos::RangePolicy<execution_space, int>(0, U.extent(0)),
KOKKOS_LAMBDA(const int& rowIdx){
for(int colIdx = 0; colIdx < U.extent_int(1); ++colIdx) {
if(colIdx < S.extent_int(0)) {
temp(rowIdx, colIdx) = U(rowIdx, colIdx)*S(colIdx);
}
}
});
Kokkos::parallel_for(
Kokkos::RangePolicy<execution_space, int>(0, U.extent(0)),
KOKKOS_LAMBDA(const int& rowIdx) {
for (int colIdx = 0; colIdx < U.extent_int(1); ++colIdx) {
if (colIdx < S.extent_int(0)) {
temp(rowIdx, colIdx) = U(rowIdx, colIdx) * S(colIdx);
}
}
});

// Second compute the right side of the product: M = temp*V = U*S*V
KokkosBlas::gemm("N", "N", 1, temp, Vt, 0, M);
Expand All @@ -63,10 +64,10 @@ void check_triple_product(const AMatrix& A, const SVector& S, const UMatrix& U,
const mag_type tol = 100 * Kokkos::ArithTraits<scalar_type>::eps();
for (int rowIdx = 0; rowIdx < A.extent_int(0); ++rowIdx) {
for (int colIdx = 0; colIdx < A.extent_int(1); ++colIdx) {
if(tol < Kokkos::abs(A_h(rowIdx, colIdx))) {
EXPECT_NEAR_KK_REL(A_h(rowIdx, colIdx), M_h(rowIdx, colIdx), tol);
if (tol < Kokkos::abs(A_h(rowIdx, colIdx))) {
EXPECT_NEAR_KK_REL(A_h(rowIdx, colIdx), M_h(rowIdx, colIdx), tol);
} else {
EXPECT_NEAR_KK(A_h(rowIdx, colIdx), M_h(rowIdx, colIdx), tol);
EXPECT_NEAR_KK(A_h(rowIdx, colIdx), M_h(rowIdx, colIdx), tol);
}
}
}
Expand All @@ -93,7 +94,7 @@ void check_unitary_orthogonal_matrix(const Matrix& M) {
Kokkos::ArithTraits<scalar_type>::one(), tol);
} else {
EXPECT_NEAR_KK(I0_h(rowIdx, colIdx),
Kokkos::ArithTraits<scalar_type>::zero(), tol);
Kokkos::ArithTraits<scalar_type>::zero(), tol);
}
}
}
Expand All @@ -109,7 +110,7 @@ void check_unitary_orthogonal_matrix(const Matrix& M) {
Kokkos::ArithTraits<scalar_type>::one(), tol);
} else {
EXPECT_NEAR_KK(I1_h(rowIdx, colIdx),
Kokkos::ArithTraits<scalar_type>::zero(), tol);
Kokkos::ArithTraits<scalar_type>::zero(), tol);
}
}
}
Expand Down Expand Up @@ -203,15 +204,15 @@ int impl_analytic_square_svd() {
const mag_type one_sqrt10 = static_cast<mag_type>(1 / Kokkos::sqrt(10));
const mag_type one_sqrt2 = static_cast<mag_type>(1 / Kokkos::sqrt(2));

EXPECT_NEAR_KK_REL(Kokkos::abs(U_real(0, 0)), one_sqrt10, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(U_real(0, 1)), 3*one_sqrt10, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(U_real(1, 0)), 3*one_sqrt10, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(U_real(1, 1)), one_sqrt10, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(U_real(0, 0)), one_sqrt10, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(U_real(0, 1)), 3 * one_sqrt10, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(U_real(1, 0)), 3 * one_sqrt10, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(U_real(1, 1)), one_sqrt10, tol);

EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(0, 0)), one_sqrt2, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(0, 1)), one_sqrt2, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(1, 0)), one_sqrt2, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(1, 1)), one_sqrt2, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(0, 0)), one_sqrt2, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(0, 1)), one_sqrt2, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(1, 0)), one_sqrt2, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(1, 1)), one_sqrt2, tol);

check_unitary_orthogonal_matrix(U);
check_unitary_orthogonal_matrix(Vt);
Expand Down Expand Up @@ -241,26 +242,27 @@ int impl_analytic_rectangular_svd() {
// USV = 1/sqrt(2) [1 1] * [5 0] * 1/(3*sqrt(2)) [3 1 -2*sqrt(2)]
// [1 -1] [0 3] [3 -1 2*sqrt(2)]
// [0 4 sqrt(2)]
A_h(0, 0) = 3;
A_h(0, 1) = 2;
A_h(0, 2) = 2;
A_h(1, 0) = 2;
A_h(1, 1) = 3;
A_h(0, 0) = 3;
A_h(0, 1) = 2;
A_h(0, 2) = 2;
A_h(1, 0) = 2;
A_h(1, 1) = 3;
A_h(1, 2) = -2;

Kokkos::deep_copy(A, A_h);
Kokkos::deep_copy(Aref, A_h);

try { KokkosLapack::svd("A", "A", A, S, U, Vt); }
catch(const std::runtime_error& e) {
try {
KokkosLapack::svd("A", "A", A, S, U, Vt);
} catch (const std::runtime_error& e) {
std::string test_string = e.what();
std::string cusolver_m_less_than_n =
"CUSOLVER does not support SVD for matrices with more columns "
"than rows, you can transpose you matrix first then compute "
"SVD of that transpose: At=VSUt, and swap the output U and Vt"
" and transpose them to recover the desired SVD.";
"CUSOLVER does not support SVD for matrices with more columns "
"than rows, you can transpose you matrix first then compute "
"SVD of that transpose: At=VSUt, and swap the output U and Vt"
" and transpose them to recover the desired SVD.";

if(test_string == cusolver_m_less_than_n) {
if (test_string == cusolver_m_less_than_n) {
return 0;
}
}
Expand Down Expand Up @@ -335,11 +337,11 @@ int impl_analytic_rectangular_svd() {
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(0, 0)), one_sqrt2, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(0, 1)), one_sqrt2, tol);
EXPECT_NEAR_KK(Kokkos::abs(Vt_real(0, 2)), 0, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(1, 0)), one_sqrt18, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(1, 1)), one_sqrt18, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(1, 2)), 4*one_sqrt18, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(2, 0)), 2*one_third, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(2, 1)), 2*one_third, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(1, 0)), one_sqrt18, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(1, 1)), one_sqrt18, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(1, 2)), 4 * one_sqrt18, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(2, 0)), 2 * one_third, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(2, 1)), 2 * one_third, tol);
EXPECT_NEAR_KK_REL(Kokkos::abs(Vt_real(2, 2)), one_third, tol);

check_unitary_orthogonal_matrix(U);
Expand Down Expand Up @@ -371,7 +373,7 @@ int test_svd() {

ret = Test::impl_analytic_square_svd<view_type_a_layout_left, Device>();

ret = Test::impl_analytic_rectangular_svd<view_type_a_layout_left, Device>();
ret = Test::impl_analytic_rectangular_svd<view_type_a_layout_left, Device>();

ret = Test::impl_test_svd<view_type_a_layout_left, Device>(0, 0);
EXPECT_EQ(ret, 0);
Expand Down

0 comments on commit b733595

Please sign in to comment.