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

202 lines
7.7 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)
#include "ceres/iterative_schur_complement_solver.h"
#include <algorithm>
#include <cstring>
2022-02-20 02:22:17 +01:00
#include <utility>
2012-04-30 23:09:08 -07:00
#include <vector>
2013-02-01 12:22:53 -08:00
2012-04-30 23:09:08 -07:00
#include "Eigen/Dense"
2024-07-07 10:24:18 -07:00
#include "absl/log/check.h"
#include "absl/log/log.h"
2012-04-30 23:09:08 -07:00
#include "ceres/block_sparse_matrix.h"
#include "ceres/block_structure.h"
2012-05-05 20:55:08 -07:00
#include "ceres/conjugate_gradients_solver.h"
#include "ceres/detect_structure.h"
2024-08-08 07:18:44 -07:00
#include "ceres/event_logger.h"
2012-04-30 23:09:08 -07:00
#include "ceres/implicit_schur_complement.h"
#include "ceres/internal/eigen.h"
2012-05-05 20:55:08 -07:00
#include "ceres/linear_solver.h"
#include "ceres/power_series_expansion_preconditioner.h"
2013-02-19 13:09:12 -08:00
#include "ceres/preconditioner.h"
2013-02-17 16:50:37 -08:00
#include "ceres/schur_jacobi_preconditioner.h"
2012-05-05 20:55:08 -07:00
#include "ceres/triplet_sparse_matrix.h"
2012-04-30 23:09:08 -07:00
#include "ceres/types.h"
2012-05-05 20:55:08 -07:00
#include "ceres/visibility_based_preconditioner.h"
2012-04-30 23:09:08 -07:00
2022-04-21 17:41:10 -07:00
namespace ceres::internal {
2012-04-30 23:09:08 -07:00
IterativeSchurComplementSolver::IterativeSchurComplementSolver(
2022-02-20 02:22:17 +01:00
LinearSolver::Options options)
: options_(std::move(options)) {}
2012-04-30 23:09:08 -07:00
2022-02-09 00:56:10 +01:00
IterativeSchurComplementSolver::~IterativeSchurComplementSolver() = default;
2012-04-30 23:09:08 -07:00
LinearSolver::Summary IterativeSchurComplementSolver::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("IterativeSchurComplementSolver::Solve");
2018-08-27 07:12:43 -07:00
CHECK(A->block_structure() != nullptr);
2022-12-21 16:01:36 -08:00
CHECK(A->transpose_block_structure() != nullptr);
const int num_eliminate_blocks = options_.elimination_groups[0];
2012-04-30 23:09:08 -07:00
// Initialize a ImplicitSchurComplement object.
2022-02-02 13:17:29 -08:00
if (schur_complement_ == nullptr) {
DetectStructure(*(A->block_structure()),
num_eliminate_blocks,
&options_.row_block_size,
&options_.e_block_size,
&options_.f_block_size);
2022-02-02 13:17:29 -08:00
schur_complement_ = std::make_unique<ImplicitSchurComplement>(options_);
2012-04-30 23:09:08 -07:00
}
schur_complement_->Init(*A, per_solve_options.D, b);
2013-09-02 22:28:40 -07:00
const int num_schur_complement_blocks =
A->block_structure()->cols.size() - num_eliminate_blocks;
2013-09-02 22:28:40 -07:00
if (num_schur_complement_blocks == 0) {
VLOG(2) << "No parameter blocks left in the schur complement.";
LinearSolver::Summary summary;
summary.num_iterations = 0;
2022-05-14 14:14:22 -07:00
summary.termination_type = LinearSolverTerminationType::SUCCESS;
2022-02-02 13:17:29 -08:00
schur_complement_->BackSubstitute(nullptr, x);
return summary;
2013-09-02 22:28:40 -07:00
}
// Initialize the solution to the Schur complement system.
2012-04-30 23:09:08 -07:00
reduced_linear_system_solution_.resize(schur_complement_->num_rows());
reduced_linear_system_solution_.setZero();
2022-08-13 11:59:16 -07:00
if (options_.use_spse_initialization) {
2023-09-27 21:08:53 +00:00
Preconditioner::Options preconditioner_options(options_);
preconditioner_options.type = SCHUR_POWER_SERIES_EXPANSION;
PowerSeriesExpansionPreconditioner pse_solver(
schur_complement_.get(),
options_.max_num_spse_iterations,
2023-09-27 21:08:53 +00:00
options_.spse_tolerance,
preconditioner_options);
pse_solver.RightMultiplyAndAccumulate(
schur_complement_->rhs().data(),
reduced_linear_system_solution_.data());
}
2012-04-30 23:09:08 -07:00
2017-05-31 19:44:23 -07:00
CreatePreconditioner(A);
2022-08-12 21:49:02 -07:00
if (preconditioner_ != nullptr) {
2017-05-31 19:44:23 -07:00
if (!preconditioner_->Update(*A, per_solve_options.D)) {
LinearSolver::Summary summary;
summary.num_iterations = 0;
2022-05-14 14:14:22 -07:00
summary.termination_type = LinearSolverTerminationType::FAILURE;
2017-05-31 19:44:23 -07:00
summary.message = "Preconditioner update failed.";
return summary;
}
2022-08-08 11:42:49 -07:00
}
2017-05-31 19:44:23 -07:00
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;
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;
LinearOperatorAdapter lhs(*schur_complement_);
LinearOperatorAdapter preconditioner(*preconditioner_);
Vector scratch[4];
for (int i = 0; i < 4; ++i) {
2022-11-27 21:35:44 +03:00
scratch[i].resize(schur_complement_->num_cols());
}
2022-08-16 15:46:22 -07:00
Vector* scratch_ptr[4] = {&scratch[0], &scratch[1], &scratch[2], &scratch[3]};
event_logger.AddEvent("Setup");
2022-08-08 11:42:49 -07:00
LinearSolver::Summary summary =
2022-08-08 11:42:49 -07:00
ConjugateGradientsSolver(cg_options,
lhs,
schur_complement_->rhs(),
preconditioner,
2022-08-14 19:14:04 -05:00
scratch_ptr,
2022-08-08 11:42:49 -07:00
reduced_linear_system_solution_);
2022-05-14 14:14:22 -07:00
if (summary.termination_type != LinearSolverTerminationType::FAILURE &&
summary.termination_type != LinearSolverTerminationType::FATAL_ERROR) {
schur_complement_->BackSubstitute(reduced_linear_system_solution_.data(),
x);
}
event_logger.AddEvent("Solve");
return summary;
}
2017-06-01 20:03:43 -07:00
void IterativeSchurComplementSolver::CreatePreconditioner(
BlockSparseMatrix* A) {
2022-08-12 21:49:02 -07:00
if (preconditioner_ != nullptr) {
2017-05-31 19:44:23 -07:00
return;
}
2023-09-27 21:08:53 +00:00
Preconditioner::Options preconditioner_options(options_);
2022-02-02 13:17:29 -08:00
CHECK(options_.context != nullptr);
2013-02-17 16:50:37 -08:00
2017-05-31 19:44:23 -07:00
switch (options_.preconditioner_type) {
2022-08-08 11:42:49 -07:00
case IDENTITY:
preconditioner_ = std::make_unique<IdentityPreconditioner>(
schur_complement_->num_cols());
break;
2017-05-31 19:44:23 -07:00
case JACOBI:
2022-02-02 13:17:29 -08:00
preconditioner_ = std::make_unique<SparseMatrixPreconditionerWrapper>(
2022-11-27 21:35:44 +03:00
schur_complement_->block_diagonal_FtF_inverse(),
preconditioner_options);
2017-05-31 19:44:23 -07:00
break;
case SCHUR_POWER_SERIES_EXPANSION:
// Ignoring the value of spse_tolerance to ensure preconditioner stays
// fixed during the iterations of cg.
preconditioner_ = std::make_unique<PowerSeriesExpansionPreconditioner>(
2023-09-27 21:08:53 +00:00
schur_complement_.get(),
options_.max_num_spse_iterations,
0,
preconditioner_options);
break;
2017-05-31 19:44:23 -07:00
case SCHUR_JACOBI:
2022-02-02 13:17:29 -08:00
preconditioner_ = std::make_unique<SchurJacobiPreconditioner>(
*A->block_structure(), preconditioner_options);
2017-05-31 19:44:23 -07:00
break;
case CLUSTER_JACOBI:
case CLUSTER_TRIDIAGONAL:
2022-02-02 13:17:29 -08:00
preconditioner_ = std::make_unique<VisibilityBasedPreconditioner>(
*A->block_structure(), preconditioner_options);
2017-05-31 19:44:23 -07:00
break;
default:
LOG(FATAL) << "Unknown Preconditioner Type";
2012-04-30 23:09:08 -07:00
}
2017-05-31 19:44:23 -07:00
};
2012-04-30 23:09:08 -07:00
2022-04-21 17:41:10 -07:00
} // namespace ceres::internal