Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2015 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: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | |
| 31 | #include "ceres/iterative_schur_complement_solver.h" |
| 32 | |
| 33 | #include <algorithm> |
| 34 | #include <cstring> |
| 35 | #include <vector> |
| 36 | |
| 37 | #include "Eigen/Dense" |
| 38 | #include "ceres/block_sparse_matrix.h" |
| 39 | #include "ceres/block_structure.h" |
| 40 | #include "ceres/conjugate_gradients_solver.h" |
| 41 | #include "ceres/detect_structure.h" |
| 42 | #include "ceres/implicit_schur_complement.h" |
| 43 | #include "ceres/internal/eigen.h" |
| 44 | #include "ceres/linear_solver.h" |
| 45 | #include "ceres/preconditioner.h" |
| 46 | #include "ceres/schur_jacobi_preconditioner.h" |
| 47 | #include "ceres/triplet_sparse_matrix.h" |
| 48 | #include "ceres/types.h" |
| 49 | #include "ceres/visibility_based_preconditioner.h" |
| 50 | #include "ceres/wall_time.h" |
| 51 | #include "glog/logging.h" |
| 52 | |
| 53 | namespace ceres { |
| 54 | namespace internal { |
| 55 | |
| 56 | IterativeSchurComplementSolver::IterativeSchurComplementSolver( |
| 57 | const LinearSolver::Options& options) |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 58 | : options_(options) {} |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 59 | |
| 60 | IterativeSchurComplementSolver::~IterativeSchurComplementSolver() {} |
| 61 | |
| 62 | LinearSolver::Summary IterativeSchurComplementSolver::SolveImpl( |
| 63 | BlockSparseMatrix* A, |
| 64 | const double* b, |
| 65 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 66 | double* x) { |
| 67 | EventLogger event_logger("IterativeSchurComplementSolver::Solve"); |
| 68 | |
| 69 | CHECK(A->block_structure() != nullptr); |
| 70 | const int num_eliminate_blocks = options_.elimination_groups[0]; |
| 71 | // Initialize a ImplicitSchurComplement object. |
| 72 | if (schur_complement_ == NULL) { |
| 73 | DetectStructure(*(A->block_structure()), |
| 74 | num_eliminate_blocks, |
| 75 | &options_.row_block_size, |
| 76 | &options_.e_block_size, |
| 77 | &options_.f_block_size); |
| 78 | schur_complement_.reset(new ImplicitSchurComplement(options_)); |
| 79 | } |
| 80 | schur_complement_->Init(*A, per_solve_options.D, b); |
| 81 | |
| 82 | const int num_schur_complement_blocks = |
| 83 | A->block_structure()->cols.size() - num_eliminate_blocks; |
| 84 | if (num_schur_complement_blocks == 0) { |
| 85 | VLOG(2) << "No parameter blocks left in the schur complement."; |
| 86 | LinearSolver::Summary summary; |
| 87 | summary.num_iterations = 0; |
| 88 | summary.termination_type = LINEAR_SOLVER_SUCCESS; |
| 89 | schur_complement_->BackSubstitute(NULL, x); |
| 90 | return summary; |
| 91 | } |
| 92 | |
| 93 | // Initialize the solution to the Schur complement system to zero. |
| 94 | reduced_linear_system_solution_.resize(schur_complement_->num_rows()); |
| 95 | reduced_linear_system_solution_.setZero(); |
| 96 | |
| 97 | LinearSolver::Options cg_options; |
| 98 | cg_options.min_num_iterations = options_.min_num_iterations; |
| 99 | cg_options.max_num_iterations = options_.max_num_iterations; |
| 100 | ConjugateGradientsSolver cg_solver(cg_options); |
| 101 | |
| 102 | LinearSolver::PerSolveOptions cg_per_solve_options; |
| 103 | cg_per_solve_options.r_tolerance = per_solve_options.r_tolerance; |
| 104 | cg_per_solve_options.q_tolerance = per_solve_options.q_tolerance; |
| 105 | |
| 106 | CreatePreconditioner(A); |
| 107 | if (preconditioner_.get() != NULL) { |
| 108 | if (!preconditioner_->Update(*A, per_solve_options.D)) { |
| 109 | LinearSolver::Summary summary; |
| 110 | summary.num_iterations = 0; |
| 111 | summary.termination_type = LINEAR_SOLVER_FAILURE; |
| 112 | summary.message = "Preconditioner update failed."; |
| 113 | return summary; |
| 114 | } |
| 115 | |
| 116 | cg_per_solve_options.preconditioner = preconditioner_.get(); |
| 117 | } |
| 118 | |
| 119 | event_logger.AddEvent("Setup"); |
| 120 | LinearSolver::Summary summary = |
| 121 | cg_solver.Solve(schur_complement_.get(), |
| 122 | schur_complement_->rhs().data(), |
| 123 | cg_per_solve_options, |
| 124 | reduced_linear_system_solution_.data()); |
| 125 | if (summary.termination_type != LINEAR_SOLVER_FAILURE && |
| 126 | summary.termination_type != LINEAR_SOLVER_FATAL_ERROR) { |
| 127 | schur_complement_->BackSubstitute(reduced_linear_system_solution_.data(), |
| 128 | x); |
| 129 | } |
| 130 | event_logger.AddEvent("Solve"); |
| 131 | return summary; |
| 132 | } |
| 133 | |
| 134 | void IterativeSchurComplementSolver::CreatePreconditioner( |
| 135 | BlockSparseMatrix* A) { |
| 136 | if (options_.preconditioner_type == IDENTITY || |
| 137 | preconditioner_.get() != NULL) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | Preconditioner::Options preconditioner_options; |
| 142 | preconditioner_options.type = options_.preconditioner_type; |
| 143 | preconditioner_options.visibility_clustering_type = |
| 144 | options_.visibility_clustering_type; |
| 145 | preconditioner_options.sparse_linear_algebra_library_type = |
| 146 | options_.sparse_linear_algebra_library_type; |
| 147 | preconditioner_options.num_threads = options_.num_threads; |
| 148 | preconditioner_options.row_block_size = options_.row_block_size; |
| 149 | preconditioner_options.e_block_size = options_.e_block_size; |
| 150 | preconditioner_options.f_block_size = options_.f_block_size; |
| 151 | preconditioner_options.elimination_groups = options_.elimination_groups; |
| 152 | CHECK(options_.context != NULL); |
| 153 | preconditioner_options.context = options_.context; |
| 154 | |
| 155 | switch (options_.preconditioner_type) { |
| 156 | case JACOBI: |
| 157 | preconditioner_.reset(new SparseMatrixPreconditionerWrapper( |
| 158 | schur_complement_->block_diagonal_FtF_inverse())); |
| 159 | break; |
| 160 | case SCHUR_JACOBI: |
| 161 | preconditioner_.reset(new SchurJacobiPreconditioner( |
| 162 | *A->block_structure(), preconditioner_options)); |
| 163 | break; |
| 164 | case CLUSTER_JACOBI: |
| 165 | case CLUSTER_TRIDIAGONAL: |
| 166 | preconditioner_.reset(new VisibilityBasedPreconditioner( |
| 167 | *A->block_structure(), preconditioner_options)); |
| 168 | break; |
| 169 | default: |
| 170 | LOG(FATAL) << "Unknown Preconditioner Type"; |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | } // namespace internal |
| 175 | } // namespace ceres |