cuda_dense_qr_test.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2022 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: joydeepb@cs.utexas.edu (Joydeep Biswas)
  30. #include <string>
  31. #include "ceres/dense_qr.h"
  32. #include "ceres/internal/eigen.h"
  33. #include "glog/logging.h"
  34. #include "gtest/gtest.h"
  35. namespace ceres::internal {
  36. #ifndef CERES_NO_CUDA
  37. TEST(CUDADenseQR, InvalidOptionOnCreate) {
  38. LinearSolver::Options options;
  39. ContextImpl context;
  40. options.context = &context;
  41. std::string error;
  42. EXPECT_TRUE(context.InitCuda(&error)) << error;
  43. auto dense_cuda_solver = CUDADenseQR::Create(options);
  44. EXPECT_EQ(dense_cuda_solver, nullptr);
  45. }
  46. // Tests the CUDA QR solver with a simple 4x4 matrix.
  47. TEST(CUDADenseQR, QR4x4Matrix) {
  48. Eigen::Matrix4d A;
  49. // clang-format off
  50. A << 4, 12, -16, 0,
  51. 12, 37, -43, 0,
  52. -16, -43, 98, 0,
  53. 0, 0, 0, 1;
  54. // clang-format on
  55. const Eigen::Vector4d b = Eigen::Vector4d::Ones();
  56. LinearSolver::Options options;
  57. ContextImpl context;
  58. options.context = &context;
  59. std::string error;
  60. EXPECT_TRUE(context.InitCuda(&error)) << error;
  61. options.dense_linear_algebra_library_type = CUDA;
  62. auto dense_cuda_solver = CUDADenseQR::Create(options);
  63. ASSERT_NE(dense_cuda_solver, nullptr);
  64. std::string error_string;
  65. ASSERT_EQ(
  66. dense_cuda_solver->Factorize(A.rows(), A.cols(), A.data(), &error_string),
  67. LinearSolverTerminationType::SUCCESS);
  68. Eigen::Vector4d x = Eigen::Vector4d::Zero();
  69. ASSERT_EQ(dense_cuda_solver->Solve(b.data(), x.data(), &error_string),
  70. LinearSolverTerminationType::SUCCESS);
  71. // Empirically observed accuracy of cuSolverDN's QR solver.
  72. const double kEpsilon = std::numeric_limits<double>::epsilon() * 1500;
  73. const Eigen::Vector4d x_expected(113.75 / 3.0, -31.0 / 3.0, 5.0 / 3.0, 1.0);
  74. EXPECT_NEAR((x - x_expected).norm() / x_expected.norm(), 0.0, kEpsilon);
  75. }
  76. // Tests the CUDA QR solver with a simple 4x4 matrix.
  77. TEST(CUDADenseQR, QR4x2Matrix) {
  78. Eigen::Matrix<double, 4, 2> A;
  79. // clang-format off
  80. A << 4, 12,
  81. 12, 37,
  82. -16, -43,
  83. 0, 0;
  84. // clang-format on
  85. const std::vector<double> b(4, 1.0);
  86. LinearSolver::Options options;
  87. ContextImpl context;
  88. options.context = &context;
  89. std::string error;
  90. EXPECT_TRUE(context.InitCuda(&error)) << error;
  91. options.dense_linear_algebra_library_type = CUDA;
  92. auto dense_cuda_solver = CUDADenseQR::Create(options);
  93. ASSERT_NE(dense_cuda_solver, nullptr);
  94. std::string error_string;
  95. ASSERT_EQ(
  96. dense_cuda_solver->Factorize(A.rows(), A.cols(), A.data(), &error_string),
  97. LinearSolverTerminationType::SUCCESS);
  98. std::vector<double> x(2, 0);
  99. ASSERT_EQ(dense_cuda_solver->Solve(b.data(), x.data(), &error_string),
  100. LinearSolverTerminationType::SUCCESS);
  101. // Empirically observed accuracy of cuSolverDN's QR solver.
  102. const double kEpsilon = std::numeric_limits<double>::epsilon() * 10;
  103. // Solution values computed with Octave.
  104. const Eigen::Vector2d x_expected(-1.143410852713177, 0.4031007751937981);
  105. EXPECT_NEAR((x[0] - x_expected[0]) / x_expected[0], 0.0, kEpsilon);
  106. EXPECT_NEAR((x[1] - x_expected[1]) / x_expected[1], 0.0, kEpsilon);
  107. }
  108. TEST(CUDADenseQR, MustFactorizeBeforeSolve) {
  109. const Eigen::Vector3d b = Eigen::Vector3d::Ones();
  110. LinearSolver::Options options;
  111. ContextImpl context;
  112. options.context = &context;
  113. std::string error;
  114. EXPECT_TRUE(context.InitCuda(&error)) << error;
  115. options.dense_linear_algebra_library_type = CUDA;
  116. auto dense_cuda_solver = CUDADenseQR::Create(options);
  117. ASSERT_NE(dense_cuda_solver, nullptr);
  118. std::string error_string;
  119. ASSERT_EQ(dense_cuda_solver->Solve(b.data(), nullptr, &error_string),
  120. LinearSolverTerminationType::FATAL_ERROR);
  121. }
  122. TEST(CUDADenseQR, Randomized1600x100Tests) {
  123. const int kNumRows = 1600;
  124. const int kNumCols = 100;
  125. using LhsType = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>;
  126. using RhsType = Eigen::Matrix<double, Eigen::Dynamic, 1>;
  127. using SolutionType = Eigen::Matrix<double, Eigen::Dynamic, 1>;
  128. LinearSolver::Options options;
  129. ContextImpl context;
  130. options.context = &context;
  131. std::string error;
  132. EXPECT_TRUE(context.InitCuda(&error)) << error;
  133. options.dense_linear_algebra_library_type = ceres::CUDA;
  134. std::unique_ptr<DenseQR> dense_qr = CUDADenseQR::Create(options);
  135. const int kNumTrials = 20;
  136. for (int i = 0; i < kNumTrials; ++i) {
  137. LhsType lhs = LhsType::Random(kNumRows, kNumCols);
  138. SolutionType x_expected = SolutionType::Random(kNumCols);
  139. RhsType rhs = lhs * x_expected;
  140. SolutionType x_computed = SolutionType::Zero(kNumCols);
  141. // Sanity check the random matrix sizes.
  142. EXPECT_EQ(lhs.rows(), kNumRows);
  143. EXPECT_EQ(lhs.cols(), kNumCols);
  144. EXPECT_EQ(rhs.rows(), kNumRows);
  145. EXPECT_EQ(rhs.cols(), 1);
  146. EXPECT_EQ(x_expected.rows(), kNumCols);
  147. EXPECT_EQ(x_expected.cols(), 1);
  148. EXPECT_EQ(x_computed.rows(), kNumCols);
  149. EXPECT_EQ(x_computed.cols(), 1);
  150. LinearSolver::Summary summary;
  151. summary.termination_type = dense_qr->FactorAndSolve(kNumRows,
  152. kNumCols,
  153. lhs.data(),
  154. rhs.data(),
  155. x_computed.data(),
  156. &summary.message);
  157. ASSERT_EQ(summary.termination_type, LinearSolverTerminationType::SUCCESS);
  158. ASSERT_NEAR((x_computed - x_expected).norm() / x_expected.norm(),
  159. 0.0,
  160. std::numeric_limits<double>::epsilon() * 400);
  161. }
  162. }
  163. #endif // CERES_NO_CUDA
  164. } // namespace ceres::internal