Files
ceres-solver/internal/ceres/schur_complement_solver.cc
T

411 lines
14 KiB
C++
Raw Normal View History

2012-04-30 23:09:08 -07:00
// Ceres Solver - A fast non-linear least squares minimizer
2023-09-19 15:29:34 -07:00
// Copyright 2023 Google Inc. All rights reserved.
// http://ceres-solver.org/
2012-04-30 23:09:08 -07:00
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Author: sameeragarwal@google.com (Sameer Agarwal)
2017-04-12 12:48:44 -07:00
#include "ceres/schur_complement_solver.h"
2012-04-30 23:09:08 -07:00
#include <algorithm>
#include <ctime>
2018-03-30 16:16:59 -07:00
#include <memory>
2022-09-12 16:20:30 -07:00
#include <utility>
2012-04-30 23:09:08 -07:00
#include <vector>
2012-05-29 00:27:57 -07:00
2017-04-12 12:48:44 -07:00
#include "Eigen/Dense"
#include "Eigen/SparseCore"
2024-09-17 10:00:39 -07:00
#include "absl/container/btree_set.h"
2024-07-18 11:37:07 -07:00
#include "absl/log/check.h"
2012-04-30 23:09:08 -07:00
#include "ceres/block_random_access_dense_matrix.h"
#include "ceres/block_random_access_matrix.h"
#include "ceres/block_random_access_sparse_matrix.h"
#include "ceres/block_sparse_matrix.h"
#include "ceres/block_structure.h"
#include "ceres/conjugate_gradients_solver.h"
2012-04-30 23:09:08 -07:00
#include "ceres/detect_structure.h"
2024-08-08 07:18:44 -07:00
#include "ceres/event_logger.h"
2013-02-01 12:22:53 -08:00
#include "ceres/internal/eigen.h"
2012-04-30 23:09:08 -07:00
#include "ceres/linear_solver.h"
2017-04-12 12:48:44 -07:00
#include "ceres/sparse_cholesky.h"
2012-04-30 23:09:08 -07:00
#include "ceres/triplet_sparse_matrix.h"
#include "ceres/types.h"
2012-05-29 00:27:57 -07:00
2022-04-21 17:41:10 -07:00
namespace ceres::internal {
namespace {
2022-08-13 21:30:30 +02:00
class BlockRandomAccessSparseMatrixAdapter final
2022-08-08 11:42:49 -07:00
: public ConjugateGradientsLinearOperator<Vector> {
2014-12-17 07:35:09 -08:00
public:
explicit BlockRandomAccessSparseMatrixAdapter(
const BlockRandomAccessSparseMatrix& m)
: m_(m) {}
void RightMultiplyAndAccumulate(const Vector& x, Vector& y) final {
m_.SymmetricRightMultiplyAndAccumulate(x.data(), y.data());
}
private:
const BlockRandomAccessSparseMatrix& m_;
};
2022-08-08 11:42:49 -07:00
class BlockRandomAccessDiagonalMatrixAdapter final
: public ConjugateGradientsLinearOperator<Vector> {
2014-12-17 07:35:09 -08:00
public:
explicit BlockRandomAccessDiagonalMatrixAdapter(
const BlockRandomAccessDiagonalMatrix& m)
: m_(m) {}
// y = y + Ax;
void RightMultiplyAndAccumulate(const Vector& x, Vector& y) final {
m_.RightMultiplyAndAccumulate(x.data(), y.data());
}
private:
const BlockRandomAccessDiagonalMatrix& m_;
};
} // namespace
2012-04-30 23:09:08 -07:00
SchurComplementSolver::SchurComplementSolver(
const LinearSolver::Options& options)
: options_(options) {
CHECK_GT(options.elimination_groups.size(), 1);
CHECK_GT(options.elimination_groups[0], 0);
2022-02-08 13:40:39 -08:00
CHECK(options.context != nullptr);
}
2012-04-30 23:09:08 -07:00
LinearSolver::Summary SchurComplementSolver::SolveImpl(
2013-04-24 11:58:24 -07:00
BlockSparseMatrix* A,
2012-04-30 23:09:08 -07:00
const double* b,
const LinearSolver::PerSolveOptions& per_solve_options,
double* x) {
2013-02-01 12:22:53 -08:00
EventLogger event_logger("SchurComplementSolver::Solve");
const CompressedRowBlockStructure* bs = A->block_structure();
2022-08-12 21:49:02 -07:00
if (eliminator_ == nullptr) {
const int num_eliminate_blocks = options_.elimination_groups[0];
const int num_f_blocks = bs->cols.size() - num_eliminate_blocks;
InitStorage(bs);
DetectStructure(*bs,
num_eliminate_blocks,
2012-04-30 23:09:08 -07:00
&options_.row_block_size,
&options_.e_block_size,
&options_.f_block_size);
// For the special case of the static structure <2,3,6> with
// exactly one f block use the SchurEliminatorForOneFBlock.
//
// TODO(sameeragarwal): A more scalable template specialization
// mechanism that does not cause binary bloat.
if (options_.row_block_size == 2 && options_.e_block_size == 3 &&
options_.f_block_size == 6 && num_f_blocks == 1) {
2022-02-02 13:17:29 -08:00
eliminator_ = std::make_unique<SchurEliminatorForOneFBlock<2, 3, 6>>();
} else {
2022-02-02 13:17:29 -08:00
eliminator_ = SchurEliminatorBase::Create(options_);
}
CHECK(eliminator_);
const bool kFullRankETE = true;
eliminator_->Init(num_eliminate_blocks, kFullRankETE, bs);
}
2017-04-12 12:48:44 -07:00
2014-12-17 07:35:09 -08:00
std::fill(x, x + A->num_cols(), 0.0);
2013-02-01 12:22:53 -08:00
event_logger.AddEvent("Setup");
2012-04-30 23:09:08 -07:00
2019-09-04 07:09:36 -07:00
eliminator_->Eliminate(BlockSparseMatrixData(*A),
b,
per_solve_options.D,
lhs_.get(),
2022-08-08 11:42:49 -07:00
rhs_.data());
2013-02-01 12:22:53 -08:00
event_logger.AddEvent("Eliminate");
2012-04-30 23:09:08 -07:00
double* reduced_solution = x + A->num_cols() - lhs_->num_cols();
const LinearSolver::Summary summary =
SolveReducedLinearSystem(per_solve_options, reduced_solution);
2013-02-01 12:22:53 -08:00
event_logger.AddEvent("ReducedSolve");
2012-04-30 23:09:08 -07:00
2022-05-14 14:14:22 -07:00
if (summary.termination_type == LinearSolverTerminationType::SUCCESS) {
2019-09-04 07:09:36 -07:00
eliminator_->BackSubstitute(
BlockSparseMatrixData(*A), b, per_solve_options.D, reduced_solution, x);
event_logger.AddEvent("BackSubstitute");
2012-04-30 23:09:08 -07:00
}
return summary;
}
DenseSchurComplementSolver::DenseSchurComplementSolver(
const LinearSolver::Options& options)
: SchurComplementSolver(options),
cholesky_(DenseCholesky::Create(options)) {}
2022-02-09 00:56:10 +01:00
DenseSchurComplementSolver::~DenseSchurComplementSolver() = default;
2012-04-30 23:09:08 -07:00
// Initialize a BlockRandomAccessDenseMatrix to store the Schur
// complement.
void DenseSchurComplementSolver::InitStorage(
const CompressedRowBlockStructure* bs) {
const int num_eliminate_blocks = options().elimination_groups[0];
2012-04-30 23:09:08 -07:00
const int num_col_blocks = bs->cols.size();
2022-08-29 21:52:30 -07:00
auto blocks = Tail(bs->cols, num_col_blocks - num_eliminate_blocks);
2022-12-21 16:01:36 -08:00
set_lhs(std::make_unique<BlockRandomAccessDenseMatrix>(
blocks, options().context, options().num_threads));
2022-08-08 11:42:49 -07:00
ResizeRhs(lhs()->num_rows());
2012-04-30 23:09:08 -07:00
}
// Solve the system Sx = r, assuming that the matrix S is stored in a
// BlockRandomAccessDenseMatrix. The linear system is solved using
// Eigen's Cholesky factorization.
LinearSolver::Summary DenseSchurComplementSolver::SolveReducedLinearSystem(
2023-01-14 05:54:24 -08:00
const LinearSolver::PerSolveOptions& /*per_solve_options*/,
double* solution) {
LinearSolver::Summary summary;
summary.num_iterations = 0;
2022-05-14 14:14:22 -07:00
summary.termination_type = LinearSolverTerminationType::SUCCESS;
summary.message = "Success.";
2022-02-20 02:22:17 +01:00
auto* m = down_cast<BlockRandomAccessDenseMatrix*>(mutable_lhs());
2012-04-30 23:09:08 -07:00
const int num_rows = m->num_rows();
// The case where there are no f blocks, and the system is block
// diagonal.
if (num_rows == 0) {
return summary;
2012-04-30 23:09:08 -07:00
}
summary.num_iterations = 1;
2022-01-21 18:33:16 -08:00
summary.termination_type = cholesky_->FactorAndSolve(
2022-08-08 11:42:49 -07:00
num_rows, m->mutable_values(), rhs().data(), solution, &summary.message);
return summary;
2012-04-30 23:09:08 -07:00
}
SparseSchurComplementSolver::SparseSchurComplementSolver(
const LinearSolver::Options& options)
2017-04-12 12:48:44 -07:00
: SchurComplementSolver(options) {
if (options.type != ITERATIVE_SCHUR) {
2018-04-06 08:39:16 -07:00
sparse_cholesky_ = SparseCholesky::Create(options);
}
2012-04-30 23:09:08 -07:00
}
2022-08-14 19:14:04 -05:00
SparseSchurComplementSolver::~SparseSchurComplementSolver() {
for (int i = 0; i < 4; ++i) {
if (scratch_[i]) {
delete scratch_[i];
scratch_[i] = nullptr;
}
}
}
2012-04-30 23:09:08 -07:00
// Determine the non-zero blocks in the Schur Complement matrix, and
// initialize a BlockRandomAccessSparseMatrix object.
void SparseSchurComplementSolver::InitStorage(
const CompressedRowBlockStructure* bs) {
const int num_eliminate_blocks = options().elimination_groups[0];
2012-04-30 23:09:08 -07:00
const int num_col_blocks = bs->cols.size();
const int num_row_blocks = bs->rows.size();
2022-08-29 21:52:30 -07:00
blocks_ = Tail(bs->cols, num_col_blocks - num_eliminate_blocks);
2012-04-30 23:09:08 -07:00
2024-09-17 10:00:39 -07:00
absl::btree_set<std::pair<int, int>> block_pairs;
2012-06-05 23:10:59 -07:00
for (int i = 0; i < blocks_.size(); ++i) {
2022-08-29 21:52:30 -07:00
block_pairs.emplace(i, i);
2012-04-30 23:09:08 -07:00
}
int r = 0;
while (r < num_row_blocks) {
int e_block_id = bs->rows[r].cells.front().block_id;
if (e_block_id >= num_eliminate_blocks) {
break;
}
2022-08-29 21:52:30 -07:00
std::vector<int> f_blocks;
2012-04-30 23:09:08 -07:00
// Add to the chunk until the first block in the row is
// different than the one in the first row for the chunk.
for (; r < num_row_blocks; ++r) {
const CompressedRow& row = bs->rows[r];
if (row.cells.front().block_id != e_block_id) {
break;
}
// Iterate over the blocks in the row, ignoring the first
// block since it is the one to be eliminated.
for (int c = 1; c < row.cells.size(); ++c) {
const Cell& cell = row.cells[c];
f_blocks.push_back(cell.block_id - num_eliminate_blocks);
}
}
2024-04-04 16:48:17 +03:00
std::sort(f_blocks.begin(), f_blocks.end());
f_blocks.erase(std::unique(f_blocks.begin(), f_blocks.end()),
f_blocks.end());
2012-04-30 23:09:08 -07:00
for (int i = 0; i < f_blocks.size(); ++i) {
for (int j = i + 1; j < f_blocks.size(); ++j) {
2022-08-29 21:52:30 -07:00
block_pairs.emplace(f_blocks[i], f_blocks[j]);
2012-04-30 23:09:08 -07:00
}
}
}
2018-09-23 10:56:34 +08:00
// Remaining rows do not contribute to the chunks and directly go
2012-04-30 23:09:08 -07:00
// into the schur complement via an outer product.
for (; r < num_row_blocks; ++r) {
const CompressedRow& row = bs->rows[r];
CHECK_GE(row.cells.front().block_id, num_eliminate_blocks);
for (int i = 0; i < row.cells.size(); ++i) {
int r_block1_id = row.cells[i].block_id - num_eliminate_blocks;
2022-02-20 02:22:17 +01:00
for (const auto& cell : row.cells) {
int r_block2_id = cell.block_id - num_eliminate_blocks;
2012-04-30 23:09:08 -07:00
if (r_block1_id <= r_block2_id) {
2022-08-29 21:52:30 -07:00
block_pairs.emplace(r_block1_id, r_block2_id);
2012-04-30 23:09:08 -07:00
}
}
}
}
2022-12-21 16:01:36 -08:00
set_lhs(std::make_unique<BlockRandomAccessSparseMatrix>(
blocks_, block_pairs, options().context, options().num_threads));
2022-08-08 11:42:49 -07:00
ResizeRhs(lhs()->num_rows());
2012-04-30 23:09:08 -07:00
}
2017-04-12 12:48:44 -07:00
LinearSolver::Summary SparseSchurComplementSolver::SolveReducedLinearSystem(
const LinearSolver::PerSolveOptions& per_solve_options, double* solution) {
if (options().type == ITERATIVE_SCHUR) {
return SolveReducedLinearSystemUsingConjugateGradients(per_solve_options,
solution);
}
LinearSolver::Summary summary;
summary.num_iterations = 0;
2022-05-14 14:14:22 -07:00
summary.termination_type = LinearSolverTerminationType::SUCCESS;
summary.message = "Success.";
const BlockSparseMatrix* bsm =
2017-04-12 12:48:44 -07:00
down_cast<const BlockRandomAccessSparseMatrix*>(lhs())->matrix();
if (bsm->num_rows() == 0) {
return summary;
2012-04-30 23:09:08 -07:00
}
2017-04-12 12:48:44 -07:00
const CompressedRowSparseMatrix::StorageType storage_type =
sparse_cholesky_->StorageType();
2022-05-14 14:14:22 -07:00
if (storage_type ==
CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR) {
if (!crs_lhs_) {
crs_lhs_ = bsm->ToCompressedRowSparseMatrix();
crs_lhs_->set_storage_type(
CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR);
} else {
bsm->UpdateCompressedRowSparseMatrix(crs_lhs_.get());
}
2013-04-26 21:17:49 -07:00
} else {
if (!crs_lhs_) {
crs_lhs_ = bsm->ToCompressedRowSparseMatrixTranspose();
crs_lhs_->set_storage_type(
CompressedRowSparseMatrix::StorageType::LOWER_TRIANGULAR);
} else {
bsm->UpdateCompressedRowSparseMatrixTranspose(crs_lhs_.get());
}
}
2017-04-12 12:48:44 -07:00
summary.num_iterations = 1;
summary.termination_type = sparse_cholesky_->FactorAndSolve(
crs_lhs_.get(), rhs().data(), solution, &summary.message);
return summary;
2012-05-29 00:27:57 -07:00
}
LinearSolver::Summary
SparseSchurComplementSolver::SolveReducedLinearSystemUsingConjugateGradients(
const LinearSolver::PerSolveOptions& per_solve_options, double* solution) {
2017-04-12 12:48:44 -07:00
CHECK(options().use_explicit_schur_complement);
const int num_rows = lhs()->num_rows();
// The case where there are no f blocks, and the system is block
// diagonal.
if (num_rows == 0) {
LinearSolver::Summary summary;
summary.num_iterations = 0;
2022-05-14 14:14:22 -07:00
summary.termination_type = LinearSolverTerminationType::SUCCESS;
summary.message = "Success.";
return summary;
}
// Only SCHUR_JACOBI is supported over here right now.
CHECK_EQ(options().preconditioner_type, SCHUR_JACOBI);
2022-08-12 21:49:02 -07:00
if (preconditioner_ == nullptr) {
2022-12-21 16:01:36 -08:00
preconditioner_ = std::make_unique<BlockRandomAccessDiagonalMatrix>(
blocks_, options().context, options().num_threads);
}
2022-02-20 02:22:17 +01:00
auto* sc = down_cast<BlockRandomAccessSparseMatrix*>(mutable_lhs());
// Extract block diagonal from the Schur complement to construct the
// schur_jacobi preconditioner.
for (int i = 0; i < blocks_.size(); ++i) {
2022-08-29 21:52:30 -07:00
const int block_size = blocks_[i].size;
int sc_r, sc_c, sc_row_stride, sc_col_stride;
CellInfo* sc_cell_info =
2018-08-27 07:12:43 -07:00
sc->GetCell(i, i, &sc_r, &sc_c, &sc_row_stride, &sc_col_stride);
CHECK(sc_cell_info != nullptr);
MatrixRef sc_m(sc_cell_info->values, sc_row_stride, sc_col_stride);
int pre_r, pre_c, pre_row_stride, pre_col_stride;
2018-08-27 07:12:43 -07:00
CellInfo* pre_cell_info = preconditioner_->GetCell(
i, i, &pre_r, &pre_c, &pre_row_stride, &pre_col_stride);
CHECK(pre_cell_info != nullptr);
MatrixRef pre_m(pre_cell_info->values, pre_row_stride, pre_col_stride);
pre_m.block(pre_r, pre_c, block_size, block_size) =
sc_m.block(sc_r, sc_c, block_size, block_size);
}
preconditioner_->Invert();
VectorRef(solution, num_rows).setZero();
2022-08-08 11:42:49 -07:00
auto lhs = std::make_unique<BlockRandomAccessSparseMatrixAdapter>(*sc);
auto preconditioner =
2022-02-02 13:17:29 -08:00
std::make_unique<BlockRandomAccessDiagonalMatrixAdapter>(
*preconditioner_);
2022-08-08 11:42:49 -07:00
ConjugateGradientsSolverOptions cg_options;
cg_options.min_num_iterations = options().min_num_iterations;
cg_options.max_num_iterations = options().max_num_iterations;
2022-08-08 11:42:49 -07:00
cg_options.residual_reset_period = options().residual_reset_period;
cg_options.q_tolerance = per_solve_options.q_tolerance;
cg_options.r_tolerance = per_solve_options.r_tolerance;
cg_solution_ = Vector::Zero(sc->num_rows());
for (int i = 0; i < 4; ++i) {
2022-08-14 19:14:04 -05:00
if (scratch_[i] == nullptr) {
scratch_[i] = new Vector(sc->num_rows());
}
2022-08-08 11:42:49 -07:00
}
auto summary = ConjugateGradientsSolver<Vector>(
cg_options, *lhs, rhs(), *preconditioner, scratch_, cg_solution_);
VectorRef(solution, sc->num_rows()) = cg_solution_;
return summary;
}
2022-04-21 17:41:10 -07:00
} // namespace ceres::internal