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" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 41 | #include "ceres/implicit_schur_complement.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 42 | #include "ceres/internal/eigen.h" |
| 43 | #include "ceres/internal/scoped_ptr.h" |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 44 | #include "ceres/linear_solver.h" |
Sameer Agarwal | 9a88bd7 | 2013-02-19 13:09:12 -0800 | [diff] [blame] | 45 | #include "ceres/preconditioner.h" |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 46 | #include "ceres/schur_jacobi_preconditioner.h" |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 47 | #include "ceres/triplet_sparse_matrix.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 48 | #include "ceres/types.h" |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 49 | #include "ceres/visibility_based_preconditioner.h" |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 50 | #include "ceres/wall_time.h" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 51 | #include "glog/logging.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 52 | |
| 53 | namespace ceres { |
| 54 | namespace internal { |
| 55 | |
| 56 | IterativeSchurComplementSolver::IterativeSchurComplementSolver( |
| 57 | const LinearSolver::Options& options) |
| 58 | : options_(options) { |
| 59 | } |
| 60 | |
| 61 | IterativeSchurComplementSolver::~IterativeSchurComplementSolver() { |
| 62 | } |
| 63 | |
| 64 | LinearSolver::Summary IterativeSchurComplementSolver::SolveImpl( |
| 65 | BlockSparseMatrixBase* A, |
| 66 | const double* b, |
| 67 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 68 | double* x) { |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 69 | EventLogger event_logger("IterativeSchurComplementSolver::Solve"); |
| 70 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 71 | CHECK_NOTNULL(A->block_structure()); |
| 72 | |
| 73 | // Initialize a ImplicitSchurComplement object. |
Sameer Agarwal | a9d8ef8 | 2012-05-14 02:28:05 -0700 | [diff] [blame] | 74 | if (schur_complement_ == NULL) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 75 | schur_complement_.reset( |
Sameer Agarwal | 0c52f1e | 2012-09-17 11:30:14 -0700 | [diff] [blame] | 76 | new ImplicitSchurComplement(options_.elimination_groups[0], |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 77 | options_.preconditioner_type == JACOBI)); |
| 78 | } |
| 79 | schur_complement_->Init(*A, per_solve_options.D, b); |
| 80 | |
| 81 | // Initialize the solution to the Schur complement system to zero. |
| 82 | // |
| 83 | // TODO(sameeragarwal): There maybe a better initialization than an |
| 84 | // all zeros solution. Explore other cheap starting points. |
| 85 | reduced_linear_system_solution_.resize(schur_complement_->num_rows()); |
| 86 | reduced_linear_system_solution_.setZero(); |
| 87 | |
| 88 | // Instantiate a conjugate gradient solver that runs on the Schur complement |
| 89 | // matrix with the block diagonal of the matrix F'F as the preconditioner. |
| 90 | LinearSolver::Options cg_options; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 91 | cg_options.max_num_iterations = options_.max_num_iterations; |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 92 | ConjugateGradientsSolver cg_solver(cg_options); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 93 | LinearSolver::PerSolveOptions cg_per_solve_options; |
| 94 | |
| 95 | cg_per_solve_options.r_tolerance = per_solve_options.r_tolerance; |
| 96 | cg_per_solve_options.q_tolerance = per_solve_options.q_tolerance; |
| 97 | |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 98 | Preconditioner::Options preconditioner_options; |
| 99 | preconditioner_options.type = options_.preconditioner_type; |
| 100 | preconditioner_options.sparse_linear_algebra_library = |
| 101 | options_.sparse_linear_algebra_library; |
| 102 | preconditioner_options.use_block_amd = options_.use_block_amd; |
| 103 | preconditioner_options.num_threads = options_.num_threads; |
| 104 | preconditioner_options.row_block_size = options_.row_block_size; |
| 105 | preconditioner_options.e_block_size = options_.e_block_size; |
| 106 | preconditioner_options.f_block_size = options_.f_block_size; |
| 107 | preconditioner_options.elimination_groups = options_.elimination_groups; |
| 108 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 109 | switch (options_.preconditioner_type) { |
| 110 | case IDENTITY: |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 111 | break; |
| 112 | case JACOBI: |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 113 | preconditioner_.reset( |
| 114 | new SparseMatrixPreconditionerWrapper( |
| 115 | schur_complement_->block_diagonal_FtF_inverse())); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 116 | break; |
| 117 | case SCHUR_JACOBI: |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 118 | if (preconditioner_.get() == NULL) { |
| 119 | preconditioner_.reset( |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 120 | new SchurJacobiPreconditioner( |
| 121 | *A->block_structure(), preconditioner_options)); |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 122 | } |
| 123 | break; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 124 | case CLUSTER_JACOBI: |
| 125 | case CLUSTER_TRIDIAGONAL: |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 126 | if (preconditioner_.get() == NULL) { |
| 127 | preconditioner_.reset( |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 128 | new VisibilityBasedPreconditioner( |
| 129 | *A->block_structure(), preconditioner_options)); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 130 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 131 | break; |
| 132 | default: |
| 133 | LOG(FATAL) << "Unknown Preconditioner Type"; |
| 134 | } |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 135 | |
| 136 | bool preconditioner_update_was_successful = true; |
| 137 | if (preconditioner_.get() != NULL) { |
| 138 | preconditioner_update_was_successful = |
| 139 | preconditioner_->Update(*A, per_solve_options.D); |
| 140 | cg_per_solve_options.preconditioner = preconditioner_.get(); |
| 141 | } |
| 142 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 143 | event_logger.AddEvent("Setup"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 144 | |
| 145 | LinearSolver::Summary cg_summary; |
| 146 | cg_summary.num_iterations = 0; |
| 147 | cg_summary.termination_type = FAILURE; |
| 148 | |
Sameer Agarwal | 290b975 | 2013-02-17 16:50:37 -0800 | [diff] [blame] | 149 | if (preconditioner_update_was_successful) { |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 150 | cg_summary = cg_solver.Solve(schur_complement_.get(), |
| 151 | schur_complement_->rhs().data(), |
| 152 | cg_per_solve_options, |
| 153 | reduced_linear_system_solution_.data()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 154 | if (cg_summary.termination_type != FAILURE) { |
| 155 | schur_complement_->BackSubstitute( |
| 156 | reduced_linear_system_solution_.data(), x); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | VLOG(2) << "CG Iterations : " << cg_summary.num_iterations; |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 161 | |
| 162 | event_logger.AddEvent("Solve"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 163 | return cg_summary; |
| 164 | } |
| 165 | |
| 166 | } // namespace internal |
| 167 | } // namespace ceres |