Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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 | // For generalized bi-partite Jacobian matrices that arise in |
| 32 | // Structure from Motion related problems, it is sometimes useful to |
| 33 | // have access to the two parts of the matrix as linear operators |
| 34 | // themselves. This class provides that functionality. |
| 35 | |
| 36 | #ifndef CERES_INTERNAL_PARTITIONED_MATRIX_VIEW_H_ |
| 37 | #define CERES_INTERNAL_PARTITIONED_MATRIX_VIEW_H_ |
| 38 | |
| 39 | #include <algorithm> |
| 40 | #include <cstring> |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 41 | #include <memory> |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 42 | #include <vector> |
| 43 | |
| 44 | #include "ceres/block_structure.h" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 45 | #include "ceres/internal/config.h" |
| 46 | #include "ceres/internal/disable_warnings.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 47 | #include "ceres/internal/eigen.h" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 48 | #include "ceres/internal/export.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 49 | #include "ceres/linear_solver.h" |
| 50 | #include "ceres/small_blas.h" |
| 51 | #include "glog/logging.h" |
| 52 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 53 | namespace ceres::internal { |
| 54 | |
| 55 | class ContextImpl; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 56 | |
| 57 | // Given generalized bi-partite matrix A = [E F], with the same block |
| 58 | // structure as required by the Schur complement based solver, found |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 59 | // in schur_complement_solver.h, provide access to the |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 60 | // matrices E and F and their outer products E'E and F'F with |
| 61 | // themselves. |
| 62 | // |
| 63 | // Lack of BlockStructure object will result in a crash and if the |
| 64 | // block structure of the matrix does not satisfy the requirements of |
| 65 | // the Schur complement solver it will result in unpredictable and |
| 66 | // wrong output. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 67 | class CERES_NO_EXPORT PartitionedMatrixViewBase { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 68 | public: |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 69 | virtual ~PartitionedMatrixViewBase(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 70 | |
| 71 | // y += E'x |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 72 | virtual void LeftMultiplyAndAccumulateE(const double* x, double* y) const = 0; |
| 73 | virtual void LeftMultiplyAndAccumulateESingleThreaded(const double* x, |
| 74 | double* y) const = 0; |
| 75 | virtual void LeftMultiplyAndAccumulateEMultiThreaded(const double* x, |
| 76 | double* y) const = 0; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 77 | |
| 78 | // y += F'x |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 79 | virtual void LeftMultiplyAndAccumulateF(const double* x, double* y) const = 0; |
| 80 | virtual void LeftMultiplyAndAccumulateFSingleThreaded(const double* x, |
| 81 | double* y) const = 0; |
| 82 | virtual void LeftMultiplyAndAccumulateFMultiThreaded(const double* x, |
| 83 | double* y) const = 0; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 84 | |
| 85 | // y += Ex |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 86 | virtual void RightMultiplyAndAccumulateE(const double* x, |
| 87 | double* y) const = 0; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 88 | |
| 89 | // y += Fx |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 90 | virtual void RightMultiplyAndAccumulateF(const double* x, |
| 91 | double* y) const = 0; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 92 | |
| 93 | // Create and return the block diagonal of the matrix E'E. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 94 | virtual std::unique_ptr<BlockSparseMatrix> CreateBlockDiagonalEtE() const = 0; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 95 | |
| 96 | // Create and return the block diagonal of the matrix F'F. Caller |
| 97 | // owns the result. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 98 | virtual std::unique_ptr<BlockSparseMatrix> CreateBlockDiagonalFtF() const = 0; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 99 | |
| 100 | // Compute the block diagonal of the matrix E'E and store it in |
| 101 | // block_diagonal. The matrix block_diagonal is expected to have a |
| 102 | // BlockStructure (preferably created using |
| 103 | // CreateBlockDiagonalMatrixEtE) which is has the same structure as |
| 104 | // the block diagonal of E'E. |
| 105 | virtual void UpdateBlockDiagonalEtE( |
| 106 | BlockSparseMatrix* block_diagonal) const = 0; |
| 107 | |
| 108 | // Compute the block diagonal of the matrix F'F and store it in |
| 109 | // block_diagonal. The matrix block_diagonal is expected to have a |
| 110 | // BlockStructure (preferably created using |
| 111 | // CreateBlockDiagonalMatrixFtF) which is has the same structure as |
| 112 | // the block diagonal of F'F. |
| 113 | virtual void UpdateBlockDiagonalFtF( |
| 114 | BlockSparseMatrix* block_diagonal) const = 0; |
| 115 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 116 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 117 | virtual int num_col_blocks_e() const = 0; |
| 118 | virtual int num_col_blocks_f() const = 0; |
| 119 | virtual int num_cols_e() const = 0; |
| 120 | virtual int num_cols_f() const = 0; |
| 121 | virtual int num_rows() const = 0; |
| 122 | virtual int num_cols() const = 0; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 123 | virtual const std::vector<int>& e_cols_partition() const = 0; |
| 124 | virtual const std::vector<int>& f_cols_partition() const = 0; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 125 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 126 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 127 | static std::unique_ptr<PartitionedMatrixViewBase> Create( |
| 128 | const LinearSolver::Options& options, const BlockSparseMatrix& matrix); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | template <int kRowBlockSize = Eigen::Dynamic, |
| 132 | int kEBlockSize = Eigen::Dynamic, |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 133 | int kFBlockSize = Eigen::Dynamic> |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 134 | class CERES_NO_EXPORT PartitionedMatrixView final |
| 135 | : public PartitionedMatrixViewBase { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 136 | public: |
| 137 | // matrix = [E F], where the matrix E contains the first |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 138 | // options.elimination_groups[0] column blocks. |
| 139 | PartitionedMatrixView(const LinearSolver::Options& options, |
| 140 | const BlockSparseMatrix& matrix); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 141 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 142 | // y += E'x |
| 143 | virtual void LeftMultiplyAndAccumulateE(const double* x, |
| 144 | double* y) const final; |
| 145 | virtual void LeftMultiplyAndAccumulateESingleThreaded(const double* x, |
| 146 | double* y) const final; |
| 147 | virtual void LeftMultiplyAndAccumulateEMultiThreaded(const double* x, |
| 148 | double* y) const final; |
| 149 | |
| 150 | // y += F'x |
| 151 | virtual void LeftMultiplyAndAccumulateF(const double* x, |
| 152 | double* y) const final; |
| 153 | virtual void LeftMultiplyAndAccumulateFSingleThreaded(const double* x, |
| 154 | double* y) const final; |
| 155 | virtual void LeftMultiplyAndAccumulateFMultiThreaded(const double* x, |
| 156 | double* y) const final; |
| 157 | |
| 158 | // y += Ex |
| 159 | virtual void RightMultiplyAndAccumulateE(const double* x, |
| 160 | double* y) const final; |
| 161 | |
| 162 | // y += Fx |
| 163 | virtual void RightMultiplyAndAccumulateF(const double* x, |
| 164 | double* y) const final; |
| 165 | |
| 166 | std::unique_ptr<BlockSparseMatrix> CreateBlockDiagonalEtE() const final; |
| 167 | std::unique_ptr<BlockSparseMatrix> CreateBlockDiagonalFtF() const final; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 168 | void UpdateBlockDiagonalEtE(BlockSparseMatrix* block_diagonal) const final; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 169 | void UpdateBlockDiagonalEtESingleThreaded( |
| 170 | BlockSparseMatrix* block_diagonal) const; |
| 171 | void UpdateBlockDiagonalEtEMultiThreaded( |
| 172 | BlockSparseMatrix* block_diagonal) const; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 173 | void UpdateBlockDiagonalFtF(BlockSparseMatrix* block_diagonal) const final; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 174 | void UpdateBlockDiagonalFtFSingleThreaded( |
| 175 | BlockSparseMatrix* block_diagonal) const; |
| 176 | void UpdateBlockDiagonalFtFMultiThreaded( |
| 177 | BlockSparseMatrix* block_diagonal) const; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 178 | // clang-format off |
| 179 | int num_col_blocks_e() const final { return num_col_blocks_e_; } |
| 180 | int num_col_blocks_f() const final { return num_col_blocks_f_; } |
| 181 | int num_cols_e() const final { return num_cols_e_; } |
| 182 | int num_cols_f() const final { return num_cols_f_; } |
| 183 | int num_rows() const final { return matrix_.num_rows(); } |
| 184 | int num_cols() const final { return matrix_.num_cols(); } |
| 185 | // clang-format on |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 186 | const std::vector<int>& e_cols_partition() const final { |
| 187 | return e_cols_partition_; |
| 188 | } |
| 189 | const std::vector<int>& f_cols_partition() const final { |
| 190 | return f_cols_partition_; |
| 191 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 192 | |
| 193 | private: |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 194 | std::unique_ptr<BlockSparseMatrix> CreateBlockDiagonalMatrixLayout( |
| 195 | int start_col_block, int end_col_block) const; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 196 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 197 | const LinearSolver::Options options_; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 198 | const BlockSparseMatrix& matrix_; |
| 199 | int num_row_blocks_e_; |
| 200 | int num_col_blocks_e_; |
| 201 | int num_col_blocks_f_; |
| 202 | int num_cols_e_; |
| 203 | int num_cols_f_; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 204 | std::vector<int> e_cols_partition_; |
| 205 | std::vector<int> f_cols_partition_; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 206 | }; |
| 207 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 208 | } // namespace ceres::internal |
| 209 | |
| 210 | #include "ceres/internal/reenable_warnings.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 211 | |
| 212 | #endif // CERES_INTERNAL_PARTITIONED_MATRIX_VIEW_H_ |