blob: 7946665b904d12704e019d711d3ccbe22ac5f54e [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
Sameer Agarwal8140f0f2013-03-12 09:45:08 -070031#if !defined(CERES_NO_SUITESPARSE) || !defined(CERES_NO_CXSPARSE)
32
Keir Mierle8ebb0732012-04-30 23:09:08 -070033#include "ceres/sparse_normal_cholesky_solver.h"
34
35#include <algorithm>
36#include <cstring>
37#include <ctime>
Sameer Agarwalb0518732012-05-29 00:27:57 -070038
39#ifndef CERES_NO_CXSPARSE
40#include "cs.h"
41#endif
42
Keir Mierle8ebb0732012-04-30 23:09:08 -070043#include "ceres/compressed_row_sparse_matrix.h"
Sameer Agarwal42a84b82013-02-01 12:22:53 -080044#include "ceres/internal/eigen.h"
45#include "ceres/internal/scoped_ptr.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070046#include "ceres/linear_solver.h"
47#include "ceres/suitesparse.h"
48#include "ceres/triplet_sparse_matrix.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070049#include "ceres/types.h"
Sameer Agarwal42a84b82013-02-01 12:22:53 -080050#include "ceres/wall_time.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070051
52namespace ceres {
53namespace internal {
54
55SparseNormalCholeskySolver::SparseNormalCholeskySolver(
56 const LinearSolver::Options& options)
Sameer Agarwalb0518732012-05-29 00:27:57 -070057 : options_(options) {
58#ifndef CERES_NO_SUITESPARSE
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -070059 factor_ = NULL;
Sameer Agarwalb0518732012-05-29 00:27:57 -070060#endif
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070061
62#ifndef CERES_NO_CXSPARSE
63 cxsparse_factor_ = NULL;
64#endif // CERES_NO_CXSPARSE
Sameer Agarwalb0518732012-05-29 00:27:57 -070065}
Keir Mierle8ebb0732012-04-30 23:09:08 -070066
67SparseNormalCholeskySolver::~SparseNormalCholeskySolver() {
Sameer Agarwalb0518732012-05-29 00:27:57 -070068#ifndef CERES_NO_SUITESPARSE
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -070069 if (factor_ != NULL) {
70 ss_.Free(factor_);
71 factor_ = NULL;
Keir Mierle8ebb0732012-04-30 23:09:08 -070072 }
Sameer Agarwalb0518732012-05-29 00:27:57 -070073#endif
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070074
75#ifndef CERES_NO_CXSPARSE
76 if (cxsparse_factor_ != NULL) {
77 cxsparse_.Free(cxsparse_factor_);
78 cxsparse_factor_ = NULL;
79 }
80#endif // CERES_NO_CXSPARSE
Keir Mierle8ebb0732012-04-30 23:09:08 -070081}
82
83LinearSolver::Summary SparseNormalCholeskySolver::SolveImpl(
84 CompressedRowSparseMatrix* A,
85 const double* b,
86 const LinearSolver::PerSolveOptions& per_solve_options,
87 double * x) {
Sameer Agarwalb0518732012-05-29 00:27:57 -070088 switch (options_.sparse_linear_algebra_library) {
89 case SUITE_SPARSE:
90 return SolveImplUsingSuiteSparse(A, b, per_solve_options, x);
91 case CX_SPARSE:
92 return SolveImplUsingCXSparse(A, b, per_solve_options, x);
93 default:
94 LOG(FATAL) << "Unknown sparse linear algebra library : "
95 << options_.sparse_linear_algebra_library;
96 }
97
98 LOG(FATAL) << "Unknown sparse linear algebra library : "
99 << options_.sparse_linear_algebra_library;
100 return LinearSolver::Summary();
101}
102
103#ifndef CERES_NO_CXSPARSE
104LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingCXSparse(
105 CompressedRowSparseMatrix* A,
106 const double* b,
107 const LinearSolver::PerSolveOptions& per_solve_options,
108 double * x) {
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800109 EventLogger event_logger("SparseNormalCholeskySolver::CXSparse::Solve");
110
Sameer Agarwalb0518732012-05-29 00:27:57 -0700111 LinearSolver::Summary summary;
112 summary.num_iterations = 1;
113 const int num_cols = A->num_cols();
114 Vector Atb = Vector::Zero(num_cols);
115 A->LeftMultiply(b, Atb.data());
116
117 if (per_solve_options.D != NULL) {
118 // Temporarily append a diagonal block to the A matrix, but undo
119 // it before returning the matrix to the user.
120 CompressedRowSparseMatrix D(per_solve_options.D, num_cols);
121 A->AppendRows(D);
122 }
123
124 VectorRef(x, num_cols).setZero();
125
126 // Wrap the augmented Jacobian in a compressed sparse column matrix.
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700127 cs_di At = cxsparse_.CreateSparseMatrixTransposeView(A);
Sameer Agarwalb0518732012-05-29 00:27:57 -0700128
129 // Compute the normal equations. J'J delta = J'f and solve them
130 // using a sparse Cholesky factorization. Notice that when compared
131 // to SuiteSparse we have to explicitly compute the transpose of Jt,
132 // and then the normal equations before they can be
133 // factorized. CHOLMOD/SuiteSparse on the other hand can just work
134 // off of Jt to compute the Cholesky factorization of the normal
135 // equations.
136 cs_di* A2 = cs_transpose(&At, 1);
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800137 cs_di* AtA = cs_multiply(&At, A2);
Sameer Agarwalb0518732012-05-29 00:27:57 -0700138
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700139 cxsparse_.Free(A2);
Sameer Agarwalb0518732012-05-29 00:27:57 -0700140 if (per_solve_options.D != NULL) {
141 A->DeleteRows(num_cols);
142 }
143
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800144 event_logger.AddEvent("Setup");
145
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700146 // Compute symbolic factorization if not available.
147 if (cxsparse_factor_ == NULL) {
148 cxsparse_factor_ = CHECK_NOTNULL(cxsparse_.AnalyzeCholesky(AtA));
149 }
150
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800151 event_logger.AddEvent("Analysis");
152
153
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700154 // Solve the linear system.
155 if (cxsparse_.SolveCholesky(AtA, cxsparse_factor_, Atb.data())) {
Sameer Agarwalb0518732012-05-29 00:27:57 -0700156 VectorRef(x, Atb.rows()) = Atb;
157 summary.termination_type = TOLERANCE;
158 }
159
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800160 event_logger.AddEvent("Solve");
161
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700162 cxsparse_.Free(AtA);
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800163
164 event_logger.AddEvent("Teardown");
Sameer Agarwalb0518732012-05-29 00:27:57 -0700165 return summary;
166}
167#else
168LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingCXSparse(
169 CompressedRowSparseMatrix* A,
170 const double* b,
171 const LinearSolver::PerSolveOptions& per_solve_options,
172 double * x) {
173 LOG(FATAL) << "No CXSparse support in Ceres.";
Keir Mierleefe7ac62012-06-24 22:25:28 -0700174
175 // Unreachable but MSVC does not know this.
176 return LinearSolver::Summary();
Sameer Agarwalb0518732012-05-29 00:27:57 -0700177}
178#endif
179
180#ifndef CERES_NO_SUITESPARSE
181LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingSuiteSparse(
182 CompressedRowSparseMatrix* A,
183 const double* b,
184 const LinearSolver::PerSolveOptions& per_solve_options,
185 double * x) {
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800186 EventLogger event_logger("SparseNormalCholeskySolver::SuiteSparse::Solve");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700187
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800188 const int num_cols = A->num_cols();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700189 LinearSolver::Summary summary;
190 Vector Atb = Vector::Zero(num_cols);
191 A->LeftMultiply(b, Atb.data());
192
193 if (per_solve_options.D != NULL) {
194 // Temporarily append a diagonal block to the A matrix, but undo it before
195 // returning the matrix to the user.
196 CompressedRowSparseMatrix D(per_solve_options.D, num_cols);
197 A->AppendRows(D);
198 }
199
200 VectorRef(x, num_cols).setZero();
201
202 scoped_ptr<cholmod_sparse> lhs(ss_.CreateSparseMatrixTransposeView(A));
203 CHECK_NOTNULL(lhs.get());
204
205 cholmod_dense* rhs = ss_.CreateDenseVector(Atb.data(), num_cols, num_cols);
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800206 event_logger.AddEvent("Setup");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700207
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700208 if (factor_ == NULL) {
209 if (options_.use_block_amd) {
210 factor_ = ss_.BlockAnalyzeCholesky(lhs.get(),
211 A->col_blocks(),
212 A->row_blocks());
213 } else {
214 factor_ = ss_.AnalyzeCholesky(lhs.get());
215 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700216
Sameer Agarwalcb83b282012-06-06 22:26:09 -0700217 if (VLOG_IS_ON(2)) {
218 cholmod_print_common("Symbolic Analysis", ss_.mutable_cc());
219 }
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700220 }
221
222 CHECK_NOTNULL(factor_);
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800223 event_logger.AddEvent("Analysis");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700224
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700225 cholmod_dense* sol = ss_.SolveCholesky(lhs.get(), factor_, rhs);
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800226 event_logger.AddEvent("Solve");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700227
228 ss_.Free(rhs);
229 rhs = NULL;
230
231 if (per_solve_options.D != NULL) {
232 A->DeleteRows(num_cols);
233 }
234
Keir Mierle8ebb0732012-04-30 23:09:08 -0700235 summary.num_iterations = 1;
236 if (sol != NULL) {
237 memcpy(x, sol->x, num_cols * sizeof(*x));
238
239 ss_.Free(sol);
240 sol = NULL;
241 summary.termination_type = TOLERANCE;
242 }
243
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800244 event_logger.AddEvent("Teardown");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700245 return summary;
246}
Sameer Agarwalb0518732012-05-29 00:27:57 -0700247#else
248LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingSuiteSparse(
249 CompressedRowSparseMatrix* A,
250 const double* b,
251 const LinearSolver::PerSolveOptions& per_solve_options,
252 double * x) {
253 LOG(FATAL) << "No SuiteSparse support in Ceres.";
Keir Mierleefe7ac62012-06-24 22:25:28 -0700254
255 // Unreachable but MSVC does not know this.
256 return LinearSolver::Summary();
Sameer Agarwalb0518732012-05-29 00:27:57 -0700257}
258#endif
Keir Mierle8ebb0732012-04-30 23:09:08 -0700259
260} // namespace internal
261} // namespace ceres
Sameer Agarwal8140f0f2013-03-12 09:45:08 -0700262
263#endif // !defined(CERES_NO_SUITESPARSE) || !defined(CERES_NO_CXSPARSE)