blob: a7c43ef4fb46c9119d2755dfc438854b2ef64f6f [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.";
Keir Mierleefe7ac62012-06-24 22:25:28 -0700153
154 // Unreachable but MSVC does not know this.
155 return LinearSolver::Summary();
Sameer Agarwalb0518732012-05-29 00:27:57 -0700156}
157#endif
158
159#ifndef CERES_NO_SUITESPARSE
160LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingSuiteSparse(
161 CompressedRowSparseMatrix* A,
162 const double* b,
163 const LinearSolver::PerSolveOptions& per_solve_options,
164 double * x) {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700165 const time_t start_time = time(NULL);
166 const int num_cols = A->num_cols();
167
168 LinearSolver::Summary summary;
169 Vector Atb = Vector::Zero(num_cols);
170 A->LeftMultiply(b, Atb.data());
171
172 if (per_solve_options.D != NULL) {
173 // Temporarily append a diagonal block to the A matrix, but undo it before
174 // returning the matrix to the user.
175 CompressedRowSparseMatrix D(per_solve_options.D, num_cols);
176 A->AppendRows(D);
177 }
178
179 VectorRef(x, num_cols).setZero();
180
181 scoped_ptr<cholmod_sparse> lhs(ss_.CreateSparseMatrixTransposeView(A));
182 CHECK_NOTNULL(lhs.get());
183
184 cholmod_dense* rhs = ss_.CreateDenseVector(Atb.data(), num_cols, num_cols);
185 const time_t init_time = time(NULL);
186
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700187 if (factor_ == NULL) {
188 if (options_.use_block_amd) {
189 factor_ = ss_.BlockAnalyzeCholesky(lhs.get(),
190 A->col_blocks(),
191 A->row_blocks());
192 } else {
193 factor_ = ss_.AnalyzeCholesky(lhs.get());
194 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700195
Sameer Agarwalcb83b282012-06-06 22:26:09 -0700196 if (VLOG_IS_ON(2)) {
197 cholmod_print_common("Symbolic Analysis", ss_.mutable_cc());
198 }
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700199 }
200
201 CHECK_NOTNULL(factor_);
202
Keir Mierle8ebb0732012-04-30 23:09:08 -0700203 const time_t symbolic_time = time(NULL);
204
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700205 cholmod_dense* sol = ss_.SolveCholesky(lhs.get(), factor_, rhs);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700206 const time_t solve_time = time(NULL);
207
208 ss_.Free(rhs);
209 rhs = NULL;
210
211 if (per_solve_options.D != NULL) {
212 A->DeleteRows(num_cols);
213 }
214
Keir Mierle8ebb0732012-04-30 23:09:08 -0700215 summary.num_iterations = 1;
216 if (sol != NULL) {
217 memcpy(x, sol->x, num_cols * sizeof(*x));
218
219 ss_.Free(sol);
220 sol = NULL;
221 summary.termination_type = TOLERANCE;
222 }
223
224 const time_t cleanup_time = time(NULL);
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700225 VLOG(2) << "time (sec) total: " << (cleanup_time - start_time)
226 << " init: " << (init_time - start_time)
227 << " symbolic: " << (symbolic_time - init_time)
228 << " solve: " << (solve_time - symbolic_time)
229 << " cleanup: " << (cleanup_time - solve_time);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700230 return summary;
231}
Sameer Agarwalb0518732012-05-29 00:27:57 -0700232#else
233LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingSuiteSparse(
234 CompressedRowSparseMatrix* A,
235 const double* b,
236 const LinearSolver::PerSolveOptions& per_solve_options,
237 double * x) {
238 LOG(FATAL) << "No SuiteSparse support in Ceres.";
Keir Mierleefe7ac62012-06-24 22:25:28 -0700239
240 // Unreachable but MSVC does not know this.
241 return LinearSolver::Summary();
Sameer Agarwalb0518732012-05-29 00:27:57 -0700242}
243#endif
Keir Mierle8ebb0732012-04-30 23:09:08 -0700244
245} // namespace internal
246} // namespace ceres