blob: 6fae2042ff78188d2dd51def89c6e1fb07a65555 [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
Keir Mierle8ebb0732012-04-30 23:09:08 -070031#include "ceres/sparse_normal_cholesky_solver.h"
32
33#include <algorithm>
34#include <cstring>
35#include <ctime>
Sameer Agarwalb0518732012-05-29 00:27:57 -070036
37#ifndef CERES_NO_CXSPARSE
38#include "cs.h"
39#endif
40
Keir Mierle8ebb0732012-04-30 23:09:08 -070041#include "ceres/compressed_row_sparse_matrix.h"
Sameer Agarwal42a84b82013-02-01 12:22:53 -080042#include "ceres/internal/eigen.h"
43#include "ceres/internal/scoped_ptr.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070044#include "ceres/linear_solver.h"
45#include "ceres/suitesparse.h"
46#include "ceres/triplet_sparse_matrix.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070047#include "ceres/types.h"
Sameer Agarwal42a84b82013-02-01 12:22:53 -080048#include "ceres/wall_time.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070049
50namespace ceres {
51namespace internal {
52
53SparseNormalCholeskySolver::SparseNormalCholeskySolver(
54 const LinearSolver::Options& options)
Sameer Agarwalb0518732012-05-29 00:27:57 -070055 : options_(options) {
56#ifndef CERES_NO_SUITESPARSE
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -070057 factor_ = NULL;
Sameer Agarwalb0518732012-05-29 00:27:57 -070058#endif
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070059
60#ifndef CERES_NO_CXSPARSE
61 cxsparse_factor_ = NULL;
62#endif // CERES_NO_CXSPARSE
Sameer Agarwalb0518732012-05-29 00:27:57 -070063}
Keir Mierle8ebb0732012-04-30 23:09:08 -070064
65SparseNormalCholeskySolver::~SparseNormalCholeskySolver() {
Sameer Agarwalb0518732012-05-29 00:27:57 -070066#ifndef CERES_NO_SUITESPARSE
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -070067 if (factor_ != NULL) {
68 ss_.Free(factor_);
69 factor_ = NULL;
Keir Mierle8ebb0732012-04-30 23:09:08 -070070 }
Sameer Agarwalb0518732012-05-29 00:27:57 -070071#endif
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070072
73#ifndef CERES_NO_CXSPARSE
74 if (cxsparse_factor_ != NULL) {
75 cxsparse_.Free(cxsparse_factor_);
76 cxsparse_factor_ = NULL;
77 }
78#endif // CERES_NO_CXSPARSE
Keir Mierle8ebb0732012-04-30 23:09:08 -070079}
80
81LinearSolver::Summary SparseNormalCholeskySolver::SolveImpl(
82 CompressedRowSparseMatrix* A,
83 const double* b,
84 const LinearSolver::PerSolveOptions& per_solve_options,
85 double * x) {
Sameer Agarwalb0518732012-05-29 00:27:57 -070086 switch (options_.sparse_linear_algebra_library) {
87 case SUITE_SPARSE:
88 return SolveImplUsingSuiteSparse(A, b, per_solve_options, x);
89 case CX_SPARSE:
90 return SolveImplUsingCXSparse(A, b, per_solve_options, x);
91 default:
92 LOG(FATAL) << "Unknown sparse linear algebra library : "
93 << options_.sparse_linear_algebra_library;
94 }
95
96 LOG(FATAL) << "Unknown sparse linear algebra library : "
97 << options_.sparse_linear_algebra_library;
98 return LinearSolver::Summary();
99}
100
101#ifndef CERES_NO_CXSPARSE
102LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingCXSparse(
103 CompressedRowSparseMatrix* A,
104 const double* b,
105 const LinearSolver::PerSolveOptions& per_solve_options,
106 double * x) {
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800107 EventLogger event_logger("SparseNormalCholeskySolver::CXSparse::Solve");
108
Sameer Agarwalb0518732012-05-29 00:27:57 -0700109 LinearSolver::Summary summary;
110 summary.num_iterations = 1;
111 const int num_cols = A->num_cols();
112 Vector Atb = Vector::Zero(num_cols);
113 A->LeftMultiply(b, Atb.data());
114
115 if (per_solve_options.D != NULL) {
116 // Temporarily append a diagonal block to the A matrix, but undo
117 // it before returning the matrix to the user.
118 CompressedRowSparseMatrix D(per_solve_options.D, num_cols);
119 A->AppendRows(D);
120 }
121
122 VectorRef(x, num_cols).setZero();
123
124 // Wrap the augmented Jacobian in a compressed sparse column matrix.
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700125 cs_di At = cxsparse_.CreateSparseMatrixTransposeView(A);
Sameer Agarwalb0518732012-05-29 00:27:57 -0700126
127 // Compute the normal equations. J'J delta = J'f and solve them
128 // using a sparse Cholesky factorization. Notice that when compared
129 // to SuiteSparse we have to explicitly compute the transpose of Jt,
130 // and then the normal equations before they can be
131 // factorized. CHOLMOD/SuiteSparse on the other hand can just work
132 // off of Jt to compute the Cholesky factorization of the normal
133 // equations.
134 cs_di* A2 = cs_transpose(&At, 1);
135 cs_di* AtA = cs_multiply(&At,A2);
136
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700137 cxsparse_.Free(A2);
Sameer Agarwalb0518732012-05-29 00:27:57 -0700138 if (per_solve_options.D != NULL) {
139 A->DeleteRows(num_cols);
140 }
141
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800142 event_logger.AddEvent("Setup");
143
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700144 // Compute symbolic factorization if not available.
145 if (cxsparse_factor_ == NULL) {
146 cxsparse_factor_ = CHECK_NOTNULL(cxsparse_.AnalyzeCholesky(AtA));
147 }
148
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800149 event_logger.AddEvent("Analysis");
150
151
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700152 // Solve the linear system.
153 if (cxsparse_.SolveCholesky(AtA, cxsparse_factor_, Atb.data())) {
Sameer Agarwalb0518732012-05-29 00:27:57 -0700154 VectorRef(x, Atb.rows()) = Atb;
155 summary.termination_type = TOLERANCE;
156 }
157
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800158 event_logger.AddEvent("Solve");
159
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700160 cxsparse_.Free(AtA);
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800161
162 event_logger.AddEvent("Teardown");
Sameer Agarwalb0518732012-05-29 00:27:57 -0700163 return summary;
164}
165#else
166LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingCXSparse(
167 CompressedRowSparseMatrix* A,
168 const double* b,
169 const LinearSolver::PerSolveOptions& per_solve_options,
170 double * x) {
171 LOG(FATAL) << "No CXSparse support in Ceres.";
Keir Mierleefe7ac62012-06-24 22:25:28 -0700172
173 // Unreachable but MSVC does not know this.
174 return LinearSolver::Summary();
Sameer Agarwalb0518732012-05-29 00:27:57 -0700175}
176#endif
177
178#ifndef CERES_NO_SUITESPARSE
179LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingSuiteSparse(
180 CompressedRowSparseMatrix* A,
181 const double* b,
182 const LinearSolver::PerSolveOptions& per_solve_options,
183 double * x) {
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800184 EventLogger event_logger("SparseNormalCholeskySolver::SuiteSparse::Solve");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700185
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800186 const int num_cols = A->num_cols();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700187 LinearSolver::Summary summary;
188 Vector Atb = Vector::Zero(num_cols);
189 A->LeftMultiply(b, Atb.data());
190
191 if (per_solve_options.D != NULL) {
192 // Temporarily append a diagonal block to the A matrix, but undo it before
193 // returning the matrix to the user.
194 CompressedRowSparseMatrix D(per_solve_options.D, num_cols);
195 A->AppendRows(D);
196 }
197
198 VectorRef(x, num_cols).setZero();
199
200 scoped_ptr<cholmod_sparse> lhs(ss_.CreateSparseMatrixTransposeView(A));
201 CHECK_NOTNULL(lhs.get());
202
203 cholmod_dense* rhs = ss_.CreateDenseVector(Atb.data(), num_cols, num_cols);
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800204 event_logger.AddEvent("Setup");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700205
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700206 if (factor_ == NULL) {
207 if (options_.use_block_amd) {
208 factor_ = ss_.BlockAnalyzeCholesky(lhs.get(),
209 A->col_blocks(),
210 A->row_blocks());
211 } else {
212 factor_ = ss_.AnalyzeCholesky(lhs.get());
213 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700214
Sameer Agarwalcb83b282012-06-06 22:26:09 -0700215 if (VLOG_IS_ON(2)) {
216 cholmod_print_common("Symbolic Analysis", ss_.mutable_cc());
217 }
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700218 }
219
220 CHECK_NOTNULL(factor_);
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800221 event_logger.AddEvent("Analysis");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700222
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700223 cholmod_dense* sol = ss_.SolveCholesky(lhs.get(), factor_, rhs);
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800224 event_logger.AddEvent("Solve");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700225
226 ss_.Free(rhs);
227 rhs = NULL;
228
229 if (per_solve_options.D != NULL) {
230 A->DeleteRows(num_cols);
231 }
232
Keir Mierle8ebb0732012-04-30 23:09:08 -0700233 summary.num_iterations = 1;
234 if (sol != NULL) {
235 memcpy(x, sol->x, num_cols * sizeof(*x));
236
237 ss_.Free(sol);
238 sol = NULL;
239 summary.termination_type = TOLERANCE;
240 }
241
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800242 event_logger.AddEvent("Teardown");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700243 return summary;
244}
Sameer Agarwalb0518732012-05-29 00:27:57 -0700245#else
246LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingSuiteSparse(
247 CompressedRowSparseMatrix* A,
248 const double* b,
249 const LinearSolver::PerSolveOptions& per_solve_options,
250 double * x) {
251 LOG(FATAL) << "No SuiteSparse support in Ceres.";
Keir Mierleefe7ac62012-06-24 22:25:28 -0700252
253 // Unreachable but MSVC does not know this.
254 return LinearSolver::Summary();
Sameer Agarwalb0518732012-05-29 00:27:57 -0700255}
256#endif
Keir Mierle8ebb0732012-04-30 23:09:08 -0700257
258} // namespace internal
259} // namespace ceres