blob: ceeb65477c477239417d05e7d64105fdf45d3aef [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
Keir Mierle8ebb0732012-04-30 23:09:08 -070039#include "ceres/compressed_row_sparse_matrix.h"
Sergey Sharybinf258e462013-08-15 14:50:08 +060040#include "ceres/cxsparse.h"
Sameer Agarwal42a84b82013-02-01 12:22:53 -080041#include "ceres/internal/eigen.h"
42#include "ceres/internal/scoped_ptr.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070043#include "ceres/linear_solver.h"
44#include "ceres/suitesparse.h"
45#include "ceres/triplet_sparse_matrix.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070046#include "ceres/types.h"
Sameer Agarwal42a84b82013-02-01 12:22:53 -080047#include "ceres/wall_time.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070048
49namespace ceres {
50namespace internal {
51
52SparseNormalCholeskySolver::SparseNormalCholeskySolver(
53 const LinearSolver::Options& options)
Sergey Sharybinf258e462013-08-15 14:50:08 +060054 : factor_(NULL),
55 cxsparse_factor_(NULL),
56 options_(options) {
Sameer Agarwalb0518732012-05-29 00:27:57 -070057}
Keir Mierle8ebb0732012-04-30 23:09:08 -070058
59SparseNormalCholeskySolver::~SparseNormalCholeskySolver() {
Sameer Agarwalb0518732012-05-29 00:27:57 -070060#ifndef CERES_NO_SUITESPARSE
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -070061 if (factor_ != NULL) {
62 ss_.Free(factor_);
63 factor_ = NULL;
Keir Mierle8ebb0732012-04-30 23:09:08 -070064 }
Sameer Agarwalb0518732012-05-29 00:27:57 -070065#endif
Petter Strandmark1e3cbd92012-08-29 09:39:56 -070066
67#ifndef CERES_NO_CXSPARSE
68 if (cxsparse_factor_ != NULL) {
69 cxsparse_.Free(cxsparse_factor_);
70 cxsparse_factor_ = NULL;
71 }
72#endif // CERES_NO_CXSPARSE
Keir Mierle8ebb0732012-04-30 23:09:08 -070073}
74
75LinearSolver::Summary SparseNormalCholeskySolver::SolveImpl(
76 CompressedRowSparseMatrix* A,
77 const double* b,
78 const LinearSolver::PerSolveOptions& per_solve_options,
79 double * x) {
Sameer Agarwal367b65e2013-08-09 10:35:37 -070080 switch (options_.sparse_linear_algebra_library_type) {
Sameer Agarwalb0518732012-05-29 00:27:57 -070081 case SUITE_SPARSE:
82 return SolveImplUsingSuiteSparse(A, b, per_solve_options, x);
83 case CX_SPARSE:
84 return SolveImplUsingCXSparse(A, b, per_solve_options, x);
85 default:
86 LOG(FATAL) << "Unknown sparse linear algebra library : "
Sameer Agarwal367b65e2013-08-09 10:35:37 -070087 << options_.sparse_linear_algebra_library_type;
Sameer Agarwalb0518732012-05-29 00:27:57 -070088 }
89
90 LOG(FATAL) << "Unknown sparse linear algebra library : "
Sameer Agarwal367b65e2013-08-09 10:35:37 -070091 << options_.sparse_linear_algebra_library_type;
Sameer Agarwalb0518732012-05-29 00:27:57 -070092 return LinearSolver::Summary();
93}
94
95#ifndef CERES_NO_CXSPARSE
96LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingCXSparse(
97 CompressedRowSparseMatrix* A,
98 const double* b,
99 const LinearSolver::PerSolveOptions& per_solve_options,
100 double * x) {
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800101 EventLogger event_logger("SparseNormalCholeskySolver::CXSparse::Solve");
102
Sameer Agarwalb0518732012-05-29 00:27:57 -0700103 LinearSolver::Summary summary;
104 summary.num_iterations = 1;
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800105 summary.termination_type = TOLERANCE;
Sameer Agarwal89a592f2013-11-26 11:35:49 -0800106 summary.message = "Success.";
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800107
Sameer Agarwalb0518732012-05-29 00:27:57 -0700108 const int num_cols = A->num_cols();
109 Vector Atb = Vector::Zero(num_cols);
110 A->LeftMultiply(b, Atb.data());
111
112 if (per_solve_options.D != NULL) {
113 // Temporarily append a diagonal block to the A matrix, but undo
114 // it before returning the matrix to the user.
115 CompressedRowSparseMatrix D(per_solve_options.D, num_cols);
116 A->AppendRows(D);
117 }
118
119 VectorRef(x, num_cols).setZero();
120
121 // Wrap the augmented Jacobian in a compressed sparse column matrix.
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700122 cs_di At = cxsparse_.CreateSparseMatrixTransposeView(A);
Sameer Agarwalb0518732012-05-29 00:27:57 -0700123
124 // Compute the normal equations. J'J delta = J'f and solve them
125 // using a sparse Cholesky factorization. Notice that when compared
126 // to SuiteSparse we have to explicitly compute the transpose of Jt,
127 // and then the normal equations before they can be
128 // factorized. CHOLMOD/SuiteSparse on the other hand can just work
129 // off of Jt to compute the Cholesky factorization of the normal
130 // equations.
Sameer Agarwald5b93bf2013-04-26 21:17:49 -0700131 cs_di* A2 = cxsparse_.TransposeMatrix(&At);
132 cs_di* AtA = cxsparse_.MatrixMatrixMultiply(&At, A2);
Sameer Agarwalb0518732012-05-29 00:27:57 -0700133
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700134 cxsparse_.Free(A2);
Sameer Agarwalb0518732012-05-29 00:27:57 -0700135 if (per_solve_options.D != NULL) {
136 A->DeleteRows(num_cols);
137 }
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800138 event_logger.AddEvent("Setup");
139
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700140 // Compute symbolic factorization if not available.
141 if (cxsparse_factor_ == NULL) {
Sameer Agarwald5b93bf2013-04-26 21:17:49 -0700142 if (options_.use_postordering) {
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800143 cxsparse_factor_ = cxsparse_.BlockAnalyzeCholesky(AtA,
144 A->col_blocks(),
145 A->col_blocks());
Sameer Agarwald5b93bf2013-04-26 21:17:49 -0700146 } else {
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800147 cxsparse_factor_ = cxsparse_.AnalyzeCholeskyWithNaturalOrdering(AtA);
Sameer Agarwald5b93bf2013-04-26 21:17:49 -0700148 }
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700149 }
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800150 event_logger.AddEvent("Analysis");
151
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800152 if (cxsparse_factor_ == NULL) {
153 summary.termination_type = FATAL_ERROR;
Sameer Agarwal89a592f2013-11-26 11:35:49 -0800154 summary.message =
155 "CXSparse failure. Unable to find symbolic factorization.";
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800156 } else if (cxsparse_.SolveCholesky(AtA, cxsparse_factor_, Atb.data())) {
Sameer Agarwalb0518732012-05-29 00:27:57 -0700157 VectorRef(x, Atb.rows()) = Atb;
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800158 } else {
159 summary.termination_type = FAILURE;
Sameer Agarwalb0518732012-05-29 00:27:57 -0700160 }
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800161 event_logger.AddEvent("Solve");
162
Petter Strandmark1e3cbd92012-08-29 09:39:56 -0700163 cxsparse_.Free(AtA);
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800164 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");
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800187 LinearSolver::Summary summary;
188 summary.termination_type = TOLERANCE;
189 summary.num_iterations = 1;
Sameer Agarwal89a592f2013-11-26 11:35:49 -0800190 summary.message = "Success.";
Keir Mierle8ebb0732012-04-30 23:09:08 -0700191
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800192 const int num_cols = A->num_cols();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700193 Vector Atb = Vector::Zero(num_cols);
194 A->LeftMultiply(b, Atb.data());
195
196 if (per_solve_options.D != NULL) {
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800197 // Temporarily append a diagonal block to the A matrix, but undo
198 // it before returning the matrix to the user.
Keir Mierle8ebb0732012-04-30 23:09:08 -0700199 CompressedRowSparseMatrix D(per_solve_options.D, num_cols);
200 A->AppendRows(D);
201 }
202
203 VectorRef(x, num_cols).setZero();
Sameer Agarwal2560b172013-04-19 08:19:11 -0700204 cholmod_sparse lhs = ss_.CreateSparseMatrixTransposeView(A);
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800205 event_logger.AddEvent("Setup");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700206
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700207 if (factor_ == NULL) {
Sameer Agarwal9189f4e2013-04-19 17:09:49 -0700208 if (options_.use_postordering) {
Sameer Agarwal79bde352013-11-21 21:33:51 -0800209 factor_ = ss_.BlockAnalyzeCholesky(&lhs,
210 A->col_blocks(),
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800211 A->row_blocks(),
Sameer Agarwal89a592f2013-11-26 11:35:49 -0800212 &summary.message);
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700213 } else {
Sameer Agarwal89a592f2013-11-26 11:35:49 -0800214 factor_ = ss_.AnalyzeCholeskyWithNaturalOrdering(&lhs, &summary.message);
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700215 }
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700216 }
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800217 event_logger.AddEvent("Analysis");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700218
Sameer Agarwal79bde352013-11-21 21:33:51 -0800219 if (factor_ == NULL) {
220 if (per_solve_options.D != NULL) {
221 A->DeleteRows(num_cols);
222 }
Sameer Agarwal79bde352013-11-21 21:33:51 -0800223 summary.termination_type = FATAL_ERROR;
224 return summary;
225 }
226
Sameer Agarwal89a592f2013-11-26 11:35:49 -0800227 summary.termination_type = ss_.Cholesky(&lhs, factor_, &summary.message);
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800228 if (summary.termination_type != TOLERANCE) {
Sameer Agarwal79bde352013-11-21 21:33:51 -0800229 if (per_solve_options.D != NULL) {
230 A->DeleteRows(num_cols);
231 }
Sameer Agarwal79bde352013-11-21 21:33:51 -0800232 return summary;
233 }
234
235 cholmod_dense* rhs = ss_.CreateDenseVector(Atb.data(), num_cols, num_cols);
Sameer Agarwal89a592f2013-11-26 11:35:49 -0800236 cholmod_dense* sol = ss_.Solve(factor_, rhs, &summary.message);
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800237 event_logger.AddEvent("Solve");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700238
239 ss_.Free(rhs);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700240 if (per_solve_options.D != NULL) {
241 A->DeleteRows(num_cols);
242 }
243
Keir Mierle8ebb0732012-04-30 23:09:08 -0700244 if (sol != NULL) {
245 memcpy(x, sol->x, num_cols * sizeof(*x));
Keir Mierle8ebb0732012-04-30 23:09:08 -0700246 ss_.Free(sol);
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800247 } else {
248 summary.termination_type = FAILURE;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700249 }
250
Sameer Agarwal42a84b82013-02-01 12:22:53 -0800251 event_logger.AddEvent("Teardown");
Keir Mierle8ebb0732012-04-30 23:09:08 -0700252 return summary;
253}
Sameer Agarwalb0518732012-05-29 00:27:57 -0700254#else
255LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingSuiteSparse(
256 CompressedRowSparseMatrix* A,
257 const double* b,
258 const LinearSolver::PerSolveOptions& per_solve_options,
259 double * x) {
260 LOG(FATAL) << "No SuiteSparse support in Ceres.";
Keir Mierleefe7ac62012-06-24 22:25:28 -0700261
262 // Unreachable but MSVC does not know this.
263 return LinearSolver::Summary();
Sameer Agarwalb0518732012-05-29 00:27:57 -0700264}
265#endif
Keir Mierle8ebb0732012-04-30 23:09:08 -0700266
267} // namespace internal
268} // namespace ceres
Sameer Agarwal8140f0f2013-03-12 09:45:08 -0700269
270#endif // !defined(CERES_NO_SUITESPARSE) || !defined(CERES_NO_CXSPARSE)