Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 3 | // http://ceres-solver.org/ |
| 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 | // This include must come before any #ifndef check on Ceres compile options. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 32 | #include "ceres/internal/config.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 33 | |
| 34 | #ifndef CERES_NO_SUITESPARSE |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 35 | |
| 36 | #include <memory> |
| 37 | #include <string> |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 38 | #include <vector> |
| 39 | |
| 40 | #include "ceres/compressed_col_sparse_matrix_utils.h" |
| 41 | #include "ceres/compressed_row_sparse_matrix.h" |
| 42 | #include "ceres/linear_solver.h" |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 43 | #include "ceres/suitesparse.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 44 | #include "ceres/triplet_sparse_matrix.h" |
| 45 | #include "cholmod.h" |
| 46 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 47 | namespace ceres::internal { |
| 48 | namespace { |
| 49 | int OrderingTypeToCHOLMODEnum(OrderingType ordering_type) { |
| 50 | if (ordering_type == OrderingType::AMD) { |
| 51 | return CHOLMOD_AMD; |
| 52 | } |
| 53 | if (ordering_type == OrderingType::NESDIS) { |
| 54 | return CHOLMOD_NESDIS; |
| 55 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 56 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 57 | if (ordering_type == OrderingType::NATURAL) { |
| 58 | return CHOLMOD_NATURAL; |
| 59 | } |
| 60 | LOG(FATAL) << "Congratulations you have discovered a bug in Ceres Solver." |
| 61 | << "Please report it to the developers. " << ordering_type; |
| 62 | return -1; |
| 63 | } |
| 64 | } // namespace |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 65 | |
| 66 | SuiteSparse::SuiteSparse() { cholmod_start(&cc_); } |
| 67 | |
| 68 | SuiteSparse::~SuiteSparse() { cholmod_finish(&cc_); } |
| 69 | |
| 70 | cholmod_sparse* SuiteSparse::CreateSparseMatrix(TripletSparseMatrix* A) { |
| 71 | cholmod_triplet triplet; |
| 72 | |
| 73 | triplet.nrow = A->num_rows(); |
| 74 | triplet.ncol = A->num_cols(); |
| 75 | triplet.nzmax = A->max_num_nonzeros(); |
| 76 | triplet.nnz = A->num_nonzeros(); |
| 77 | triplet.i = reinterpret_cast<void*>(A->mutable_rows()); |
| 78 | triplet.j = reinterpret_cast<void*>(A->mutable_cols()); |
| 79 | triplet.x = reinterpret_cast<void*>(A->mutable_values()); |
| 80 | triplet.stype = 0; // Matrix is not symmetric. |
| 81 | triplet.itype = CHOLMOD_INT; |
| 82 | triplet.xtype = CHOLMOD_REAL; |
| 83 | triplet.dtype = CHOLMOD_DOUBLE; |
| 84 | |
| 85 | return cholmod_triplet_to_sparse(&triplet, triplet.nnz, &cc_); |
| 86 | } |
| 87 | |
| 88 | cholmod_sparse* SuiteSparse::CreateSparseMatrixTranspose( |
| 89 | TripletSparseMatrix* A) { |
| 90 | cholmod_triplet triplet; |
| 91 | |
| 92 | triplet.ncol = A->num_rows(); // swap row and columns |
| 93 | triplet.nrow = A->num_cols(); |
| 94 | triplet.nzmax = A->max_num_nonzeros(); |
| 95 | triplet.nnz = A->num_nonzeros(); |
| 96 | |
| 97 | // swap rows and columns |
| 98 | triplet.j = reinterpret_cast<void*>(A->mutable_rows()); |
| 99 | triplet.i = reinterpret_cast<void*>(A->mutable_cols()); |
| 100 | triplet.x = reinterpret_cast<void*>(A->mutable_values()); |
| 101 | triplet.stype = 0; // Matrix is not symmetric. |
| 102 | triplet.itype = CHOLMOD_INT; |
| 103 | triplet.xtype = CHOLMOD_REAL; |
| 104 | triplet.dtype = CHOLMOD_DOUBLE; |
| 105 | |
| 106 | return cholmod_triplet_to_sparse(&triplet, triplet.nnz, &cc_); |
| 107 | } |
| 108 | |
| 109 | cholmod_sparse SuiteSparse::CreateSparseMatrixTransposeView( |
| 110 | CompressedRowSparseMatrix* A) { |
| 111 | cholmod_sparse m; |
| 112 | m.nrow = A->num_cols(); |
| 113 | m.ncol = A->num_rows(); |
| 114 | m.nzmax = A->num_nonzeros(); |
| 115 | m.nz = nullptr; |
| 116 | m.p = reinterpret_cast<void*>(A->mutable_rows()); |
| 117 | m.i = reinterpret_cast<void*>(A->mutable_cols()); |
| 118 | m.x = reinterpret_cast<void*>(A->mutable_values()); |
| 119 | m.z = nullptr; |
| 120 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 121 | if (A->storage_type() == |
| 122 | CompressedRowSparseMatrix::StorageType::LOWER_TRIANGULAR) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 123 | m.stype = 1; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 124 | } else if (A->storage_type() == |
| 125 | CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 126 | m.stype = -1; |
| 127 | } else { |
| 128 | m.stype = 0; |
| 129 | } |
| 130 | |
| 131 | m.itype = CHOLMOD_INT; |
| 132 | m.xtype = CHOLMOD_REAL; |
| 133 | m.dtype = CHOLMOD_DOUBLE; |
| 134 | m.sorted = 1; |
| 135 | m.packed = 1; |
| 136 | |
| 137 | return m; |
| 138 | } |
| 139 | |
| 140 | cholmod_dense SuiteSparse::CreateDenseVectorView(const double* x, int size) { |
| 141 | cholmod_dense v; |
| 142 | v.nrow = size; |
| 143 | v.ncol = 1; |
| 144 | v.nzmax = size; |
| 145 | v.d = size; |
| 146 | v.x = const_cast<void*>(reinterpret_cast<const void*>(x)); |
| 147 | v.xtype = CHOLMOD_REAL; |
| 148 | v.dtype = CHOLMOD_DOUBLE; |
| 149 | return v; |
| 150 | } |
| 151 | |
| 152 | cholmod_dense* SuiteSparse::CreateDenseVector(const double* x, |
| 153 | int in_size, |
| 154 | int out_size) { |
| 155 | CHECK_LE(in_size, out_size); |
| 156 | cholmod_dense* v = cholmod_zeros(out_size, 1, CHOLMOD_REAL, &cc_); |
| 157 | if (x != nullptr) { |
| 158 | memcpy(v->x, x, in_size * sizeof(*x)); |
| 159 | } |
| 160 | return v; |
| 161 | } |
| 162 | |
| 163 | cholmod_factor* SuiteSparse::AnalyzeCholesky(cholmod_sparse* A, |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 164 | OrderingType ordering_type, |
| 165 | std::string* message) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 166 | cc_.nmethods = 1; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 167 | cc_.method[0].ordering = OrderingTypeToCHOLMODEnum(ordering_type); |
| 168 | |
| 169 | // postordering with a NATURAL ordering leads to a significant regression in |
| 170 | // performance. See https://github.com/ceres-solver/ceres-solver/issues/905 |
| 171 | if (ordering_type == OrderingType::NATURAL) { |
| 172 | cc_.postorder = 0; |
| 173 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 174 | |
| 175 | cholmod_factor* factor = cholmod_analyze(A, &cc_); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 176 | |
| 177 | if (cc_.status != CHOLMOD_OK) { |
| 178 | *message = |
| 179 | StringPrintf("cholmod_analyze failed. error code: %d", cc_.status); |
| 180 | return nullptr; |
| 181 | } |
| 182 | |
| 183 | CHECK(factor != nullptr); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 184 | if (VLOG_IS_ON(2)) { |
| 185 | cholmod_print_common(const_cast<char*>("Symbolic Analysis"), &cc_); |
| 186 | } |
| 187 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 188 | return factor; |
| 189 | } |
| 190 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 191 | cholmod_factor* SuiteSparse::AnalyzeCholeskyWithGivenOrdering( |
| 192 | cholmod_sparse* A, const std::vector<int>& ordering, std::string* message) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 193 | CHECK_EQ(ordering.size(), A->nrow); |
| 194 | |
| 195 | cc_.nmethods = 1; |
| 196 | cc_.method[0].ordering = CHOLMOD_GIVEN; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 197 | cholmod_factor* factor = |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 198 | cholmod_analyze_p(A, const_cast<int*>(ordering.data()), nullptr, 0, &cc_); |
| 199 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 200 | if (cc_.status != CHOLMOD_OK) { |
| 201 | *message = |
| 202 | StringPrintf("cholmod_analyze failed. error code: %d", cc_.status); |
| 203 | return nullptr; |
| 204 | } |
| 205 | |
| 206 | CHECK(factor != nullptr); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 207 | if (VLOG_IS_ON(2)) { |
| 208 | cholmod_print_common(const_cast<char*>("Symbolic Analysis"), &cc_); |
| 209 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 210 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 211 | return factor; |
| 212 | } |
| 213 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 214 | bool SuiteSparse::BlockOrdering(const cholmod_sparse* A, |
| 215 | OrderingType ordering_type, |
| 216 | const std::vector<Block>& row_blocks, |
| 217 | const std::vector<Block>& col_blocks, |
| 218 | std::vector<int>* ordering) { |
| 219 | if (ordering_type == OrderingType::NATURAL) { |
| 220 | ordering->resize(A->nrow); |
| 221 | for (int i = 0; i < A->nrow; ++i) { |
| 222 | (*ordering)[i] = i; |
| 223 | } |
| 224 | return true; |
| 225 | } |
| 226 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 227 | const int num_row_blocks = row_blocks.size(); |
| 228 | const int num_col_blocks = col_blocks.size(); |
| 229 | |
| 230 | // Arrays storing the compressed column structure of the matrix |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 231 | // encoding the block sparsity of A. |
| 232 | std::vector<int> block_cols; |
| 233 | std::vector<int> block_rows; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 234 | |
| 235 | CompressedColumnScalarMatrixToBlockMatrix(reinterpret_cast<const int*>(A->i), |
| 236 | reinterpret_cast<const int*>(A->p), |
| 237 | row_blocks, |
| 238 | col_blocks, |
| 239 | &block_rows, |
| 240 | &block_cols); |
| 241 | cholmod_sparse_struct block_matrix; |
| 242 | block_matrix.nrow = num_row_blocks; |
| 243 | block_matrix.ncol = num_col_blocks; |
| 244 | block_matrix.nzmax = block_rows.size(); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 245 | block_matrix.p = reinterpret_cast<void*>(block_cols.data()); |
| 246 | block_matrix.i = reinterpret_cast<void*>(block_rows.data()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 247 | block_matrix.x = nullptr; |
| 248 | block_matrix.stype = A->stype; |
| 249 | block_matrix.itype = CHOLMOD_INT; |
| 250 | block_matrix.xtype = CHOLMOD_PATTERN; |
| 251 | block_matrix.dtype = CHOLMOD_DOUBLE; |
| 252 | block_matrix.sorted = 1; |
| 253 | block_matrix.packed = 1; |
| 254 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 255 | std::vector<int> block_ordering(num_row_blocks); |
| 256 | if (!Ordering(&block_matrix, ordering_type, block_ordering.data())) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 257 | return false; |
| 258 | } |
| 259 | |
| 260 | BlockOrderingToScalarOrdering(row_blocks, block_ordering, ordering); |
| 261 | return true; |
| 262 | } |
| 263 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 264 | cholmod_factor* SuiteSparse::BlockAnalyzeCholesky( |
| 265 | cholmod_sparse* A, |
| 266 | OrderingType ordering_type, |
| 267 | const std::vector<Block>& row_blocks, |
| 268 | const std::vector<Block>& col_blocks, |
| 269 | std::string* message) { |
| 270 | std::vector<int> ordering; |
| 271 | if (!BlockOrdering(A, ordering_type, row_blocks, col_blocks, &ordering)) { |
| 272 | return nullptr; |
| 273 | } |
| 274 | return AnalyzeCholeskyWithGivenOrdering(A, ordering, message); |
| 275 | } |
| 276 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 277 | LinearSolverTerminationType SuiteSparse::Cholesky(cholmod_sparse* A, |
| 278 | cholmod_factor* L, |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 279 | std::string* message) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 280 | CHECK(A != nullptr); |
| 281 | CHECK(L != nullptr); |
| 282 | |
| 283 | // Save the current print level and silence CHOLMOD, otherwise |
| 284 | // CHOLMOD is prone to dumping stuff to stderr, which can be |
| 285 | // distracting when the error (matrix is indefinite) is not a fatal |
| 286 | // failure. |
| 287 | const int old_print_level = cc_.print; |
| 288 | cc_.print = 0; |
| 289 | |
| 290 | cc_.quick_return_if_not_posdef = 1; |
| 291 | int cholmod_status = cholmod_factorize(A, L, &cc_); |
| 292 | cc_.print = old_print_level; |
| 293 | |
| 294 | switch (cc_.status) { |
| 295 | case CHOLMOD_NOT_INSTALLED: |
| 296 | *message = "CHOLMOD failure: Method not installed."; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 297 | return LinearSolverTerminationType::FATAL_ERROR; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 298 | case CHOLMOD_OUT_OF_MEMORY: |
| 299 | *message = "CHOLMOD failure: Out of memory."; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 300 | return LinearSolverTerminationType::FATAL_ERROR; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 301 | case CHOLMOD_TOO_LARGE: |
| 302 | *message = "CHOLMOD failure: Integer overflow occurred."; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 303 | return LinearSolverTerminationType::FATAL_ERROR; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 304 | case CHOLMOD_INVALID: |
| 305 | *message = "CHOLMOD failure: Invalid input."; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 306 | return LinearSolverTerminationType::FATAL_ERROR; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 307 | case CHOLMOD_NOT_POSDEF: |
| 308 | *message = "CHOLMOD warning: Matrix not positive definite."; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 309 | return LinearSolverTerminationType::FAILURE; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 310 | case CHOLMOD_DSMALL: |
| 311 | *message = |
| 312 | "CHOLMOD warning: D for LDL' or diag(L) or " |
| 313 | "LL' has tiny absolute value."; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 314 | return LinearSolverTerminationType::FAILURE; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 315 | case CHOLMOD_OK: |
| 316 | if (cholmod_status != 0) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 317 | return LinearSolverTerminationType::SUCCESS; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | *message = |
| 321 | "CHOLMOD failure: cholmod_factorize returned false " |
| 322 | "but cholmod_common::status is CHOLMOD_OK." |
| 323 | "Please report this to ceres-solver@googlegroups.com."; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 324 | return LinearSolverTerminationType::FATAL_ERROR; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 325 | default: |
| 326 | *message = StringPrintf( |
| 327 | "Unknown cholmod return code: %d. " |
| 328 | "Please report this to ceres-solver@googlegroups.com.", |
| 329 | cc_.status); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 330 | return LinearSolverTerminationType::FATAL_ERROR; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 333 | return LinearSolverTerminationType::FATAL_ERROR; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | cholmod_dense* SuiteSparse::Solve(cholmod_factor* L, |
| 337 | cholmod_dense* b, |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 338 | std::string* message) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 339 | if (cc_.status != CHOLMOD_OK) { |
| 340 | *message = "cholmod_solve failed. CHOLMOD status is not CHOLMOD_OK"; |
| 341 | return nullptr; |
| 342 | } |
| 343 | |
| 344 | return cholmod_solve(CHOLMOD_A, L, b, &cc_); |
| 345 | } |
| 346 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 347 | bool SuiteSparse::Ordering(cholmod_sparse* matrix, |
| 348 | OrderingType ordering_type, |
| 349 | int* ordering) { |
| 350 | CHECK_NE(ordering_type, OrderingType::NATURAL); |
| 351 | if (ordering_type == OrderingType::AMD) { |
| 352 | return cholmod_amd(matrix, nullptr, 0, ordering, &cc_); |
| 353 | } |
| 354 | |
| 355 | #ifdef CERES_NO_CHOLMOD_PARTITION |
| 356 | return false; |
| 357 | #else |
| 358 | std::vector<int> CParent(matrix->nrow, 0); |
| 359 | std::vector<int> CMember(matrix->nrow, 0); |
| 360 | return cholmod_nested_dissection( |
| 361 | matrix, nullptr, 0, ordering, CParent.data(), CMember.data(), &cc_); |
| 362 | #endif |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | bool SuiteSparse::ConstrainedApproximateMinimumDegreeOrdering( |
| 366 | cholmod_sparse* matrix, int* constraints, int* ordering) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 367 | return cholmod_camd(matrix, nullptr, 0, constraints, ordering, &cc_); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 368 | } |
| 369 | |
| 370 | bool SuiteSparse::IsNestedDissectionAvailable() { |
| 371 | #ifdef CERES_NO_CHOLMOD_PARTITION |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 372 | return false; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 373 | #else |
| 374 | return true; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 375 | #endif |
| 376 | } |
| 377 | |
| 378 | std::unique_ptr<SparseCholesky> SuiteSparseCholesky::Create( |
| 379 | const OrderingType ordering_type) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 380 | return std::unique_ptr<SparseCholesky>( |
| 381 | new SuiteSparseCholesky(ordering_type)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | SuiteSparseCholesky::SuiteSparseCholesky(const OrderingType ordering_type) |
| 385 | : ordering_type_(ordering_type), factor_(nullptr) {} |
| 386 | |
| 387 | SuiteSparseCholesky::~SuiteSparseCholesky() { |
| 388 | if (factor_ != nullptr) { |
| 389 | ss_.Free(factor_); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | LinearSolverTerminationType SuiteSparseCholesky::Factorize( |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 394 | CompressedRowSparseMatrix* lhs, std::string* message) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 395 | if (lhs == nullptr) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 396 | *message = "Failure: Input lhs is nullptr."; |
| 397 | return LinearSolverTerminationType::FATAL_ERROR; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | cholmod_sparse cholmod_lhs = ss_.CreateSparseMatrixTransposeView(lhs); |
| 401 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 402 | // If a factorization does not exist, compute the symbolic |
| 403 | // factorization first. |
| 404 | // |
| 405 | // If the ordering type is NATURAL, then there is no fill reducing |
| 406 | // ordering to be computed, regardless of block structure, so we can |
| 407 | // just call the scalar version of symbolic factorization. For |
| 408 | // SuiteSparse this is the common case since we have already |
| 409 | // pre-ordered the columns of the Jacobian. |
| 410 | // |
| 411 | // Similarly regardless of ordering type, if there is no block |
| 412 | // structure in the matrix we call the scalar version of symbolic |
| 413 | // factorization. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 414 | if (factor_ == nullptr) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 415 | if (ordering_type_ == OrderingType::NATURAL || |
| 416 | (lhs->col_blocks().empty() || lhs->row_blocks().empty())) { |
| 417 | factor_ = ss_.AnalyzeCholesky(&cholmod_lhs, ordering_type_, message); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 418 | } else { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 419 | factor_ = ss_.BlockAnalyzeCholesky(&cholmod_lhs, |
| 420 | ordering_type_, |
| 421 | lhs->col_blocks(), |
| 422 | lhs->row_blocks(), |
| 423 | message); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 427 | if (factor_ == nullptr) { |
| 428 | return LinearSolverTerminationType::FATAL_ERROR; |
| 429 | } |
| 430 | |
| 431 | // Compute and return the numeric factorization. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 432 | return ss_.Cholesky(&cholmod_lhs, factor_, message); |
| 433 | } |
| 434 | |
| 435 | CompressedRowSparseMatrix::StorageType SuiteSparseCholesky::StorageType() |
| 436 | const { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 437 | return ((ordering_type_ == OrderingType::NATURAL) |
| 438 | ? CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR |
| 439 | : CompressedRowSparseMatrix::StorageType::LOWER_TRIANGULAR); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | LinearSolverTerminationType SuiteSparseCholesky::Solve(const double* rhs, |
| 443 | double* solution, |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 444 | std::string* message) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 445 | // Error checking |
| 446 | if (factor_ == nullptr) { |
| 447 | *message = "Solve called without a call to Factorize first."; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 448 | return LinearSolverTerminationType::FATAL_ERROR; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | const int num_cols = factor_->n; |
| 452 | cholmod_dense cholmod_rhs = ss_.CreateDenseVectorView(rhs, num_cols); |
| 453 | cholmod_dense* cholmod_dense_solution = |
| 454 | ss_.Solve(factor_, &cholmod_rhs, message); |
| 455 | |
| 456 | if (cholmod_dense_solution == nullptr) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 457 | return LinearSolverTerminationType::FAILURE; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | memcpy(solution, cholmod_dense_solution->x, num_cols * sizeof(*solution)); |
| 461 | ss_.Free(cholmod_dense_solution); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 462 | return LinearSolverTerminationType::SUCCESS; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 465 | } // namespace ceres::internal |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 466 | |
| 467 | #endif // CERES_NO_SUITESPARSE |