blob: cf5562ac77183a0f8875bb75cac29a33625f012b [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// 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 Agarwal42a84b82013-02-01 12:22:53 -080036
Keir Mierle8ebb0732012-04-30 23:09:08 -070037#include "Eigen/Dense"
38#include "ceres/block_sparse_matrix.h"
39#include "ceres/block_structure.h"
Keir Mierlef7898fb2012-05-05 20:55:08 -070040#include "ceres/conjugate_gradients_solver.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070041#include "ceres/implicit_schur_complement.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070042#include "ceres/internal/eigen.h"
43#include "ceres/internal/scoped_ptr.h"
Keir Mierlef7898fb2012-05-05 20:55:08 -070044#include "ceres/linear_solver.h"
Sameer Agarwal9a88bd72013-02-19 13:09:12 -080045#include "ceres/preconditioner.h"
Sameer Agarwal290b9752013-02-17 16:50:37 -080046#include "ceres/schur_jacobi_preconditioner.h"
Keir Mierlef7898fb2012-05-05 20:55:08 -070047#include "ceres/triplet_sparse_matrix.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070048#include "ceres/types.h"
Keir Mierlef7898fb2012-05-05 20:55:08 -070049#include "ceres/visibility_based_preconditioner.h"
Sameer Agarwal42a84b82013-02-01 12:22:53 -080050#include "ceres/wall_time.h"
Sameer Agarwal0beab862012-08-13 15:12:01 -070051#include "glog/logging.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070052
53namespace ceres {
54namespace internal {
55
56IterativeSchurComplementSolver::IterativeSchurComplementSolver(
57 const LinearSolver::Options& options)
58 : options_(options) {
59}
60
61IterativeSchurComplementSolver::~IterativeSchurComplementSolver() {
62}
63
64LinearSolver::Summary IterativeSchurComplementSolver::SolveImpl(
65 BlockSparseMatrixBase* A,
66 const double* b,
67 const LinearSolver::PerSolveOptions& per_solve_options,
68 double* x) {
Sameer Agarwal42a84b82013-02-01 12:22:53 -080069 EventLogger event_logger("IterativeSchurComplementSolver::Solve");
70
Keir Mierle8ebb0732012-04-30 23:09:08 -070071 CHECK_NOTNULL(A->block_structure());
72
73 // Initialize a ImplicitSchurComplement object.
Sameer Agarwala9d8ef82012-05-14 02:28:05 -070074 if (schur_complement_ == NULL) {
Keir Mierle8ebb0732012-04-30 23:09:08 -070075 schur_complement_.reset(
Sameer Agarwal0c52f1e2012-09-17 11:30:14 -070076 new ImplicitSchurComplement(options_.elimination_groups[0],
Keir Mierle8ebb0732012-04-30 23:09:08 -070077 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 Mierle8ebb0732012-04-30 23:09:08 -070091 cg_options.max_num_iterations = options_.max_num_iterations;
Keir Mierlef7898fb2012-05-05 20:55:08 -070092 ConjugateGradientsSolver cg_solver(cg_options);
Keir Mierle8ebb0732012-04-30 23:09:08 -070093 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 Agarwal290b9752013-02-17 16:50:37 -080098 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 Mierle8ebb0732012-04-30 23:09:08 -0700109 switch (options_.preconditioner_type) {
110 case IDENTITY:
Keir Mierle8ebb0732012-04-30 23:09:08 -0700111 break;
112 case JACOBI:
Sameer Agarwal290b9752013-02-17 16:50:37 -0800113 preconditioner_.reset(
114 new SparseMatrixPreconditionerWrapper(
115 schur_complement_->block_diagonal_FtF_inverse()));
Keir Mierle8ebb0732012-04-30 23:09:08 -0700116 break;
117 case SCHUR_JACOBI:
Sameer Agarwal290b9752013-02-17 16:50:37 -0800118 if (preconditioner_.get() == NULL) {
119 preconditioner_.reset(
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800120 new SchurJacobiPreconditioner(
121 *A->block_structure(), preconditioner_options));
Sameer Agarwal290b9752013-02-17 16:50:37 -0800122 }
123 break;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700124 case CLUSTER_JACOBI:
125 case CLUSTER_TRIDIAGONAL:
Sameer Agarwal290b9752013-02-17 16:50:37 -0800126 if (preconditioner_.get() == NULL) {
127 preconditioner_.reset(
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800128 new VisibilityBasedPreconditioner(
129 *A->block_structure(), preconditioner_options));
Keir Mierle8ebb0732012-04-30 23:09:08 -0700130 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700131 break;
132 default:
133 LOG(FATAL) << "Unknown Preconditioner Type";
134 }
Sameer Agarwal290b9752013-02-17 16:50:37 -0800135
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 Agarwal42a84b82013-02-01 12:22:53 -0800143 event_logger.AddEvent("Setup");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700144
145 LinearSolver::Summary cg_summary;
146 cg_summary.num_iterations = 0;
147 cg_summary.termination_type = FAILURE;
148
Sameer Agarwal290b9752013-02-17 16:50:37 -0800149 if (preconditioner_update_was_successful) {
Keir Mierlef7898fb2012-05-05 20:55:08 -0700150 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 Mierle8ebb0732012-04-30 23:09:08 -0700154 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 Agarwal42a84b82013-02-01 12:22:53 -0800161
162 event_logger.AddEvent("Solve");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700163 return cg_summary;
164}
165
166} // namespace internal
167} // namespace ceres