blob: 448bbd5bc69ce7c68479504bccb9c9bad7f55c37 [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"
42#include "ceres/linear_solver.h"
43#include "ceres/suitesparse.h"
44#include "ceres/triplet_sparse_matrix.h"
45#include "ceres/internal/eigen.h"
46#include "ceres/internal/scoped_ptr.h"
47#include "ceres/types.h"
48
49namespace ceres {
50namespace internal {
51
52SparseNormalCholeskySolver::SparseNormalCholeskySolver(
53 const LinearSolver::Options& options)
Sameer Agarwalb0518732012-05-29 00:27:57 -070054 : options_(options) {
55#ifndef CERES_NO_SUITESPARSE
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -070056 factor_ = NULL;
Sameer Agarwalb0518732012-05-29 00:27:57 -070057#endif
58}
Keir Mierle8ebb0732012-04-30 23:09:08 -070059
60SparseNormalCholeskySolver::~SparseNormalCholeskySolver() {
Sameer Agarwalb0518732012-05-29 00:27:57 -070061#ifndef CERES_NO_SUITESPARSE
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -070062 if (factor_ != NULL) {
63 ss_.Free(factor_);
64 factor_ = NULL;
Keir Mierle8ebb0732012-04-30 23:09:08 -070065 }
Sameer Agarwalb0518732012-05-29 00:27:57 -070066#endif
Keir Mierle8ebb0732012-04-30 23:09:08 -070067}
68
69LinearSolver::Summary SparseNormalCholeskySolver::SolveImpl(
70 CompressedRowSparseMatrix* A,
71 const double* b,
72 const LinearSolver::PerSolveOptions& per_solve_options,
73 double * x) {
Sameer Agarwalb0518732012-05-29 00:27:57 -070074 switch (options_.sparse_linear_algebra_library) {
75 case SUITE_SPARSE:
76 return SolveImplUsingSuiteSparse(A, b, per_solve_options, x);
77 case CX_SPARSE:
78 return SolveImplUsingCXSparse(A, b, per_solve_options, x);
79 default:
80 LOG(FATAL) << "Unknown sparse linear algebra library : "
81 << options_.sparse_linear_algebra_library;
82 }
83
84 LOG(FATAL) << "Unknown sparse linear algebra library : "
85 << options_.sparse_linear_algebra_library;
86 return LinearSolver::Summary();
87}
88
89#ifndef CERES_NO_CXSPARSE
90LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingCXSparse(
91 CompressedRowSparseMatrix* A,
92 const double* b,
93 const LinearSolver::PerSolveOptions& per_solve_options,
94 double * x) {
95 LinearSolver::Summary summary;
96 summary.num_iterations = 1;
97 const int num_cols = A->num_cols();
98 Vector Atb = Vector::Zero(num_cols);
99 A->LeftMultiply(b, Atb.data());
100
101 if (per_solve_options.D != NULL) {
102 // Temporarily append a diagonal block to the A matrix, but undo
103 // it before returning the matrix to the user.
104 CompressedRowSparseMatrix D(per_solve_options.D, num_cols);
105 A->AppendRows(D);
106 }
107
108 VectorRef(x, num_cols).setZero();
109
110 // Wrap the augmented Jacobian in a compressed sparse column matrix.
111 cs_di At;
112 At.m = A->num_cols();
113 At.n = A->num_rows();
114 At.nz = -1;
115 At.nzmax = A->num_nonzeros();
116 At.p = A->mutable_rows();
117 At.i = A->mutable_cols();
118 At.x = A->mutable_values();
119
120 // Compute the normal equations. J'J delta = J'f and solve them
121 // using a sparse Cholesky factorization. Notice that when compared
122 // to SuiteSparse we have to explicitly compute the transpose of Jt,
123 // and then the normal equations before they can be
124 // factorized. CHOLMOD/SuiteSparse on the other hand can just work
125 // off of Jt to compute the Cholesky factorization of the normal
126 // equations.
127 cs_di* A2 = cs_transpose(&At, 1);
128 cs_di* AtA = cs_multiply(&At,A2);
129
130 cs_free(A2);
131 if (per_solve_options.D != NULL) {
132 A->DeleteRows(num_cols);
133 }
134
135 // This recomputes the symbolic factorization every time it is
136 // invoked. It will perhaps be worth it to cache the symbolic
137 // factorization the way we do for SuiteSparse.
138 if (cs_cholsol(1, AtA, Atb.data())) {
139 VectorRef(x, Atb.rows()) = Atb;
140 summary.termination_type = TOLERANCE;
141 }
142
143 cs_free(AtA);
144 return summary;
145}
146#else
147LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingCXSparse(
148 CompressedRowSparseMatrix* A,
149 const double* b,
150 const LinearSolver::PerSolveOptions& per_solve_options,
151 double * x) {
152 LOG(FATAL) << "No CXSparse support in Ceres.";
153}
154#endif
155
156#ifndef CERES_NO_SUITESPARSE
157LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingSuiteSparse(
158 CompressedRowSparseMatrix* A,
159 const double* b,
160 const LinearSolver::PerSolveOptions& per_solve_options,
161 double * x) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700162 const time_t start_time = time(NULL);
163 const int num_cols = A->num_cols();
164
165 LinearSolver::Summary summary;
166 Vector Atb = Vector::Zero(num_cols);
167 A->LeftMultiply(b, Atb.data());
168
169 if (per_solve_options.D != NULL) {
170 // Temporarily append a diagonal block to the A matrix, but undo it before
171 // returning the matrix to the user.
172 CompressedRowSparseMatrix D(per_solve_options.D, num_cols);
173 A->AppendRows(D);
174 }
175
176 VectorRef(x, num_cols).setZero();
177
178 scoped_ptr<cholmod_sparse> lhs(ss_.CreateSparseMatrixTransposeView(A));
179 CHECK_NOTNULL(lhs.get());
180
181 cholmod_dense* rhs = ss_.CreateDenseVector(Atb.data(), num_cols, num_cols);
182 const time_t init_time = time(NULL);
183
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700184 if (factor_ == NULL) {
185 if (options_.use_block_amd) {
186 factor_ = ss_.BlockAnalyzeCholesky(lhs.get(),
187 A->col_blocks(),
188 A->row_blocks());
189 } else {
190 factor_ = ss_.AnalyzeCholesky(lhs.get());
191 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700192
Sameer Agarwalcb83b282012-06-06 22:26:09 -0700193 if (VLOG_IS_ON(2)) {
194 cholmod_print_common("Symbolic Analysis", ss_.mutable_cc());
195 }
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700196 }
197
198 CHECK_NOTNULL(factor_);
199
Keir Mierle8ebb0732012-04-30 23:09:08 -0700200 const time_t symbolic_time = time(NULL);
201
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700202 cholmod_dense* sol = ss_.SolveCholesky(lhs.get(), factor_, rhs);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700203 const time_t solve_time = time(NULL);
204
205 ss_.Free(rhs);
206 rhs = NULL;
207
208 if (per_solve_options.D != NULL) {
209 A->DeleteRows(num_cols);
210 }
211
Keir Mierle8ebb0732012-04-30 23:09:08 -0700212 summary.num_iterations = 1;
213 if (sol != NULL) {
214 memcpy(x, sol->x, num_cols * sizeof(*x));
215
216 ss_.Free(sol);
217 sol = NULL;
218 summary.termination_type = TOLERANCE;
219 }
220
221 const time_t cleanup_time = time(NULL);
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700222 VLOG(2) << "time (sec) total: " << (cleanup_time - start_time)
223 << " init: " << (init_time - start_time)
224 << " symbolic: " << (symbolic_time - init_time)
225 << " solve: " << (solve_time - symbolic_time)
226 << " cleanup: " << (cleanup_time - solve_time);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700227 return summary;
228}
Sameer Agarwalb0518732012-05-29 00:27:57 -0700229#else
230LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingSuiteSparse(
231 CompressedRowSparseMatrix* A,
232 const double* b,
233 const LinearSolver::PerSolveOptions& per_solve_options,
234 double * x) {
235 LOG(FATAL) << "No SuiteSparse support in Ceres.";
236}
237#endif
Keir Mierle8ebb0732012-04-30 23:09:08 -0700238
239} // namespace internal
240} // namespace ceres