blob: 474c37f7ca4f5199d2b47c5c1adf4badd65e17da [file] [log] [blame]
Keir Mierlef7898fb2012-05-05 20:55:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
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: keir@google.com (Keir Mierle)
30
Keir Mierlee2a6cdc2012-05-07 06:39:56 -070031#include "ceres/block_jacobi_preconditioner.h"
Keir Mierlef7898fb2012-05-05 20:55:08 -070032
33#include "Eigen/Cholesky"
34#include "ceres/block_sparse_matrix.h"
35#include "ceres/block_structure.h"
36#include "ceres/casts.h"
Keir Mierlee2a6cdc2012-05-07 06:39:56 -070037#include "ceres/integral_types.h"
Keir Mierlef7898fb2012-05-05 20:55:08 -070038#include "ceres/internal/eigen.h"
39
40namespace ceres {
41namespace internal {
42
Keir Mierle716e2942012-08-10 16:53:37 -070043BlockJacobiPreconditioner::BlockJacobiPreconditioner(const LinearOperator& A)
44 : num_rows_(A.num_rows()),
45 block_structure_(
46 *(down_cast<const BlockSparseMatrix*>(&A)->block_structure())) {
Keir Mierlef7898fb2012-05-05 20:55:08 -070047 // Calculate the amount of storage needed.
48 int storage_needed = 0;
49 for (int c = 0; c < block_structure_.cols.size(); ++c) {
50 int size = block_structure_.cols[c].size;
51 storage_needed += size * size;
52 }
53
54 // Size the offsets and storage.
55 blocks_.resize(block_structure_.cols.size());
56 block_storage_.resize(storage_needed);
57
58 // Put pointers to the storage in the offsets.
Keir Mierlee2a6cdc2012-05-07 06:39:56 -070059 double* block_cursor = &block_storage_[0];
Keir Mierlef7898fb2012-05-05 20:55:08 -070060 for (int c = 0; c < block_structure_.cols.size(); ++c) {
61 int size = block_structure_.cols[c].size;
62 blocks_[c] = block_cursor;
63 block_cursor += size * size;
64 }
65}
66
Keir Mierlee2a6cdc2012-05-07 06:39:56 -070067BlockJacobiPreconditioner::~BlockJacobiPreconditioner() {
Keir Mierlef7898fb2012-05-05 20:55:08 -070068}
69
Keir Mierlee2a6cdc2012-05-07 06:39:56 -070070void BlockJacobiPreconditioner::Update(const LinearOperator& matrix, const double* D) {
Keir Mierlef7898fb2012-05-05 20:55:08 -070071 const BlockSparseMatrix& A = *(down_cast<const BlockSparseMatrix*>(&matrix));
72 const CompressedRowBlockStructure* bs = A.block_structure();
73
74 // Compute the diagonal blocks by block inner products.
75 std::fill(block_storage_.begin(), block_storage_.end(), 0.0);
76 for (int r = 0; r < bs->rows.size(); ++r) {
77 const int row_block_size = bs->rows[r].block.size;
78 const vector<Cell>& cells = bs->rows[r].cells;
79 const double* row_values = A.RowBlockValues(r);
80 for (int c = 0; c < cells.size(); ++c) {
81 const int col_block_size = bs->cols[cells[c].block_id].size;
82 ConstMatrixRef m(row_values + cells[c].position,
83 row_block_size,
84 col_block_size);
85
86 MatrixRef(blocks_[cells[c].block_id],
87 col_block_size,
88 col_block_size).noalias() += m.transpose() * m;
Keir Mierlee2a6cdc2012-05-07 06:39:56 -070089
90 // TODO(keir): Figure out when the below expression is actually faster
91 // than doing the full rank update. The issue is that for smaller sizes,
92 // the rankUpdate() function is slower than the full product done above.
93 //
94 // On the typical bundling problems, the above product is ~5% faster.
95 //
96 // MatrixRef(blocks_[cells[c].block_id],
97 // col_block_size,
98 // col_block_size).selfadjointView<Eigen::Upper>().rankUpdate(m);
99 //
Keir Mierlef7898fb2012-05-05 20:55:08 -0700100 }
101 }
102
Keir Mierlee2a6cdc2012-05-07 06:39:56 -0700103 // Add the diagonal and invert each block.
Keir Mierlef7898fb2012-05-05 20:55:08 -0700104 for (int c = 0; c < bs->cols.size(); ++c) {
105 const int size = block_structure_.cols[c].size;
Keir Mierlee2a6cdc2012-05-07 06:39:56 -0700106 const int position = block_structure_.cols[c].position;
Keir Mierlef8bd7fa2012-05-08 05:39:32 -0700107 MatrixRef block(blocks_[c], size, size);
Keir Mierlee2a6cdc2012-05-07 06:39:56 -0700108
Keir Mierlef8bd7fa2012-05-08 05:39:32 -0700109 if (D != NULL) {
110 block.diagonal() += ConstVectorRef(D + position, size).array().square().matrix();
111 }
Keir Mierlee2a6cdc2012-05-07 06:39:56 -0700112
Keir Mierlef8bd7fa2012-05-08 05:39:32 -0700113 block = block.selfadjointView<Eigen::Upper>()
114 .ldlt()
115 .solve(Matrix::Identity(size, size));
Keir Mierlef7898fb2012-05-05 20:55:08 -0700116 }
117}
118
Keir Mierlee2a6cdc2012-05-07 06:39:56 -0700119void BlockJacobiPreconditioner::RightMultiply(const double* x, double* y) const {
Keir Mierlef7898fb2012-05-05 20:55:08 -0700120 for (int c = 0; c < block_structure_.cols.size(); ++c) {
121 const int size = block_structure_.cols[c].size;
122 const int position = block_structure_.cols[c].position;
123 ConstMatrixRef D(blocks_[c], size, size);
124 ConstVectorRef x_block(x + position, size);
125 VectorRef y_block(y + position, size);
126 y_block += D * x_block;
127 }
128}
129
Keir Mierlee2a6cdc2012-05-07 06:39:56 -0700130void BlockJacobiPreconditioner::LeftMultiply(const double* x, double* y) const {
Keir Mierlef7898fb2012-05-05 20:55:08 -0700131 RightMultiply(x, y);
132}
133
134} // namespace internal
135} // namespace ceres