Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 1 | // 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 Agarwal | 8140f0f | 2013-03-12 09:45:08 -0700 | [diff] [blame] | 31 | #if !defined(CERES_NO_SUITESPARSE) || !defined(CERES_NO_CXSPARSE) |
| 32 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 33 | #include "ceres/sparse_normal_cholesky_solver.h" |
| 34 | |
| 35 | #include <algorithm> |
| 36 | #include <cstring> |
| 37 | #include <ctime> |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 38 | |
| 39 | #ifndef CERES_NO_CXSPARSE |
| 40 | #include "cs.h" |
| 41 | #endif |
| 42 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 43 | #include "ceres/compressed_row_sparse_matrix.h" |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 44 | #include "ceres/internal/eigen.h" |
| 45 | #include "ceres/internal/scoped_ptr.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 46 | #include "ceres/linear_solver.h" |
| 47 | #include "ceres/suitesparse.h" |
| 48 | #include "ceres/triplet_sparse_matrix.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 49 | #include "ceres/types.h" |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 50 | #include "ceres/wall_time.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 51 | |
| 52 | namespace ceres { |
| 53 | namespace internal { |
| 54 | |
| 55 | SparseNormalCholeskySolver::SparseNormalCholeskySolver( |
| 56 | const LinearSolver::Options& options) |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 57 | : options_(options) { |
| 58 | #ifndef CERES_NO_SUITESPARSE |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 59 | factor_ = NULL; |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 60 | #endif |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 61 | |
| 62 | #ifndef CERES_NO_CXSPARSE |
| 63 | cxsparse_factor_ = NULL; |
| 64 | #endif // CERES_NO_CXSPARSE |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 65 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 66 | |
| 67 | SparseNormalCholeskySolver::~SparseNormalCholeskySolver() { |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 68 | #ifndef CERES_NO_SUITESPARSE |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 69 | if (factor_ != NULL) { |
| 70 | ss_.Free(factor_); |
| 71 | factor_ = NULL; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 72 | } |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 73 | #endif |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 74 | |
| 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 Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | LinearSolver::Summary SparseNormalCholeskySolver::SolveImpl( |
| 84 | CompressedRowSparseMatrix* A, |
| 85 | const double* b, |
| 86 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 87 | double * x) { |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 88 | 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 |
| 104 | LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingCXSparse( |
| 105 | CompressedRowSparseMatrix* A, |
| 106 | const double* b, |
| 107 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 108 | double * x) { |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 109 | EventLogger event_logger("SparseNormalCholeskySolver::CXSparse::Solve"); |
| 110 | |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 111 | 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 Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 127 | cs_di At = cxsparse_.CreateSparseMatrixTransposeView(A); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 128 | |
| 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 Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 137 | cs_di* AtA = cs_multiply(&At, A2); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 138 | |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 139 | cxsparse_.Free(A2); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 140 | if (per_solve_options.D != NULL) { |
| 141 | A->DeleteRows(num_cols); |
| 142 | } |
| 143 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 144 | event_logger.AddEvent("Setup"); |
| 145 | |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 146 | // Compute symbolic factorization if not available. |
| 147 | if (cxsparse_factor_ == NULL) { |
| 148 | cxsparse_factor_ = CHECK_NOTNULL(cxsparse_.AnalyzeCholesky(AtA)); |
| 149 | } |
| 150 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 151 | event_logger.AddEvent("Analysis"); |
| 152 | |
| 153 | |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 154 | // Solve the linear system. |
| 155 | if (cxsparse_.SolveCholesky(AtA, cxsparse_factor_, Atb.data())) { |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 156 | VectorRef(x, Atb.rows()) = Atb; |
| 157 | summary.termination_type = TOLERANCE; |
| 158 | } |
| 159 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 160 | event_logger.AddEvent("Solve"); |
| 161 | |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 162 | cxsparse_.Free(AtA); |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 163 | |
| 164 | event_logger.AddEvent("Teardown"); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 165 | return summary; |
| 166 | } |
| 167 | #else |
| 168 | LinearSolver::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 Mierle | efe7ac6 | 2012-06-24 22:25:28 -0700 | [diff] [blame] | 174 | |
| 175 | // Unreachable but MSVC does not know this. |
| 176 | return LinearSolver::Summary(); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 177 | } |
| 178 | #endif |
| 179 | |
| 180 | #ifndef CERES_NO_SUITESPARSE |
| 181 | LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingSuiteSparse( |
| 182 | CompressedRowSparseMatrix* A, |
| 183 | const double* b, |
| 184 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 185 | double * x) { |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 186 | EventLogger event_logger("SparseNormalCholeskySolver::SuiteSparse::Solve"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 187 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 188 | const int num_cols = A->num_cols(); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 189 | 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 Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 206 | event_logger.AddEvent("Setup"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 207 | |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 208 | 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 Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 216 | |
Sameer Agarwal | cb83b28 | 2012-06-06 22:26:09 -0700 | [diff] [blame] | 217 | if (VLOG_IS_ON(2)) { |
| 218 | cholmod_print_common("Symbolic Analysis", ss_.mutable_cc()); |
| 219 | } |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | CHECK_NOTNULL(factor_); |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 223 | event_logger.AddEvent("Analysis"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 224 | |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 225 | cholmod_dense* sol = ss_.SolveCholesky(lhs.get(), factor_, rhs); |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 226 | event_logger.AddEvent("Solve"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 227 | |
| 228 | ss_.Free(rhs); |
| 229 | rhs = NULL; |
| 230 | |
| 231 | if (per_solve_options.D != NULL) { |
| 232 | A->DeleteRows(num_cols); |
| 233 | } |
| 234 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 235 | 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 Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 244 | event_logger.AddEvent("Teardown"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 245 | return summary; |
| 246 | } |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 247 | #else |
| 248 | LinearSolver::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 Mierle | efe7ac6 | 2012-06-24 22:25:28 -0700 | [diff] [blame] | 254 | |
| 255 | // Unreachable but MSVC does not know this. |
| 256 | return LinearSolver::Summary(); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 257 | } |
| 258 | #endif |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 259 | |
| 260 | } // namespace internal |
| 261 | } // namespace ceres |
Sameer Agarwal | 8140f0f | 2013-03-12 09:45:08 -0700 | [diff] [blame] | 262 | |
| 263 | #endif // !defined(CERES_NO_SUITESPARSE) || !defined(CERES_NO_CXSPARSE) |