blob: 8589a3b6a68e52d37caf57f69ae059aed12d051a [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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 Schuh3de38b02024-06-25 18:25:10 -070041#include <memory>
Austin Schuh70cc9552019-01-21 19:46:48 -080042#include <vector>
43
44#include "ceres/block_structure.h"
Austin Schuh3de38b02024-06-25 18:25:10 -070045#include "ceres/internal/config.h"
46#include "ceres/internal/disable_warnings.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080047#include "ceres/internal/eigen.h"
Austin Schuh3de38b02024-06-25 18:25:10 -070048#include "ceres/internal/export.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080049#include "ceres/linear_solver.h"
50#include "ceres/small_blas.h"
51#include "glog/logging.h"
52
Austin Schuh3de38b02024-06-25 18:25:10 -070053namespace ceres::internal {
54
55class ContextImpl;
Austin Schuh70cc9552019-01-21 19:46:48 -080056
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 Schuh3de38b02024-06-25 18:25:10 -070059// in schur_complement_solver.h, provide access to the
Austin Schuh70cc9552019-01-21 19:46:48 -080060// 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 Schuh3de38b02024-06-25 18:25:10 -070067class CERES_NO_EXPORT PartitionedMatrixViewBase {
Austin Schuh70cc9552019-01-21 19:46:48 -080068 public:
Austin Schuh3de38b02024-06-25 18:25:10 -070069 virtual ~PartitionedMatrixViewBase();
Austin Schuh70cc9552019-01-21 19:46:48 -080070
71 // y += E'x
Austin Schuh3de38b02024-06-25 18:25:10 -070072 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 Schuh70cc9552019-01-21 19:46:48 -080077
78 // y += F'x
Austin Schuh3de38b02024-06-25 18:25:10 -070079 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 Schuh70cc9552019-01-21 19:46:48 -080084
85 // y += Ex
Austin Schuh3de38b02024-06-25 18:25:10 -070086 virtual void RightMultiplyAndAccumulateE(const double* x,
87 double* y) const = 0;
Austin Schuh70cc9552019-01-21 19:46:48 -080088
89 // y += Fx
Austin Schuh3de38b02024-06-25 18:25:10 -070090 virtual void RightMultiplyAndAccumulateF(const double* x,
91 double* y) const = 0;
Austin Schuh70cc9552019-01-21 19:46:48 -080092
93 // Create and return the block diagonal of the matrix E'E.
Austin Schuh3de38b02024-06-25 18:25:10 -070094 virtual std::unique_ptr<BlockSparseMatrix> CreateBlockDiagonalEtE() const = 0;
Austin Schuh70cc9552019-01-21 19:46:48 -080095
96 // Create and return the block diagonal of the matrix F'F. Caller
97 // owns the result.
Austin Schuh3de38b02024-06-25 18:25:10 -070098 virtual std::unique_ptr<BlockSparseMatrix> CreateBlockDiagonalFtF() const = 0;
Austin Schuh70cc9552019-01-21 19:46:48 -080099
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 Schuh1d1e6ea2020-12-23 21:56:30 -0800116 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800117 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 Schuh3de38b02024-06-25 18:25:10 -0700123 virtual const std::vector<int>& e_cols_partition() const = 0;
124 virtual const std::vector<int>& f_cols_partition() const = 0;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800125 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800126
Austin Schuh3de38b02024-06-25 18:25:10 -0700127 static std::unique_ptr<PartitionedMatrixViewBase> Create(
128 const LinearSolver::Options& options, const BlockSparseMatrix& matrix);
Austin Schuh70cc9552019-01-21 19:46:48 -0800129};
130
131template <int kRowBlockSize = Eigen::Dynamic,
132 int kEBlockSize = Eigen::Dynamic,
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800133 int kFBlockSize = Eigen::Dynamic>
Austin Schuh3de38b02024-06-25 18:25:10 -0700134class CERES_NO_EXPORT PartitionedMatrixView final
135 : public PartitionedMatrixViewBase {
Austin Schuh70cc9552019-01-21 19:46:48 -0800136 public:
137 // matrix = [E F], where the matrix E contains the first
Austin Schuh3de38b02024-06-25 18:25:10 -0700138 // options.elimination_groups[0] column blocks.
139 PartitionedMatrixView(const LinearSolver::Options& options,
140 const BlockSparseMatrix& matrix);
Austin Schuh70cc9552019-01-21 19:46:48 -0800141
Austin Schuh3de38b02024-06-25 18:25:10 -0700142 // 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 Schuh1d1e6ea2020-12-23 21:56:30 -0800168 void UpdateBlockDiagonalEtE(BlockSparseMatrix* block_diagonal) const final;
Austin Schuh3de38b02024-06-25 18:25:10 -0700169 void UpdateBlockDiagonalEtESingleThreaded(
170 BlockSparseMatrix* block_diagonal) const;
171 void UpdateBlockDiagonalEtEMultiThreaded(
172 BlockSparseMatrix* block_diagonal) const;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800173 void UpdateBlockDiagonalFtF(BlockSparseMatrix* block_diagonal) const final;
Austin Schuh3de38b02024-06-25 18:25:10 -0700174 void UpdateBlockDiagonalFtFSingleThreaded(
175 BlockSparseMatrix* block_diagonal) const;
176 void UpdateBlockDiagonalFtFMultiThreaded(
177 BlockSparseMatrix* block_diagonal) const;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800178 // 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 Schuh3de38b02024-06-25 18:25:10 -0700186 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 Schuh70cc9552019-01-21 19:46:48 -0800192
193 private:
Austin Schuh3de38b02024-06-25 18:25:10 -0700194 std::unique_ptr<BlockSparseMatrix> CreateBlockDiagonalMatrixLayout(
195 int start_col_block, int end_col_block) const;
Austin Schuh70cc9552019-01-21 19:46:48 -0800196
Austin Schuh3de38b02024-06-25 18:25:10 -0700197 const LinearSolver::Options options_;
Austin Schuh70cc9552019-01-21 19:46:48 -0800198 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 Schuh3de38b02024-06-25 18:25:10 -0700204 std::vector<int> e_cols_partition_;
205 std::vector<int> f_cols_partition_;
Austin Schuh70cc9552019-01-21 19:46:48 -0800206};
207
Austin Schuh3de38b02024-06-25 18:25:10 -0700208} // namespace ceres::internal
209
210#include "ceres/internal/reenable_warnings.h"
Austin Schuh70cc9552019-01-21 19:46:48 -0800211
212#endif // CERES_INTERNAL_PARTITIONED_MATRIX_VIEW_H_