blob: 3275cc0d9aa55562aaef029021910065180f2405 [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 {
92 sparse_cholesky = AppleAccelerateCholesky<double>::Create(ordering_type);
93 }
94 break;
95#else
96 LOG(FATAL) << "Ceres was compiled without support for Apple's Accelerate "
97 << "framework solvers.";
98#endif
99
100 default:
101 LOG(FATAL) << "Unknown sparse linear algebra library type : "
102 << SparseLinearAlgebraLibraryTypeToString(
103 options.sparse_linear_algebra_library_type);
104 }
105
106 if (options.max_num_refinement_iterations > 0) {
107 std::unique_ptr<IterativeRefiner> refiner(
108 new IterativeRefiner(options.max_num_refinement_iterations));
109 sparse_cholesky = std::unique_ptr<SparseCholesky>(new RefinedSparseCholesky(
110 std::move(sparse_cholesky), std::move(refiner)));
111 }
112 return sparse_cholesky;
113}
114
115SparseCholesky::~SparseCholesky() {}
116
117LinearSolverTerminationType SparseCholesky::FactorAndSolve(
118 CompressedRowSparseMatrix* lhs,
119 const double* rhs,
120 double* solution,
121 std::string* message) {
122 LinearSolverTerminationType termination_type = Factorize(lhs, message);
123 if (termination_type == LINEAR_SOLVER_SUCCESS) {
124 termination_type = Solve(rhs, solution, message);
125 }
126 return termination_type;
127}
128
129CompressedRowSparseMatrix::StorageType StorageTypeForSparseLinearAlgebraLibrary(
130 SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type) {
131 if (sparse_linear_algebra_library_type == SUITE_SPARSE) {
132 return CompressedRowSparseMatrix::UPPER_TRIANGULAR;
133 }
134 return CompressedRowSparseMatrix::LOWER_TRIANGULAR;
135}
136
137RefinedSparseCholesky::RefinedSparseCholesky(
138 std::unique_ptr<SparseCholesky> sparse_cholesky,
139 std::unique_ptr<IterativeRefiner> iterative_refiner)
140 : sparse_cholesky_(std::move(sparse_cholesky)),
141 iterative_refiner_(std::move(iterative_refiner)) {}
142
143RefinedSparseCholesky::~RefinedSparseCholesky() {}
144
145CompressedRowSparseMatrix::StorageType RefinedSparseCholesky::StorageType()
146 const {
147 return sparse_cholesky_->StorageType();
148}
149
150LinearSolverTerminationType RefinedSparseCholesky::Factorize(
151 CompressedRowSparseMatrix* lhs, std::string* message) {
152 lhs_ = lhs;
153 return sparse_cholesky_->Factorize(lhs, message);
154}
155
156LinearSolverTerminationType RefinedSparseCholesky::Solve(const double* rhs,
157 double* solution,
158 std::string* message) {
159 CHECK(lhs_ != nullptr);
160 auto termination_type = sparse_cholesky_->Solve(rhs, solution, message);
161 if (termination_type != LINEAR_SOLVER_SUCCESS) {
162 return termination_type;
163 }
164
165 iterative_refiner_->Refine(*lhs_, rhs, sparse_cholesky_.get(), solution);
166 return LINEAR_SOLVER_SUCCESS;
167}
168
169} // namespace internal
170} // namespace ceres