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 | |
| 31 | #include <algorithm> |
| 32 | #include <ctime> |
| 33 | #include <set> |
| 34 | #include <vector> |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 35 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 36 | #include "Eigen/Dense" |
| 37 | #include "ceres/block_random_access_dense_matrix.h" |
| 38 | #include "ceres/block_random_access_matrix.h" |
| 39 | #include "ceres/block_random_access_sparse_matrix.h" |
| 40 | #include "ceres/block_sparse_matrix.h" |
| 41 | #include "ceres/block_structure.h" |
Sergey Sharybin | f258e46 | 2013-08-15 14:50:08 +0600 | [diff] [blame] | 42 | #include "ceres/cxsparse.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 43 | #include "ceres/detect_structure.h" |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 44 | #include "ceres/internal/eigen.h" |
| 45 | #include "ceres/internal/port.h" |
| 46 | #include "ceres/internal/scoped_ptr.h" |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 47 | #include "ceres/lapack.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 48 | #include "ceres/linear_solver.h" |
| 49 | #include "ceres/schur_complement_solver.h" |
| 50 | #include "ceres/suitesparse.h" |
| 51 | #include "ceres/triplet_sparse_matrix.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 52 | #include "ceres/types.h" |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 53 | #include "ceres/wall_time.h" |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 54 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 55 | namespace ceres { |
| 56 | namespace internal { |
| 57 | |
| 58 | LinearSolver::Summary SchurComplementSolver::SolveImpl( |
Sameer Agarwal | c1e10d9 | 2013-04-24 11:58:24 -0700 | [diff] [blame] | 59 | BlockSparseMatrix* A, |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 60 | const double* b, |
| 61 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 62 | double* x) { |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 63 | EventLogger event_logger("SchurComplementSolver::Solve"); |
| 64 | |
Sameer Agarwal | a9d8ef8 | 2012-05-14 02:28:05 -0700 | [diff] [blame] | 65 | if (eliminator_.get() == NULL) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 66 | InitStorage(A->block_structure()); |
| 67 | DetectStructure(*A->block_structure(), |
Sameer Agarwal | 0c52f1e | 2012-09-17 11:30:14 -0700 | [diff] [blame] | 68 | options_.elimination_groups[0], |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 69 | &options_.row_block_size, |
| 70 | &options_.e_block_size, |
| 71 | &options_.f_block_size); |
| 72 | eliminator_.reset(CHECK_NOTNULL(SchurEliminatorBase::Create(options_))); |
Sameer Agarwal | 0c52f1e | 2012-09-17 11:30:14 -0700 | [diff] [blame] | 73 | eliminator_->Init(options_.elimination_groups[0], A->block_structure()); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 74 | }; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 75 | fill(x, x + A->num_cols(), 0.0); |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 76 | event_logger.AddEvent("Setup"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 77 | |
| 78 | LinearSolver::Summary summary; |
| 79 | summary.num_iterations = 1; |
| 80 | summary.termination_type = FAILURE; |
| 81 | eliminator_->Eliminate(A, b, per_solve_options.D, lhs_.get(), rhs_.get()); |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 82 | event_logger.AddEvent("Eliminate"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 83 | |
| 84 | double* reduced_solution = x + A->num_cols() - lhs_->num_cols(); |
| 85 | const bool status = SolveReducedLinearSystem(reduced_solution); |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 86 | event_logger.AddEvent("ReducedSolve"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 87 | |
| 88 | if (!status) { |
| 89 | return summary; |
| 90 | } |
| 91 | |
| 92 | eliminator_->BackSubstitute(A, b, per_solve_options.D, reduced_solution, x); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 93 | summary.termination_type = TOLERANCE; |
| 94 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 95 | event_logger.AddEvent("BackSubstitute"); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 96 | return summary; |
| 97 | } |
| 98 | |
| 99 | // Initialize a BlockRandomAccessDenseMatrix to store the Schur |
| 100 | // complement. |
| 101 | void DenseSchurComplementSolver::InitStorage( |
| 102 | const CompressedRowBlockStructure* bs) { |
Sameer Agarwal | 0c52f1e | 2012-09-17 11:30:14 -0700 | [diff] [blame] | 103 | const int num_eliminate_blocks = options().elimination_groups[0]; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 104 | const int num_col_blocks = bs->cols.size(); |
| 105 | |
| 106 | vector<int> blocks(num_col_blocks - num_eliminate_blocks, 0); |
| 107 | for (int i = num_eliminate_blocks, j = 0; |
| 108 | i < num_col_blocks; |
| 109 | ++i, ++j) { |
| 110 | blocks[j] = bs->cols[i].size; |
| 111 | } |
| 112 | |
| 113 | set_lhs(new BlockRandomAccessDenseMatrix(blocks)); |
| 114 | set_rhs(new double[lhs()->num_rows()]); |
| 115 | } |
| 116 | |
| 117 | // Solve the system Sx = r, assuming that the matrix S is stored in a |
| 118 | // BlockRandomAccessDenseMatrix. The linear system is solved using |
| 119 | // Eigen's Cholesky factorization. |
| 120 | bool DenseSchurComplementSolver::SolveReducedLinearSystem(double* solution) { |
| 121 | const BlockRandomAccessDenseMatrix* m = |
| 122 | down_cast<const BlockRandomAccessDenseMatrix*>(lhs()); |
| 123 | const int num_rows = m->num_rows(); |
| 124 | |
| 125 | // The case where there are no f blocks, and the system is block |
| 126 | // diagonal. |
| 127 | if (num_rows == 0) { |
| 128 | return true; |
| 129 | } |
| 130 | |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 131 | if (options().dense_linear_algebra_library_type == EIGEN) { |
| 132 | // TODO(sameeragarwal): Add proper error handling; this completely ignores |
| 133 | // the quality of the solution to the solve. |
| 134 | VectorRef(solution, num_rows) = |
| 135 | ConstMatrixRef(m->values(), num_rows, num_rows) |
| 136 | .selfadjointView<Eigen::Upper>() |
| 137 | .llt() |
| 138 | .solve(ConstVectorRef(rhs(), num_rows)); |
| 139 | return true; |
| 140 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 141 | |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 142 | VectorRef(solution, num_rows) = ConstVectorRef(rhs(), num_rows); |
| 143 | const int info = LAPACK::SolveInPlaceUsingCholesky(num_rows, |
| 144 | m->values(), |
| 145 | solution); |
| 146 | return (info == 0); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Sameer Agarwal | 8140f0f | 2013-03-12 09:45:08 -0700 | [diff] [blame] | 149 | #if !defined(CERES_NO_SUITESPARSE) || !defined(CERES_NO_CXSPARE) |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 150 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 151 | SparseSchurComplementSolver::SparseSchurComplementSolver( |
| 152 | const LinearSolver::Options& options) |
Sergey Sharybin | f258e46 | 2013-08-15 14:50:08 +0600 | [diff] [blame] | 153 | : SchurComplementSolver(options), |
| 154 | factor_(NULL), |
| 155 | cxsparse_factor_(NULL) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | SparseSchurComplementSolver::~SparseSchurComplementSolver() { |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 159 | #ifndef CERES_NO_SUITESPARSE |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 160 | if (factor_ != NULL) { |
| 161 | ss_.Free(factor_); |
| 162 | factor_ = NULL; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 163 | } |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 164 | #endif // CERES_NO_SUITESPARSE |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 165 | |
| 166 | #ifndef CERES_NO_CXSPARSE |
| 167 | if (cxsparse_factor_ != NULL) { |
| 168 | cxsparse_.Free(cxsparse_factor_); |
| 169 | cxsparse_factor_ = NULL; |
| 170 | } |
| 171 | #endif // CERES_NO_CXSPARSE |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Determine the non-zero blocks in the Schur Complement matrix, and |
| 175 | // initialize a BlockRandomAccessSparseMatrix object. |
| 176 | void SparseSchurComplementSolver::InitStorage( |
| 177 | const CompressedRowBlockStructure* bs) { |
Sameer Agarwal | 0c52f1e | 2012-09-17 11:30:14 -0700 | [diff] [blame] | 178 | const int num_eliminate_blocks = options().elimination_groups[0]; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 179 | const int num_col_blocks = bs->cols.size(); |
| 180 | const int num_row_blocks = bs->rows.size(); |
| 181 | |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 182 | blocks_.resize(num_col_blocks - num_eliminate_blocks, 0); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 183 | for (int i = num_eliminate_blocks; i < num_col_blocks; ++i) { |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 184 | blocks_[i - num_eliminate_blocks] = bs->cols[i].size; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | set<pair<int, int> > block_pairs; |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 188 | for (int i = 0; i < blocks_.size(); ++i) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 189 | block_pairs.insert(make_pair(i, i)); |
| 190 | } |
| 191 | |
| 192 | int r = 0; |
| 193 | while (r < num_row_blocks) { |
| 194 | int e_block_id = bs->rows[r].cells.front().block_id; |
| 195 | if (e_block_id >= num_eliminate_blocks) { |
| 196 | break; |
| 197 | } |
| 198 | vector<int> f_blocks; |
| 199 | |
| 200 | // Add to the chunk until the first block in the row is |
| 201 | // different than the one in the first row for the chunk. |
| 202 | for (; r < num_row_blocks; ++r) { |
| 203 | const CompressedRow& row = bs->rows[r]; |
| 204 | if (row.cells.front().block_id != e_block_id) { |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | // Iterate over the blocks in the row, ignoring the first |
| 209 | // block since it is the one to be eliminated. |
| 210 | for (int c = 1; c < row.cells.size(); ++c) { |
| 211 | const Cell& cell = row.cells[c]; |
| 212 | f_blocks.push_back(cell.block_id - num_eliminate_blocks); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | sort(f_blocks.begin(), f_blocks.end()); |
| 217 | f_blocks.erase(unique(f_blocks.begin(), f_blocks.end()), f_blocks.end()); |
| 218 | for (int i = 0; i < f_blocks.size(); ++i) { |
| 219 | for (int j = i + 1; j < f_blocks.size(); ++j) { |
| 220 | block_pairs.insert(make_pair(f_blocks[i], f_blocks[j])); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // Remaing rows do not contribute to the chunks and directly go |
| 226 | // into the schur complement via an outer product. |
| 227 | for (; r < num_row_blocks; ++r) { |
| 228 | const CompressedRow& row = bs->rows[r]; |
| 229 | CHECK_GE(row.cells.front().block_id, num_eliminate_blocks); |
| 230 | for (int i = 0; i < row.cells.size(); ++i) { |
| 231 | int r_block1_id = row.cells[i].block_id - num_eliminate_blocks; |
| 232 | for (int j = 0; j < row.cells.size(); ++j) { |
| 233 | int r_block2_id = row.cells[j].block_id - num_eliminate_blocks; |
| 234 | if (r_block1_id <= r_block2_id) { |
| 235 | block_pairs.insert(make_pair(r_block1_id, r_block2_id)); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 241 | set_lhs(new BlockRandomAccessSparseMatrix(blocks_, block_pairs)); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 242 | set_rhs(new double[lhs()->num_rows()]); |
| 243 | } |
| 244 | |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 245 | bool SparseSchurComplementSolver::SolveReducedLinearSystem(double* solution) { |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 246 | switch (options().sparse_linear_algebra_library_type) { |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 247 | case SUITE_SPARSE: |
| 248 | return SolveReducedLinearSystemUsingSuiteSparse(solution); |
| 249 | case CX_SPARSE: |
| 250 | return SolveReducedLinearSystemUsingCXSparse(solution); |
| 251 | default: |
| 252 | LOG(FATAL) << "Unknown sparse linear algebra library : " |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 253 | << options().sparse_linear_algebra_library_type; |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | LOG(FATAL) << "Unknown sparse linear algebra library : " |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 257 | << options().sparse_linear_algebra_library_type; |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 258 | return false; |
| 259 | } |
| 260 | |
| 261 | #ifndef CERES_NO_SUITESPARSE |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 262 | // Solve the system Sx = r, assuming that the matrix S is stored in a |
| 263 | // BlockRandomAccessSparseMatrix. The linear system is solved using |
| 264 | // CHOLMOD's sparse cholesky factorization routines. |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 265 | bool SparseSchurComplementSolver::SolveReducedLinearSystemUsingSuiteSparse( |
| 266 | double* solution) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 267 | TripletSparseMatrix* tsm = |
| 268 | const_cast<TripletSparseMatrix*>( |
| 269 | down_cast<const BlockRandomAccessSparseMatrix*>(lhs())->matrix()); |
| 270 | |
| 271 | const int num_rows = tsm->num_rows(); |
| 272 | |
| 273 | // The case where there are no f blocks, and the system is block |
| 274 | // diagonal. |
| 275 | if (num_rows == 0) { |
| 276 | return true; |
| 277 | } |
| 278 | |
Sameer Agarwal | d5b93bf | 2013-04-26 21:17:49 -0700 | [diff] [blame] | 279 | cholmod_sparse* cholmod_lhs = NULL; |
| 280 | if (options().use_postordering) { |
| 281 | // If we are going to do a full symbolic analysis of the schur |
| 282 | // complement matrix from scratch and not rely on the |
| 283 | // pre-ordering, then the fastest path in cholmod_factorize is the |
| 284 | // one corresponding to upper triangular matrices. |
| 285 | |
| 286 | // Create a upper triangular symmetric matrix. |
| 287 | cholmod_lhs = ss_.CreateSparseMatrix(tsm); |
| 288 | cholmod_lhs->stype = 1; |
| 289 | |
| 290 | if (factor_ == NULL) { |
| 291 | factor_ = ss_.BlockAnalyzeCholesky(cholmod_lhs, blocks_, blocks_); |
| 292 | } |
| 293 | } else { |
| 294 | // If we are going to use the natural ordering (i.e. rely on the |
| 295 | // pre-ordering computed by solver_impl.cc), then the fastest |
| 296 | // path in cholmod_factorize is the one corresponding to lower |
| 297 | // triangular matrices. |
| 298 | |
| 299 | // Create a upper triangular symmetric matrix. |
| 300 | cholmod_lhs = ss_.CreateSparseMatrixTranspose(tsm); |
| 301 | cholmod_lhs->stype = -1; |
| 302 | |
| 303 | if (factor_ == NULL) { |
| 304 | factor_ = ss_.AnalyzeCholeskyWithNaturalOrdering(cholmod_lhs); |
| 305 | } |
| 306 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 307 | |
| 308 | cholmod_dense* cholmod_rhs = |
| 309 | ss_.CreateDenseVector(const_cast<double*>(rhs()), num_rows, num_rows); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 310 | cholmod_dense* cholmod_solution = |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 311 | ss_.SolveCholesky(cholmod_lhs, factor_, cholmod_rhs); |
| 312 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 313 | ss_.Free(cholmod_lhs); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 314 | ss_.Free(cholmod_rhs); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 315 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 316 | if (cholmod_solution == NULL) { |
Sameer Agarwal | bdabc29 | 2012-11-07 11:26:32 -0800 | [diff] [blame] | 317 | LOG(WARNING) << "CHOLMOD solve failed."; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | |
| 321 | VectorRef(solution, num_rows) |
| 322 | = VectorRef(static_cast<double*>(cholmod_solution->x), num_rows); |
| 323 | ss_.Free(cholmod_solution); |
| 324 | return true; |
| 325 | } |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 326 | #else |
| 327 | bool SparseSchurComplementSolver::SolveReducedLinearSystemUsingSuiteSparse( |
| 328 | double* solution) { |
| 329 | LOG(FATAL) << "No SuiteSparse support in Ceres."; |
| 330 | return false; |
| 331 | } |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 332 | #endif // CERES_NO_SUITESPARSE |
| 333 | |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 334 | #ifndef CERES_NO_CXSPARSE |
| 335 | // Solve the system Sx = r, assuming that the matrix S is stored in a |
| 336 | // BlockRandomAccessSparseMatrix. The linear system is solved using |
| 337 | // CXSparse's sparse cholesky factorization routines. |
| 338 | bool SparseSchurComplementSolver::SolveReducedLinearSystemUsingCXSparse( |
| 339 | double* solution) { |
| 340 | // Extract the TripletSparseMatrix that is used for actually storing S. |
| 341 | TripletSparseMatrix* tsm = |
| 342 | const_cast<TripletSparseMatrix*>( |
| 343 | down_cast<const BlockRandomAccessSparseMatrix*>(lhs())->matrix()); |
| 344 | |
| 345 | const int num_rows = tsm->num_rows(); |
| 346 | |
| 347 | // The case where there are no f blocks, and the system is block |
| 348 | // diagonal. |
| 349 | if (num_rows == 0) { |
| 350 | return true; |
| 351 | } |
| 352 | |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 353 | cs_di* lhs = CHECK_NOTNULL(cxsparse_.CreateSparseMatrix(tsm)); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 354 | VectorRef(solution, num_rows) = ConstVectorRef(rhs(), num_rows); |
| 355 | |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 356 | // Compute symbolic factorization if not available. |
| 357 | if (cxsparse_factor_ == NULL) { |
Sameer Agarwal | d5b93bf | 2013-04-26 21:17:49 -0700 | [diff] [blame] | 358 | cxsparse_factor_ = |
| 359 | CHECK_NOTNULL(cxsparse_.BlockAnalyzeCholesky(lhs, blocks_, blocks_)); |
Petter Strandmark | 1e3cbd9 | 2012-08-29 09:39:56 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | // Solve the linear system. |
| 363 | bool ok = cxsparse_.SolveCholesky(lhs, cxsparse_factor_, solution); |
| 364 | |
| 365 | cxsparse_.Free(lhs); |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 366 | return ok; |
| 367 | } |
| 368 | #else |
| 369 | bool SparseSchurComplementSolver::SolveReducedLinearSystemUsingCXSparse( |
| 370 | double* solution) { |
| 371 | LOG(FATAL) << "No CXSparse support in Ceres."; |
| 372 | return false; |
| 373 | } |
| 374 | #endif // CERES_NO_CXPARSE |
| 375 | |
Sameer Agarwal | 700d50d | 2013-03-12 16:12:42 -0700 | [diff] [blame] | 376 | #endif // !defined(CERES_NO_SUITESPARSE) || !defined(CERES_NO_CXSPARE) |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 377 | } // namespace internal |
| 378 | } // namespace ceres |