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 | #ifndef CERES_NO_SUITESPARSE |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 32 | #include "ceres/suitesparse.h" |
| 33 | |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 34 | #include <vector> |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 35 | #include "cholmod.h" |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 36 | #include "ceres/compressed_col_sparse_matrix_utils.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 37 | #include "ceres/compressed_row_sparse_matrix.h" |
Sameer Agarwal | 79bde35 | 2013-11-21 21:33:51 -0800 | [diff] [blame] | 38 | #include "ceres/linear_solver.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 39 | #include "ceres/triplet_sparse_matrix.h" |
Sameer Agarwal | 222ca20 | 2013-04-01 09:11:07 -0700 | [diff] [blame] | 40 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 41 | namespace ceres { |
| 42 | namespace internal { |
Sameer Agarwal | 222ca20 | 2013-04-01 09:11:07 -0700 | [diff] [blame] | 43 | |
| 44 | SuiteSparse::SuiteSparse() { |
| 45 | cholmod_start(&cc_); |
| 46 | } |
| 47 | |
| 48 | SuiteSparse::~SuiteSparse() { |
| 49 | cholmod_finish(&cc_); |
| 50 | } |
| 51 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 52 | cholmod_sparse* SuiteSparse::CreateSparseMatrix(TripletSparseMatrix* A) { |
| 53 | cholmod_triplet triplet; |
| 54 | |
| 55 | triplet.nrow = A->num_rows(); |
| 56 | triplet.ncol = A->num_cols(); |
| 57 | triplet.nzmax = A->max_num_nonzeros(); |
| 58 | triplet.nnz = A->num_nonzeros(); |
| 59 | triplet.i = reinterpret_cast<void*>(A->mutable_rows()); |
| 60 | triplet.j = reinterpret_cast<void*>(A->mutable_cols()); |
| 61 | triplet.x = reinterpret_cast<void*>(A->mutable_values()); |
| 62 | triplet.stype = 0; // Matrix is not symmetric. |
| 63 | triplet.itype = CHOLMOD_INT; |
| 64 | triplet.xtype = CHOLMOD_REAL; |
| 65 | triplet.dtype = CHOLMOD_DOUBLE; |
| 66 | |
| 67 | return cholmod_triplet_to_sparse(&triplet, triplet.nnz, &cc_); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | cholmod_sparse* SuiteSparse::CreateSparseMatrixTranspose( |
| 72 | TripletSparseMatrix* A) { |
| 73 | cholmod_triplet triplet; |
| 74 | |
| 75 | triplet.ncol = A->num_rows(); // swap row and columns |
| 76 | triplet.nrow = A->num_cols(); |
| 77 | triplet.nzmax = A->max_num_nonzeros(); |
| 78 | triplet.nnz = A->num_nonzeros(); |
| 79 | |
| 80 | // swap rows and columns |
| 81 | triplet.j = reinterpret_cast<void*>(A->mutable_rows()); |
| 82 | triplet.i = reinterpret_cast<void*>(A->mutable_cols()); |
| 83 | triplet.x = reinterpret_cast<void*>(A->mutable_values()); |
| 84 | triplet.stype = 0; // Matrix is not symmetric. |
| 85 | triplet.itype = CHOLMOD_INT; |
| 86 | triplet.xtype = CHOLMOD_REAL; |
| 87 | triplet.dtype = CHOLMOD_DOUBLE; |
| 88 | |
| 89 | return cholmod_triplet_to_sparse(&triplet, triplet.nnz, &cc_); |
| 90 | } |
| 91 | |
Sameer Agarwal | 2560b17 | 2013-04-19 08:19:11 -0700 | [diff] [blame] | 92 | cholmod_sparse SuiteSparse::CreateSparseMatrixTransposeView( |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 93 | CompressedRowSparseMatrix* A) { |
Sameer Agarwal | 2560b17 | 2013-04-19 08:19:11 -0700 | [diff] [blame] | 94 | cholmod_sparse m; |
| 95 | m.nrow = A->num_cols(); |
| 96 | m.ncol = A->num_rows(); |
| 97 | m.nzmax = A->num_nonzeros(); |
| 98 | m.nz = NULL; |
| 99 | m.p = reinterpret_cast<void*>(A->mutable_rows()); |
| 100 | m.i = reinterpret_cast<void*>(A->mutable_cols()); |
| 101 | m.x = reinterpret_cast<void*>(A->mutable_values()); |
| 102 | m.z = NULL; |
| 103 | m.stype = 0; // Matrix is not symmetric. |
| 104 | m.itype = CHOLMOD_INT; |
| 105 | m.xtype = CHOLMOD_REAL; |
| 106 | m.dtype = CHOLMOD_DOUBLE; |
| 107 | m.sorted = 1; |
| 108 | m.packed = 1; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 109 | |
| 110 | return m; |
| 111 | } |
| 112 | |
| 113 | cholmod_dense* SuiteSparse::CreateDenseVector(const double* x, |
| 114 | int in_size, |
| 115 | int out_size) { |
| 116 | CHECK_LE(in_size, out_size); |
| 117 | cholmod_dense* v = cholmod_zeros(out_size, 1, CHOLMOD_REAL, &cc_); |
| 118 | if (x != NULL) { |
| 119 | memcpy(v->x, x, in_size*sizeof(*x)); |
| 120 | } |
| 121 | return v; |
| 122 | } |
| 123 | |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 124 | cholmod_factor* SuiteSparse::AnalyzeCholesky(cholmod_sparse* A, |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 125 | string* message) { |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 126 | // Cholmod can try multiple re-ordering strategies to find a fill |
| 127 | // reducing ordering. Here we just tell it use AMD with automatic |
| 128 | // matrix dependence choice of supernodal versus simplicial |
| 129 | // factorization. |
| 130 | cc_.nmethods = 1; |
| 131 | cc_.method[0].ordering = CHOLMOD_AMD; |
| 132 | cc_.supernodal = CHOLMOD_AUTO; |
Sameer Agarwal | 222ca20 | 2013-04-01 09:11:07 -0700 | [diff] [blame] | 133 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 134 | cholmod_factor* factor = cholmod_analyze(A, &cc_); |
Sameer Agarwal | 222ca20 | 2013-04-01 09:11:07 -0700 | [diff] [blame] | 135 | if (VLOG_IS_ON(2)) { |
| 136 | cholmod_print_common(const_cast<char*>("Symbolic Analysis"), &cc_); |
| 137 | } |
| 138 | |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 139 | if (cc_.status != CHOLMOD_OK) { |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 140 | *message = StringPrintf("cholmod_analyze failed. error code: %d", |
Sameer Agarwal | d73acd0 | 2013-12-02 12:02:03 -0800 | [diff] [blame] | 141 | cc_.status); |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | return CHECK_NOTNULL(factor); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 148 | cholmod_factor* SuiteSparse::BlockAnalyzeCholesky( |
| 149 | cholmod_sparse* A, |
| 150 | const vector<int>& row_blocks, |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 151 | const vector<int>& col_blocks, |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 152 | string* message) { |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 153 | vector<int> ordering; |
| 154 | if (!BlockAMDOrdering(A, row_blocks, col_blocks, &ordering)) { |
| 155 | return NULL; |
| 156 | } |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 157 | return AnalyzeCholeskyWithUserOrdering(A, ordering, message); |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 160 | cholmod_factor* SuiteSparse::AnalyzeCholeskyWithUserOrdering( |
| 161 | cholmod_sparse* A, |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 162 | const vector<int>& ordering, |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 163 | string* message) { |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 164 | CHECK_EQ(ordering.size(), A->nrow); |
Sameer Agarwal | 222ca20 | 2013-04-01 09:11:07 -0700 | [diff] [blame] | 165 | |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 166 | cc_.nmethods = 1; |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 167 | cc_.method[0].ordering = CHOLMOD_GIVEN; |
Sameer Agarwal | 222ca20 | 2013-04-01 09:11:07 -0700 | [diff] [blame] | 168 | |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 169 | cholmod_factor* factor = |
| 170 | cholmod_analyze_p(A, const_cast<int*>(&ordering[0]), NULL, 0, &cc_); |
Sameer Agarwal | 222ca20 | 2013-04-01 09:11:07 -0700 | [diff] [blame] | 171 | if (VLOG_IS_ON(2)) { |
| 172 | cholmod_print_common(const_cast<char*>("Symbolic Analysis"), &cc_); |
| 173 | } |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 174 | if (cc_.status != CHOLMOD_OK) { |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 175 | *message = StringPrintf("cholmod_analyze failed. error code: %d", |
Sameer Agarwal | d73acd0 | 2013-12-02 12:02:03 -0800 | [diff] [blame] | 176 | cc_.status); |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 177 | return NULL; |
| 178 | } |
Sameer Agarwal | 222ca20 | 2013-04-01 09:11:07 -0700 | [diff] [blame] | 179 | |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 180 | return CHECK_NOTNULL(factor); |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Sameer Agarwal | cbdeb79 | 2013-04-22 10:18:18 -0700 | [diff] [blame] | 183 | cholmod_factor* SuiteSparse::AnalyzeCholeskyWithNaturalOrdering( |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 184 | cholmod_sparse* A, |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 185 | string* message) { |
Sameer Agarwal | 2560b17 | 2013-04-19 08:19:11 -0700 | [diff] [blame] | 186 | cc_.nmethods = 1; |
| 187 | cc_.method[0].ordering = CHOLMOD_NATURAL; |
| 188 | cc_.postorder = 0; |
| 189 | |
| 190 | cholmod_factor* factor = cholmod_analyze(A, &cc_); |
Sameer Agarwal | 2560b17 | 2013-04-19 08:19:11 -0700 | [diff] [blame] | 191 | if (VLOG_IS_ON(2)) { |
| 192 | cholmod_print_common(const_cast<char*>("Symbolic Analysis"), &cc_); |
| 193 | } |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 194 | if (cc_.status != CHOLMOD_OK) { |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 195 | *message = StringPrintf("cholmod_analyze failed. error code: %d", |
Sameer Agarwal | d73acd0 | 2013-12-02 12:02:03 -0800 | [diff] [blame] | 196 | cc_.status); |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 197 | return NULL; |
| 198 | } |
Sameer Agarwal | 2560b17 | 2013-04-19 08:19:11 -0700 | [diff] [blame] | 199 | |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 200 | return CHECK_NOTNULL(factor); |
Sameer Agarwal | 2560b17 | 2013-04-19 08:19:11 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 203 | bool SuiteSparse::BlockAMDOrdering(const cholmod_sparse* A, |
| 204 | const vector<int>& row_blocks, |
| 205 | const vector<int>& col_blocks, |
| 206 | vector<int>* ordering) { |
| 207 | const int num_row_blocks = row_blocks.size(); |
| 208 | const int num_col_blocks = col_blocks.size(); |
| 209 | |
| 210 | // Arrays storing the compressed column structure of the matrix |
| 211 | // incoding the block sparsity of A. |
| 212 | vector<int> block_cols; |
| 213 | vector<int> block_rows; |
| 214 | |
Sameer Agarwal | 344c09f | 2013-04-20 16:07:56 -0700 | [diff] [blame] | 215 | CompressedColumnScalarMatrixToBlockMatrix(reinterpret_cast<const int*>(A->i), |
| 216 | reinterpret_cast<const int*>(A->p), |
| 217 | row_blocks, |
| 218 | col_blocks, |
| 219 | &block_rows, |
| 220 | &block_cols); |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 221 | |
| 222 | cholmod_sparse_struct block_matrix; |
| 223 | block_matrix.nrow = num_row_blocks; |
| 224 | block_matrix.ncol = num_col_blocks; |
| 225 | block_matrix.nzmax = block_rows.size(); |
| 226 | block_matrix.p = reinterpret_cast<void*>(&block_cols[0]); |
| 227 | block_matrix.i = reinterpret_cast<void*>(&block_rows[0]); |
| 228 | block_matrix.x = NULL; |
| 229 | block_matrix.stype = A->stype; |
| 230 | block_matrix.itype = CHOLMOD_INT; |
| 231 | block_matrix.xtype = CHOLMOD_PATTERN; |
| 232 | block_matrix.dtype = CHOLMOD_DOUBLE; |
| 233 | block_matrix.sorted = 1; |
| 234 | block_matrix.packed = 1; |
| 235 | |
| 236 | vector<int> block_ordering(num_row_blocks); |
| 237 | if (!cholmod_amd(&block_matrix, NULL, 0, &block_ordering[0], &cc_)) { |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | BlockOrderingToScalarOrdering(row_blocks, block_ordering, ordering); |
| 242 | return true; |
| 243 | } |
| 244 | |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 245 | LinearSolverTerminationType SuiteSparse::Cholesky(cholmod_sparse* A, |
| 246 | cholmod_factor* L, |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 247 | string* message) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 248 | CHECK_NOTNULL(A); |
| 249 | CHECK_NOTNULL(L); |
| 250 | |
Sameer Agarwal | 222ca20 | 2013-04-01 09:11:07 -0700 | [diff] [blame] | 251 | // Save the current print level and silence CHOLMOD, otherwise |
| 252 | // CHOLMOD is prone to dumping stuff to stderr, which can be |
| 253 | // distracting when the error (matrix is indefinite) is not a fatal |
| 254 | // failure. |
| 255 | const int old_print_level = cc_.print; |
| 256 | cc_.print = 0; |
| 257 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 258 | cc_.quick_return_if_not_posdef = 1; |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 259 | int cholmod_status = cholmod_factorize(A, L, &cc_); |
Sameer Agarwal | 222ca20 | 2013-04-01 09:11:07 -0700 | [diff] [blame] | 260 | cc_.print = old_print_level; |
| 261 | |
| 262 | // TODO(sameeragarwal): This switch statement is not consistent. It |
| 263 | // treats all kinds of CHOLMOD failures as warnings. Some of these |
| 264 | // like out of memory are definitely not warnings. The problem is |
| 265 | // that the return value Cholesky is two valued, but the state of |
| 266 | // the linear solver is really three valued. SUCCESS, |
| 267 | // NON_FATAL_FAILURE (e.g., indefinite matrix) and FATAL_FAILURE |
| 268 | // (e.g. out of memory). |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 269 | switch (cc_.status) { |
| 270 | case CHOLMOD_NOT_INSTALLED: |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 271 | *message = "CHOLMOD failure: Method not installed."; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 272 | return LINEAR_SOLVER_FATAL_ERROR; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 273 | case CHOLMOD_OUT_OF_MEMORY: |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 274 | *message = "CHOLMOD failure: Out of memory."; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 275 | return LINEAR_SOLVER_FATAL_ERROR; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 276 | case CHOLMOD_TOO_LARGE: |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 277 | *message = "CHOLMOD failure: Integer overflow occured."; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 278 | return LINEAR_SOLVER_FATAL_ERROR; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 279 | case CHOLMOD_INVALID: |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 280 | *message = "CHOLMOD failure: Invalid input."; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 281 | return LINEAR_SOLVER_FATAL_ERROR; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 282 | case CHOLMOD_NOT_POSDEF: |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 283 | *message = "CHOLMOD warning: Matrix not positive definite."; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 284 | return LINEAR_SOLVER_FAILURE; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 285 | case CHOLMOD_DSMALL: |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 286 | *message = "CHOLMOD warning: D for LDL' or diag(L) or " |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 287 | "LL' has tiny absolute value."; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 288 | return LINEAR_SOLVER_FAILURE; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 289 | case CHOLMOD_OK: |
Sameer Agarwal | 3faac6a | 2013-11-28 07:13:26 -0800 | [diff] [blame] | 290 | if (cholmod_status != 0) { |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 291 | return LINEAR_SOLVER_SUCCESS; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 292 | } |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 293 | |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 294 | *message = "CHOLMOD failure: cholmod_factorize returned false " |
Sameer Agarwal | d73acd0 | 2013-12-02 12:02:03 -0800 | [diff] [blame] | 295 | "but cholmod_common::status is CHOLMOD_OK." |
| 296 | "Please report this to ceres-solver@googlegroups.com."; |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 297 | return LINEAR_SOLVER_FATAL_ERROR; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 298 | default: |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 299 | *message = |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 300 | StringPrintf("Unknown cholmod return code: %d. " |
| 301 | "Please report this to ceres-solver@googlegroups.com.", |
| 302 | cc_.status); |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 303 | return LINEAR_SOLVER_FATAL_ERROR; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 304 | } |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 305 | |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 306 | return LINEAR_SOLVER_FATAL_ERROR; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | cholmod_dense* SuiteSparse::Solve(cholmod_factor* L, |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 310 | cholmod_dense* b, |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 311 | string* message) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 312 | if (cc_.status != CHOLMOD_OK) { |
Sameer Agarwal | ed92366 | 2013-11-28 06:50:43 -0800 | [diff] [blame] | 313 | *message = "cholmod_solve failed. CHOLMOD status is not CHOLMOD_OK"; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 314 | return NULL; |
| 315 | } |
| 316 | |
| 317 | return cholmod_solve(CHOLMOD_A, L, b, &cc_); |
| 318 | } |
| 319 | |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 320 | bool SuiteSparse::ApproximateMinimumDegreeOrdering(cholmod_sparse* matrix, |
Sameer Agarwal | f7ed22e | 2013-04-19 14:24:48 -0700 | [diff] [blame] | 321 | int* ordering) { |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 322 | return cholmod_amd(matrix, NULL, 0, ordering, &cc_); |
Sameer Agarwal | f7ed22e | 2013-04-19 14:24:48 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 325 | bool SuiteSparse::ConstrainedApproximateMinimumDegreeOrdering( |
Sameer Agarwal | d5b93bf | 2013-04-26 21:17:49 -0700 | [diff] [blame] | 326 | cholmod_sparse* matrix, |
| 327 | int* constraints, |
| 328 | int* ordering) { |
| 329 | #ifndef CERES_NO_CAMD |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 330 | return cholmod_camd(matrix, NULL, 0, constraints, ordering, &cc_); |
Sameer Agarwal | d5b93bf | 2013-04-26 21:17:49 -0700 | [diff] [blame] | 331 | #else |
| 332 | LOG(FATAL) << "Congratulations you have found a bug in Ceres." |
| 333 | << "Ceres Solver was compiled with SuiteSparse " |
| 334 | << "version 4.1.0 or less. Calling this function " |
| 335 | << "in that case is a bug. Please contact the" |
Sameer Agarwal | 0e0a454 | 2013-04-29 17:27:26 -0700 | [diff] [blame] | 336 | << "the Ceres Solver developers."; |
Sameer Agarwal | b16e118 | 2013-11-25 05:47:43 -0800 | [diff] [blame] | 337 | return false; |
Sameer Agarwal | d5b93bf | 2013-04-26 21:17:49 -0700 | [diff] [blame] | 338 | #endif |
| 339 | } |
| 340 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 341 | } // namespace internal |
| 342 | } // namespace ceres |
| 343 | |
| 344 | #endif // CERES_NO_SUITESPARSE |