blob: d93dd8d9ed41ca0769268e640752e82d8c957366 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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 Schuh3de38b02024-06-25 18:25:10 -070032#include "ceres/internal/config.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080033
34#ifndef CERES_NO_SUITESPARSE
Austin Schuh3de38b02024-06-25 18:25:10 -070035
36#include <memory>
37#include <string>
Austin Schuh70cc9552019-01-21 19:46:48 -080038#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 Schuh1d1e6ea2020-12-23 21:56:30 -080043#include "ceres/suitesparse.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080044#include "ceres/triplet_sparse_matrix.h"
45#include "cholmod.h"
46
Austin Schuh3de38b02024-06-25 18:25:10 -070047namespace ceres::internal {
48namespace {
49int 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 Schuh70cc9552019-01-21 19:46:48 -080056
Austin Schuh3de38b02024-06-25 18:25:10 -070057 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 Schuh70cc9552019-01-21 19:46:48 -080065
66SuiteSparse::SuiteSparse() { cholmod_start(&cc_); }
67
68SuiteSparse::~SuiteSparse() { cholmod_finish(&cc_); }
69
70cholmod_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
88cholmod_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
109cholmod_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 Schuh3de38b02024-06-25 18:25:10 -0700121 if (A->storage_type() ==
122 CompressedRowSparseMatrix::StorageType::LOWER_TRIANGULAR) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800123 m.stype = 1;
Austin Schuh3de38b02024-06-25 18:25:10 -0700124 } else if (A->storage_type() ==
125 CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800126 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
140cholmod_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
152cholmod_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
163cholmod_factor* SuiteSparse::AnalyzeCholesky(cholmod_sparse* A,
Austin Schuh3de38b02024-06-25 18:25:10 -0700164 OrderingType ordering_type,
165 std::string* message) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800166 cc_.nmethods = 1;
Austin Schuh3de38b02024-06-25 18:25:10 -0700167 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 Schuh70cc9552019-01-21 19:46:48 -0800174
175 cholmod_factor* factor = cholmod_analyze(A, &cc_);
Austin Schuh70cc9552019-01-21 19:46:48 -0800176
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 Schuh3de38b02024-06-25 18:25:10 -0700184 if (VLOG_IS_ON(2)) {
185 cholmod_print_common(const_cast<char*>("Symbolic Analysis"), &cc_);
186 }
187
Austin Schuh70cc9552019-01-21 19:46:48 -0800188 return factor;
189}
190
Austin Schuh3de38b02024-06-25 18:25:10 -0700191cholmod_factor* SuiteSparse::AnalyzeCholeskyWithGivenOrdering(
192 cholmod_sparse* A, const std::vector<int>& ordering, std::string* message) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800193 CHECK_EQ(ordering.size(), A->nrow);
194
195 cc_.nmethods = 1;
196 cc_.method[0].ordering = CHOLMOD_GIVEN;
Austin Schuh70cc9552019-01-21 19:46:48 -0800197 cholmod_factor* factor =
Austin Schuh3de38b02024-06-25 18:25:10 -0700198 cholmod_analyze_p(A, const_cast<int*>(ordering.data()), nullptr, 0, &cc_);
199
Austin Schuh70cc9552019-01-21 19:46:48 -0800200 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 Schuh70cc9552019-01-21 19:46:48 -0800207 if (VLOG_IS_ON(2)) {
208 cholmod_print_common(const_cast<char*>("Symbolic Analysis"), &cc_);
209 }
Austin Schuh70cc9552019-01-21 19:46:48 -0800210
Austin Schuh70cc9552019-01-21 19:46:48 -0800211 return factor;
212}
213
Austin Schuh3de38b02024-06-25 18:25:10 -0700214bool 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 Schuh70cc9552019-01-21 19:46:48 -0800227 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 Schuh3de38b02024-06-25 18:25:10 -0700231 // encoding the block sparsity of A.
232 std::vector<int> block_cols;
233 std::vector<int> block_rows;
Austin Schuh70cc9552019-01-21 19:46:48 -0800234
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 Schuh3de38b02024-06-25 18:25:10 -0700245 block_matrix.p = reinterpret_cast<void*>(block_cols.data());
246 block_matrix.i = reinterpret_cast<void*>(block_rows.data());
Austin Schuh70cc9552019-01-21 19:46:48 -0800247 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 Schuh3de38b02024-06-25 18:25:10 -0700255 std::vector<int> block_ordering(num_row_blocks);
256 if (!Ordering(&block_matrix, ordering_type, block_ordering.data())) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800257 return false;
258 }
259
260 BlockOrderingToScalarOrdering(row_blocks, block_ordering, ordering);
261 return true;
262}
263
Austin Schuh3de38b02024-06-25 18:25:10 -0700264cholmod_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 Schuh70cc9552019-01-21 19:46:48 -0800277LinearSolverTerminationType SuiteSparse::Cholesky(cholmod_sparse* A,
278 cholmod_factor* L,
Austin Schuh3de38b02024-06-25 18:25:10 -0700279 std::string* message) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800280 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 Schuh3de38b02024-06-25 18:25:10 -0700297 return LinearSolverTerminationType::FATAL_ERROR;
Austin Schuh70cc9552019-01-21 19:46:48 -0800298 case CHOLMOD_OUT_OF_MEMORY:
299 *message = "CHOLMOD failure: Out of memory.";
Austin Schuh3de38b02024-06-25 18:25:10 -0700300 return LinearSolverTerminationType::FATAL_ERROR;
Austin Schuh70cc9552019-01-21 19:46:48 -0800301 case CHOLMOD_TOO_LARGE:
302 *message = "CHOLMOD failure: Integer overflow occurred.";
Austin Schuh3de38b02024-06-25 18:25:10 -0700303 return LinearSolverTerminationType::FATAL_ERROR;
Austin Schuh70cc9552019-01-21 19:46:48 -0800304 case CHOLMOD_INVALID:
305 *message = "CHOLMOD failure: Invalid input.";
Austin Schuh3de38b02024-06-25 18:25:10 -0700306 return LinearSolverTerminationType::FATAL_ERROR;
Austin Schuh70cc9552019-01-21 19:46:48 -0800307 case CHOLMOD_NOT_POSDEF:
308 *message = "CHOLMOD warning: Matrix not positive definite.";
Austin Schuh3de38b02024-06-25 18:25:10 -0700309 return LinearSolverTerminationType::FAILURE;
Austin Schuh70cc9552019-01-21 19:46:48 -0800310 case CHOLMOD_DSMALL:
311 *message =
312 "CHOLMOD warning: D for LDL' or diag(L) or "
313 "LL' has tiny absolute value.";
Austin Schuh3de38b02024-06-25 18:25:10 -0700314 return LinearSolverTerminationType::FAILURE;
Austin Schuh70cc9552019-01-21 19:46:48 -0800315 case CHOLMOD_OK:
316 if (cholmod_status != 0) {
Austin Schuh3de38b02024-06-25 18:25:10 -0700317 return LinearSolverTerminationType::SUCCESS;
Austin Schuh70cc9552019-01-21 19:46:48 -0800318 }
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 Schuh3de38b02024-06-25 18:25:10 -0700324 return LinearSolverTerminationType::FATAL_ERROR;
Austin Schuh70cc9552019-01-21 19:46:48 -0800325 default:
326 *message = StringPrintf(
327 "Unknown cholmod return code: %d. "
328 "Please report this to ceres-solver@googlegroups.com.",
329 cc_.status);
Austin Schuh3de38b02024-06-25 18:25:10 -0700330 return LinearSolverTerminationType::FATAL_ERROR;
Austin Schuh70cc9552019-01-21 19:46:48 -0800331 }
332
Austin Schuh3de38b02024-06-25 18:25:10 -0700333 return LinearSolverTerminationType::FATAL_ERROR;
Austin Schuh70cc9552019-01-21 19:46:48 -0800334}
335
336cholmod_dense* SuiteSparse::Solve(cholmod_factor* L,
337 cholmod_dense* b,
Austin Schuh3de38b02024-06-25 18:25:10 -0700338 std::string* message) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800339 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 Schuh3de38b02024-06-25 18:25:10 -0700347bool 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 Schuh70cc9552019-01-21 19:46:48 -0800363}
364
365bool SuiteSparse::ConstrainedApproximateMinimumDegreeOrdering(
366 cholmod_sparse* matrix, int* constraints, int* ordering) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800367 return cholmod_camd(matrix, nullptr, 0, constraints, ordering, &cc_);
Austin Schuh3de38b02024-06-25 18:25:10 -0700368}
369
370bool SuiteSparse::IsNestedDissectionAvailable() {
371#ifdef CERES_NO_CHOLMOD_PARTITION
Austin Schuh70cc9552019-01-21 19:46:48 -0800372 return false;
Austin Schuh3de38b02024-06-25 18:25:10 -0700373#else
374 return true;
Austin Schuh70cc9552019-01-21 19:46:48 -0800375#endif
376}
377
378std::unique_ptr<SparseCholesky> SuiteSparseCholesky::Create(
379 const OrderingType ordering_type) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800380 return std::unique_ptr<SparseCholesky>(
381 new SuiteSparseCholesky(ordering_type));
Austin Schuh70cc9552019-01-21 19:46:48 -0800382}
383
384SuiteSparseCholesky::SuiteSparseCholesky(const OrderingType ordering_type)
385 : ordering_type_(ordering_type), factor_(nullptr) {}
386
387SuiteSparseCholesky::~SuiteSparseCholesky() {
388 if (factor_ != nullptr) {
389 ss_.Free(factor_);
390 }
391}
392
393LinearSolverTerminationType SuiteSparseCholesky::Factorize(
Austin Schuh3de38b02024-06-25 18:25:10 -0700394 CompressedRowSparseMatrix* lhs, std::string* message) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800395 if (lhs == nullptr) {
Austin Schuh3de38b02024-06-25 18:25:10 -0700396 *message = "Failure: Input lhs is nullptr.";
397 return LinearSolverTerminationType::FATAL_ERROR;
Austin Schuh70cc9552019-01-21 19:46:48 -0800398 }
399
400 cholmod_sparse cholmod_lhs = ss_.CreateSparseMatrixTransposeView(lhs);
401
Austin Schuh3de38b02024-06-25 18:25:10 -0700402 // 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 Schuh70cc9552019-01-21 19:46:48 -0800414 if (factor_ == nullptr) {
Austin Schuh3de38b02024-06-25 18:25:10 -0700415 if (ordering_type_ == OrderingType::NATURAL ||
416 (lhs->col_blocks().empty() || lhs->row_blocks().empty())) {
417 factor_ = ss_.AnalyzeCholesky(&cholmod_lhs, ordering_type_, message);
Austin Schuh70cc9552019-01-21 19:46:48 -0800418 } else {
Austin Schuh3de38b02024-06-25 18:25:10 -0700419 factor_ = ss_.BlockAnalyzeCholesky(&cholmod_lhs,
420 ordering_type_,
421 lhs->col_blocks(),
422 lhs->row_blocks(),
423 message);
Austin Schuh70cc9552019-01-21 19:46:48 -0800424 }
425 }
426
Austin Schuh3de38b02024-06-25 18:25:10 -0700427 if (factor_ == nullptr) {
428 return LinearSolverTerminationType::FATAL_ERROR;
429 }
430
431 // Compute and return the numeric factorization.
Austin Schuh70cc9552019-01-21 19:46:48 -0800432 return ss_.Cholesky(&cholmod_lhs, factor_, message);
433}
434
435CompressedRowSparseMatrix::StorageType SuiteSparseCholesky::StorageType()
436 const {
Austin Schuh3de38b02024-06-25 18:25:10 -0700437 return ((ordering_type_ == OrderingType::NATURAL)
438 ? CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR
439 : CompressedRowSparseMatrix::StorageType::LOWER_TRIANGULAR);
Austin Schuh70cc9552019-01-21 19:46:48 -0800440}
441
442LinearSolverTerminationType SuiteSparseCholesky::Solve(const double* rhs,
443 double* solution,
Austin Schuh3de38b02024-06-25 18:25:10 -0700444 std::string* message) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800445 // Error checking
446 if (factor_ == nullptr) {
447 *message = "Solve called without a call to Factorize first.";
Austin Schuh3de38b02024-06-25 18:25:10 -0700448 return LinearSolverTerminationType::FATAL_ERROR;
Austin Schuh70cc9552019-01-21 19:46:48 -0800449 }
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 Schuh3de38b02024-06-25 18:25:10 -0700457 return LinearSolverTerminationType::FAILURE;
Austin Schuh70cc9552019-01-21 19:46:48 -0800458 }
459
460 memcpy(solution, cholmod_dense_solution->x, num_cols * sizeof(*solution));
461 ss_.Free(cholmod_dense_solution);
Austin Schuh3de38b02024-06-25 18:25:10 -0700462 return LinearSolverTerminationType::SUCCESS;
Austin Schuh70cc9552019-01-21 19:46:48 -0800463}
464
Austin Schuh3de38b02024-06-25 18:25:10 -0700465} // namespace ceres::internal
Austin Schuh70cc9552019-01-21 19:46:48 -0800466
467#endif // CERES_NO_SUITESPARSE