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 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 31 | #include "ceres/sparse_normal_cholesky_solver.h" |
| 32 | |
| 33 | #include <algorithm> |
| 34 | #include <cstring> |
| 35 | #include <ctime> |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 36 | |
| 37 | #ifndef CERES_NO_CXSPARSE |
| 38 | #include "cs.h" |
| 39 | #endif |
| 40 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 41 | #include "ceres/compressed_row_sparse_matrix.h" |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 42 | #include "ceres/internal/eigen.h" |
| 43 | #include "ceres/internal/scoped_ptr.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 44 | #include "ceres/linear_solver.h" |
| 45 | #include "ceres/suitesparse.h" |
| 46 | #include "ceres/triplet_sparse_matrix.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 47 | #include "ceres/types.h" |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 48 | #include "ceres/wall_time.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 49 | |
| 50 | namespace ceres { |
| 51 | namespace internal { |
| 52 | |
| 53 | SparseNormalCholeskySolver::SparseNormalCholeskySolver( |
| 54 | const LinearSolver::Options& options) |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 55 | : options_(options) { |
| 56 | #ifndef CERES_NO_SUITESPARSE |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 57 | factor_ = NULL; |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 58 | #endif |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 59 | |
| 60 | #ifndef CERES_NO_CXSPARSE |
| 61 | cxsparse_factor_ = NULL; |
| 62 | #endif // CERES_NO_CXSPARSE |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 63 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 64 | |
| 65 | SparseNormalCholeskySolver::~SparseNormalCholeskySolver() { |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 66 | #ifndef CERES_NO_SUITESPARSE |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 67 | if (factor_ != NULL) { |
| 68 | ss_.Free(factor_); |
| 69 | factor_ = NULL; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 70 | } |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 71 | #endif |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 72 | |
| 73 | #ifndef CERES_NO_CXSPARSE |
| 74 | if (cxsparse_factor_ != NULL) { |
| 75 | cxsparse_.Free(cxsparse_factor_); |
| 76 | cxsparse_factor_ = NULL; |
| 77 | } |
| 78 | #endif // CERES_NO_CXSPARSE |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | LinearSolver::Summary SparseNormalCholeskySolver::SolveImpl( |
| 82 | CompressedRowSparseMatrix* A, |
| 83 | const double* b, |
| 84 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 85 | double * x) { |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 86 | switch (options_.sparse_linear_algebra_library) { |
| 87 | case SUITE_SPARSE: |
| 88 | return SolveImplUsingSuiteSparse(A, b, per_solve_options, x); |
| 89 | case CX_SPARSE: |
| 90 | return SolveImplUsingCXSparse(A, b, per_solve_options, x); |
| 91 | default: |
| 92 | LOG(FATAL) << "Unknown sparse linear algebra library : " |
| 93 | << options_.sparse_linear_algebra_library; |
| 94 | } |
| 95 | |
| 96 | LOG(FATAL) << "Unknown sparse linear algebra library : " |
| 97 | << options_.sparse_linear_algebra_library; |
| 98 | return LinearSolver::Summary(); |
| 99 | } |
| 100 | |
| 101 | #ifndef CERES_NO_CXSPARSE |
| 102 | LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingCXSparse( |
| 103 | CompressedRowSparseMatrix* A, |
| 104 | const double* b, |
| 105 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 106 | double * x) { |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 107 | EventLogger event_logger("SparseNormalCholeskySolver::CXSparse::Solve"); |
| 108 | |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 109 | LinearSolver::Summary summary; |
| 110 | summary.num_iterations = 1; |
| 111 | const int num_cols = A->num_cols(); |
| 112 | Vector Atb = Vector::Zero(num_cols); |
| 113 | A->LeftMultiply(b, Atb.data()); |
| 114 | |
| 115 | if (per_solve_options.D != NULL) { |
| 116 | // Temporarily append a diagonal block to the A matrix, but undo |
| 117 | // it before returning the matrix to the user. |
| 118 | CompressedRowSparseMatrix D(per_solve_options.D, num_cols); |
| 119 | A->AppendRows(D); |
| 120 | } |
| 121 | |
| 122 | VectorRef(x, num_cols).setZero(); |
| 123 | |
| 124 | // Wrap the augmented Jacobian in a compressed sparse column matrix. |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 125 | cs_di At = cxsparse_.CreateSparseMatrixTransposeView(A); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 126 | |
| 127 | // Compute the normal equations. J'J delta = J'f and solve them |
| 128 | // using a sparse Cholesky factorization. Notice that when compared |
| 129 | // to SuiteSparse we have to explicitly compute the transpose of Jt, |
| 130 | // and then the normal equations before they can be |
| 131 | // factorized. CHOLMOD/SuiteSparse on the other hand can just work |
| 132 | // off of Jt to compute the Cholesky factorization of the normal |
| 133 | // equations. |
| 134 | cs_di* A2 = cs_transpose(&At, 1); |
| 135 | cs_di* AtA = cs_multiply(&At,A2); |
| 136 | |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 137 | cxsparse_.Free(A2); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 138 | if (per_solve_options.D != NULL) { |
| 139 | A->DeleteRows(num_cols); |
| 140 | } |
| 141 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 142 | event_logger.AddEvent("Setup"); |
| 143 | |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 144 | // Compute symbolic factorization if not available. |
| 145 | if (cxsparse_factor_ == NULL) { |
| 146 | cxsparse_factor_ = CHECK_NOTNULL(cxsparse_.AnalyzeCholesky(AtA)); |
| 147 | } |
| 148 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 149 | event_logger.AddEvent("Analysis"); |
| 150 | |
| 151 | |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 152 | // Solve the linear system. |
| 153 | if (cxsparse_.SolveCholesky(AtA, cxsparse_factor_, Atb.data())) { |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 154 | VectorRef(x, Atb.rows()) = Atb; |
| 155 | summary.termination_type = TOLERANCE; |
| 156 | } |
| 157 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 158 | event_logger.AddEvent("Solve"); |
| 159 | |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 160 | cxsparse_.Free(AtA); |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 161 | |
| 162 | event_logger.AddEvent("Teardown"); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 163 | return summary; |
| 164 | } |
| 165 | #else |
| 166 | LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingCXSparse( |
| 167 | CompressedRowSparseMatrix* A, |
| 168 | const double* b, |
| 169 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 170 | double * x) { |
| 171 | LOG(FATAL) << "No CXSparse support in Ceres."; |
Keir Mierle | efe7ac6 | 2012-06-24 22:25:28 -0700 | [diff] [blame] | 172 | |
| 173 | // Unreachable but MSVC does not know this. |
| 174 | return LinearSolver::Summary(); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 175 | } |
| 176 | #endif |
| 177 | |
| 178 | #ifndef CERES_NO_SUITESPARSE |
| 179 | LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingSuiteSparse( |
| 180 | CompressedRowSparseMatrix* A, |
| 181 | const double* b, |
| 182 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 183 | double * x) { |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 184 | EventLogger event_logger("SparseNormalCholeskySolver::SuiteSparse::Solve"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 185 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 186 | const int num_cols = A->num_cols(); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 187 | LinearSolver::Summary summary; |
| 188 | Vector Atb = Vector::Zero(num_cols); |
| 189 | A->LeftMultiply(b, Atb.data()); |
| 190 | |
| 191 | if (per_solve_options.D != NULL) { |
| 192 | // Temporarily append a diagonal block to the A matrix, but undo it before |
| 193 | // returning the matrix to the user. |
| 194 | CompressedRowSparseMatrix D(per_solve_options.D, num_cols); |
| 195 | A->AppendRows(D); |
| 196 | } |
| 197 | |
| 198 | VectorRef(x, num_cols).setZero(); |
| 199 | |
| 200 | scoped_ptr<cholmod_sparse> lhs(ss_.CreateSparseMatrixTransposeView(A)); |
| 201 | CHECK_NOTNULL(lhs.get()); |
| 202 | |
| 203 | cholmod_dense* rhs = ss_.CreateDenseVector(Atb.data(), num_cols, num_cols); |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 204 | event_logger.AddEvent("Setup"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 205 | |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 206 | if (factor_ == NULL) { |
| 207 | if (options_.use_block_amd) { |
| 208 | factor_ = ss_.BlockAnalyzeCholesky(lhs.get(), |
| 209 | A->col_blocks(), |
| 210 | A->row_blocks()); |
| 211 | } else { |
| 212 | factor_ = ss_.AnalyzeCholesky(lhs.get()); |
| 213 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 214 | |
Sameer Agarwal | cb83b28 | 2012-06-06 22:26:09 -0700 | [diff] [blame] | 215 | if (VLOG_IS_ON(2)) { |
| 216 | cholmod_print_common("Symbolic Analysis", ss_.mutable_cc()); |
| 217 | } |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | CHECK_NOTNULL(factor_); |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 221 | event_logger.AddEvent("Analysis"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 222 | |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 223 | cholmod_dense* sol = ss_.SolveCholesky(lhs.get(), factor_, rhs); |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 224 | event_logger.AddEvent("Solve"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 225 | |
| 226 | ss_.Free(rhs); |
| 227 | rhs = NULL; |
| 228 | |
| 229 | if (per_solve_options.D != NULL) { |
| 230 | A->DeleteRows(num_cols); |
| 231 | } |
| 232 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 233 | summary.num_iterations = 1; |
| 234 | if (sol != NULL) { |
| 235 | memcpy(x, sol->x, num_cols * sizeof(*x)); |
| 236 | |
| 237 | ss_.Free(sol); |
| 238 | sol = NULL; |
| 239 | summary.termination_type = TOLERANCE; |
| 240 | } |
| 241 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 242 | event_logger.AddEvent("Teardown"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 243 | return summary; |
| 244 | } |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 245 | #else |
| 246 | LinearSolver::Summary SparseNormalCholeskySolver::SolveImplUsingSuiteSparse( |
| 247 | CompressedRowSparseMatrix* A, |
| 248 | const double* b, |
| 249 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 250 | double * x) { |
| 251 | LOG(FATAL) << "No SuiteSparse support in Ceres."; |
Keir Mierle | efe7ac6 | 2012-06-24 22:25:28 -0700 | [diff] [blame] | 252 | |
| 253 | // Unreachable but MSVC does not know this. |
| 254 | return LinearSolver::Summary(); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 255 | } |
| 256 | #endif |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 257 | |
| 258 | } // namespace internal |
| 259 | } // namespace ceres |