suitesparse.cc 16 KB

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