2012-04-30 23:09:08 -07:00
|
|
|
// Ceres Solver - A fast non-linear least squares minimizer
|
2015-03-17 22:30:16 -07:00
|
|
|
// Copyright 2015 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"
|
2014-07-17 14:35:18 -07:00
|
|
|
|
2012-04-30 23:09:08 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <ctime>
|
2018-03-30 16:16:59 -07:00
|
|
|
#include <memory>
|
2012-04-30 23:09:08 -07:00
|
|
|
#include <set>
|
|
|
|
|
#include <vector>
|
2012-05-29 00:27:57 -07:00
|
|
|
|
2017-04-12 12:48:44 -07:00
|
|
|
#include "Eigen/Dense"
|
|
|
|
|
#include "Eigen/SparseCore"
|
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"
|
2014-09-29 07:53:54 -07:00
|
|
|
#include "ceres/conjugate_gradients_solver.h"
|
2012-04-30 23:09:08 -07:00
|
|
|
#include "ceres/detect_structure.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"
|
2013-02-01 12:22:53 -08:00
|
|
|
#include "ceres/wall_time.h"
|
2012-05-29 00:27:57 -07:00
|
|
|
|
2012-04-30 23:09:08 -07:00
|
|
|
namespace ceres {
|
|
|
|
|
namespace internal {
|
2014-12-17 07:35:09 -08:00
|
|
|
|
|
|
|
|
using std::make_pair;
|
|
|
|
|
using std::pair;
|
|
|
|
|
using std::set;
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
2014-09-29 07:53:54 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
class BlockRandomAccessSparseMatrixAdapter : public LinearOperator {
|
2014-12-17 07:35:09 -08:00
|
|
|
public:
|
2014-09-29 07:53:54 -07:00
|
|
|
explicit BlockRandomAccessSparseMatrixAdapter(
|
|
|
|
|
const BlockRandomAccessSparseMatrix& m)
|
2019-08-05 20:39:01 -07:00
|
|
|
: m_(m) {}
|
2014-09-29 07:53:54 -07:00
|
|
|
|
|
|
|
|
virtual ~BlockRandomAccessSparseMatrixAdapter() {}
|
|
|
|
|
|
|
|
|
|
// y = y + Ax;
|
2019-07-14 00:16:13 +02:00
|
|
|
void RightMultiply(const double* x, double* y) const final {
|
2014-09-29 07:53:54 -07:00
|
|
|
m_.SymmetricRightMultiply(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// y = y + A'x;
|
2019-07-14 00:16:13 +02:00
|
|
|
void LeftMultiply(const double* x, double* y) const final {
|
2014-09-29 07:53:54 -07:00
|
|
|
m_.SymmetricRightMultiply(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 00:16:13 +02:00
|
|
|
int num_rows() const final { return m_.num_rows(); }
|
|
|
|
|
int num_cols() const final { return m_.num_rows(); }
|
2014-09-29 07:53:54 -07:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const BlockRandomAccessSparseMatrix& m_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class BlockRandomAccessDiagonalMatrixAdapter : public LinearOperator {
|
2014-12-17 07:35:09 -08:00
|
|
|
public:
|
2014-09-29 07:53:54 -07:00
|
|
|
explicit BlockRandomAccessDiagonalMatrixAdapter(
|
|
|
|
|
const BlockRandomAccessDiagonalMatrix& m)
|
2019-08-05 20:39:01 -07:00
|
|
|
: m_(m) {}
|
2014-09-29 07:53:54 -07:00
|
|
|
|
|
|
|
|
virtual ~BlockRandomAccessDiagonalMatrixAdapter() {}
|
|
|
|
|
|
|
|
|
|
// y = y + Ax;
|
2019-07-14 00:16:13 +02:00
|
|
|
void RightMultiply(const double* x, double* y) const final {
|
2014-09-29 07:53:54 -07:00
|
|
|
m_.RightMultiply(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// y = y + A'x;
|
2019-07-14 00:16:13 +02:00
|
|
|
void LeftMultiply(const double* x, double* y) const final {
|
2014-09-29 07:53:54 -07:00
|
|
|
m_.RightMultiply(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 00:16:13 +02:00
|
|
|
int num_rows() const final { return m_.num_rows(); }
|
|
|
|
|
int num_cols() const final { return m_.num_rows(); }
|
2014-09-29 07:53:54 -07:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const BlockRandomAccessDiagonalMatrix& m_;
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-05 20:39:01 -07:00
|
|
|
} // namespace
|
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");
|
|
|
|
|
|
2019-08-05 20:39:01 -07:00
|
|
|
const CompressedRowBlockStructure* bs = A->block_structure();
|
2012-05-14 02:28:05 -07:00
|
|
|
if (eliminator_.get() == NULL) {
|
2019-08-05 20:39:01 -07:00
|
|
|
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);
|
2019-08-05 20:39:01 -07:00
|
|
|
|
|
|
|
|
// 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.
|
2020-09-20 21:45:24 +02:00
|
|
|
if (options_.row_block_size == 2 && options_.e_block_size == 3 &&
|
|
|
|
|
options_.f_block_size == 6 && num_f_blocks == 1) {
|
2019-08-05 20:39:01 -07:00
|
|
|
eliminator_.reset(new SchurEliminatorForOneFBlock<2, 3, 6>);
|
|
|
|
|
} else {
|
|
|
|
|
eliminator_.reset(SchurEliminatorBase::Create(options_));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CHECK(eliminator_);
|
2017-04-09 00:45:12 -07:00
|
|
|
const bool kFullRankETE = true;
|
2019-08-05 20:39:01 -07:00
|
|
|
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(),
|
|
|
|
|
rhs_.get());
|
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();
|
2013-11-25 05:47:43 -08:00
|
|
|
const LinearSolver::Summary summary =
|
2014-09-29 07:53:54 -07:00
|
|
|
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
|
|
|
|
2013-11-27 10:24:03 -08:00
|
|
|
if (summary.termination_type == LINEAR_SOLVER_SUCCESS) {
|
2019-09-04 07:09:36 -07:00
|
|
|
eliminator_->BackSubstitute(
|
|
|
|
|
BlockSparseMatrixData(*A), b, per_solve_options.D, reduced_solution, x);
|
2013-11-25 05:47:43 -08:00
|
|
|
event_logger.AddEvent("BackSubstitute");
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return summary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize a BlockRandomAccessDenseMatrix to store the Schur
|
|
|
|
|
// complement.
|
|
|
|
|
void DenseSchurComplementSolver::InitStorage(
|
|
|
|
|
const CompressedRowBlockStructure* bs) {
|
2012-09-17 11:30:14 -07:00
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
vector<int> blocks(num_col_blocks - num_eliminate_blocks, 0);
|
2019-08-05 20:39:01 -07:00
|
|
|
for (int i = num_eliminate_blocks, j = 0; i < num_col_blocks; ++i, ++j) {
|
2012-04-30 23:09:08 -07:00
|
|
|
blocks[j] = bs->cols[i].size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set_lhs(new BlockRandomAccessDenseMatrix(blocks));
|
|
|
|
|
set_rhs(new double[lhs()->num_rows()]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.
|
2019-08-05 20:39:01 -07:00
|
|
|
LinearSolver::Summary DenseSchurComplementSolver::SolveReducedLinearSystem(
|
|
|
|
|
const LinearSolver::PerSolveOptions& per_solve_options, double* solution) {
|
2013-11-25 05:47:43 -08:00
|
|
|
LinearSolver::Summary summary;
|
|
|
|
|
summary.num_iterations = 0;
|
2013-11-27 10:24:03 -08:00
|
|
|
summary.termination_type = LINEAR_SOLVER_SUCCESS;
|
2013-11-26 11:35:49 -08:00
|
|
|
summary.message = "Success.";
|
2013-11-25 05:47:43 -08:00
|
|
|
|
2022-01-21 18:33:16 -08:00
|
|
|
BlockRandomAccessDenseMatrix* 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) {
|
2013-11-25 05:47:43 -08:00
|
|
|
return summary;
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
|
2013-11-25 05:47:43 -08:00
|
|
|
summary.num_iterations = 1;
|
2022-01-21 18:33:16 -08:00
|
|
|
summary.termination_type = cholesky_->FactorAndSolve(
|
|
|
|
|
num_rows, m->mutable_values(), rhs(), solution, &summary.message);
|
2013-11-25 05:47:43 -08:00
|
|
|
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) {
|
2017-06-02 10:28:54 -07:00
|
|
|
if (options.type != ITERATIVE_SCHUR) {
|
2018-04-06 08:39:16 -07:00
|
|
|
sparse_cholesky_ = SparseCholesky::Create(options);
|
2017-06-02 10:28:54 -07:00
|
|
|
}
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 20:39:01 -07:00
|
|
|
SparseSchurComplementSolver::~SparseSchurComplementSolver() {}
|
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) {
|
2012-09-17 11:30:14 -07:00
|
|
|
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();
|
|
|
|
|
|
2012-06-05 23:10:59 -07:00
|
|
|
blocks_.resize(num_col_blocks - num_eliminate_blocks, 0);
|
2012-04-30 23:09:08 -07:00
|
|
|
for (int i = num_eliminate_blocks; i < num_col_blocks; ++i) {
|
2012-06-05 23:10:59 -07:00
|
|
|
blocks_[i - num_eliminate_blocks] = bs->cols[i].size;
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-03 10:41:01 -07:00
|
|
|
set<pair<int, int>> block_pairs;
|
2012-06-05 23:10:59 -07:00
|
|
|
for (int i = 0; i < blocks_.size(); ++i) {
|
2012-04-30 23:09:08 -07:00
|
|
|
block_pairs.insert(make_pair(i, i));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
vector<int> f_blocks;
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sort(f_blocks.begin(), f_blocks.end());
|
|
|
|
|
f_blocks.erase(unique(f_blocks.begin(), f_blocks.end()), f_blocks.end());
|
|
|
|
|
for (int i = 0; i < f_blocks.size(); ++i) {
|
|
|
|
|
for (int j = i + 1; j < f_blocks.size(); ++j) {
|
|
|
|
|
block_pairs.insert(make_pair(f_blocks[i], f_blocks[j]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
for (int j = 0; j < row.cells.size(); ++j) {
|
|
|
|
|
int r_block2_id = row.cells[j].block_id - num_eliminate_blocks;
|
|
|
|
|
if (r_block1_id <= r_block2_id) {
|
|
|
|
|
block_pairs.insert(make_pair(r_block1_id, r_block2_id));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-05 23:10:59 -07:00
|
|
|
set_lhs(new BlockRandomAccessSparseMatrix(blocks_, block_pairs));
|
2012-04-30 23:09:08 -07:00
|
|
|
set_rhs(new double[lhs()->num_rows()]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-12 12:48:44 -07:00
|
|
|
LinearSolver::Summary SparseSchurComplementSolver::SolveReducedLinearSystem(
|
|
|
|
|
const LinearSolver::PerSolveOptions& per_solve_options, double* solution) {
|
2014-09-29 07:53:54 -07:00
|
|
|
if (options().type == ITERATIVE_SCHUR) {
|
|
|
|
|
return SolveReducedLinearSystemUsingConjugateGradients(per_solve_options,
|
|
|
|
|
solution);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-25 05:47:43 -08:00
|
|
|
LinearSolver::Summary summary;
|
|
|
|
|
summary.num_iterations = 0;
|
2013-11-27 10:24:03 -08:00
|
|
|
summary.termination_type = LINEAR_SOLVER_SUCCESS;
|
2013-11-26 11:35:49 -08:00
|
|
|
summary.message = "Success.";
|
2013-11-25 05:47:43 -08:00
|
|
|
|
2017-04-12 12:48:44 -07:00
|
|
|
const TripletSparseMatrix* tsm =
|
|
|
|
|
down_cast<const BlockRandomAccessSparseMatrix*>(lhs())->matrix();
|
|
|
|
|
if (tsm->num_rows() == 0) {
|
2013-11-25 05:47:43 -08:00
|
|
|
return summary;
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
|
2018-03-30 16:16:59 -07:00
|
|
|
std::unique_ptr<CompressedRowSparseMatrix> lhs;
|
2017-04-12 12:48:44 -07:00
|
|
|
const CompressedRowSparseMatrix::StorageType storage_type =
|
|
|
|
|
sparse_cholesky_->StorageType();
|
|
|
|
|
if (storage_type == CompressedRowSparseMatrix::UPPER_TRIANGULAR) {
|
|
|
|
|
lhs.reset(CompressedRowSparseMatrix::FromTripletSparseMatrix(*tsm));
|
|
|
|
|
lhs->set_storage_type(CompressedRowSparseMatrix::UPPER_TRIANGULAR);
|
2013-04-26 21:17:49 -07:00
|
|
|
} else {
|
2017-04-12 12:48:44 -07:00
|
|
|
lhs.reset(
|
|
|
|
|
CompressedRowSparseMatrix::FromTripletSparseMatrixTransposed(*tsm));
|
|
|
|
|
lhs->set_storage_type(CompressedRowSparseMatrix::LOWER_TRIANGULAR);
|
2014-07-17 14:35:18 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-20 09:09:05 -07:00
|
|
|
*lhs->mutable_col_blocks() = blocks_;
|
|
|
|
|
*lhs->mutable_row_blocks() = blocks_;
|
|
|
|
|
|
2017-04-12 12:48:44 -07:00
|
|
|
summary.num_iterations = 1;
|
|
|
|
|
summary.termination_type = sparse_cholesky_->FactorAndSolve(
|
|
|
|
|
lhs.get(), rhs(), solution, &summary.message);
|
2014-07-17 14:35:18 -07:00
|
|
|
return summary;
|
2012-05-29 00:27:57 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-29 07:53:54 -07:00
|
|
|
LinearSolver::Summary
|
|
|
|
|
SparseSchurComplementSolver::SolveReducedLinearSystemUsingConjugateGradients(
|
2019-08-05 20:39:01 -07:00
|
|
|
const LinearSolver::PerSolveOptions& per_solve_options, double* solution) {
|
2017-04-12 12:48:44 -07:00
|
|
|
CHECK(options().use_explicit_schur_complement);
|
2014-09-29 07:53:54 -07:00
|
|
|
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;
|
|
|
|
|
summary.termination_type = LINEAR_SOLVER_SUCCESS;
|
|
|
|
|
summary.message = "Success.";
|
|
|
|
|
return summary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only SCHUR_JACOBI is supported over here right now.
|
|
|
|
|
CHECK_EQ(options().preconditioner_type, SCHUR_JACOBI);
|
|
|
|
|
|
|
|
|
|
if (preconditioner_.get() == NULL) {
|
|
|
|
|
preconditioner_.reset(new BlockRandomAccessDiagonalMatrix(blocks_));
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 18:33:16 -08:00
|
|
|
BlockRandomAccessSparseMatrix* sc =
|
|
|
|
|
down_cast<BlockRandomAccessSparseMatrix*>(mutable_lhs());
|
2014-09-29 07:53:54 -07:00
|
|
|
|
|
|
|
|
// Extract block diagonal from the Schur complement to construct the
|
|
|
|
|
// schur_jacobi preconditioner.
|
2019-08-05 20:39:01 -07:00
|
|
|
for (int i = 0; i < blocks_.size(); ++i) {
|
2014-09-29 07:53:54 -07:00
|
|
|
const int block_size = blocks_[i];
|
|
|
|
|
|
|
|
|
|
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);
|
2014-09-29 07:53:54 -07:00
|
|
|
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);
|
2014-09-29 07:53:54 -07:00
|
|
|
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();
|
|
|
|
|
|
2018-03-30 16:16:59 -07:00
|
|
|
std::unique_ptr<LinearOperator> lhs_adapter(
|
2014-09-29 07:53:54 -07:00
|
|
|
new BlockRandomAccessSparseMatrixAdapter(*sc));
|
2018-03-30 16:16:59 -07:00
|
|
|
std::unique_ptr<LinearOperator> preconditioner_adapter(
|
2014-09-29 07:53:54 -07:00
|
|
|
new BlockRandomAccessDiagonalMatrixAdapter(*preconditioner_));
|
|
|
|
|
|
|
|
|
|
LinearSolver::Options cg_options;
|
|
|
|
|
cg_options.min_num_iterations = options().min_num_iterations;
|
|
|
|
|
cg_options.max_num_iterations = options().max_num_iterations;
|
|
|
|
|
ConjugateGradientsSolver cg_solver(cg_options);
|
|
|
|
|
|
|
|
|
|
LinearSolver::PerSolveOptions cg_per_solve_options;
|
|
|
|
|
cg_per_solve_options.r_tolerance = per_solve_options.r_tolerance;
|
|
|
|
|
cg_per_solve_options.q_tolerance = per_solve_options.q_tolerance;
|
|
|
|
|
cg_per_solve_options.preconditioner = preconditioner_adapter.get();
|
|
|
|
|
|
2019-08-05 20:39:01 -07:00
|
|
|
return cg_solver.Solve(
|
|
|
|
|
lhs_adapter.get(), rhs(), cg_per_solve_options, solution);
|
2014-09-29 07:53:54 -07:00
|
|
|
}
|
|
|
|
|
|
2012-04-30 23:09:08 -07:00
|
|
|
} // namespace internal
|
|
|
|
|
} // namespace ceres
|