Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2010, 2011, 2012 Google Inc. All rights reserved. |
| 3 | // http://code.google.com/p/ceres-solver/ |
| 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> |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 36 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 37 | #include "Eigen/Dense" |
| 38 | #include "ceres/block_sparse_matrix.h" |
| 39 | #include "ceres/block_structure.h" |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 40 | #include "ceres/conjugate_gradients_solver.h" |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 41 | #include "ceres/detect_structure.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 42 | #include "ceres/implicit_schur_complement.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 43 | #include "ceres/internal/eigen.h" |
| 44 | #include "ceres/internal/scoped_ptr.h" |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 45 | #include "ceres/linear_solver.h" |
Sameer Agarwal | 9a88bd7 | 2013-02-19 13:09:12 -0800 | [diff] [blame] | 46 | #include "ceres/preconditioner.h" |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 47 | #include "ceres/schur_jacobi_preconditioner.h" |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 48 | #include "ceres/triplet_sparse_matrix.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 49 | #include "ceres/types.h" |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 50 | #include "ceres/visibility_based_preconditioner.h" |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 51 | #include "ceres/wall_time.h" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 52 | #include "glog/logging.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 53 | |
| 54 | namespace ceres { |
| 55 | namespace internal { |
| 56 | |
| 57 | IterativeSchurComplementSolver::IterativeSchurComplementSolver( |
| 58 | const LinearSolver::Options& options) |
| 59 | : options_(options) { |
| 60 | } |
| 61 | |
| 62 | IterativeSchurComplementSolver::~IterativeSchurComplementSolver() { |
| 63 | } |
| 64 | |
| 65 | LinearSolver::Summary IterativeSchurComplementSolver::SolveImpl( |
Sameer Agarwal | c1e10d9 | 2013-04-24 11:58:24 -0700 | [diff] [blame] | 66 | BlockSparseMatrix* A, |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 67 | const double* b, |
| 68 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 69 | double* x) { |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 70 | EventLogger event_logger("IterativeSchurComplementSolver::Solve"); |
| 71 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 72 | CHECK_NOTNULL(A->block_structure()); |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 73 | const int num_eliminate_blocks = options_.elimination_groups[0]; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 74 | // Initialize a ImplicitSchurComplement object. |
Sameer Agarwal | a9d8ef8 | 2012-05-14 02:28:05 -0700 | [diff] [blame] | 75 | if (schur_complement_ == NULL) { |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 76 | DetectStructure(*(A->block_structure()), |
| 77 | num_eliminate_blocks, |
| 78 | &options_.row_block_size, |
| 79 | &options_.e_block_size, |
| 80 | &options_.f_block_size); |
| 81 | schur_complement_.reset(new ImplicitSchurComplement(options_)); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 82 | } |
| 83 | schur_complement_->Init(*A, per_solve_options.D, b); |
| 84 | |
Sameer Agarwal | 0338f9a | 2013-09-02 22:28:40 -0700 | [diff] [blame] | 85 | const int num_schur_complement_blocks = |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 86 | A->block_structure()->cols.size() - num_eliminate_blocks; |
Sameer Agarwal | 0338f9a | 2013-09-02 22:28:40 -0700 | [diff] [blame] | 87 | if (num_schur_complement_blocks == 0) { |
| 88 | VLOG(2) << "No parameter blocks left in the schur complement."; |
| 89 | LinearSolver::Summary cg_summary; |
| 90 | cg_summary.num_iterations = 0; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 91 | cg_summary.termination_type = LINEAR_SOLVER_SUCCESS; |
Sameer Agarwal | 0338f9a | 2013-09-02 22:28:40 -0700 | [diff] [blame] | 92 | schur_complement_->BackSubstitute(NULL, x); |
| 93 | return cg_summary; |
| 94 | } |
| 95 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 96 | // Initialize the solution to the Schur complement system to zero. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 97 | reduced_linear_system_solution_.resize(schur_complement_->num_rows()); |
| 98 | reduced_linear_system_solution_.setZero(); |
| 99 | |
Sameer Agarwal | 5a161a2 | 2013-10-29 22:08:15 -0700 | [diff] [blame] | 100 | // Instantiate a conjugate gradient solver that runs on the Schur |
| 101 | // complement matrix with the block diagonal of the matrix F'F as |
| 102 | // the preconditioner. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 103 | LinearSolver::Options cg_options; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 104 | cg_options.max_num_iterations = options_.max_num_iterations; |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 105 | ConjugateGradientsSolver cg_solver(cg_options); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 106 | LinearSolver::PerSolveOptions cg_per_solve_options; |
| 107 | |
| 108 | cg_per_solve_options.r_tolerance = per_solve_options.r_tolerance; |
| 109 | cg_per_solve_options.q_tolerance = per_solve_options.q_tolerance; |
| 110 | |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 111 | Preconditioner::Options preconditioner_options; |
| 112 | preconditioner_options.type = options_.preconditioner_type; |
Sameer Agarwal | f06b9fa | 2013-10-27 21:38:13 -0700 | [diff] [blame] | 113 | preconditioner_options.visibility_clustering_type = |
| 114 | options_.visibility_clustering_type; |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 115 | preconditioner_options.sparse_linear_algebra_library_type = |
| 116 | options_.sparse_linear_algebra_library_type; |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 117 | preconditioner_options.num_threads = options_.num_threads; |
| 118 | preconditioner_options.row_block_size = options_.row_block_size; |
| 119 | preconditioner_options.e_block_size = options_.e_block_size; |
| 120 | preconditioner_options.f_block_size = options_.f_block_size; |
| 121 | preconditioner_options.elimination_groups = options_.elimination_groups; |
| 122 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 123 | switch (options_.preconditioner_type) { |
| 124 | case IDENTITY: |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 125 | break; |
| 126 | case JACOBI: |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 127 | preconditioner_.reset( |
| 128 | new SparseMatrixPreconditionerWrapper( |
| 129 | schur_complement_->block_diagonal_FtF_inverse())); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 130 | break; |
| 131 | case SCHUR_JACOBI: |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 132 | if (preconditioner_.get() == NULL) { |
| 133 | preconditioner_.reset( |
Sameer Agarwal | 0338f9a | 2013-09-02 22:28:40 -0700 | [diff] [blame] | 134 | new SchurJacobiPreconditioner(*A->block_structure(), |
| 135 | preconditioner_options)); |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 136 | } |
| 137 | break; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 138 | case CLUSTER_JACOBI: |
| 139 | case CLUSTER_TRIDIAGONAL: |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 140 | if (preconditioner_.get() == NULL) { |
| 141 | preconditioner_.reset( |
Sameer Agarwal | 0338f9a | 2013-09-02 22:28:40 -0700 | [diff] [blame] | 142 | new VisibilityBasedPreconditioner(*A->block_structure(), |
| 143 | preconditioner_options)); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 144 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 145 | break; |
| 146 | default: |
| 147 | LOG(FATAL) << "Unknown Preconditioner Type"; |
| 148 | } |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 149 | |
| 150 | bool preconditioner_update_was_successful = true; |
| 151 | if (preconditioner_.get() != NULL) { |
| 152 | preconditioner_update_was_successful = |
| 153 | preconditioner_->Update(*A, per_solve_options.D); |
| 154 | cg_per_solve_options.preconditioner = preconditioner_.get(); |
| 155 | } |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 156 | event_logger.AddEvent("Setup"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 157 | |
| 158 | LinearSolver::Summary cg_summary; |
| 159 | cg_summary.num_iterations = 0; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 160 | cg_summary.termination_type = LINEAR_SOLVER_FAILURE; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 161 | |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 162 | // TODO(sameeragarwal): Refactor preconditioners to return a more |
Sameer Agarwal | 89a592f | 2013-11-26 11:35:49 -0800 | [diff] [blame] | 163 | // sane message. |
| 164 | cg_summary.message = "Preconditioner update failed."; |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 165 | if (preconditioner_update_was_successful) { |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 166 | cg_summary = cg_solver.Solve(schur_complement_.get(), |
| 167 | schur_complement_->rhs().data(), |
| 168 | cg_per_solve_options, |
| 169 | reduced_linear_system_solution_.data()); |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 170 | if (cg_summary.termination_type != LINEAR_SOLVER_FAILURE && |
| 171 | cg_summary.termination_type != LINEAR_SOLVER_FATAL_ERROR) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 172 | schur_complement_->BackSubstitute( |
| 173 | reduced_linear_system_solution_.data(), x); |
| 174 | } |
| 175 | } |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 176 | event_logger.AddEvent("Solve"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 177 | return cg_summary; |
| 178 | } |
| 179 | |
| 180 | } // namespace internal |
| 181 | } // namespace ceres |