|
Packit |
ea1746 |
// Ceres Solver - A fast non-linear least squares minimizer
|
|
Packit |
ea1746 |
// Copyright 2015 Google Inc. All rights reserved.
|
|
Packit |
ea1746 |
// http://ceres-solver.org/
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
ea1746 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer.
|
|
Packit |
ea1746 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
ea1746 |
// and/or other materials provided with the distribution.
|
|
Packit |
ea1746 |
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
Packit |
ea1746 |
// used to endorse or promote products derived from this software without
|
|
Packit |
ea1746 |
// specific prior written permission.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
ea1746 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
ea1746 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
ea1746 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
ea1746 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
ea1746 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
ea1746 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
ea1746 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
ea1746 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
ea1746 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
ea1746 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Author: sameeragarwal@google.com (Sameer Agarwal)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/schur_complement_solver.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <algorithm>
|
|
Packit |
ea1746 |
#include <ctime>
|
|
Packit |
ea1746 |
#include <set>
|
|
Packit |
ea1746 |
#include <vector>
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "Eigen/Dense"
|
|
Packit |
ea1746 |
#include "Eigen/SparseCore"
|
|
Packit |
ea1746 |
#include "ceres/block_random_access_dense_matrix.h"
|
|
Packit |
ea1746 |
#include "ceres/block_random_access_matrix.h"
|
|
Packit |
ea1746 |
#include "ceres/block_random_access_sparse_matrix.h"
|
|
Packit |
ea1746 |
#include "ceres/block_sparse_matrix.h"
|
|
Packit |
ea1746 |
#include "ceres/block_structure.h"
|
|
Packit |
ea1746 |
#include "ceres/conjugate_gradients_solver.h"
|
|
Packit |
ea1746 |
#include "ceres/detect_structure.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/eigen.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/scoped_ptr.h"
|
|
Packit |
ea1746 |
#include "ceres/lapack.h"
|
|
Packit |
ea1746 |
#include "ceres/linear_solver.h"
|
|
Packit |
ea1746 |
#include "ceres/sparse_cholesky.h"
|
|
Packit |
ea1746 |
#include "ceres/triplet_sparse_matrix.h"
|
|
Packit |
ea1746 |
#include "ceres/types.h"
|
|
Packit |
ea1746 |
#include "ceres/wall_time.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
using std::make_pair;
|
|
Packit |
ea1746 |
using std::pair;
|
|
Packit |
ea1746 |
using std::set;
|
|
Packit |
ea1746 |
using std::vector;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
class BlockRandomAccessSparseMatrixAdapter : public LinearOperator {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
explicit BlockRandomAccessSparseMatrixAdapter(
|
|
Packit |
ea1746 |
const BlockRandomAccessSparseMatrix& m)
|
|
Packit |
ea1746 |
: m_(m) {
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
virtual ~BlockRandomAccessSparseMatrixAdapter() {}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// y = y + Ax;
|
|
Packit |
ea1746 |
virtual void RightMultiply(const double* x, double* y) const {
|
|
Packit |
ea1746 |
m_.SymmetricRightMultiply(x, y);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// y = y + A'x;
|
|
Packit |
ea1746 |
virtual void LeftMultiply(const double* x, double* y) const {
|
|
Packit |
ea1746 |
m_.SymmetricRightMultiply(x, y);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
virtual int num_rows() const { return m_.num_rows(); }
|
|
Packit |
ea1746 |
virtual int num_cols() const { return m_.num_rows(); }
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
const BlockRandomAccessSparseMatrix& m_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
class BlockRandomAccessDiagonalMatrixAdapter : public LinearOperator {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
explicit BlockRandomAccessDiagonalMatrixAdapter(
|
|
Packit |
ea1746 |
const BlockRandomAccessDiagonalMatrix& m)
|
|
Packit |
ea1746 |
: m_(m) {
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
virtual ~BlockRandomAccessDiagonalMatrixAdapter() {}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// y = y + Ax;
|
|
Packit |
ea1746 |
virtual void RightMultiply(const double* x, double* y) const {
|
|
Packit |
ea1746 |
m_.RightMultiply(x, y);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// y = y + A'x;
|
|
Packit |
ea1746 |
virtual void LeftMultiply(const double* x, double* y) const {
|
|
Packit |
ea1746 |
m_.RightMultiply(x, y);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
virtual int num_rows() const { return m_.num_rows(); }
|
|
Packit |
ea1746 |
virtual int num_cols() const { return m_.num_rows(); }
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
const BlockRandomAccessDiagonalMatrix& m_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
LinearSolver::Summary SchurComplementSolver::SolveImpl(
|
|
Packit |
ea1746 |
BlockSparseMatrix* A,
|
|
Packit |
ea1746 |
const double* b,
|
|
Packit |
ea1746 |
const LinearSolver::PerSolveOptions& per_solve_options,
|
|
Packit |
ea1746 |
double* x) {
|
|
Packit |
ea1746 |
EventLogger event_logger("SchurComplementSolver::Solve");
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (eliminator_.get() == NULL) {
|
|
Packit |
ea1746 |
InitStorage(A->block_structure());
|
|
Packit |
ea1746 |
DetectStructure(*A->block_structure(),
|
|
Packit |
ea1746 |
options_.elimination_groups[0],
|
|
Packit |
ea1746 |
&options_.row_block_size,
|
|
Packit |
ea1746 |
&options_.e_block_size,
|
|
Packit |
ea1746 |
&options_.f_block_size);
|
|
Packit |
ea1746 |
eliminator_.reset(CHECK_NOTNULL(SchurEliminatorBase::Create(options_)));
|
|
Packit |
ea1746 |
const bool kFullRankETE = true;
|
|
Packit |
ea1746 |
eliminator_->Init(
|
|
Packit |
ea1746 |
options_.elimination_groups[0], kFullRankETE, A->block_structure());
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
std::fill(x, x + A->num_cols(), 0.0);
|
|
Packit |
ea1746 |
event_logger.AddEvent("Setup");
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
eliminator_->Eliminate(A, b, per_solve_options.D, lhs_.get(), rhs_.get());
|
|
Packit |
ea1746 |
event_logger.AddEvent("Eliminate");
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
double* reduced_solution = x + A->num_cols() - lhs_->num_cols();
|
|
Packit |
ea1746 |
const LinearSolver::Summary summary =
|
|
Packit |
ea1746 |
SolveReducedLinearSystem(per_solve_options, reduced_solution);
|
|
Packit |
ea1746 |
event_logger.AddEvent("ReducedSolve");
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (summary.termination_type == LINEAR_SOLVER_SUCCESS) {
|
|
Packit |
ea1746 |
eliminator_->BackSubstitute(A, b, per_solve_options.D, reduced_solution, x);
|
|
Packit |
ea1746 |
event_logger.AddEvent("BackSubstitute");
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
return summary;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Initialize a BlockRandomAccessDenseMatrix to store the Schur
|
|
Packit |
ea1746 |
// complement.
|
|
Packit |
ea1746 |
void DenseSchurComplementSolver::InitStorage(
|
|
Packit |
ea1746 |
const CompressedRowBlockStructure* bs) {
|
|
Packit |
ea1746 |
const int num_eliminate_blocks = options().elimination_groups[0];
|
|
Packit |
ea1746 |
const int num_col_blocks = bs->cols.size();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
vector<int> blocks(num_col_blocks - num_eliminate_blocks, 0);
|
|
Packit |
ea1746 |
for (int i = num_eliminate_blocks, j = 0;
|
|
Packit |
ea1746 |
i < num_col_blocks;
|
|
Packit |
ea1746 |
++i, ++j) {
|
|
Packit |
ea1746 |
blocks[j] = bs->cols[i].size;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
set_lhs(new BlockRandomAccessDenseMatrix(blocks));
|
|
Packit |
ea1746 |
set_rhs(new double[lhs()->num_rows()]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Solve the system Sx = r, assuming that the matrix S is stored in a
|
|
Packit |
ea1746 |
// BlockRandomAccessDenseMatrix. The linear system is solved using
|
|
Packit |
ea1746 |
// Eigen's Cholesky factorization.
|
|
Packit |
ea1746 |
LinearSolver::Summary
|
|
Packit |
ea1746 |
DenseSchurComplementSolver::SolveReducedLinearSystem(
|
|
Packit |
ea1746 |
const LinearSolver::PerSolveOptions& per_solve_options,
|
|
Packit |
ea1746 |
double* solution) {
|
|
Packit |
ea1746 |
LinearSolver::Summary summary;
|
|
Packit |
ea1746 |
summary.num_iterations = 0;
|
|
Packit |
ea1746 |
summary.termination_type = LINEAR_SOLVER_SUCCESS;
|
|
Packit |
ea1746 |
summary.message = "Success.";
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const BlockRandomAccessDenseMatrix* m =
|
|
Packit |
ea1746 |
down_cast<const BlockRandomAccessDenseMatrix*>(lhs());
|
|
Packit |
ea1746 |
const int num_rows = m->num_rows();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// The case where there are no f blocks, and the system is block
|
|
Packit |
ea1746 |
// diagonal.
|
|
Packit |
ea1746 |
if (num_rows == 0) {
|
|
Packit |
ea1746 |
return summary;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
summary.num_iterations = 1;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (options().dense_linear_algebra_library_type == EIGEN) {
|
|
Packit |
ea1746 |
Eigen::LLT<Matrix, Eigen::Upper> llt =
|
|
Packit |
ea1746 |
ConstMatrixRef(m->values(), num_rows, num_rows)
|
|
Packit |
ea1746 |
.selfadjointView<Eigen::Upper>()
|
|
Packit |
ea1746 |
.llt();
|
|
Packit |
ea1746 |
if (llt.info() != Eigen::Success) {
|
|
Packit |
ea1746 |
summary.termination_type = LINEAR_SOLVER_FAILURE;
|
|
Packit |
ea1746 |
summary.message =
|
|
Packit |
ea1746 |
"Eigen failure. Unable to perform dense Cholesky factorization.";
|
|
Packit |
ea1746 |
return summary;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
VectorRef(solution, num_rows) = llt.solve(ConstVectorRef(rhs(), num_rows));
|
|
Packit |
ea1746 |
} else {
|
|
Packit |
ea1746 |
VectorRef(solution, num_rows) = ConstVectorRef(rhs(), num_rows);
|
|
Packit |
ea1746 |
summary.termination_type =
|
|
Packit |
ea1746 |
LAPACK::SolveInPlaceUsingCholesky(num_rows,
|
|
Packit |
ea1746 |
m->values(),
|
|
Packit |
ea1746 |
solution,
|
|
Packit |
ea1746 |
&summary.message);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
return summary;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
SparseSchurComplementSolver::SparseSchurComplementSolver(
|
|
Packit |
ea1746 |
const LinearSolver::Options& options)
|
|
Packit |
ea1746 |
: SchurComplementSolver(options) {
|
|
Packit |
ea1746 |
if (options.type != ITERATIVE_SCHUR) {
|
|
Packit |
ea1746 |
sparse_cholesky_.reset(
|
|
Packit |
ea1746 |
SparseCholesky::Create(options.sparse_linear_algebra_library_type,
|
|
Packit |
ea1746 |
options.use_postordering ? AMD : NATURAL));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
SparseSchurComplementSolver::~SparseSchurComplementSolver() {
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Determine the non-zero blocks in the Schur Complement matrix, and
|
|
Packit |
ea1746 |
// initialize a BlockRandomAccessSparseMatrix object.
|
|
Packit |
ea1746 |
void SparseSchurComplementSolver::InitStorage(
|
|
Packit |
ea1746 |
const CompressedRowBlockStructure* bs) {
|
|
Packit |
ea1746 |
const int num_eliminate_blocks = options().elimination_groups[0];
|
|
Packit |
ea1746 |
const int num_col_blocks = bs->cols.size();
|
|
Packit |
ea1746 |
const int num_row_blocks = bs->rows.size();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
blocks_.resize(num_col_blocks - num_eliminate_blocks, 0);
|
|
Packit |
ea1746 |
for (int i = num_eliminate_blocks; i < num_col_blocks; ++i) {
|
|
Packit |
ea1746 |
blocks_[i - num_eliminate_blocks] = bs->cols[i].size;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
set<pair<int, int> > block_pairs;
|
|
Packit |
ea1746 |
for (int i = 0; i < blocks_.size(); ++i) {
|
|
Packit |
ea1746 |
block_pairs.insert(make_pair(i, i));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
int r = 0;
|
|
Packit |
ea1746 |
while (r < num_row_blocks) {
|
|
Packit |
ea1746 |
int e_block_id = bs->rows[r].cells.front().block_id;
|
|
Packit |
ea1746 |
if (e_block_id >= num_eliminate_blocks) {
|
|
Packit |
ea1746 |
break;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
vector<int> f_blocks;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Add to the chunk until the first block in the row is
|
|
Packit |
ea1746 |
// different than the one in the first row for the chunk.
|
|
Packit |
ea1746 |
for (; r < num_row_blocks; ++r) {
|
|
Packit |
ea1746 |
const CompressedRow& row = bs->rows[r];
|
|
Packit |
ea1746 |
if (row.cells.front().block_id != e_block_id) {
|
|
Packit |
ea1746 |
break;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Iterate over the blocks in the row, ignoring the first
|
|
Packit |
ea1746 |
// block since it is the one to be eliminated.
|
|
Packit |
ea1746 |
for (int c = 1; c < row.cells.size(); ++c) {
|
|
Packit |
ea1746 |
const Cell& cell = row.cells[c];
|
|
Packit |
ea1746 |
f_blocks.push_back(cell.block_id - num_eliminate_blocks);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
sort(f_blocks.begin(), f_blocks.end());
|
|
Packit |
ea1746 |
f_blocks.erase(unique(f_blocks.begin(), f_blocks.end()), f_blocks.end());
|
|
Packit |
ea1746 |
for (int i = 0; i < f_blocks.size(); ++i) {
|
|
Packit |
ea1746 |
for (int j = i + 1; j < f_blocks.size(); ++j) {
|
|
Packit |
ea1746 |
block_pairs.insert(make_pair(f_blocks[i], f_blocks[j]));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Remaing rows do not contribute to the chunks and directly go
|
|
Packit |
ea1746 |
// into the schur complement via an outer product.
|
|
Packit |
ea1746 |
for (; r < num_row_blocks; ++r) {
|
|
Packit |
ea1746 |
const CompressedRow& row = bs->rows[r];
|
|
Packit |
ea1746 |
CHECK_GE(row.cells.front().block_id, num_eliminate_blocks);
|
|
Packit |
ea1746 |
for (int i = 0; i < row.cells.size(); ++i) {
|
|
Packit |
ea1746 |
int r_block1_id = row.cells[i].block_id - num_eliminate_blocks;
|
|
Packit |
ea1746 |
for (int j = 0; j < row.cells.size(); ++j) {
|
|
Packit |
ea1746 |
int r_block2_id = row.cells[j].block_id - num_eliminate_blocks;
|
|
Packit |
ea1746 |
if (r_block1_id <= r_block2_id) {
|
|
Packit |
ea1746 |
block_pairs.insert(make_pair(r_block1_id, r_block2_id));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
set_lhs(new BlockRandomAccessSparseMatrix(blocks_, block_pairs));
|
|
Packit |
ea1746 |
set_rhs(new double[lhs()->num_rows()]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
LinearSolver::Summary SparseSchurComplementSolver::SolveReducedLinearSystem(
|
|
Packit |
ea1746 |
const LinearSolver::PerSolveOptions& per_solve_options, double* solution) {
|
|
Packit |
ea1746 |
if (options().type == ITERATIVE_SCHUR) {
|
|
Packit |
ea1746 |
return SolveReducedLinearSystemUsingConjugateGradients(per_solve_options,
|
|
Packit |
ea1746 |
solution);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
LinearSolver::Summary summary;
|
|
Packit |
ea1746 |
summary.num_iterations = 0;
|
|
Packit |
ea1746 |
summary.termination_type = LINEAR_SOLVER_SUCCESS;
|
|
Packit |
ea1746 |
summary.message = "Success.";
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const TripletSparseMatrix* tsm =
|
|
Packit |
ea1746 |
down_cast<const BlockRandomAccessSparseMatrix*>(lhs())->matrix();
|
|
Packit |
ea1746 |
if (tsm->num_rows() == 0) {
|
|
Packit |
ea1746 |
return summary;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
scoped_ptr<CompressedRowSparseMatrix> lhs;
|
|
Packit |
ea1746 |
const CompressedRowSparseMatrix::StorageType storage_type =
|
|
Packit |
ea1746 |
sparse_cholesky_->StorageType();
|
|
Packit |
ea1746 |
if (storage_type == CompressedRowSparseMatrix::UPPER_TRIANGULAR) {
|
|
Packit |
ea1746 |
lhs.reset(CompressedRowSparseMatrix::FromTripletSparseMatrix(*tsm));
|
|
Packit |
ea1746 |
lhs->set_storage_type(CompressedRowSparseMatrix::UPPER_TRIANGULAR);
|
|
Packit |
ea1746 |
} else {
|
|
Packit |
ea1746 |
lhs.reset(
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix::FromTripletSparseMatrixTransposed(*tsm));
|
|
Packit |
ea1746 |
lhs->set_storage_type(CompressedRowSparseMatrix::LOWER_TRIANGULAR);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
*lhs->mutable_col_blocks() = blocks_;
|
|
Packit |
ea1746 |
*lhs->mutable_row_blocks() = blocks_;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
summary.num_iterations = 1;
|
|
Packit |
ea1746 |
summary.termination_type = sparse_cholesky_->FactorAndSolve(
|
|
Packit |
ea1746 |
lhs.get(), rhs(), solution, &summary.message);
|
|
Packit |
ea1746 |
return summary;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
LinearSolver::Summary
|
|
Packit |
ea1746 |
SparseSchurComplementSolver::SolveReducedLinearSystemUsingConjugateGradients(
|
|
Packit |
ea1746 |
const LinearSolver::PerSolveOptions& per_solve_options,
|
|
Packit |
ea1746 |
double* solution) {
|
|
Packit |
ea1746 |
CHECK(options().use_explicit_schur_complement);
|
|
Packit |
ea1746 |
const int num_rows = lhs()->num_rows();
|
|
Packit |
ea1746 |
// The case where there are no f blocks, and the system is block
|
|
Packit |
ea1746 |
// diagonal.
|
|
Packit |
ea1746 |
if (num_rows == 0) {
|
|
Packit |
ea1746 |
LinearSolver::Summary summary;
|
|
Packit |
ea1746 |
summary.num_iterations = 0;
|
|
Packit |
ea1746 |
summary.termination_type = LINEAR_SOLVER_SUCCESS;
|
|
Packit |
ea1746 |
summary.message = "Success.";
|
|
Packit |
ea1746 |
return summary;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Only SCHUR_JACOBI is supported over here right now.
|
|
Packit |
ea1746 |
CHECK_EQ(options().preconditioner_type, SCHUR_JACOBI);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (preconditioner_.get() == NULL) {
|
|
Packit |
ea1746 |
preconditioner_.reset(new BlockRandomAccessDiagonalMatrix(blocks_));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
BlockRandomAccessSparseMatrix* sc =
|
|
Packit |
ea1746 |
down_cast<BlockRandomAccessSparseMatrix*>(
|
|
Packit |
ea1746 |
const_cast<BlockRandomAccessMatrix*>(lhs()));
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Extract block diagonal from the Schur complement to construct the
|
|
Packit |
ea1746 |
// schur_jacobi preconditioner.
|
|
Packit |
ea1746 |
for (int i = 0; i < blocks_.size(); ++i) {
|
|
Packit |
ea1746 |
const int block_size = blocks_[i];
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
int sc_r, sc_c, sc_row_stride, sc_col_stride;
|
|
Packit |
ea1746 |
CellInfo* sc_cell_info =
|
|
Packit |
ea1746 |
CHECK_NOTNULL(sc->GetCell(i, i,
|
|
Packit |
ea1746 |
&sc_r, &sc_c,
|
|
Packit |
ea1746 |
&sc_row_stride, &sc_col_stride));
|
|
Packit |
ea1746 |
MatrixRef sc_m(sc_cell_info->values, sc_row_stride, sc_col_stride);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
int pre_r, pre_c, pre_row_stride, pre_col_stride;
|
|
Packit |
ea1746 |
CellInfo* pre_cell_info = CHECK_NOTNULL(
|
|
Packit |
ea1746 |
preconditioner_->GetCell(i, i,
|
|
Packit |
ea1746 |
&pre_r, &pre_c,
|
|
Packit |
ea1746 |
&pre_row_stride, &pre_col_stride));
|
|
Packit |
ea1746 |
MatrixRef pre_m(pre_cell_info->values, pre_row_stride, pre_col_stride);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
pre_m.block(pre_r, pre_c, block_size, block_size) =
|
|
Packit |
ea1746 |
sc_m.block(sc_r, sc_c, block_size, block_size);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
preconditioner_->Invert();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
VectorRef(solution, num_rows).setZero();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
scoped_ptr<LinearOperator> lhs_adapter(
|
|
Packit |
ea1746 |
new BlockRandomAccessSparseMatrixAdapter(*sc));
|
|
Packit |
ea1746 |
scoped_ptr<LinearOperator> preconditioner_adapter(
|
|
Packit |
ea1746 |
new BlockRandomAccessDiagonalMatrixAdapter(*preconditioner_));
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
LinearSolver::Options cg_options;
|
|
Packit |
ea1746 |
cg_options.min_num_iterations = options().min_num_iterations;
|
|
Packit |
ea1746 |
cg_options.max_num_iterations = options().max_num_iterations;
|
|
Packit |
ea1746 |
ConjugateGradientsSolver cg_solver(cg_options);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
LinearSolver::PerSolveOptions cg_per_solve_options;
|
|
Packit |
ea1746 |
cg_per_solve_options.r_tolerance = per_solve_options.r_tolerance;
|
|
Packit |
ea1746 |
cg_per_solve_options.q_tolerance = per_solve_options.q_tolerance;
|
|
Packit |
ea1746 |
cg_per_solve_options.preconditioner = preconditioner_adapter.get();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
return cg_solver.Solve(lhs_adapter.get(),
|
|
Packit |
ea1746 |
rhs(),
|
|
Packit |
ea1746 |
cg_per_solve_options,
|
|
Packit |
ea1746 |
solution);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|