Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame^] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2018 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: alexs.mac@gmail.com (Alex Stewart) |
| 30 | |
| 31 | #ifndef CERES_INTERNAL_ACCELERATE_SPARSE_H_ |
| 32 | #define CERES_INTERNAL_ACCELERATE_SPARSE_H_ |
| 33 | |
| 34 | // This include must come before any #ifndef check on Ceres compile options. |
| 35 | #include "ceres/internal/port.h" |
| 36 | |
| 37 | #ifndef CERES_NO_ACCELERATE_SPARSE |
| 38 | |
| 39 | #include <memory> |
| 40 | #include <string> |
| 41 | #include <vector> |
| 42 | |
| 43 | #include "ceres/linear_solver.h" |
| 44 | #include "ceres/sparse_cholesky.h" |
| 45 | #include "Accelerate.h" |
| 46 | |
| 47 | namespace ceres { |
| 48 | namespace internal { |
| 49 | |
| 50 | class CompressedRowSparseMatrix; |
| 51 | class TripletSparseMatrix; |
| 52 | |
| 53 | template<typename Scalar> |
| 54 | struct SparseTypesTrait { |
| 55 | }; |
| 56 | |
| 57 | template<> |
| 58 | struct SparseTypesTrait<double> { |
| 59 | typedef DenseVector_Double DenseVector; |
| 60 | typedef SparseMatrix_Double SparseMatrix; |
| 61 | typedef SparseOpaqueSymbolicFactorization SymbolicFactorization; |
| 62 | typedef SparseOpaqueFactorization_Double NumericFactorization; |
| 63 | }; |
| 64 | |
| 65 | template<> |
| 66 | struct SparseTypesTrait<float> { |
| 67 | typedef DenseVector_Float DenseVector; |
| 68 | typedef SparseMatrix_Float SparseMatrix; |
| 69 | typedef SparseOpaqueSymbolicFactorization SymbolicFactorization; |
| 70 | typedef SparseOpaqueFactorization_Float NumericFactorization; |
| 71 | }; |
| 72 | |
| 73 | template<typename Scalar> |
| 74 | class AccelerateSparse { |
| 75 | public: |
| 76 | using DenseVector = typename SparseTypesTrait<Scalar>::DenseVector; |
| 77 | // Use ASSparseMatrix to avoid collision with ceres::internal::SparseMatrix. |
| 78 | using ASSparseMatrix = typename SparseTypesTrait<Scalar>::SparseMatrix; |
| 79 | using SymbolicFactorization = typename SparseTypesTrait<Scalar>::SymbolicFactorization; |
| 80 | using NumericFactorization = typename SparseTypesTrait<Scalar>::NumericFactorization; |
| 81 | |
| 82 | // Solves a linear system given its symbolic (reference counted within |
| 83 | // NumericFactorization) and numeric factorization. |
| 84 | void Solve(NumericFactorization* numeric_factor, |
| 85 | DenseVector* rhs_and_solution); |
| 86 | |
| 87 | // Note: Accelerate's API passes/returns its objects by value, but as the |
| 88 | // objects contain pointers to the underlying data these copies are |
| 89 | // all shallow (in some cases Accelerate also reference counts the |
| 90 | // objects internally). |
| 91 | ASSparseMatrix CreateSparseMatrixTransposeView(CompressedRowSparseMatrix* A); |
| 92 | // Computes a symbolic factorisation of A that can be used in Solve(). |
| 93 | SymbolicFactorization AnalyzeCholesky(ASSparseMatrix* A); |
| 94 | // Compute the numeric Cholesky factorization of A, given its |
| 95 | // symbolic factorization. |
| 96 | NumericFactorization Cholesky(ASSparseMatrix* A, |
| 97 | SymbolicFactorization* symbolic_factor); |
| 98 | // Reuse the NumericFactorization from a previous matrix with the same |
| 99 | // symbolic factorization to represent a new numeric factorization. |
| 100 | void Cholesky(ASSparseMatrix* A, NumericFactorization* numeric_factor); |
| 101 | |
| 102 | private: |
| 103 | std::vector<long> column_starts_; |
| 104 | // Storage for the values of A if Scalar != double (necessitating a copy). |
| 105 | Eigen::Matrix<Scalar, Eigen::Dynamic, 1> values_; |
| 106 | }; |
| 107 | |
| 108 | // An implementation of SparseCholesky interface using Apple's Accelerate |
| 109 | // framework. |
| 110 | template<typename Scalar> |
| 111 | class AppleAccelerateCholesky : public SparseCholesky { |
| 112 | public: |
| 113 | // Factory |
| 114 | static std::unique_ptr<SparseCholesky> Create(OrderingType ordering_type); |
| 115 | |
| 116 | // SparseCholesky interface. |
| 117 | virtual ~AppleAccelerateCholesky(); |
| 118 | virtual CompressedRowSparseMatrix::StorageType StorageType() const; |
| 119 | virtual LinearSolverTerminationType Factorize(CompressedRowSparseMatrix* lhs, |
| 120 | std::string* message); |
| 121 | virtual LinearSolverTerminationType Solve(const double* rhs, |
| 122 | double* solution, |
| 123 | std::string* message); |
| 124 | |
| 125 | private: |
| 126 | AppleAccelerateCholesky(const OrderingType ordering_type); |
| 127 | void FreeSymbolicFactorization(); |
| 128 | void FreeNumericFactorization(); |
| 129 | |
| 130 | const OrderingType ordering_type_; |
| 131 | AccelerateSparse<Scalar> as_; |
| 132 | std::unique_ptr<typename AccelerateSparse<Scalar>::SymbolicFactorization> |
| 133 | symbolic_factor_; |
| 134 | std::unique_ptr<typename AccelerateSparse<Scalar>::NumericFactorization> |
| 135 | numeric_factor_; |
| 136 | // Copy of rhs/solution if Scalar != double (necessitating a copy). |
| 137 | Eigen::Matrix<Scalar, Eigen::Dynamic, 1> scalar_rhs_and_solution_; |
| 138 | }; |
| 139 | |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | #endif // CERES_NO_ACCELERATE_SPARSE |
| 144 | |
| 145 | #endif // CERES_INTERNAL_ACCELERATE_SPARSE_H_ |