blob: 91cdf671b1afb241520e12fd28e51aa02e79bd4a [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2017 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
31#include "ceres/sparse_cholesky.h"
32
33#include "ceres/accelerate_sparse.h"
34#include "ceres/cxsparse.h"
35#include "ceres/eigensparse.h"
36#include "ceres/float_cxsparse.h"
37#include "ceres/float_suitesparse.h"
38#include "ceres/iterative_refiner.h"
39#include "ceres/suitesparse.h"
40
41namespace ceres {
42namespace internal {
43
44std::unique_ptr<SparseCholesky> SparseCholesky::Create(
45 const LinearSolver::Options& options) {
46 const OrderingType ordering_type = options.use_postordering ? AMD : NATURAL;
47 std::unique_ptr<SparseCholesky> sparse_cholesky;
48
49 switch (options.sparse_linear_algebra_library_type) {
50 case SUITE_SPARSE:
51#ifndef CERES_NO_SUITESPARSE
52 if (options.use_mixed_precision_solves) {
53 sparse_cholesky = FloatSuiteSparseCholesky::Create(ordering_type);
54 } else {
55 sparse_cholesky = SuiteSparseCholesky::Create(ordering_type);
56 }
57 break;
58#else
59 LOG(FATAL) << "Ceres was compiled without support for SuiteSparse.";
60#endif
61
62 case EIGEN_SPARSE:
63#ifdef CERES_USE_EIGEN_SPARSE
64 if (options.use_mixed_precision_solves) {
65 sparse_cholesky = FloatEigenSparseCholesky::Create(ordering_type);
66 } else {
67 sparse_cholesky = EigenSparseCholesky::Create(ordering_type);
68 }
69 break;
70#else
71 LOG(FATAL) << "Ceres was compiled without support for "
72 << "Eigen's sparse Cholesky factorization routines.";
73#endif
74
75 case CX_SPARSE:
76#ifndef CERES_NO_CXSPARSE
77 if (options.use_mixed_precision_solves) {
78 sparse_cholesky = FloatCXSparseCholesky::Create(ordering_type);
79 } else {
80 sparse_cholesky = CXSparseCholesky::Create(ordering_type);
81 }
82 break;
83#else
84 LOG(FATAL) << "Ceres was compiled without support for CXSparse.";
85#endif
86
87 case ACCELERATE_SPARSE:
88#ifndef CERES_NO_ACCELERATE_SPARSE
89 if (options.use_mixed_precision_solves) {
90 sparse_cholesky = AppleAccelerateCholesky<float>::Create(ordering_type);
91 } else {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080092 sparse_cholesky =
93 AppleAccelerateCholesky<double>::Create(ordering_type);
Austin Schuh70cc9552019-01-21 19:46:48 -080094 }
95 break;
96#else
97 LOG(FATAL) << "Ceres was compiled without support for Apple's Accelerate "
98 << "framework solvers.";
99#endif
100
101 default:
102 LOG(FATAL) << "Unknown sparse linear algebra library type : "
103 << SparseLinearAlgebraLibraryTypeToString(
104 options.sparse_linear_algebra_library_type);
105 }
106
107 if (options.max_num_refinement_iterations > 0) {
108 std::unique_ptr<IterativeRefiner> refiner(
109 new IterativeRefiner(options.max_num_refinement_iterations));
110 sparse_cholesky = std::unique_ptr<SparseCholesky>(new RefinedSparseCholesky(
111 std::move(sparse_cholesky), std::move(refiner)));
112 }
113 return sparse_cholesky;
114}
115
116SparseCholesky::~SparseCholesky() {}
117
118LinearSolverTerminationType SparseCholesky::FactorAndSolve(
119 CompressedRowSparseMatrix* lhs,
120 const double* rhs,
121 double* solution,
122 std::string* message) {
123 LinearSolverTerminationType termination_type = Factorize(lhs, message);
124 if (termination_type == LINEAR_SOLVER_SUCCESS) {
125 termination_type = Solve(rhs, solution, message);
126 }
127 return termination_type;
128}
129
Austin Schuh70cc9552019-01-21 19:46:48 -0800130RefinedSparseCholesky::RefinedSparseCholesky(
131 std::unique_ptr<SparseCholesky> sparse_cholesky,
132 std::unique_ptr<IterativeRefiner> iterative_refiner)
133 : sparse_cholesky_(std::move(sparse_cholesky)),
134 iterative_refiner_(std::move(iterative_refiner)) {}
135
136RefinedSparseCholesky::~RefinedSparseCholesky() {}
137
138CompressedRowSparseMatrix::StorageType RefinedSparseCholesky::StorageType()
139 const {
140 return sparse_cholesky_->StorageType();
141}
142
143LinearSolverTerminationType RefinedSparseCholesky::Factorize(
144 CompressedRowSparseMatrix* lhs, std::string* message) {
145 lhs_ = lhs;
146 return sparse_cholesky_->Factorize(lhs, message);
147}
148
149LinearSolverTerminationType RefinedSparseCholesky::Solve(const double* rhs,
150 double* solution,
151 std::string* message) {
152 CHECK(lhs_ != nullptr);
153 auto termination_type = sparse_cholesky_->Solve(rhs, solution, message);
154 if (termination_type != LINEAR_SOLVER_SUCCESS) {
155 return termination_type;
156 }
157
158 iterative_refiner_->Refine(*lhs_, rhs, sparse_cholesky_.get(), solution);
159 return LINEAR_SOLVER_SUCCESS;
160}
161
162} // namespace internal
163} // namespace ceres