Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | |
| 31 | #include "ceres/sparse_cholesky.h" |
| 32 | |
| 33 | #include <memory> |
| 34 | #include <numeric> |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 35 | #include <random> |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 36 | #include <vector> |
| 37 | |
| 38 | #include "Eigen/Dense" |
| 39 | #include "Eigen/SparseCore" |
| 40 | #include "ceres/block_sparse_matrix.h" |
| 41 | #include "ceres/compressed_row_sparse_matrix.h" |
| 42 | #include "ceres/inner_product_computer.h" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 43 | #include "ceres/internal/config.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 44 | #include "ceres/internal/eigen.h" |
| 45 | #include "ceres/iterative_refiner.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 46 | #include "glog/logging.h" |
| 47 | #include "gmock/gmock.h" |
| 48 | #include "gtest/gtest.h" |
| 49 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 50 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 51 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 52 | namespace { |
| 53 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 54 | std::unique_ptr<BlockSparseMatrix> CreateRandomFullRankMatrix( |
| 55 | const int num_col_blocks, |
| 56 | const int min_col_block_size, |
| 57 | const int max_col_block_size, |
| 58 | const double block_density, |
| 59 | std::mt19937& prng) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 60 | // Create a random matrix |
| 61 | BlockSparseMatrix::RandomMatrixOptions options; |
| 62 | options.num_col_blocks = num_col_blocks; |
| 63 | options.min_col_block_size = min_col_block_size; |
| 64 | options.max_col_block_size = max_col_block_size; |
| 65 | |
| 66 | options.num_row_blocks = 2 * num_col_blocks; |
| 67 | options.min_row_block_size = 1; |
| 68 | options.max_row_block_size = max_col_block_size; |
| 69 | options.block_density = block_density; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 70 | auto random_matrix = BlockSparseMatrix::CreateRandomMatrix(options, prng); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 71 | |
| 72 | // Add a diagonal block sparse matrix to make it full rank. |
| 73 | Vector diagonal = Vector::Ones(random_matrix->num_cols()); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 74 | auto block_diagonal = BlockSparseMatrix::CreateDiagonalMatrix( |
| 75 | diagonal.data(), random_matrix->block_structure()->cols); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 76 | random_matrix->AppendRows(*block_diagonal); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 77 | return random_matrix; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 80 | bool ComputeExpectedSolution(const CompressedRowSparseMatrix& lhs, |
| 81 | const Vector& rhs, |
| 82 | Vector* solution) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 83 | Matrix eigen_lhs; |
| 84 | lhs.ToDenseMatrix(&eigen_lhs); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 85 | if (lhs.storage_type() == |
| 86 | CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 87 | Matrix full_lhs = eigen_lhs.selfadjointView<Eigen::Upper>(); |
| 88 | Eigen::LLT<Matrix, Eigen::Upper> llt = |
| 89 | eigen_lhs.selfadjointView<Eigen::Upper>().llt(); |
| 90 | if (llt.info() != Eigen::Success) { |
| 91 | return false; |
| 92 | } |
| 93 | *solution = llt.solve(rhs); |
| 94 | return (llt.info() == Eigen::Success); |
| 95 | } |
| 96 | |
| 97 | Matrix full_lhs = eigen_lhs.selfadjointView<Eigen::Lower>(); |
| 98 | Eigen::LLT<Matrix, Eigen::Lower> llt = |
| 99 | eigen_lhs.selfadjointView<Eigen::Lower>().llt(); |
| 100 | if (llt.info() != Eigen::Success) { |
| 101 | return false; |
| 102 | } |
| 103 | *solution = llt.solve(rhs); |
| 104 | return (llt.info() == Eigen::Success); |
| 105 | } |
| 106 | |
| 107 | void SparseCholeskySolverUnitTest( |
| 108 | const SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type, |
| 109 | const OrderingType ordering_type, |
| 110 | const bool use_block_structure, |
| 111 | const int num_blocks, |
| 112 | const int min_block_size, |
| 113 | const int max_block_size, |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 114 | const double block_density, |
| 115 | std::mt19937& prng) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 116 | LinearSolver::Options sparse_cholesky_options; |
| 117 | sparse_cholesky_options.sparse_linear_algebra_library_type = |
| 118 | sparse_linear_algebra_library_type; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 119 | sparse_cholesky_options.ordering_type = ordering_type; |
| 120 | auto sparse_cholesky = SparseCholesky::Create(sparse_cholesky_options); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 121 | const CompressedRowSparseMatrix::StorageType storage_type = |
| 122 | sparse_cholesky->StorageType(); |
| 123 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 124 | auto m = CreateRandomFullRankMatrix( |
| 125 | num_blocks, min_block_size, max_block_size, block_density, prng); |
| 126 | auto inner_product_computer = InnerProductComputer::Create(*m, storage_type); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 127 | inner_product_computer->Compute(); |
| 128 | CompressedRowSparseMatrix* lhs = inner_product_computer->mutable_result(); |
| 129 | |
| 130 | if (!use_block_structure) { |
| 131 | lhs->mutable_row_blocks()->clear(); |
| 132 | lhs->mutable_col_blocks()->clear(); |
| 133 | } |
| 134 | |
| 135 | Vector rhs = Vector::Random(lhs->num_rows()); |
| 136 | Vector expected(lhs->num_rows()); |
| 137 | Vector actual(lhs->num_rows()); |
| 138 | |
| 139 | EXPECT_TRUE(ComputeExpectedSolution(*lhs, rhs, &expected)); |
| 140 | std::string message; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 141 | EXPECT_EQ( |
| 142 | sparse_cholesky->FactorAndSolve(lhs, rhs.data(), actual.data(), &message), |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 143 | LinearSolverTerminationType::SUCCESS); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 144 | Matrix eigen_lhs; |
| 145 | lhs->ToDenseMatrix(&eigen_lhs); |
| 146 | EXPECT_NEAR((actual - expected).norm() / actual.norm(), |
| 147 | 0.0, |
| 148 | std::numeric_limits<double>::epsilon() * 20) |
| 149 | << "\n" |
| 150 | << eigen_lhs; |
| 151 | } |
| 152 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 153 | using Param = |
| 154 | ::testing::tuple<SparseLinearAlgebraLibraryType, OrderingType, bool>; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 155 | |
| 156 | std::string ParamInfoToString(testing::TestParamInfo<Param> info) { |
| 157 | Param param = info.param; |
| 158 | std::stringstream ss; |
| 159 | ss << SparseLinearAlgebraLibraryTypeToString(::testing::get<0>(param)) << "_" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 160 | << ::testing::get<1>(param) << "_" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 161 | << (::testing::get<2>(param) ? "UseBlockStructure" : "NoBlockStructure"); |
| 162 | return ss.str(); |
| 163 | } |
| 164 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 165 | } // namespace |
| 166 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 167 | class SparseCholeskyTest : public ::testing::TestWithParam<Param> {}; |
| 168 | |
| 169 | TEST_P(SparseCholeskyTest, FactorAndSolve) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 170 | constexpr int kMinNumBlocks = 1; |
| 171 | constexpr int kMaxNumBlocks = 10; |
| 172 | constexpr int kNumTrials = 10; |
| 173 | constexpr int kMinBlockSize = 1; |
| 174 | constexpr int kMaxBlockSize = 5; |
| 175 | |
| 176 | Param param = GetParam(); |
| 177 | |
| 178 | std::mt19937 prng; |
| 179 | std::uniform_real_distribution<double> distribution(0.1, 1.0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 180 | |
| 181 | for (int num_blocks = kMinNumBlocks; num_blocks < kMaxNumBlocks; |
| 182 | ++num_blocks) { |
| 183 | for (int trial = 0; trial < kNumTrials; ++trial) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 184 | const double block_density = distribution(prng); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 185 | SparseCholeskySolverUnitTest(::testing::get<0>(param), |
| 186 | ::testing::get<1>(param), |
| 187 | ::testing::get<2>(param), |
| 188 | num_blocks, |
| 189 | kMinBlockSize, |
| 190 | kMaxBlockSize, |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 191 | block_density, |
| 192 | prng); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 197 | namespace { |
| 198 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 199 | #ifndef CERES_NO_SUITESPARSE |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 200 | INSTANTIATE_TEST_SUITE_P( |
| 201 | SuiteSparseCholesky, |
| 202 | SparseCholeskyTest, |
| 203 | ::testing::Combine(::testing::Values(SUITE_SPARSE), |
| 204 | ::testing::Values(OrderingType::AMD, |
| 205 | OrderingType::NATURAL), |
| 206 | ::testing::Values(true, false)), |
| 207 | ParamInfoToString); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 208 | #endif |
| 209 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 210 | #if !defined(CERES_NO_SUITESPARSE) && !defined(CERES_NO_CHOLMOD_PARTITION) |
| 211 | INSTANTIATE_TEST_SUITE_P( |
| 212 | SuiteSparseCholeskyMETIS, |
| 213 | SparseCholeskyTest, |
| 214 | ::testing::Combine(::testing::Values(SUITE_SPARSE), |
| 215 | ::testing::Values(OrderingType::NESDIS), |
| 216 | ::testing::Values(true, false)), |
| 217 | ParamInfoToString); |
| 218 | #endif // !defined(CERES_NO_SUITESPARSE) && |
| 219 | // !defined(CERES_NO_CHOLMOD_PARTITION) |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 220 | |
| 221 | #ifndef CERES_NO_ACCELERATE_SPARSE |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 222 | INSTANTIATE_TEST_SUITE_P( |
| 223 | AccelerateSparseCholesky, |
| 224 | SparseCholeskyTest, |
| 225 | ::testing::Combine(::testing::Values(ACCELERATE_SPARSE), |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 226 | ::testing::Values(OrderingType::AMD, |
| 227 | OrderingType::NESDIS, |
| 228 | OrderingType::NATURAL), |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 229 | ::testing::Values(true, false)), |
| 230 | ParamInfoToString); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 231 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 232 | INSTANTIATE_TEST_SUITE_P( |
| 233 | AccelerateSparseCholeskySingle, |
| 234 | SparseCholeskyTest, |
| 235 | ::testing::Combine(::testing::Values(ACCELERATE_SPARSE), |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 236 | ::testing::Values(OrderingType::AMD, |
| 237 | OrderingType::NESDIS, |
| 238 | OrderingType::NATURAL), |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 239 | ::testing::Values(true, false)), |
| 240 | ParamInfoToString); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 241 | #endif |
| 242 | |
| 243 | #ifdef CERES_USE_EIGEN_SPARSE |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 244 | INSTANTIATE_TEST_SUITE_P( |
| 245 | EigenSparseCholesky, |
| 246 | SparseCholeskyTest, |
| 247 | ::testing::Combine(::testing::Values(EIGEN_SPARSE), |
| 248 | ::testing::Values(OrderingType::AMD, |
| 249 | OrderingType::NATURAL), |
| 250 | ::testing::Values(true, false)), |
| 251 | ParamInfoToString); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 252 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 253 | INSTANTIATE_TEST_SUITE_P( |
| 254 | EigenSparseCholeskySingle, |
| 255 | SparseCholeskyTest, |
| 256 | ::testing::Combine(::testing::Values(EIGEN_SPARSE), |
| 257 | ::testing::Values(OrderingType::AMD, |
| 258 | OrderingType::NATURAL), |
| 259 | ::testing::Values(true, false)), |
| 260 | ParamInfoToString); |
| 261 | #endif // CERES_USE_EIGEN_SPARSE |
| 262 | |
| 263 | #if defined(CERES_USE_EIGEN_SPARSE) && !defined(CERES_NO_EIGEN_METIS) |
| 264 | INSTANTIATE_TEST_SUITE_P( |
| 265 | EigenSparseCholeskyMETIS, |
| 266 | SparseCholeskyTest, |
| 267 | ::testing::Combine(::testing::Values(EIGEN_SPARSE), |
| 268 | ::testing::Values(OrderingType::NESDIS), |
| 269 | ::testing::Values(true, false)), |
| 270 | ParamInfoToString); |
| 271 | |
| 272 | INSTANTIATE_TEST_SUITE_P( |
| 273 | EigenSparseCholeskySingleMETIS, |
| 274 | SparseCholeskyTest, |
| 275 | ::testing::Combine(::testing::Values(EIGEN_SPARSE), |
| 276 | ::testing::Values(OrderingType::NESDIS), |
| 277 | ::testing::Values(true, false)), |
| 278 | ParamInfoToString); |
| 279 | #endif // defined(CERES_USE_EIGEN_SPARSE) && !defined(CERES_NO_EIGEN_METIS) |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 280 | |
| 281 | class MockSparseCholesky : public SparseCholesky { |
| 282 | public: |
| 283 | MOCK_CONST_METHOD0(StorageType, CompressedRowSparseMatrix::StorageType()); |
| 284 | MOCK_METHOD2(Factorize, |
| 285 | LinearSolverTerminationType(CompressedRowSparseMatrix* lhs, |
| 286 | std::string* message)); |
| 287 | MOCK_METHOD3(Solve, |
| 288 | LinearSolverTerminationType(const double* rhs, |
| 289 | double* solution, |
| 290 | std::string* message)); |
| 291 | }; |
| 292 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 293 | class MockSparseIterativeRefiner : public SparseIterativeRefiner { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 294 | public: |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 295 | MockSparseIterativeRefiner() : SparseIterativeRefiner(1) {} |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 296 | MOCK_METHOD4(Refine, |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 297 | void(const SparseMatrix& lhs, |
| 298 | const double* rhs, |
| 299 | SparseCholesky* sparse_cholesky, |
| 300 | double* solution)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 301 | }; |
| 302 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 303 | using testing::_; |
| 304 | using testing::Return; |
| 305 | |
| 306 | TEST(RefinedSparseCholesky, StorageType) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 307 | auto sparse_cholesky = std::make_unique<MockSparseCholesky>(); |
| 308 | auto iterative_refiner = std::make_unique<MockSparseIterativeRefiner>(); |
| 309 | EXPECT_CALL(*sparse_cholesky, StorageType()) |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 310 | .Times(1) |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 311 | .WillRepeatedly( |
| 312 | Return(CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR)); |
| 313 | EXPECT_CALL(*iterative_refiner, Refine(_, _, _, _)).Times(0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 314 | RefinedSparseCholesky refined_sparse_cholesky(std::move(sparse_cholesky), |
| 315 | std::move(iterative_refiner)); |
| 316 | EXPECT_EQ(refined_sparse_cholesky.StorageType(), |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 317 | CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | TEST(RefinedSparseCholesky, Factorize) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 321 | auto* mock_sparse_cholesky = new MockSparseCholesky; |
| 322 | auto* mock_iterative_refiner = new MockSparseIterativeRefiner; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 323 | EXPECT_CALL(*mock_sparse_cholesky, Factorize(_, _)) |
| 324 | .Times(1) |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 325 | .WillRepeatedly(Return(LinearSolverTerminationType::SUCCESS)); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 326 | EXPECT_CALL(*mock_iterative_refiner, Refine(_, _, _, _)).Times(0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 327 | std::unique_ptr<SparseCholesky> sparse_cholesky(mock_sparse_cholesky); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 328 | std::unique_ptr<SparseIterativeRefiner> iterative_refiner( |
| 329 | mock_iterative_refiner); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 330 | RefinedSparseCholesky refined_sparse_cholesky(std::move(sparse_cholesky), |
| 331 | std::move(iterative_refiner)); |
| 332 | CompressedRowSparseMatrix m(1, 1, 1); |
| 333 | std::string message; |
| 334 | EXPECT_EQ(refined_sparse_cholesky.Factorize(&m, &message), |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 335 | LinearSolverTerminationType::SUCCESS); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | TEST(RefinedSparseCholesky, FactorAndSolveWithUnsuccessfulFactorization) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 339 | auto* mock_sparse_cholesky = new MockSparseCholesky; |
| 340 | auto* mock_iterative_refiner = new MockSparseIterativeRefiner; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 341 | EXPECT_CALL(*mock_sparse_cholesky, Factorize(_, _)) |
| 342 | .Times(1) |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 343 | .WillRepeatedly(Return(LinearSolverTerminationType::FAILURE)); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 344 | EXPECT_CALL(*mock_sparse_cholesky, Solve(_, _, _)).Times(0); |
| 345 | EXPECT_CALL(*mock_iterative_refiner, Refine(_, _, _, _)).Times(0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 346 | std::unique_ptr<SparseCholesky> sparse_cholesky(mock_sparse_cholesky); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 347 | std::unique_ptr<SparseIterativeRefiner> iterative_refiner( |
| 348 | mock_iterative_refiner); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 349 | RefinedSparseCholesky refined_sparse_cholesky(std::move(sparse_cholesky), |
| 350 | std::move(iterative_refiner)); |
| 351 | CompressedRowSparseMatrix m(1, 1, 1); |
| 352 | std::string message; |
| 353 | double rhs; |
| 354 | double solution; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 355 | EXPECT_EQ( |
| 356 | refined_sparse_cholesky.FactorAndSolve(&m, &rhs, &solution, &message), |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 357 | LinearSolverTerminationType::FAILURE); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 358 | }; |
| 359 | |
| 360 | TEST(RefinedSparseCholesky, FactorAndSolveWithSuccess) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 361 | auto* mock_sparse_cholesky = new MockSparseCholesky; |
| 362 | std::unique_ptr<MockSparseIterativeRefiner> mock_iterative_refiner( |
| 363 | new MockSparseIterativeRefiner); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 364 | EXPECT_CALL(*mock_sparse_cholesky, Factorize(_, _)) |
| 365 | .Times(1) |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 366 | .WillRepeatedly(Return(LinearSolverTerminationType::SUCCESS)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 367 | EXPECT_CALL(*mock_sparse_cholesky, Solve(_, _, _)) |
| 368 | .Times(1) |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 369 | .WillRepeatedly(Return(LinearSolverTerminationType::SUCCESS)); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 370 | EXPECT_CALL(*mock_iterative_refiner, Refine(_, _, _, _)).Times(1); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 371 | |
| 372 | std::unique_ptr<SparseCholesky> sparse_cholesky(mock_sparse_cholesky); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 373 | std::unique_ptr<SparseIterativeRefiner> iterative_refiner( |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 374 | std::move(mock_iterative_refiner)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 375 | RefinedSparseCholesky refined_sparse_cholesky(std::move(sparse_cholesky), |
| 376 | std::move(iterative_refiner)); |
| 377 | CompressedRowSparseMatrix m(1, 1, 1); |
| 378 | std::string message; |
| 379 | double rhs; |
| 380 | double solution; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 381 | EXPECT_EQ( |
| 382 | refined_sparse_cholesky.FactorAndSolve(&m, &rhs, &solution, &message), |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 383 | LinearSolverTerminationType::SUCCESS); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 384 | }; |
| 385 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 386 | } // namespace |
| 387 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 388 | } // namespace ceres::internal |