blob: 0a7ea97f2d75aebd8a0a69f97de3161f5189740b [file] [log] [blame]
Sameer Agarwal3faac6a2013-11-28 07:13:26 -08001// Ceres Solver - A fast non-linear least squares minimizer
Keir Mierle8ebb0732012-04-30 23:09:08 -07002// 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//
31// A simple C++ interface to the SuiteSparse and CHOLMOD libraries.
32
33#ifndef CERES_INTERNAL_SUITESPARSE_H_
34#define CERES_INTERNAL_SUITESPARSE_H_
35
Alex Stewartea765852014-05-07 20:46:17 +010036// This include must come before any #ifndef check on Ceres compile options.
37#include "ceres/internal/port.h"
Sameer Agarwald5b93bf2013-04-26 21:17:49 -070038
Keir Mierle8ebb0732012-04-30 23:09:08 -070039#ifndef CERES_NO_SUITESPARSE
40
41#include <cstring>
42#include <string>
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -070043#include <vector>
Keir Mierle8ebb0732012-04-30 23:09:08 -070044
Keir Mierle8ebb0732012-04-30 23:09:08 -070045#include "ceres/internal/port.h"
Sameer Agarwal79bde352013-11-21 21:33:51 -080046#include "ceres/linear_solver.h"
Sameer Agarwal509f68c2013-02-20 01:39:03 -080047#include "cholmod.h"
48#include "glog/logging.h"
Sergey Sharybinf258e462013-08-15 14:50:08 +060049#include "SuiteSparseQR.hpp"
Keir Mierle8ebb0732012-04-30 23:09:08 -070050
Sameer Agarwald5b93bf2013-04-26 21:17:49 -070051// Before SuiteSparse version 4.2.0, cholmod_camd was only enabled
52// if SuiteSparse was compiled with Metis support. This makes
53// calling and linking into cholmod_camd problematic even though it
54// has nothing to do with Metis. This has been fixed reliably in
55// 4.2.0.
56//
57// The fix was actually committed in 4.1.0, but there is
58// some confusion about a silent update to the tar ball, so we are
59// being conservative and choosing the next minor version where
60// things are stable.
Sameer Agarwalac626962013-05-06 07:04:26 -070061#if (SUITESPARSE_VERSION < 4002)
Sameer Agarwald5b93bf2013-04-26 21:17:49 -070062#define CERES_NO_CAMD
63#endif
64
Sergey Sharybinf258e462013-08-15 14:50:08 +060065// UF_long is deprecated but SuiteSparse_long is only available in
Sameer Agarwalad2819a2013-08-17 23:44:09 -070066// newer versions of SuiteSparse. So for older versions of
67// SuiteSparse, we define SuiteSparse_long to be the same as UF_long,
68// which is what recent versions of SuiteSparse do anyways.
69#ifndef SuiteSparse_long
Sameer Agarwal19184532013-08-19 14:15:48 -070070#define SuiteSparse_long UF_long
Sergey Sharybinf258e462013-08-15 14:50:08 +060071#endif
72
Keir Mierle8ebb0732012-04-30 23:09:08 -070073namespace ceres {
74namespace internal {
75
76class CompressedRowSparseMatrix;
77class TripletSparseMatrix;
78
79// The raw CHOLMOD and SuiteSparseQR libraries have a slightly
80// cumbersome c like calling format. This object abstracts it away and
81// provides the user with a simpler interface. The methods here cannot
82// be static as a cholmod_common object serves as a global variable
83// for all cholmod function calls.
84class SuiteSparse {
85 public:
Sameer Agarwal222ca202013-04-01 09:11:07 -070086 SuiteSparse();
87 ~SuiteSparse();
Keir Mierle8ebb0732012-04-30 23:09:08 -070088
89 // Functions for building cholmod_sparse objects from sparse
90 // matrices stored in triplet form. The matrix A is not
91 // modifed. Called owns the result.
92 cholmod_sparse* CreateSparseMatrix(TripletSparseMatrix* A);
93
94 // This function works like CreateSparseMatrix, except that the
95 // return value corresponds to A' rather than A.
96 cholmod_sparse* CreateSparseMatrixTranspose(TripletSparseMatrix* A);
97
98 // Create a cholmod_sparse wrapper around the contents of A. This is
99 // a shallow object, which refers to the contents of A and does not
Sameer Agarwal2560b172013-04-19 08:19:11 -0700100 // use the SuiteSparse machinery to allocate memory.
101 cholmod_sparse CreateSparseMatrixTransposeView(CompressedRowSparseMatrix* A);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700102
103 // Given a vector x, build a cholmod_dense vector of size out_size
104 // with the first in_size entries copied from x. If x is NULL, then
105 // an all zeros vector is returned. Caller owns the result.
106 cholmod_dense* CreateDenseVector(const double* x, int in_size, int out_size);
107
108 // The matrix A is scaled using the matrix whose diagonal is the
109 // vector scale. mode describes how scaling is applied. Possible
110 // values are CHOLMOD_ROW for row scaling - diag(scale) * A,
111 // CHOLMOD_COL for column scaling - A * diag(scale) and CHOLMOD_SYM
112 // for symmetric scaling which scales both the rows and the columns
113 // - diag(scale) * A * diag(scale).
114 void Scale(cholmod_dense* scale, int mode, cholmod_sparse* A) {
115 cholmod_scale(scale, mode, A, &cc_);
116 }
117
118 // Create and return a matrix m = A * A'. Caller owns the
119 // result. The matrix A is not modified.
120 cholmod_sparse* AATranspose(cholmod_sparse* A) {
121 cholmod_sparse*m = cholmod_aat(A, NULL, A->nrow, 1, &cc_);
122 m->stype = 1; // Pay attention to the upper triangular part.
123 return m;
124 }
125
126 // y = alpha * A * x + beta * y. Only y is modified.
127 void SparseDenseMultiply(cholmod_sparse* A, double alpha, double beta,
128 cholmod_dense* x, cholmod_dense* y) {
129 double alpha_[2] = {alpha, 0};
130 double beta_[2] = {beta, 0};
131 cholmod_sdmult(A, 0, alpha_, beta_, x, y, &cc_);
132 }
133
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700134 // Find an ordering of A or AA' (if A is unsymmetric) that minimizes
135 // the fill-in in the Cholesky factorization of the corresponding
136 // matrix. This is done by using the AMD algorithm.
137 //
138 // Using this ordering, the symbolic Cholesky factorization of A (or
139 // AA') is computed and returned.
140 //
141 // A is not modified, only the pattern of non-zeros of A is used,
142 // the actual numerical values in A are of no consequence.
143 //
Sameer Agarwaled923662013-11-28 06:50:43 -0800144 // message contains an explanation of the failures if any.
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800145 //
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700146 // Caller owns the result.
Sameer Agarwaled923662013-11-28 06:50:43 -0800147 cholmod_factor* AnalyzeCholesky(cholmod_sparse* A, string* message);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700148
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700149 cholmod_factor* BlockAnalyzeCholesky(cholmod_sparse* A,
150 const vector<int>& row_blocks,
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800151 const vector<int>& col_blocks,
Sameer Agarwaled923662013-11-28 06:50:43 -0800152 string* message);
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700153
154 // If A is symmetric, then compute the symbolic Cholesky
155 // factorization of A(ordering, ordering). If A is unsymmetric, then
156 // compute the symbolic factorization of
157 // A(ordering,:) A(ordering,:)'.
158 //
159 // A is not modified, only the pattern of non-zeros of A is used,
160 // the actual numerical values in A are of no consequence.
161 //
Sameer Agarwaled923662013-11-28 06:50:43 -0800162 // message contains an explanation of the failures if any.
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800163 //
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700164 // Caller owns the result.
165 cholmod_factor* AnalyzeCholeskyWithUserOrdering(cholmod_sparse* A,
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800166 const vector<int>& ordering,
Sameer Agarwaled923662013-11-28 06:50:43 -0800167 string* message);
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700168
Sameer Agarwal2560b172013-04-19 08:19:11 -0700169 // Perform a symbolic factorization of A without re-ordering A. No
170 // postordering of the elimination tree is performed. This ensures
171 // that the symbolic factor does not introduce an extra permutation
172 // on the matrix. See the documentation for CHOLMOD for more details.
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800173 //
Sameer Agarwaled923662013-11-28 06:50:43 -0800174 // message contains an explanation of the failures if any.
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800175 cholmod_factor* AnalyzeCholeskyWithNaturalOrdering(cholmod_sparse* A,
Sameer Agarwaled923662013-11-28 06:50:43 -0800176 string* message);
Sameer Agarwal2560b172013-04-19 08:19:11 -0700177
Keir Mierle8ebb0732012-04-30 23:09:08 -0700178 // Use the symbolic factorization in L, to find the numerical
179 // factorization for the matrix A or AA^T. Return true if
180 // successful, false otherwise. L contains the numeric factorization
181 // on return.
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800182 //
Sameer Agarwaled923662013-11-28 06:50:43 -0800183 // message contains an explanation of the failures if any.
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800184 LinearSolverTerminationType Cholesky(cholmod_sparse* A,
185 cholmod_factor* L,
Sameer Agarwaled923662013-11-28 06:50:43 -0800186 string* message);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700187
188 // Given a Cholesky factorization of a matrix A = LL^T, solve the
189 // linear system Ax = b, and return the result. If the Solve fails
190 // NULL is returned. Caller owns the result.
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800191 //
Sameer Agarwaled923662013-11-28 06:50:43 -0800192 // message contains an explanation of the failures if any.
193 cholmod_dense* Solve(cholmod_factor* L, cholmod_dense* b, string* message);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700194
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -0700195 // By virtue of the modeling layer in Ceres being block oriented,
196 // all the matrices used by Ceres are also block oriented. When
197 // doing sparse direct factorization of these matrices the
198 // fill-reducing ordering algorithms (in particular AMD) can either
199 // be run on the block or the scalar form of these matrices. The two
200 // SuiteSparse::AnalyzeCholesky methods allows the the client to
201 // compute the symbolic factorization of a matrix by either using
202 // AMD on the matrix or a user provided ordering of the rows.
203 //
204 // But since the underlying matrices are block oriented, it is worth
205 // running AMD on just the block structre of these matrices and then
206 // lifting these block orderings to a full scalar ordering. This
207 // preserves the block structure of the permuted matrix, and exposes
208 // more of the super-nodal structure of the matrix to the numerical
209 // factorization routines.
210 //
211 // Find the block oriented AMD ordering of a matrix A, whose row and
212 // column blocks are given by row_blocks, and col_blocks
213 // respectively. The matrix may or may not be symmetric. The entries
214 // of col_blocks do not need to sum to the number of columns in
215 // A. If this is the case, only the first sum(col_blocks) are used
216 // to compute the ordering.
217 bool BlockAMDOrdering(const cholmod_sparse* A,
218 const vector<int>& row_blocks,
219 const vector<int>& col_blocks,
220 vector<int>* ordering);
221
Sameer Agarwalf7ed22e2013-04-19 14:24:48 -0700222 // Find a fill reducing approximate minimum degree
223 // ordering. ordering is expected to be large enough to hold the
224 // ordering.
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800225 bool ApproximateMinimumDegreeOrdering(cholmod_sparse* matrix, int* ordering);
Sameer Agarwalf7ed22e2013-04-19 14:24:48 -0700226
Sameer Agarwald5b93bf2013-04-26 21:17:49 -0700227
228 // Before SuiteSparse version 4.2.0, cholmod_camd was only enabled
229 // if SuiteSparse was compiled with Metis support. This makes
230 // calling and linking into cholmod_camd problematic even though it
231 // has nothing to do with Metis. This has been fixed reliably in
232 // 4.2.0.
233 //
234 // The fix was actually committed in 4.1.0, but there is
235 // some confusion about a silent update to the tar ball, so we are
236 // being conservative and choosing the next minor version where
237 // things are stable.
238 static bool IsConstrainedApproximateMinimumDegreeOrderingAvailable() {
239 return (SUITESPARSE_VERSION>4001);
240 }
241
242 // Find a fill reducing approximate minimum degree
243 // ordering. constraints is an array which associates with each
244 // column of the matrix an elimination group. i.e., all columns in
245 // group 0 are eliminated first, all columns in group 1 are
246 // eliminated next etc. This function finds a fill reducing ordering
247 // that obeys these constraints.
248 //
249 // Calling ApproximateMinimumDegreeOrdering is equivalent to calling
250 // ConstrainedApproximateMinimumDegreeOrdering with a constraint
251 // array that puts all columns in the same elimination group.
252 //
253 // If CERES_NO_CAMD is defined then calling this function will
254 // result in a crash.
Sameer Agarwalb16e1182013-11-25 05:47:43 -0800255 bool ConstrainedApproximateMinimumDegreeOrdering(cholmod_sparse* matrix,
Sameer Agarwald5b93bf2013-04-26 21:17:49 -0700256 int* constraints,
257 int* ordering);
258
Keir Mierle8ebb0732012-04-30 23:09:08 -0700259 void Free(cholmod_sparse* m) { cholmod_free_sparse(&m, &cc_); }
260 void Free(cholmod_dense* m) { cholmod_free_dense(&m, &cc_); }
261 void Free(cholmod_factor* m) { cholmod_free_factor(&m, &cc_); }
262
263 void Print(cholmod_sparse* m, const string& name) {
264 cholmod_print_sparse(m, const_cast<char*>(name.c_str()), &cc_);
265 }
266
267 void Print(cholmod_dense* m, const string& name) {
268 cholmod_print_dense(m, const_cast<char*>(name.c_str()), &cc_);
269 }
270
271 void Print(cholmod_triplet* m, const string& name) {
272 cholmod_print_triplet(m, const_cast<char*>(name.c_str()), &cc_);
273 }
274
275 cholmod_common* mutable_cc() { return &cc_; }
276
277 private:
278 cholmod_common cc_;
279};
280
281} // namespace internal
282} // namespace ceres
283
Sameer Agarwald61b68a2013-08-16 17:02:56 -0700284#else // CERES_NO_SUITESPARSE
Sergey Sharybinf258e462013-08-15 14:50:08 +0600285
286class SuiteSparse {};
287typedef void cholmod_factor;
288
Keir Mierle8ebb0732012-04-30 23:09:08 -0700289#endif // CERES_NO_SUITESPARSE
290
291#endif // CERES_INTERNAL_SUITESPARSE_H_